root/trunk/vendor/bluecloth/tests/bctestcase.rb

Revision 420, 6.9 kB (checked in by tobi, 3 years ago)

redcloth, bluecloth and rubypants are now bundeled with typo

Line 
1 #!/usr/bin/ruby
2 #
3 # This is an abstract test case class for building Test::Unit unit tests for the
4 # BlueCloth module. It consolidates most of the maintenance work that must be
5 # done to build a test file by adjusting the $LOAD_PATH appropriately, as well
6 # as adding some other useful methods that make building, maintaining, and using
7 # the tests for programming much easier (IMHO). See the docs for Test::Unit for
8 # more info on the particulars of unit testing.
9 #
10 # == Synopsis
11 #
12 #       # Allow the test to be run from anywhere:
13 #       if !defined?( BlueCloth ) || !defined?( BlueCloth::TestCase )
14 #               basedir = File::dirname( __FILE__ )
15 #               require File::join( basedir, 'bctestcase' )
16 #       end
17 #
18 #       class MySomethingTest < BlueCloth::TestCase
19 #               def setup
20 #                       super()
21 #                       @foo = 'bar'
22 #               end
23 #
24 #               def test_00_something
25 #                       obj = nil
26 #                       assert_nothing_raised { obj = MySomething::new }
27 #                       assert_instance_of MySomething, obj
28 #                       assert_respond_to :myMethod, obj
29 #               end
30 #
31 #       end
32 #
33 # == Rcsid
34 #
35 # $Id: lingtestcase.rb,v 1.3 2003/09/11 05:00:56 deveiant Exp $
36 #
37 # == Authors
38 #
39 # * Michael Granger <ged@FaerieMUD.org>
40 #
41 #:include: COPYRIGHT
42 #
43 #---
44 #
45 # Please see the file COPYRIGHT in the 'docs' directory for licensing details.
46 #
47
48 $DebugPattern ||= nil
49
50 begin
51         basedir = File::dirname( File::dirname(__FILE__) )
52         unless $LOAD_PATH.include?( "#{basedir}/lib" )
53                 $LOAD_PATH.unshift "#{basedir}/lib"
54         end
55 end
56
57 require "test/unit"
58 require "bluecloth"
59
60
61 class BlueCloth
62
63         ### The abstract base class for BlueCloth test cases.
64         class TestCase < Test::Unit::TestCase
65
66                 @methodCounter = 0
67                 @setupBlocks = []
68                 @teardownBlocks = []
69                 class << self
70                         attr_accessor :methodCounter, :setupBlocks, :teardownBlocks
71                 end
72
73
74                 ### Inheritance callback -- adds @setupBlocks and @teardownBlocks ivars
75                 ### and accessors to the inheriting class.
76                 def self::inherited( klass )
77                         klass.module_eval {
78                                 @setupBlocks = []
79                                 @teardownBlocks = []
80
81                                 class << self
82                                         attr_accessor :setupBlocks, :teardownBlocks
83                                 end
84                         }
85                         klass.methodCounter = 0
86                 end
87                
88
89
90                 ### Output the specified <tt>msgs</tt> joined together to
91                 ### <tt>STDERR</tt> if <tt>$DEBUG</tt> is set.
92                 def self::debugMsg( *msgs )
93                         return unless $DEBUG
94                         self.message "DEBUG>>> %s" % msgs.join('')
95                 end
96
97                 ### Output the specified <tt>msgs</tt> joined together to
98                 ### <tt>STDOUT</tt>.
99                 def self::message( *msgs )
100                         $stderr.puts msgs.join('')
101                         $stderr.flush
102                 end
103
104
105                 ### Add a setup block for the current testcase
106                 def self::addSetupBlock( &block )
107                         self.methodCounter += 1
108                         newMethodName = "setup_#{self.methodCounter}".intern
109                         define_method( newMethodName, &block )
110                         self.setupBlocks.push newMethodName
111                 end
112                        
113                 ### Add a teardown block for the current testcase
114                 def self::addTeardownBlock( &block )
115                         self.methodCounter += 1
116                         newMethodName = "teardown_#{self.methodCounter}".intern
117                         define_method( newMethodName, &block )
118                         self.teardownBlocks.unshift newMethodName
119                 end
120                        
121
122                 #############################################################
123                 ###     I N S T A N C E   M E T H O D S
124                 #############################################################
125
126                 ### A dummy test method to allow this Test::Unit::TestCase to be
127                 ### subclassed without complaining about the lack of tests.
128                 def test_0_dummy
129                 end
130
131
132                 ### Forward-compatibility method for namechange in Test::Unit
133                 def setup( *args )
134                         self.class.setupBlocks.each {|sblock|
135                                 debugMsg "Calling setup block method #{sblock}"
136                                 self.send( sblock )
137                         }
138                         super( *args )
139                 end
140                 alias_method :set_up, :setup
141
142
143                 ### Forward-compatibility method for namechange in Test::Unit
144                 def teardown( *args )
145                         super( *args )
146                         self.class.teardownBlocks.each {|tblock|
147                                 debugMsg "Calling teardown block method #{tblock}"
148                                 self.send( tblock )
149                         }
150                 end
151                 alias_method :tear_down, :teardown
152
153
154                 ### Skip the current step (called from #setup) with the +reason+ given.
155         def skip( reason=nil )
156                         if reason
157                                 msg = "Skipping %s: %s" % [ @method_name, reason ]
158                         else
159                                 msg = "Skipping %s: No reason given." % @method_name
160                         end
161                        
162                         $stderr.puts( msg ) if $VERBOSE
163                         @method_name = :skipped_test
164         end
165                
166                
167         def skipped_test # :nodoc:
168         end     
169                
170        
171         ### Add the specified +block+ to the code that gets executed by #setup.
172         def addSetupBlock( &block ); self.class.addSetupBlock( &block ); end
173        
174                
175         ### Add the specified +block+ to the code that gets executed by #teardown.
176         def addTeardownBlock( &block ); self.class.addTeardownBlock( &block ); end
177        
178        
179                 ### Instance alias for the like-named class method.
180                 def message( *msgs )
181                         self.class.message( *msgs )
182                 end
183
184
185                 ### Instance alias for the like-named class method
186                 def debugMsg( *msgs )
187                         self.class.debugMsg( *msgs )
188                 end
189
190
191                 ### Output a separator line made up of <tt>length</tt> of the specified
192                 ### <tt>char</tt>.
193                 def writeLine( length=75, char="-" )
194                         $stderr.puts "\r" + (char * length )
195                 end
196
197
198                 ### Output a header for delimiting tests
199                 def printTestHeader( desc )
200                         return unless $VERBOSE || $DEBUG
201                         message ">>> %s <<<" % desc
202                 end
203
204
205                 ### Try to force garbage collection to start.
206                 def collectGarbage
207                         a = []
208                         1000.times { a << {} }
209                         a = nil
210                         GC.start
211                 end
212
213
214                 ### Output the name of the test as it's running if in verbose mode.
215                 def run( result )
216                         $stderr.puts self.name if $VERBOSE || $DEBUG
217
218                         # Support debugging for individual tests
219                         olddb = nil
220                         if $DebugPattern && $DebugPattern =~ @method_name
221                                 olddb = $DEBUG
222                                 $DEBUG = true
223                         end
224                        
225                         super
226
227                         $DEBUG = olddb unless olddb.nil?
228                 end
229
230
231                 #############################################################
232                 ###     E X T R A   A S S E R T I O N S
233                 #############################################################
234
235                 ### Negative of assert_respond_to
236                 def assert_not_respond_to( obj, meth )
237                         msg = "%s expected NOT to respond to '%s'" %
238                                 [ obj.inspect, meth ]
239                         assert_block( msg ) {
240                                 !obj.respond_to?( meth )
241                         }
242                 end
243
244
245                 ### Assert that the instance variable specified by +sym+ of an +object+
246                 ### is equal to the specified +value+. The '@' at the beginning of the
247                 ### +sym+ will be prepended if not present.
248                 def assert_ivar_equal( value, object, sym )
249                         sym = "@#{sym}".intern unless /^@/ =~ sym.to_s
250                         msg = "Instance variable '%s'\n\tof <%s>\n\texpected to be <%s>\n" %
251                                 [ sym, object.inspect, value.inspect ]
252                         msg += "\tbut was: <%s>" % object.instance_variable_get(sym)
253                         assert_block( msg ) {
254                                 value == object.instance_variable_get(sym)
255                         }
256                 end
257
258
259                 ### Assert that the specified +object+ has an instance variable which
260                 ### matches the specified +sym+. The '@' at the beginning of the +sym+
261                 ### will be prepended if not present.
262                 def assert_has_ivar( sym, object )
263                         sym = "@#{sym}" unless /^@/ =~ sym.to_s
264                         msg = "Object <%s> expected to have an instance variable <%s>" %
265                                 [ object.inspect, sym ]
266                         assert_block( msg ) {
267                                 object.instance_variables.include?( sym.to_s )
268                         }
269                 end
270
271         end # class TestCase
272
273 end # class BlueCloth
274
Note: See TracBrowser for help on using the browser.