File: helpers_test.rb

package info (click to toggle)
ruby-shoulda-context 2.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: ruby: 1,712; sh: 200; makefile: 4
file content (155 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (2)
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'test_helper'

class HelpersTest < PARENT_TEST_CASE
  context "an array of values" do
    setup do
      @a = ['abc', 'def', 3]
    end

    [/b/, 'abc', 3].each do |x|
      should "contain #{x.inspect}" do
        assert_raises(ASSERTION_CLASS) do
          assert_does_not_contain @a, x
        end
        assert_contains @a, x
      end
    end

    should "not contain 'wtf'" do
      assert_raises(ASSERTION_CLASS) {assert_contains @a, 'wtf'}
      assert_does_not_contain @a, 'wtf'
    end

    should "be the same as another array, ordered differently" do
      assert_same_elements(@a, [3, "def", "abc"])
      assert_raises(ASSERTION_CLASS) do
        assert_same_elements(@a, [3, 3, "def", "abc"])
      end
      assert_same_elements([@a, "abc"].flatten, ["abc", 3, "def", "abc"])
      assert_raises(ASSERTION_CLASS) do
        assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
      end
    end

    should "only count the number of occurrences once for each unique value" do
      a1 = [@a, "abc"].flatten
      a1.expects(:select).times(3).returns(["abc", "abc"], ["def"], [3])
      assert_same_elements(a1, ["abc", 3, "def", "abc"])
    end
  end

  context "a matching matcher" do
    setup do
      @matcher = stub(
        "matcher",
        matches?: true,
        failure_message: "bad failure message",
        failure_message_when_negated: "big time failure"
      )
    end

    should "pass when given to assert_accepts with no message expectation" do
      assert_accepts @matcher, 'target'
    end

    should "pass when given to assert_accepts with a matching message" do
      assert_accepts @matcher, 'target', :message => /big time/
    end

    should "fail when given to assert_accepts with non-matching message" do
      assert_raises ASSERTION_CLASS do
        assert_accepts @matcher, 'target', :message => /small time/
      end
    end

    context "when given to assert_rejects" do
      context "and matcher has :does_not_match?" do
        setup do
          @error = nil
          begin
            @matcher.stubs(:matches?).returns(false)
            @matcher.stubs(:does_not_match?).returns(true)
            assert_rejects @matcher, 'target'
          rescue ASSERTION_CLASS => @error
          end
        end

        should "pass" do
          assert_nil @error
        end
      end

      context "and matcher does not have :does_not_match?" do
        setup do
          @error = nil
          begin
            assert_rejects @matcher, 'target'
          rescue ASSERTION_CLASS => @error
          end
        end

        should "fail" do
          refute_nil @error
        end

        should "use the error message from the matcher" do
          assert_equal 'big time failure', @error.message
        end
      end
    end
  end

  context "a non-matching matcher" do
    setup do
      @matcher = stub(
        "matcher",
        matches?: false,
        failure_message: "big time failure",
        failure_message_when_negated: "bad failure message"
      )
    end

    should "pass when given to assert_rejects with no message expectation" do
      assert_rejects @matcher, 'target'
    end

    should "pass when given to assert_rejects with a matching message" do
      assert_rejects @matcher, 'target', :message => /big time/
    end

    should "fail when given to assert_rejects with a non-matching message" do
      assert_raises ASSERTION_CLASS do
        assert_rejects @matcher, 'target', :message => /small time/
      end
    end

    context "when given to assert_accepts" do
      setup do
        begin
          assert_accepts @matcher, 'target'
        rescue ASSERTION_CLASS => @error
        end
      end

      should "fail" do
        refute_nil @error
      end

      should "use the error message from the matcher" do
        assert_equal 'big time failure', @error.message
      end
    end
  end

  should "assign context to a support matching on assert_accepts" do
    matcher = stub('matcher', :matches? => true)
    matcher.expects(:in_context).with(self)
    assert_accepts matcher, nil
  end

  should "assign context to a support matching on assert_rejects" do
    matcher = stub('matcher', :matches? => false)
    matcher.expects(:in_context).with(self)
    assert_rejects matcher, nil
  end
end