|
| 1 | +require "spec_helper" |
| 2 | + |
| 3 | +RSpec.describe "Integration with describable matchers not handled specially", |
| 4 | + type: :integration do |
| 5 | + context "when the expected value contains a built-in matcher (not an aliased matcher)" do |
| 6 | + it "produces the correct failure message when used in the positive" do |
| 7 | + as_both_colored_and_uncolored do |color_enabled| |
| 8 | + snippet = <<~TEST.strip |
| 9 | + actual = { |
| 10 | + number: "not a number" |
| 11 | + } |
| 12 | + expected = hash_including( |
| 13 | + number: be_a(Numeric) |
| 14 | + ) |
| 15 | + expect(actual).to match(expected) |
| 16 | + TEST |
| 17 | + program = make_plain_test_program(snippet, color_enabled: color_enabled) |
| 18 | + |
| 19 | + expected_output = |
| 20 | + build_expected_output( |
| 21 | + color_enabled: color_enabled, |
| 22 | + snippet: "expect(actual).to match(expected)", |
| 23 | + expectation: |
| 24 | + proc do |
| 25 | + line do |
| 26 | + plain "Expected " |
| 27 | + actual %|{ number: "not a number" }| |
| 28 | + plain " to match " |
| 29 | + expected "#<a hash including (number: #<be a kind of Numeric>)>" |
| 30 | + plain "." |
| 31 | + end |
| 32 | + end, |
| 33 | + diff: |
| 34 | + proc do |
| 35 | + plain_line " {" |
| 36 | + expected_line "- number: #<be a kind of Numeric>" |
| 37 | + actual_line %|+ number: "not a number"| |
| 38 | + plain_line " }" |
| 39 | + end |
| 40 | + ) |
| 41 | + |
| 42 | + expect(program).to produce_output_when_run(expected_output).in_color( |
| 43 | + color_enabled |
| 44 | + ) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + it "produces the correct failure message when used in the negative" do |
| 49 | + as_both_colored_and_uncolored do |color_enabled| |
| 50 | + snippet = <<~TEST.strip |
| 51 | + actual = { |
| 52 | + number: 4 |
| 53 | + } |
| 54 | + expected = hash_including( |
| 55 | + number: be_a(Numeric) |
| 56 | + ) |
| 57 | + expect(actual).not_to match(expected) |
| 58 | + TEST |
| 59 | + program = make_plain_test_program(snippet, color_enabled: color_enabled) |
| 60 | + |
| 61 | + expected_output = |
| 62 | + build_expected_output( |
| 63 | + color_enabled: color_enabled, |
| 64 | + snippet: "expect(actual).not_to match(expected)", |
| 65 | + expectation: |
| 66 | + proc do |
| 67 | + line do |
| 68 | + plain "Expected " |
| 69 | + actual "{ number: 4 }" |
| 70 | + plain " not to match " |
| 71 | + expected "#<a hash including (number: #<be a kind of Numeric>)>" |
| 72 | + plain "." |
| 73 | + end |
| 74 | + end |
| 75 | + ) |
| 76 | + |
| 77 | + expect(program).to produce_output_when_run(expected_output).in_color( |
| 78 | + color_enabled |
| 79 | + ) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "when the expected value contains a custom matcher defined via RSpec::Matchers::DSL" do |
| 85 | + it "produces the correct failure message" do |
| 86 | + as_both_colored_and_uncolored do |color_enabled| |
| 87 | + snippet = <<~TEST.strip |
| 88 | + actual = { |
| 89 | + number: "something else" |
| 90 | + } |
| 91 | + expected = hash_including( |
| 92 | + number: failing_custom_matcher_from_dsl(42) |
| 93 | + ) |
| 94 | + expect(actual).to match(expected) |
| 95 | + TEST |
| 96 | + program = make_plain_test_program(snippet, color_enabled: color_enabled) |
| 97 | + |
| 98 | + expected_output = |
| 99 | + build_expected_output( |
| 100 | + color_enabled: color_enabled, |
| 101 | + snippet: "expect(actual).to match(expected)", |
| 102 | + newline_before_expectation: true, |
| 103 | + expectation: |
| 104 | + proc do |
| 105 | + line do |
| 106 | + plain "Expected " |
| 107 | + actual %|{ number: "something else" }| |
| 108 | + end |
| 109 | + |
| 110 | + line do |
| 111 | + plain "to match " |
| 112 | + expected "#<a hash including (number: #<custom matcher defined via the DSL with value 42>)>" |
| 113 | + end |
| 114 | + end, |
| 115 | + diff: |
| 116 | + proc do |
| 117 | + plain_line " {" |
| 118 | + expected_line "- number: #<custom matcher defined via the DSL with value 42>" |
| 119 | + actual_line %|+ number: "something else"| |
| 120 | + plain_line " }" |
| 121 | + end |
| 122 | + ) |
| 123 | + |
| 124 | + expect(program).to produce_output_when_run(expected_output).in_color( |
| 125 | + color_enabled |
| 126 | + ) |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + context "when the expected value contains a custom matcher defined from scratch" do |
| 132 | + it "produces the correct failure message" do |
| 133 | + as_both_colored_and_uncolored do |color_enabled| |
| 134 | + snippet = <<~TEST.strip |
| 135 | + actual = { |
| 136 | + number: "something else" |
| 137 | + } |
| 138 | + expected = hash_including( |
| 139 | + number: failing_custom_matcher_from_scratch(42) |
| 140 | + ) |
| 141 | + expect(actual).to match(expected) |
| 142 | + TEST |
| 143 | + program = make_plain_test_program(snippet, color_enabled: color_enabled) |
| 144 | + |
| 145 | + expected_output = |
| 146 | + build_expected_output( |
| 147 | + color_enabled: color_enabled, |
| 148 | + snippet: "expect(actual).to match(expected)", |
| 149 | + newline_before_expectation: true, |
| 150 | + expectation: |
| 151 | + proc do |
| 152 | + line do |
| 153 | + plain "Expected " |
| 154 | + actual %|{ number: "something else" }| |
| 155 | + end |
| 156 | + |
| 157 | + line do |
| 158 | + plain "to match " |
| 159 | + expected "#<a hash including (number: #<custom matcher defined from scratch with value 42>)>" |
| 160 | + end |
| 161 | + end, |
| 162 | + diff: |
| 163 | + proc do |
| 164 | + plain_line " {" |
| 165 | + expected_line "- number: #<custom matcher defined from scratch with value 42>" |
| 166 | + actual_line %|+ number: "something else"| |
| 167 | + plain_line " }" |
| 168 | + end |
| 169 | + ) |
| 170 | + |
| 171 | + expect(program).to produce_output_when_run(expected_output).in_color( |
| 172 | + color_enabled |
| 173 | + ) |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | +end |
0 commit comments