Module: MiniTest::Spec::Magic::Assertions

Included in:
Test::Unit::TestCase
Defined in:
lib/minitest/spec/magic/assertions.rb

Overview

Shamelessly “stolen” from github.com/thoughtbot/shoulda-context

Adds the following expectations to MiniTest:

array.must_be_same_as(other) # => see #assert_same_elements
array.must_contain(item) # => see #assert_contains
array.wont_contain(item) # => see #assert_does_not_contain

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.includedObject



11
12
13
14
15
16
17
18
# File 'lib/minitest/spec/magic/assertions.rb', line 11

def self.included(*)
  MiniTest::Expectations.tap do |x|
    x.infect_an_assertion :assert_contains, :must_contain, :reverse
    x.infect_an_assertion :assert_does_not_contain, :wont_contain, :reverse
    x.infect_an_assertion :assert_same_elements, :must_be_same_as
  end
  super
end

Instance Method Details

#assert_accepts(matcher, target, options = {}) ⇒ Object

Asserts that the given matcher returns true when target is passed to #matches?



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/minitest/spec/magic/assertions.rb', line 65

def assert_accepts(matcher, target, options = {})
  if matcher.respond_to?(:in_context)
    matcher.in_context(self)
  end

  if matcher.matches?(target)
    assert_block { true }
    if options[:message]
      assert_match options[:message], matcher.negative_failure_message
    end
  else
    assert_block(matcher.failure_message) { false }
  end
end

#assert_contains(collection, x, extra_msg = "") ⇒ Object

Asserts that the given collection contains item x. If x is a regular expression, ensure that at least one element from the collection matches x. extra_msg is appended to the error message if the assertion fails.

assert_contains(['a', '1'], /\d/) => passes
assert_contains(['a', '1'], 'a') => passes
assert_contains(['a', '1'], /not there/) => fails


40
41
42
43
44
45
46
47
48
49
# File 'lib/minitest/spec/magic/assertions.rb', line 40

def assert_contains(collection, x, extra_msg = "")
  collection = [collection] unless collection.is_a?(Array)
  msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
  case x
  when Regexp
    assert(collection.detect { |e| e =~ x }, msg)
  else
    assert(collection.include?(x), msg)
  end
end

#assert_does_not_contain(collection, x, extra_msg = "") ⇒ Object

Asserts that the given collection does not contain item x. If x is a regular expression, ensure that none of the elements from the collection match x.



53
54
55
56
57
58
59
60
61
62
# File 'lib/minitest/spec/magic/assertions.rb', line 53

def assert_does_not_contain(collection, x, extra_msg = "")
  collection = [collection] unless collection.is_a?(Array)
  msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
  case x
  when Regexp
    assert(!collection.detect { |e| e =~ x }, msg)
  else
    assert(!collection.include?(x), msg)
  end
end

#assert_rejects(matcher, target, options = {}) ⇒ Object

Asserts that the given matcher returns false when target is passed to #matches?



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/minitest/spec/magic/assertions.rb', line 81

def assert_rejects(matcher, target, options = {})
  if matcher.respond_to?(:in_context)
    matcher.in_context(self)
  end

  unless matcher.matches?(target)
    assert_block { true }
    if options[:message]
      assert_match options[:message], matcher.failure_message
    end
  else
    assert_block(matcher.negative_failure_message) { false }
  end
end

#assert_same_elements(a1, a2, msg = nil) ⇒ Object

Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.

assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes


23
24
25
26
27
28
29
30
31
32
# File 'lib/minitest/spec/magic/assertions.rb', line 23

def assert_same_elements(a1, a2, msg = nil)
  [:select, :inject, :size].each do |m|
    [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array?  It doesn't respond to #{m}.") }
  end

  assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
  assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }

  assert_equal(a1h, a2h, msg)
end