File: multi_matcher_diff_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (119 lines) | stat: -rw-r--r-- 4,271 bytes parent folder | download
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
module RSpec
  module Matchers
    RSpec.describe MultiMatcherDiff do

      before do
        stub_const("::RSpec::Matchers::MultiMatcherDiff::DESCRIPTION_MAX_LENGTH", 30)
      end

      class FakeDiffer
        def self.diff(actual, expected)
          [actual, expected].inspect
        end
      end

      let(:differ) { FakeDiffer }

      let(:message) { "a message" }
      let(:actual) { "actual value" }

      let(:wrapped_value) { described_class.from("expected value", actual) }

      def create_matcher(stubs)
        instance_double(BuiltIn::BaseMatcher, stubs.merge(
          :matches? => true,
          :actual => actual,
          :failure_message => ""
        ))
      end

      let(:matcher_1) { create_matcher(:description => "matcher 1 description", :expected => "expected 1") }
      let(:matcher_2) { create_matcher(:description => "matcher 2 description", :expected => "expected 2") }
      let(:matcher_3) { create_matcher(:description => "matcher 3 description", :expected => "expected 3") }

      let(:long_description) { "a very very long description for my custom smart matcher, which can be used for everything" }
      let(:truncated_description) { "a very very long descriptio..." }
      let(:matcher_with_long_description) { create_matcher(:description => long_description, :expected => "expected value") }

      describe ".from" do
        it "wraps provided value in MultiMatcherDiff" do
          expect(wrapped_value).to be_a(described_class)
        end

        it "returns original value if it was already wrapped" do
          expect(described_class.from(wrapped_value, actual)).to be(wrapped_value)
        end
      end

      describe ".for_many_matchers" do
        let(:wrapped_value) { described_class.for_many_matchers([matcher_1, matcher_2, matcher_3]) }

        it "has a diff for all matchers with their description" do
          expect(wrapped_value.message_with_diff(
            message, differ
          )).to eq(dedent <<-EOS)
            |a message
            |Diff for (matcher 1 description):["actual value", "expected 1"]
            |Diff for (matcher 2 description):["actual value", "expected 2"]
            |Diff for (matcher 3 description):["actual value", "expected 3"]
          EOS
        end
      end

      describe "#message_with_diff" do
        it "returns a message warning if the diff is empty" do
          allow(FakeDiffer).to receive(:diff) { "\e[0m\n\e[0m" }
          expect(wrapped_value.message_with_diff(
            message, differ
          )).to eq(dedent <<-EOS)
            |a message
            |Diff:
            |  <The diff is empty, are your objects producing identical `#inspect` output?>
          EOS
        end

        it "returns just provided message if diff is just whitespace" do
          allow(FakeDiffer).to receive(:diff) { "  \n   \t" }
          expect(wrapped_value.message_with_diff(
            message, differ
          )).to eq(dedent <<-EOS)
            |a message
          EOS
        end

        it "returns regular message with diff when single expected" do
          expect(wrapped_value.message_with_diff(
            message, differ
          )).to eq(dedent <<-EOS)
            |a message
            |Diff:["actual value", "expected value"]
          EOS
        end

        it "returns message with diff and matcher description when single expected with matcher" do
          matcher = include("expected value")
          matcher.matches?(actual)
          wrapped_value = described_class.for_many_matchers([matcher])

          expect(wrapped_value.message_with_diff(
            message, differ
          )).to eq(dedent <<-EOS)
            |a message
            |Diff for (include "expected value"):["actual value", ["expected value"]]
          EOS
        end

        it "returns message with diff and truncated matcher description if it is too long" do
          wrapped_value = described_class.for_many_matchers([matcher_with_long_description])

          expect(wrapped_value.message_with_diff(
            message, differ
          )).to eq(dedent <<-EOS)
            |a message
            |Diff for (#{truncated_description}):["actual value", "expected value"]
          EOS
        end
      end
    end
  end
end