Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/be_between_spec.rb

raw symbols SCIP
1 module RSpec::Matchers::BuiltIn
2 RSpec.describe BeBetween do
3 class SizeMatters
4 include Comparable
5 attr_reader :str
6 def <=>(other)
7 str.size <=> other.str.size
8 end
9 def initialize(str)
10 @str = str
11 end
12 def inspect
13 @str
14 end
15 end
16
17 shared_examples "be_between" do |mode|
18 it "passes if target is between min and max" do
19 expect(5).to matcher(1, 10)
20 end
21
22 it "fails if target is not between min and max" do
23 expect {
24 # It does not go to 11
25 expect(11).to matcher(1, 10)
26 }.to fail_with("expected 11 to be between 1 and 10 (#{mode})")
27 end
28
29 it "works with strings" do
30 expect("baz").to matcher("bar", "foo")
31
32 expect {
33 expect("foo").to matcher("bar", "baz")
34 }.to fail_with("expected \"foo\" to be between \"bar\" and \"baz\" (#{mode})")
35 end
36
37 it "works with other Comparable objects" do
38 expect(SizeMatters.new("--")).to matcher(SizeMatters.new("-"), SizeMatters.new("---"))
39
40 expect {
41 expect(SizeMatters.new("---")).to matcher(SizeMatters.new("-"), SizeMatters.new("--"))
42 }.to fail_with("expected --- to be between - and -- (#{mode})")
43 end
44 end
45
46 shared_examples "not_to be_between" do |mode|
47 it "passes if target is not between min and max" do
48 expect(11).not_to matcher(1, 10)
49 end
50
51 it "fails if target is between min and max" do
52 expect {
53 expect(5).not_to matcher(1, 10)
54 }.to fail_with("expected 5 not to be between 1 and 10 (#{mode})")
55 end
56 end
57
58 shared_examples "composing with other matchers" do |mode|
59 it "passes when the matchers both match" do
60 expect([nil, 3]).to include(matcher(2, 4), a_nil_value)
61 end
62
63 it "works with mixed types" do
64 expect(["baz", Math::PI]).to include(matcher(3.1, 3.2), matcher("bar", "foo"))
65
66 expect {
67 expect(["baz", 2.14]).to include(matcher(3.1, 3.2), matcher("bar", "foo") )
68 }.to fail_with("expected [\"baz\", 2.14] to include (a value between 3.1 and 3.2 (#{mode}))")
69 end
70
71 it "provides a description" do
72 description = include(matcher(2, 4), an_instance_of(Float)).description
73 expect(description).to eq("include (a value between 2 and 4 (#{mode})) and (an instance of Float)")
74 end
75
76 it "fails with a clear error message when the matchers do not match" do
77 expect {
78 expect([nil, 1]).to include(matcher(2, 4), a_nil_value)
79 }.to fail_with("expected [nil, 1] to include (a value between 2 and 4 (#{mode}))")
80 end
81 end
82
83 it_behaves_like "an RSpec value matcher", :valid_value => (10), :invalid_value => (11) do
84 let(:matcher) { be_between(1, 10) }
85 end
86
87 describe "expect(...).to be_between(min, max) (inclusive)" do
88 it_behaves_like "be_between", :inclusive do
89 def matcher(min, max)
90 be_between(min, max)
91 end
92 end
93
94 it "is inclusive" do
95 expect(1).to be_between(1, 10)
96 expect(10).to be_between(1, 10)
97 end
98
99 it "indicates it was not comparable if it does not respond to `<=` and `>=`" do
100 expect {
101 expect(nil).to be_between(0, 10)
102 }.to fail_with("expected nil to be between 0 and 10 (inclusive), but it does not respond to `<=` and `>=`")
103 end
104 end
105
106 describe "expect(...).to be_between(min, max) (exclusive)" do
107 it_behaves_like "be_between", :exclusive do
108 def matcher(min, max)
109 be_between(min, max).exclusive
110 end
111 end
112
113 it "indicates it was not comparable if it does not respond to `<` and `>`" do
114 expect {
115 expect(nil).to be_between(0, 10).exclusive
116 }.to fail_with("expected nil to be between 0 and 10 (exclusive), but it does not respond to `<` and `>`")
117 end
118
119 it "is exclusive" do
120 expect { expect(1).to be_between(1, 10).exclusive }.to fail
121 expect { expect(10).to be_between(1, 10).exclusive }.to fail
122 end
123 end
124
125 describe "expect(...).not_to be_between(min, max) (inclusive)" do
126 it_behaves_like "not_to be_between", :inclusive do
127 def matcher(min, max)
128 be_between(min, max)
129 end
130 end
131 end
132
133 describe "expect(...).not_to be_between(min, max) (exclusive)" do
134 it_behaves_like "not_to be_between", :exclusive do
135 def matcher(min, max)
136 be_between(min, max).exclusive
137 end
138 end
139 end
140
141 describe "composing with other matchers (inclusive)" do
142 it_behaves_like "composing with other matchers", :inclusive do
143 def matcher(min, max)
144 a_value_between(min, max)
145 end
146 end
147 end
148
149 describe "composing with other matchers (exclusive)" do
150 it_behaves_like "composing with other matchers", :exclusive do
151 def matcher(min, max)
152 a_value_between(min, max).exclusive
153 end
154 end
155 end
156 end
157 end