PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/has_markup/vendor/gems/BlueCloth-1.0.0/tests/bctestcase.rb

https://github.com/technicalpickles/flockup
Ruby | 274 lines | 133 code | 58 blank | 83 comment | 8 complexity | b6527fa189b0ca1f7267a8c6d57b48c7 MD5 | raw file
Possible License(s): GPL-2.0
  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. $DebugPattern ||= nil
  48. begin
  49. basedir = File::dirname( File::dirname(__FILE__) )
  50. unless $LOAD_PATH.include?( "#{basedir}/lib" )
  51. $LOAD_PATH.unshift "#{basedir}/lib"
  52. end
  53. end
  54. require "test/unit"
  55. require "bluecloth"
  56. class BlueCloth
  57. ### The abstract base class for BlueCloth test cases.
  58. class TestCase < Test::Unit::TestCase
  59. @methodCounter = 0
  60. @setupBlocks = []
  61. @teardownBlocks = []
  62. class << self
  63. attr_accessor :methodCounter, :setupBlocks, :teardownBlocks
  64. end
  65. ### Inheritance callback -- adds @setupBlocks and @teardownBlocks ivars
  66. ### and accessors to the inheriting class.
  67. def self::inherited( klass )
  68. klass.module_eval {
  69. @setupBlocks = []
  70. @teardownBlocks = []
  71. class << self
  72. attr_accessor :setupBlocks, :teardownBlocks
  73. end
  74. }
  75. klass.methodCounter = 0
  76. end
  77. ### Output the specified <tt>msgs</tt> joined together to
  78. ### <tt>STDERR</tt> if <tt>$DEBUG</tt> is set.
  79. def self::debugMsg( *msgs )
  80. return unless $DEBUG
  81. self.message "DEBUG>>> %s" % msgs.join('')
  82. end
  83. ### Output the specified <tt>msgs</tt> joined together to
  84. ### <tt>STDOUT</tt>.
  85. def self::message( *msgs )
  86. $stderr.puts msgs.join('')
  87. $stderr.flush
  88. end
  89. ### Add a setup block for the current testcase
  90. def self::addSetupBlock( &block )
  91. self.methodCounter += 1
  92. newMethodName = "setup_#{self.methodCounter}".intern
  93. define_method( newMethodName, &block )
  94. self.setupBlocks.push newMethodName
  95. end
  96. ### Add a teardown block for the current testcase
  97. def self::addTeardownBlock( &block )
  98. self.methodCounter += 1
  99. newMethodName = "teardown_#{self.methodCounter}".intern
  100. define_method( newMethodName, &block )
  101. self.teardownBlocks.unshift newMethodName
  102. end
  103. #############################################################
  104. ### I N S T A N C E M E T H O D S
  105. #############################################################
  106. ### A dummy test method to allow this Test::Unit::TestCase to be
  107. ### subclassed without complaining about the lack of tests.
  108. def test_0_dummy
  109. end
  110. ### Forward-compatibility method for namechange in Test::Unit
  111. def setup( *args )
  112. self.class.setupBlocks.each {|sblock|
  113. debugMsg "Calling setup block method #{sblock}"
  114. self.send( sblock )
  115. }
  116. super( *args )
  117. end
  118. alias_method :set_up, :setup
  119. ### Forward-compatibility method for namechange in Test::Unit
  120. def teardown( *args )
  121. super( *args )
  122. self.class.teardownBlocks.each {|tblock|
  123. debugMsg "Calling teardown block method #{tblock}"
  124. self.send( tblock )
  125. }
  126. end
  127. alias_method :tear_down, :teardown
  128. ### Skip the current step (called from #setup) with the +reason+ given.
  129. def skip( reason=nil )
  130. if reason
  131. msg = "Skipping %s: %s" % [ @method_name, reason ]
  132. else
  133. msg = "Skipping %s: No reason given." % @method_name
  134. end
  135. $stderr.puts( msg ) if $VERBOSE
  136. @method_name = :skipped_test
  137. end
  138. def skipped_test # :nodoc:
  139. end
  140. ### Add the specified +block+ to the code that gets executed by #setup.
  141. def addSetupBlock( &block ); self.class.addSetupBlock( &block ); end
  142. ### Add the specified +block+ to the code that gets executed by #teardown.
  143. def addTeardownBlock( &block ); self.class.addTeardownBlock( &block ); end
  144. ### Instance alias for the like-named class method.
  145. def message( *msgs )
  146. self.class.message( *msgs )
  147. end
  148. ### Instance alias for the like-named class method
  149. def debugMsg( *msgs )
  150. self.class.debugMsg( *msgs )
  151. end
  152. ### Output a separator line made up of <tt>length</tt> of the specified
  153. ### <tt>char</tt>.
  154. def writeLine( length=75, char="-" )
  155. $stderr.puts "\r" + (char * length )
  156. end
  157. ### Output a header for delimiting tests
  158. def printTestHeader( desc )
  159. return unless $VERBOSE || $DEBUG
  160. message ">>> %s <<<" % desc
  161. end
  162. ### Try to force garbage collection to start.
  163. def collectGarbage
  164. a = []
  165. 1000.times { a << {} }
  166. a = nil
  167. GC.start
  168. end
  169. ### Output the name of the test as it's running if in verbose mode.
  170. def run( result )
  171. $stderr.puts self.name if $VERBOSE || $DEBUG
  172. # Support debugging for individual tests
  173. olddb = nil
  174. if $DebugPattern && $DebugPattern =~ @method_name
  175. olddb = $DEBUG
  176. $DEBUG = true
  177. end
  178. super
  179. $DEBUG = olddb unless olddb.nil?
  180. end
  181. #############################################################
  182. ### E X T R A A S S E R T I O N S
  183. #############################################################
  184. ### Negative of assert_respond_to
  185. def assert_not_respond_to( obj, meth )
  186. msg = "%s expected NOT to respond to '%s'" %
  187. [ obj.inspect, meth ]
  188. assert_block( msg ) {
  189. !obj.respond_to?( meth )
  190. }
  191. end
  192. ### Assert that the instance variable specified by +sym+ of an +object+
  193. ### is equal to the specified +value+. The '@' at the beginning of the
  194. ### +sym+ will be prepended if not present.
  195. def assert_ivar_equal( value, object, sym )
  196. sym = "@#{sym}".intern unless /^@/ =~ sym.to_s
  197. msg = "Instance variable '%s'\n\tof <%s>\n\texpected to be <%s>\n" %
  198. [ sym, object.inspect, value.inspect ]
  199. msg += "\tbut was: <%s>" % object.instance_variable_get(sym)
  200. assert_block( msg ) {
  201. value == object.instance_variable_get(sym)
  202. }
  203. end
  204. ### Assert that the specified +object+ has an instance variable which
  205. ### matches the specified +sym+. The '@' at the beginning of the +sym+
  206. ### will be prepended if not present.
  207. def assert_has_ivar( sym, object )
  208. sym = "@#{sym}" unless /^@/ =~ sym.to_s
  209. msg = "Object <%s> expected to have an instance variable <%s>" %
  210. [ object.inspect, sym ]
  211. assert_block( msg ) {
  212. object.instance_variables.include?( sym.to_s )
  213. }
  214. end
  215. end # class TestCase
  216. end # class BlueCloth