module TddDeploy::Assertions
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
Constants
- GROUP_ELT_TAG
- HEADER_ELT_TAG
- RESULT_ELT_TAG
Private Class Methods
# File lib/tdd_deploy/assertions.rb, line 200 def self.included(mod) Stats.test_results = {} end
Public Instance Methods
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
# File lib/tdd_deploy/assertions.rb, line 36 def assert key, predicate, msg assert_primative key, predicate, msg end
# File lib/tdd_deploy/assertions.rb, line 40 def assert_equal key, expect, value, msg assert_primative key, expect == value, msg end
# 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
# File lib/tdd_deploy/assertions.rb, line 49 def assert_nil key, value, msg assert_primative key, value.nil?, msg end
# File lib/tdd_deploy/assertions.rb, line 53 def assert_not_nil key, value, msg assert_primative key, !value.nil?, msg end
calls the block and passes only if 'block' raises 'exception'
# 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, like 'pass', is used to insert a failure message where an assertion is unnecessary
# File lib/tdd_deploy/assertions.rb, line 87 def fail key, msg assert_primative key, false, msg end
number of failures recorded under 'key'
# File lib/tdd_deploy/assertions.rb, line 154 def failure_count(key) return nil unless Stats.test_results[key] failure_messages(key).length end
returns all failure messages in same format as 'test_messages(key)'. this is simply: Stats.test_results[key]
.select { |tmp| !tmp }
# File lib/tdd_deploy/assertions.rb, line 174 def failure_messages(key) Stats.test_results[key].select { |tmp| !tmp[0] } end
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
# 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 is used to insert a passing message for cases where an assertion is unnecessary
# File lib/tdd_deploy/assertions.rb, line 82 def pass key, msg assert_primative key, true, msg end
refute assertions are simple negations of the corresponding assert
# File lib/tdd_deploy/assertions.rb, line 69 def refute key, predicate, msg assert_primative key, !predicate, msg end
# File lib/tdd_deploy/assertions.rb, line 73 def refute_equal key, expect, value, msg assert_primative key, expect != value, msg end
# File lib/tdd_deploy/assertions.rb, line 77 def refute_nil key, predicate, msg assert_primative key, !predicate, msg end
removes all failed test results
# 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_tests
clears all test results
# File lib/tdd_deploy/assertions.rb, line 126 def reset_tests Stats.test_results = {} end
number of tests recorded under 'key'
# 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
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
# File lib/tdd_deploy/assertions.rb, line 168 def test_messages(key) Stats.test_results[key] end
test_results
returns the test_results
hash
# File lib/tdd_deploy/assertions.rb, line 121 def test_results Stats.test_results end
total_failures
: total number of failures accross all keys
# 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_tests
: total number of tests accross all keys
# File lib/tdd_deploy/assertions.rb, line 149 def total_tests Stats.test_results.values.reduce(0) { |memo, obj| memo += obj.length } end
Private Instance Methods
# File lib/tdd_deploy/assertions.rb, line 194 def add_message(key, result, msg) Stats.test_results ||= {} Stats.test_results[key] ||= [] Stats.test_results[key].push([result, msg]) end
private methods
# File lib/tdd_deploy/assertions.rb, line 180 def assert_primative key, predicate, msg predicate ? test_passed(key, "Passed: #{msg}") : test_failed(key, "Failed: #{msg}") predicate end
# File lib/tdd_deploy/assertions.rb, line 105 def formatted_test_results_for_key key str = "<#{GROUP_ELT_TAG} class=\"test-result-group\" id=\"test-result-group-#{key}\">\n<#{HEADER_ELT_TAG} class=\"test-result-header\" id=\"test-result-header-#{key}\">Results for '#{key}'</#{HEADER_ELT_TAG}>\n" if failure_count(key) == 0 str += "<#{RESULT_ELT_TAG} class=\"test-result-summary-success\" id=\"test-result-summary-#{key}\">All #{test_count(key)} Tests Passed</#{RESULT_ELT_TAG}>\n" else str += "<#{RESULT_ELT_TAG} class=\"test-result-summary-failure\" id=\"test-result-summary-#{key}\">#{failure_count(key)} of #{test_count(key)} Tests Failed</#{RESULT_ELT_TAG}>\n" end toggle = true tmp = Stats.test_results[key].map { |msg| toggle = !toggle ; "<#{RESULT_ELT_TAG} class=\"#{(toggle ? "even" : "odd")}\">#{msg}</#{RESULT_ELT_TAG}>\n" } str += tmp.join("\n") + "\n" if Stats.test_results str + "</#{GROUP_ELT_TAG}>\n" end
test message handling
# File lib/tdd_deploy/assertions.rb, line 186 def test_failed(key, msg) add_message(key, false, msg) end
# File lib/tdd_deploy/assertions.rb, line 190 def test_passed(key, msg) add_message(key, true, msg) end