Module: TddDeploy::Assertions

Included in:
Base, DeployTestMethods
Defined in:
lib/tdd_deploy/assertions.rb

Overview

TddDeploy re-implements popular assertions so that they can be used in multi-host testing.

These assertions differ from usual TDD assertions in two ways:

  • all assertions are ‘keyed’ - all the test results are grouped by

keys.

  • that they do not stop tests after failing. Rather they continue and

accumulate failure counts and messages which can be displayed using *announce_formatted_test_results()*.

all assertions return boolean true or false

Defined Under Namespace

Classes: Stats

Constant Summary collapse

GROUP_ELT_TAG =
'ul'
HEADER_ELT_TAG =
'h2'
RESULT_ELT_TAG =
'li'

Instance Method Summary collapse

Instance Method Details

#assert(key, predicate, msg) ⇒ Object

Assertions all return true or false. The last parameter is always the assertions message and is optional.

assert(key, prediccate, msg) returns true if prediccate is true, else adds msg to failure messages and returns false



36
37
38
# File 'lib/tdd_deploy/assertions.rb', line 36

def assert key, predicate, msg
  assert_primative key, predicate, msg
end

#assert_equal(key, expect, value, msg) ⇒ Object



40
41
42
# File 'lib/tdd_deploy/assertions.rb', line 40

def assert_equal key, expect, value, msg
  assert_primative key, expect == value, msg
end

#assert_match(key, regx, value, msg) ⇒ Object



44
45
46
47
# File 'lib/tdd_deploy/assertions.rb', line 44

def assert_match key, regx, value, msg
  regx = Regexp.new(regx.to_s) unless regx.instance_of? Regexp
  assert_primative key, regx.match(value), msg
end

#assert_nil(key, value, msg) ⇒ Object



49
50
51
# File 'lib/tdd_deploy/assertions.rb', line 49

def assert_nil key, value, msg
  assert_primative key, value.nil?, msg
end

#assert_not_nil(key, value, msg) ⇒ Object



53
54
55
# File 'lib/tdd_deploy/assertions.rb', line 53

def assert_not_nil key, value, msg
  assert_primative key, !value.nil?, msg
end

#assert_raises(key, exception = Exception, msg, &block) ⇒ Object

calls the block and passes only if ‘block’ raises ‘exception’



58
59
60
61
62
63
64
65
66
# File 'lib/tdd_deploy/assertions.rb', line 58

def assert_raises key, exception = Exception, msg, &block
  begin
    block.call
  rescue exception => e
    pass key, msg
    return true
  end
  fail key, msg
end

#fail(key, msg) ⇒ Object

fail, like ‘pass’, is used to insert a failure message where an assertion is unnecessary



87
88
89
# File 'lib/tdd_deploy/assertions.rb', line 87

def fail key, msg
  assert_primative key, false, msg
end

#failure_count(key) ⇒ Object

number of failures recorded under ‘key’



154
155
156
157
# File 'lib/tdd_deploy/assertions.rb', line 154

def failure_count(key)
  return nil unless Stats.test_results[key]
  failure_messages(key).length
end

#failure_messages(key) ⇒ Object

returns all failure messages in same format as ‘test_messages(key)’. this is simply: Stats.test_results.select { |tmp| !tmp }



174
175
176
# File 'lib/tdd_deploy/assertions.rb', line 174

def failure_messages(key)
  Stats.test_results[key].select { |tmp| !tmp[0] }
end

#formatted_test_resultsObject

don’t use formatted_test_results or formatted_test_results_for_key use the supplied test_results.html.erb template instead formatted_test_results returns the string string of all test messages



96
97
98
99
100
101
102
# File 'lib/tdd_deploy/assertions.rb', line 96

def formatted_test_results
  str = ''
  Stats.test_results.keys.sort.each do |key|
    str += formatted_test_results_for_key(key)
  end
  str
end

#pass(key, msg) ⇒ Object

pass is used to insert a passing message for cases where an assertion is unnecessary



82
83
84
# File 'lib/tdd_deploy/assertions.rb', line 82

def pass key, msg
  assert_primative key, true, msg
end

#refute(key, predicate, msg) ⇒ Object

refute assertions are simple negations of the corresponding assert



69
70
71
# File 'lib/tdd_deploy/assertions.rb', line 69

def refute key, predicate, msg
  assert_primative key, !predicate, msg
end

#refute_equal(key, expect, value, msg) ⇒ Object



73
74
75
# File 'lib/tdd_deploy/assertions.rb', line 73

def refute_equal key, expect, value, msg
  assert_primative key, expect != value, msg
end

#refute_nil(key, predicate, msg) ⇒ Object



77
78
79
# File 'lib/tdd_deploy/assertions.rb', line 77

def refute_nil key, predicate, msg  
  assert_primative key, !predicate, msg
end

#remove_failed_testsObject

removes all failed test results



131
132
133
134
135
136
137
# File 'lib/tdd_deploy/assertions.rb', line 131

def remove_failed_tests
  tmp = {}
  Stats.test_results.each do |host, results|
    tmp[host] = results.select { |tmp| tmp[0] }.uniq.sort
  end
  Stats.test_results = tmp
end

#reset_testsObject

reset_tests clears all test results



126
127
128
# File 'lib/tdd_deploy/assertions.rb', line 126

def reset_tests
  Stats.test_results = {}
end

#test_count(key) ⇒ Object

number of tests recorded under ‘key’



160
161
162
163
# File 'lib/tdd_deploy/assertions.rb', line 160

def test_count(key)
  return nil unless Stats.test_results[key]
  Stats.test_results[key].length
end

#test_messages(key) ⇒ Object

all tests messages saved under ‘key’, returns an array of 2-element arrays. first element is ‘true’ or ‘false’ - indicates passed or failed second element is the success message



168
169
170
# File 'lib/tdd_deploy/assertions.rb', line 168

def test_messages(key)
  Stats.test_results[key]
end

#test_resultsObject

test_results returns the test_results hash



121
122
123
# File 'lib/tdd_deploy/assertions.rb', line 121

def test_results
  Stats.test_results
end

#total_failuresObject

total_failures: total number of failures accross all keys



140
141
142
143
144
145
146
# File 'lib/tdd_deploy/assertions.rb', line 140

def total_failures
  count = 0
  Stats.test_results.values.each do |messages|
    count += messages.select { |msg| !msg[0] }.length
  end
  count
end

#total_testsObject

total_tests: total number of tests accross all keys



149
150
151
# File 'lib/tdd_deploy/assertions.rb', line 149

def total_tests
  Stats.test_results.values.reduce(0) { |memo, obj| memo += obj.length }
end