Class BareTest::Assertion
In: lib/baretest/assertion/failure.rb
lib/baretest/assertion/skip.rb
lib/baretest/assertion/support.rb
lib/baretest/assertion/context.rb
lib/baretest/assertion.rb
Parent: Object

Defines an assertion An assertion belongs to a suite and consists of a description and a block. To verify the assertion, the suite‘s (and its ancestors) setup blocks are executed, then the assertions block is executed and after that, the suite‘s (and ancestors) teardown blocks are invoked.

An assertion has 5 possible states, see Assertion#status for a list of them.

There are various helper methods in BareTest::Assertion::Support which help you defining nicer diagnostics or just easier ways to test common scenarios. The following are test helpers:

  • Kernel#raises(exception_class=StandardError)
  • Kernel#within_delta(a, b, delta)
  • Kernel#equal_unordered(a,b)
  • Enumerable#equal_unordered(other)

Methods

Classes and Modules

Module BareTest::Assertion::Support
Class BareTest::Assertion::Context
Class BareTest::Assertion::Failure
Class BareTest::Assertion::Skip

Constants

PassthroughExceptions = [NoMemoryError, SignalException, Interrupt, SystemExit]   The exceptions baretest will not rescue (NoMemoryError, SignalException, Interrupt and SystemExit)

Attributes

block  [R]  The block specifying the assertion
code  [RW]  The file this assertion is specified in. Not contructed by Assertion itself.
description  [R]  The description of this assertion.
file  [RW]  The file this assertion is specified in. Not contructed by Assertion itself.
line  [RW]  The line this assertion is specified on. Not contructed by Assertion itself.
lines  [RW]  The lines this assertion spans. Not contructed by Assertion itself.
suite  [R]  The suite this assertion belongs to

Public Class methods

suite:The suite the Assertion belongs to
description:A descriptive string about what this Assertion tests.
&block:The block definition. Without one, the Assertion will have a :pending status.

[Source]

    # File lib/baretest/assertion.rb, line 63
63:     def initialize(suite, description, opt=nil, &block)
64:       @suite       = suite
65:       @description = (description || "No description given")
66:       @block       = block
67:       @skipped     = false
68:       if opt then
69:         skip_reason = opt[:skip]
70:         skip(skip_reason == true ? "Tagged as skipped" : skip_reason) if skip_reason
71:       end
72:     end

Public Instance methods

Returns a Status Executes with_setup, then the assertions defining block, and in the end and_teardown. Usually with_setup and and_teardown are supplied by the containing suite.

[Source]

     # File lib/baretest/assertion.rb, line 112
112:     def execute(with_setup=nil, and_teardown=nil)
113:       if @skipped then
114:         status = Status.new(self, :manually_skipped, nil, @skipped)
115:       elsif !@block
116:         status = Status.new(self, :pending)
117:       else
118:         handlers        = @suite ? @suite.ancestors.inject({}) { |handlers, suite| handlers.merge(suite.verification_exception_handlers) } : nil
119:         context         = ::BareTest::Assertion::Context.new(self)
120:         status          = execute_phase(:setup, context, with_setup.map { |s| [[s.value], s.block] }) if with_setup
121:         setup_failed    = status
122:         status          = execute_phase(:exercise, context, @block, handlers) unless status
123:         teardown_status = execute_phase(:teardown, context, and_teardown.map { |t| [nil, t] }) unless (setup_failed || !and_teardown)
124:         status = status || teardown_status || Status.new(self, :success, context)
125:       end
126: 
127:       status
128:     end

A phase can result in either success, skip, failure or error Execute_phase will simply return nil upon success, all other cases will generate a full Status instance. This is for practical reasons - it means you can go through several phases, looking for the first non-nil one.

[Source]

     # File lib/baretest/assertion.rb, line 135
135:     def execute_phase(name, context, code, handlers=nil)
136:       status         = nil
137:       skip_reason    = nil
138:       failure_reason = nil
139:       exception      = nil
140: 
141:       begin
142:         if code.is_a?(Array) then
143:           code.each do |args, block| context.instance_exec(*args, &block) end
144:         else
145:           unless context.instance_eval(&code)
146:             failure_reason = "Assertion failed" 
147:             status         = :failure
148:           end
149:         end
150:       rescue *PassthroughExceptions
151:         raise # passthrough-exceptions must be passed through
152:       rescue ::BareTest::Assertion::Failure => failure
153:         status         = :failure
154:         failure_reason = failure.message
155:       rescue ::BareTest::Assertion::Skip => skip
156:         status         = :manually_skipped
157:         skip_reason    = skip.message
158:       rescue Exception => exception
159:         exception_class = exception.class
160:         handled_by      = handlers && handlers.find { |handling, handler| exception_class <= handling }
161:         if handled_by then
162:           handler       = handled_by.last
163:           return handler.call(self, context, exception) # FIXME: ugly mid-method return - work around it returning a complete Status
164:         end
165:         status         = :error
166:         failure_reason = "An error occurred during #{name}: #{exception}"
167:         exception      = exception
168:       end
169: 
170:       status && Status.new(self, status, context, skip_reason, failure_reason, exception)
171:     end

An ID, usable for persistence

[Source]

    # File lib/baretest/assertion.rb, line 75
75:     def id
76:       @id ||= [
77:         @description,
78:         *(@suite && @suite.ancestors.map { |suite| suite.description })
79:       ].compact.join("\f")
80:     end

The description allows substitutes in the form ":identifier" and ":{identifier}" (the latter in case of ajanced characters that don‘t belong to the identifier, like ":{identifier}stuff"). This method will interploate those substitutes. This is relevant with regards to setup variants.

[Source]

     # File lib/baretest/assertion.rb, line 98
 98:     def interpolated_description(substitutes)
 99:       if substitutes.empty? then
100:         @description
101:       else
102:         @description.gsub(/:(?:#{substitutes.keys.join('|')})\b/) { |m|
103:           substitutes[m[1..-1].to_sym]
104:         }
105:       end
106:     end

Marks this assertion as manually skipped.

[Source]

    # File lib/baretest/assertion.rb, line 88
88:     def skip(reason=nil)
89:       @skipped ||= []
90:       @skipped  |= reason ? Array(reason) : ['Manually skipped']
91:     end

Returns whether this assertion has been marked as manually skipped.

[Source]

    # File lib/baretest/assertion.rb, line 83
83:     def skipped?
84:       !!@skipped
85:     end

[Validate]