Module: Wrong::Assert

Included in:
MiniTest::Unit::TestCase, Wrong, Wrong
Defined in:
lib/wrong/assert.rb,
lib/wrong/message/test_context.rb,
lib/wrong/adapters/test_unit.rb

Defined Under Namespace

Classes: AssertionFailedError

Instance Method Summary collapse

Instance Method Details

#assert(*args, &block) ⇒ Object

Actual signature: assert(explanation = nil, depth = 0, &block)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wrong/assert.rb', line 21

def assert(*args, &block)
  # to notice (and fail fast from) odd recursion problem
  raise "Reentry bug while trying to assert(#{args.join(', ')})" if (@_inside_wrong_assert ||= nil)
  @_inside_wrong_assert = true

  if block.nil?
    begin
      super(*args) # if there's a framework assert method (sans block), then call it
    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
ensure
  @_inside_wrong_assert = false
end

#deny(*args, &block) ⇒ Object

Actual signature: deny(explanation = nil, depth = 0, &block)



41
42
43
44
45
46
47
48
49
# File 'lib/wrong/assert.rb', line 41

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_classObject



16
17
18
# File 'lib/wrong/assert.rb', line 16

def failure_class
  AssertionFailedError
end

#failure_message(method_sym, block, predicate) ⇒ Object

todo: integrate with / use Chunk somehow?



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wrong/message/test_context.rb', line 5

def failure_message(method_sym, block, predicate)
  upper_portion = super
  
  first_test_line = caller.find{|line|line =~ /(_test.rb|_spec.rb)/}
  raise "Can't find test or spec in call chain: #{caller.join('|')}" if first_test_line.nil?
  file, failure_line_number = first_test_line.split(":",2)

  lines = File.readlines(file)
  line_number = failure_line_number.to_i - 1
  to_show = []
  begin
    line = lines[line_number]
    to_show.unshift(line)
    line_number -= 1
  end while !(line =~ /^\s+(test|it)[ ]+/ || line =~ /^\s+def test_\w+/)

  to_show[to_show.length-1] = to_show[to_show.length-1].chomp + 
    "      ASSERTION FAILURE #{file}:#{failure_line_number.to_i}\n"
  
  upper_portion + "\n\n" + to_show.join
end

#increment_assertion_countObject

override (redefine) in adapter if necessary



59
60
# File 'lib/wrong/assert.rb', line 59

def increment_assertion_count
end