Module: Detest
- Extended by:
- Detest
- Included in:
- Detest
- Defined in:
- lib/detest.rb,
lib/detest/long.rb,
lib/detest/mini.rb,
lib/detest/spec.rb,
lib/detest/unit.rb,
lib/detest/inochi.rb
Defined Under Namespace
Modules: FailureDetails Classes: BINDINGS=Hash.new{|h,k|h[k]={}}, Suite
Constant Summary collapse
- Describe =
for hooks
D
- PROJECT =
Official name of this project.
'Detest'
- TAGLINE =
Short single-line description of this project.
'Assertion testing library for Ruby'
- WEBSITE =
Address of this project’s official home page.
'http://snk.tuxfamily.org/lib/detest/'
- VERSION =
Number of this release of this project.
'3.1.3'
- RELDATE =
Date of this release of this project.
'2011-04-22'
- INSTDIR =
Location of this release of this project.
File.('../../..', __FILE__)
- GEMDEPS =
RubyGems required by this project during runtime.
{}
- D =
allow before and after hooks to be specified via the following method syntax when this module is mixed-in:
D .<< { puts "before all nested tests" } D .< { puts "before each nested test" } D .> { puts "after each nested test" } D .>> { puts "after all nested tests" }
self
Class Attribute Summary collapse
-
.debug ⇒ Object
writeonly
Launch an interactive debugger during assertion failures so the user can investigate them?.
-
.stats ⇒ Object
readonly
Hash of counts of major events in test execution:.
-
.trace ⇒ Object
readonly
Hierarchical trace of all tests executed, where each test is represented by its description, is mapped to an Array of nested tests, and may contain zero or more assertion failures.
Class Method Summary collapse
-
.<(&block) ⇒ Object
Registers the given block to be executed before each nested test inside this test.
-
.<<(&block) ⇒ Object
Registers the given block to be executed before all nested tests inside this test.
-
.>(&block) ⇒ Object
Registers the given block to be executed after each nested test inside this test.
-
.>>(&block) ⇒ Object
Registers the given block to be executed after all nested tests inside this test.
-
.C(symbol, message = nil, &block) ⇒ Object
Asserts that the given symbol is thrown when the given block is executed.
-
.C!(symbol, message = nil, &block) ⇒ Object
Asserts that the given symbol is not thrown when the given block is executed.
-
.C?(symbol, message = nil, &block) ⇒ Boolean
Returns true if the given symbol is thrown when the given block is executed.
-
.D(*description, &block) ⇒ Object
Defines a new test composed of the given description and the given block to execute.
-
.D!(*description, &block) ⇒ Object
Defines a new test that is explicitly insulated from the tests that contain it and also from the top-level Ruby environment.
-
.E(*kinds_then_message, &block) ⇒ Object
Asserts that one of the given kinds of exceptions is raised when the given block is executed.
-
.E!(*kinds_then_message, &block) ⇒ Object
Asserts that one of the given kinds of exceptions is not raised when the given block is executed.
-
.E?(*kinds_then_message, &block) ⇒ Boolean
Returns true if one of the given kinds of exceptions is raised when the given block is executed.
-
.F?(condition = nil, message = nil, &block) ⇒ Boolean
Returns true if the result of the given block is either nil or false.
-
.I(*messages) ⇒ Object
Adds the given messages to the test execution report beneath the currently running test.
-
.I! ⇒ Object
Starts an interactive debugging session at the location where this method was called.
-
.info ⇒ Object
Returns the details of the failure that is currently being debugged by the user.
-
.inspect ⇒ Object
Description of this release of this project.
-
.N(condition = nil, message = nil, &block) ⇒ Object
Asserts that the given condition or the result of the given block is nil.
- .N!(condition = nil, message = nil, &block) ⇒ Object
- .N?(condition = nil, message = nil, &block) ⇒ Boolean
-
.reset ⇒ Object
Clear all test results that were recorded thus far.
-
.S(identifier, &block) ⇒ Object
Mechanism for sharing code between tests.
-
.S!(identifier, &block) ⇒ Object
Shares the given code block under the given identifier and then immediately injects that code block into the closest insulated Detest test that contains the call to this method.
-
.S?(identifier) ⇒ Boolean
Checks whether any code has been shared under the given identifier.
-
.start ⇒ Object
Executes all tests defined thus far and stores the results in Detest.trace and Detest.stats.
-
.stop ⇒ Object
Stops the execution of the Detest.start method or raises an exception if that method is not currently executing.
-
.T(condition = nil, message = nil, &block) ⇒ Object
(also: F!)
Asserts that the given condition or the result of the given block is neither nil nor false and returns that result.
-
.T!(condition = nil, message = nil, &block) ⇒ Object
(also: F)
Asserts that the given condition or the result of the given block is either nil or false and returns that result.
-
.T?(condition = nil, message = nil, &block) ⇒ Boolean
Returns true if the given condition or the result of the given block is neither nil nor false.
Instance Method Summary collapse
Class Attribute Details
.debug=(value) ⇒ Object
Launch an interactive debugger during assertion failures so the user can investigate them?
The default value is $DEBUG.
31 32 33 |
# File 'lib/detest.rb', line 31 def debug=(value) @debug = value end |
.stats ⇒ Object (readonly)
Hash of counts of major events in test execution:
- :time
-
Number of seconds elapsed for test execution.
- :pass
-
Number of assertions that held true.
- :fail
-
Number of assertions that did not hold true.
- :error
-
Number of exceptions that were not rescued.
48 49 50 |
# File 'lib/detest.rb', line 48 def stats @stats end |
.trace ⇒ Object (readonly)
Hierarchical trace of all tests executed, where each test is represented by its description, is mapped to an Array of nested tests, and may contain zero or more assertion failures.
Assertion failures are represented as a Hash:
- :fail
-
Description of the assertion failure.
- :call
-
Stack trace leading to the point of failure.
- :code
-
Source code surrounding the point of failure.
- :bind
-
Location where local variables in ‘:vars` were extracted.
- :vars
-
Local variables visible at the point of failure.
72 73 74 |
# File 'lib/detest.rb', line 72 def trace @trace end |
Class Method Details
.<(&block) ⇒ Object
Registers the given block to be executed before each nested test inside this test.
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/detest.rb', line 161 def <(klass = nil, &block) if klass # this method is being used as a check for inheritance # # NOTE: we cannot call super() here because this module # extends itself, thereby causing an infinite loop! # ancestors.include? klass else raise ArgumentError, 'block must be given' unless block @suite.before_each << block end end |
.<<(&block) ⇒ Object
Registers the given block to be executed before all nested tests inside this test.
204 205 206 207 |
# File 'lib/detest.rb', line 204 def << &block raise ArgumentError, 'block must be given' unless block @suite.before_all << block end |
.>(&block) ⇒ Object
Registers the given block to be executed after each nested test inside this test.
187 188 189 190 |
# File 'lib/detest.rb', line 187 def > &block raise ArgumentError, 'block must be given' unless block @suite.after_each << block end |
.>>(&block) ⇒ Object
Registers the given block to be executed after all nested tests inside this test.
221 222 223 224 |
# File 'lib/detest.rb', line 221 def >> &block raise ArgumentError, 'block must be given' unless block @suite.after_all << block end |
.C(symbol, message = nil, &block) ⇒ Object
Asserts that the given symbol is thrown when the given block is executed.
494 495 496 |
# File 'lib/detest.rb', line 494 def C symbol, = nil, &block assert_catch :assert, symbol, , &block end |
.C!(symbol, message = nil, &block) ⇒ Object
Asserts that the given symbol is not thrown when the given block is executed.
520 521 522 |
# File 'lib/detest.rb', line 520 def C! symbol, = nil, &block assert_catch :negate, symbol, , &block end |
.C?(symbol, message = nil, &block) ⇒ Boolean
Returns true if the given symbol is thrown when the given block is executed. Otherwise, returns false.
542 543 544 |
# File 'lib/detest.rb', line 542 def C? symbol, = nil, &block assert_catch :sample, symbol, , &block end |
.D(*description, &block) ⇒ Object
Defines a new test composed of the given description and the given block to execute.
This test may contain nested tests.
Tests at the outer-most level are automatically insulated from the top-level Ruby environment.
106 107 108 |
# File 'lib/detest.rb', line 106 def D *description, &block create_test @tests.empty?, *description, &block end |
.D!(*description, &block) ⇒ Object
Defines a new test that is explicitly insulated from the tests that contain it and also from the top-level Ruby environment.
This test may contain nested tests.
143 144 145 |
# File 'lib/detest.rb', line 143 def D! *description, &block create_test true, *description, &block end |
.E(*kinds_then_message, &block) ⇒ Object
Asserts that one of the given kinds of exceptions is raised when the given block is executed.
401 402 403 |
# File 'lib/detest.rb', line 401 def E *, &block assert_raise :assert, *, &block end |
.E!(*kinds_then_message, &block) ⇒ Object
Asserts that one of the given kinds of exceptions is not raised when the given block is executed.
428 429 430 |
# File 'lib/detest.rb', line 428 def E! *, &block assert_raise :negate, *, &block end |
.E?(*kinds_then_message, &block) ⇒ Boolean
Returns true if one of the given kinds of exceptions is raised when the given block is executed. Otherwise, returns false.
462 463 464 |
# File 'lib/detest.rb', line 462 def E? *, &block assert_raise :sample, *, &block end |
.F?(condition = nil, message = nil, &block) ⇒ Boolean
Returns true if the result of the given block is either nil or false. Otherwise, returns false.
323 324 325 |
# File 'lib/detest.rb', line 323 def F? condition = nil, = nil, &block assert_yield :sample, false, condition, , &block end |
.I(*messages) ⇒ Object
Adds the given messages to the test execution report beneath the currently running test.
You can think of “I” as to “inform” the user.
564 565 566 |
# File 'lib/detest.rb', line 564 def I * @trace.concat end |
.I! ⇒ Object
Starts an interactive debugging session at the location where this method was called.
You can think of “I!” as to “investigate” the program.
574 575 576 |
# File 'lib/detest.rb', line 574 def I! debug end |
.info ⇒ Object
Returns the details of the failure that is currently being debugged by the user.
718 719 720 |
# File 'lib/detest.rb', line 718 def info @trace.last end |
.inspect ⇒ Object
Description of this release of this project.
31 32 33 |
# File 'lib/detest/inochi.rb', line 31 def self.inspect "#{PROJECT} #{VERSION} (#{RELDATE})" end |
.N(condition = nil, message = nil, &block) ⇒ Object
Asserts that the given condition or the result of the given block is nil.
351 352 353 |
# File 'lib/detest.rb', line 351 def N condition = nil, = nil, &block assert_yield :assert, nil, condition, , &block end |
.N!(condition = nil, message = nil, &block) ⇒ Object
355 356 357 |
# File 'lib/detest.rb', line 355 def N! condition = nil, = nil, &block assert_yield :negate, nil, condition, , &block end |
.N?(condition = nil, message = nil, &block) ⇒ Boolean
359 360 361 |
# File 'lib/detest.rb', line 359 def N? condition = nil, = nil, &block assert_yield :sample, nil, condition, , &block end |
.reset ⇒ Object
Clear all test results that were recorded thus far.
709 710 711 712 |
# File 'lib/detest.rb', line 709 def reset @stats.clear @trace.clear end |
.S(identifier, &block) ⇒ Object
Mechanism for sharing code between tests.
If a block is given, it is shared under the given identifier. Otherwise, the code block that was previously shared under the given identifier is injected into the closest insulated Detest test that contains the call to this method.
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/detest.rb', line 607 def S identifier, &block if block_given? if already_shared = @share[identifier] raise ArgumentError, "A code block #{already_shared.inspect} has "\ "already been shared under the identifier #{identifier.inspect}." end @share[identifier] = block elsif block = @share[identifier] if @tests.empty? raise "Cannot inject code block #{block.inspect} shared under "\ "identifier #{identifier.inspect} outside of a Detest test." else # find the closest insulated parent test; this should always # succeed because root-level tests are insulated by default test = @tests.reverse.find {|t| t.sandbox } or raise IndexError test.sandbox.instance_eval(&block) end else raise ArgumentError, "No code block is shared under identifier "\ "#{identifier.inspect}." end end |
.S!(identifier, &block) ⇒ Object
Shares the given code block under the given identifier and then immediately injects that code block into the closest insulated Detest test that contains the call to this method.
653 654 655 656 657 |
# File 'lib/detest.rb', line 653 def S! identifier, &block raise 'block must be given' unless block_given? S identifier, &block S identifier end |
.S?(identifier) ⇒ Boolean
Checks whether any code has been shared under the given identifier.
662 663 664 |
# File 'lib/detest.rb', line 662 def S? identifier @share.key? identifier end |
.start ⇒ Object
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/detest.rb', line 670 def start # execute the tests start_time = Time.now catch :DIFECTS_STOP do BINDINGS.track do execute end end @stats[:time] = Time.now - start_time # print test results unless @stats.key? :fail or @stats.key? :error # # show execution trace only if all tests passed. # otherwise, we will be repeating already printed # failure details and obstructing the developer! # display @trace end display @stats ensure @tests.clear @share.clear @files.clear end |
.stop ⇒ Object
Stops the execution of the start method or raises an exception if that method is not currently executing.
702 703 704 |
# File 'lib/detest.rb', line 702 def stop throw :DIFECTS_STOP end |
.T(condition = nil, message = nil, &block) ⇒ Object Also known as: F!
Asserts that the given condition or the result of the given block is neither nil nor false and returns that result.
251 252 253 |
# File 'lib/detest.rb', line 251 def T condition = nil, = nil, &block assert_yield :assert, true, condition, , &block end |
.T!(condition = nil, message = nil, &block) ⇒ Object Also known as: F
Asserts that the given condition or the result of the given block is either nil or false and returns that result.
274 275 276 |
# File 'lib/detest.rb', line 274 def T! condition = nil, = nil, &block assert_yield :negate, true, condition, , &block end |
.T?(condition = nil, message = nil, &block) ⇒ Boolean
Returns true if the given condition or the result of the given block is neither nil nor false. Otherwise, returns false.
299 300 301 |
# File 'lib/detest.rb', line 299 def T? condition = nil, = nil, &block assert_yield :sample, true, condition, , &block end |
Instance Method Details
#after(what, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/detest/spec.rb', line 21 def after what, &block meth = case what when :each then :> when :all then :>> else raise ArgumentError, what end send meth, &block end |
#before(what, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/detest/spec.rb', line 10 def before what, &block meth = case what when :each then :< when :all then :<< else raise ArgumentError, what end send meth, &block end |