Module: Wrong::Assert
- Included in:
- MiniTest::Unit::TestCase, Test::Unit::TestCase
- Defined in:
- lib/wrong/assert.rb,
lib/wrong/message/array_diff.rb,
lib/wrong/message/string_diff.rb,
lib/wrong/message/test_context.rb
Defined Under Namespace
Modules: ArrayDiff, StringDiff Classes: AssertionFailedError
Instance Method Summary collapse
-
#assert(*args, &block) ⇒ Object
Actual signature: assert(explanation = nil, depth = 0, block).
-
#capturing(*streams) ⇒ Object
Usage: capturing { puts “hi” } => “hin” capturing(:stderr) { $stderr.puts “hi” } => “hin” out, err = capturing(:stdout, :stderr) { … }.
-
#deny(*args, &block) ⇒ Object
Actual signature: deny(explanation = nil, depth = 0, block).
- #failure_class ⇒ Object
-
#failure_message(method_sym, block, predicate) ⇒ Object
todo: integrate with / use Chunk somehow?.
- #rescuing ⇒ Object
Instance Method Details
#assert(*args, &block) ⇒ Object
Actual signature: assert(explanation = nil, depth = 0, block)
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wrong/assert.rb', line 26 def assert(*args, &block) if block.nil? begin super rescue NoMethodError => e # note: we're not raising an AssertionFailedError because this is a programmer error, not a failed assertion raise "You must pass a block to Wrong's assert and deny methods" end else aver(:assert, *args, &block) end end |
#capturing(*streams) ⇒ Object
Usage: capturing { puts “hi” } => “hin” capturing(:stderr) { $stderr.puts “hi” } => “hin” out, err = capturing(:stdout, :stderr) { … }
see www.justskins.com/forums/closing-stderr-105096.html for more explanation
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wrong/assert.rb', line 66 def capturing(*streams) streams = [:stdout] if streams.empty? original = {} captured = {} # reassign the $ variable (which is used by well-behaved code e.g. puts) streams.each do |stream| original[stream] = (stream == :stdout ? $stdout : $stderr) captured[stream] = StringIO.new case stream when :stdout $stdout = captured[stream] when :stderr $stderr = captured[stream] end end yield # return either one string, or an array of two strings if streams.size == 1 captured[streams.first].string else [captured[streams[0]].string, captured[streams[1]].string] end ensure streams.each do |stream| # bail if stream was reassigned inside the block if (stream == :stdout ? $stdout : $stderr) != captured[stream] raise "#{stream} was reassigned while being captured" end # support nested calls to capturing original[stream] << captured[stream].string if original[stream].is_a? StringIO case stream when :stdout $stdout = original[stream] when :stderr $stderr = original[stream] end end end |
#deny(*args, &block) ⇒ Object
Actual signature: deny(explanation = nil, depth = 0, block)
40 41 42 43 44 45 46 47 48 |
# File 'lib/wrong/assert.rb', line 40 def deny(*args, &block) if block.nil? test = args.first msg = args[1] assert !test, msg # this makes it get passed up to the framework else aver(:deny, *args, &block) end end |
#failure_class ⇒ Object
21 22 23 |
# File 'lib/wrong/assert.rb', line 21 def failure_class AssertionFailedError end |
#failure_message(method_sym, block, predicate) ⇒ Object
todo: integrate with / use Chunk somehow?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/wrong/message/test_context.rb', line 4 def (method_sym, block, predicate) = super if predicate.is_a?(Predicated::Equal) && predicate.left.is_a?(Enumerable) && predicate.right.is_a?(Enumerable) diffs = Diff::LCS.sdiff(predicate.left, predicate.right) # left_offset = 0 left_arr = [] right_arr = [] diff_arr = [] diffs.each do |diff| left_elem = diff.old_element.nil? ? "nil" : diff.old_element.inspect right_elem = diff.new_element.nil? ? "nil" : diff.new_element.inspect max_length = [left_elem.length, right_elem.length].max left_arr << left_elem.ljust(max_length) unless diff.action == "+" right_arr << right_elem.ljust(max_length) unless diff.action == "-" diff_arr << (diff.action == "=" ? " ".ljust(max_length) : "^".ljust(max_length)) end left_str, right_str, diff_str = ArrayDiff.compute_and_format(predicate.left, predicate.right) << "\n\narray diff:\n" << left_str + "\n" << right_str + "\n" << diff_str + "\n" end end |
#rescuing ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/wrong/assert.rb', line 50 def rescuing error = nil begin yield rescue Exception, RuntimeError => e error = e end error end |