Module: Tap::Test::TapTest

Defined in:
lib/tap/test/tap_test.rb

Overview

Tap-specific testing methods to help with testing Tasks, such as the checking of audits and test-specific modification of application configuration.

Class Methods

See Test::Unit::TestCase for documentation of the class methods added by TapTest.

Defined Under Namespace

Classes: ExpAudit, ExpMerge, Tracer

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Returns the test-method-specific application.



40
41
42
# File 'lib/tap/test/tap_test.rb', line 40

def app
  @app
end

Instance Method Details

#app_configObject

The configurations used to initialize self.app



201
202
203
# File 'lib/tap/test/tap_test.rb', line 201

def app_config
  method_root.config.to_hash.merge(:quiet => true, :debug => true)
end

#assert_audit_equal(expected, audit, nesting = []) ⇒ Object

Asserts that an audit is as expected. The expected audit should be an ExpAudit (just a subclass of Array) that records the sources and the values at each step in the audit trail. Proc objects can be provided in place of expected records that are hard or impossible to provide directly; the Proc will be used to validate the actual record. Merges must be marked in the ExpAudit using ExpMerge.

Simple assertion:

a = Tap::Support::Audit.new
a._record(:a, 'a')
a._record(:b, 'b')

e = ExpAudit[[:a, 'a'], [:b, 'b']]
assert_audit_equal(e, a)

Assertion validating a record with a Proc (any number of records can be validated with a Proc):

a = Tap::Support::Audit.new
a._record(:a, 'a')
a._record(:b, 'b')

e = ExpAudit[
     lambda {|source, value| source == :a && value == 'a'},
    [:b, 'b']]
assert_audit_equal(e, a)

Assertion with merge:

a = Tap::Support::Audit.new
a._record(:a, 'a')
a._record(:b, 'b')

b = Tap::Support::Audit.new
b._record(:c, 'c')
b._record(:d, 'd')

c = Tap::Support::Audit.merge(a,b)
c._record(:e, 'e')
c._record(:f, 'f')

ea = ExpAudit[[:a, "a"], [:b, "b"]]
eb = ExpAudit[[:c, "c"], [:d, "d"]]
e = ExpAudit[ExpMerge[ea, eb], [:e, "e"], [:f, "f"]]

assert_audit_equal(e, c)

When assert_audit_equal fails, a string of indicies is provided to help locate which record was unequal. For instance in the last example, say we used:

ea = ExpAudit[[:a, "a"], [:b, "FLUNK"]]
eb = ExpAudit[[:c, "c"], [:d, "d"]]
e = ExpAudit[ExpMerge[ea, eb], [:e, "e"], [:f, "f"]]

The failure message will read something like ‘unequal record 0:0:1’ indicating it was e[0] that failed. Working through it, remembering that ExpAudit and ExpMerge are just subclasses of Array:

e              # => ExpAudit[ExpMerge[ea, eb], [:e, "e"], [:f, "f"]]
e[0]           # => ExpMerge[ea, eb]
e[0][0]        # => ExpAudit[[:a, "a"], [:b, "FLUNK"]]
e[0][0][1]     # => [:b, "FLUNK"]


176
177
178
179
# File 'lib/tap/test/tap_test.rb', line 176

def assert_audit_equal(expected, audit, nesting=[])
  actual = audit._collect_records {|source, value| [source, value]}
  assert_audit_records_equal(expected, actual, nesting)
end

#assert_audit_records_equal(expected, actual, nesting = []) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tap/test/tap_test.rb', line 181

def assert_audit_records_equal(expected, actual, nesting=[])
  assert_equal ExpAudit, expected.class
  assert_equal expected.length, actual.length, "unequal number of records"
  
  expected.each_with_index do |exp_record, i|
    case exp_record
    when ExpMerge
      flunk "empty merge #{(nesting + [i]).join(':')}" if exp_record.empty?
      exp_record.each_with_index do |exp_audit, j|
        assert_audit_records_equal(exp_audit, actual[i][j], nesting + [i,j]) 
      end
    when Proc
      assert exp_record.call(*actual[i]), "unconfirmed record #{(nesting + [i]).join(':')}"
    else
      assert_equal exp_record, actual[i], "unequal record #{(nesting + [i]).join(':')}"
    end
  end
end

#assert_audits_equal(expected, audits) ⇒ Object

Asserts that an array of audits are all equal, basically feeding each pair of audits to assert_audit_equal.



103
104
105
106
107
108
# File 'lib/tap/test/tap_test.rb', line 103

def assert_audits_equal(expected, audits)
  error_msg = "expected <#{audits.length}> audits, but was <#{expected.length}>"
  Utils.each_pair_with_index(expected, audits, error_msg) do |exp, audit, index|
    assert_audit_equal(exp, audit, [index])
  end
end

#setupObject

Setup creates a test-method-specific application that is initialized to the method_root, and uses the directories and absolute paths from trs (the test root structure, see Tap::Test::FileTest).

Also makes sure Tap::App.instance returns the test method app.



47
48
49
50
51
# File 'lib/tap/test/tap_test.rb', line 47

def setup
  super
  @app = Tap::App.new(app_config)
  Tap::App.instance = @app
end

#with_config(config = {}, app = self.app, &block) ⇒ Object

Reconfigures app with the input configurations for the duration of the block.

app = Tap::App.new(:quiet => true, :debug => false)
with_config({:quiet => false}, app) do 
  app.quiet                    # => false
  app.debug                    # => false
end

app.quiet                      # => true
app.debug                      # => false


217
218
219
220
221
222
223
224
225
226
227
# File 'lib/tap/test/tap_test.rb', line 217

def with_config(config={}, app=self.app, &block)
  begin
    hold = app.config.to_hash
    
    app.reconfigure(config)
    
    yield block if block_given?
  ensure
    app.send(:initialize_config, hold)
  end
end