Debian Codegraph @

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

raw symbols SCIP
1 module RSpec
2 module Matchers
3 RSpec.describe "expect(actual).to be_within(delta).of(expected)" do
4 it_behaves_like "an RSpec value matcher", :valid_value => 5, :invalid_value => -5 do
5 let(:matcher) { be_within(2).of(4.0) }
6 end
7
8 it "passes when actual == expected" do
9 expect(5.0).to be_within(0.5).of(5.0)
10 end
11
12 it "passes when actual < (expected + delta)" do
13 expect(5.49).to be_within(0.5).of(5.0)
14 end
15
16 it "passes when actual > (expected - delta)" do
17 expect(4.51).to be_within(0.5).of(5.0)
18 end
19
20 it "passes when actual == (expected - delta)" do
21 expect(4.5).to be_within(0.5).of(5.0)
22 end
23
24 it "passes when actual == (expected + delta)" do
25 expect(5.5).to be_within(0.5).of(5.0)
26 end
27
28 it "passes with integer arguments that are near each other" do
29 expect(1.0001).to be_within(5).percent_of(1)
30 end
31
32 it "passes with negative arguments" do
33 expect(-1.0001).to be_within(5).percent_of(-1)
34 end
35
36 it "fails when actual < (expected - delta)" do
37 expect {
38 expect(4.49).to be_within(0.5).of(5.0)
39 }.to fail_with("expected 4.49 to be within 0.5 of 5.0")
40 end
41
42 it "fails when actual > (expected + delta)" do
43 expect {
44 expect(5.51).to be_within(0.5).of(5.0)
45 }.to fail_with("expected 5.51 to be within 0.5 of 5.0")
46 end
47
48 it "works with Time" do
49 expect(Time.now).to be_within(0.1).of(Time.now)
50 end
51
52 it "provides a description" do
53 matcher = be_within(0.5).of(5.0)
54 matcher.matches?(5.1)
55 expect(matcher.description).to eq "be within 0.5 of 5.0"
56 end
57
58 it "formats expected within description" do
59 klass = Class.new { def inspect; "5"; end }
60 matcher = be_within(0.5).of(klass.new)
61 expect(matcher.description).to eq "be within 0.5 of 5"
62 end
63
64 it "raises an error if no expected value is given" do
65 expect {
66 expect(5.1).to be_within(0.5)
67 }.to raise_error(ArgumentError, /must set an expected value using #of/)
68 end
69
70 it "fails if the actual is not numeric" do
71 expect {
72 expect(nil).to be_within(0.1).of(0)
73 }.to fail_with("expected nil to be within 0.1 of 0, but it could not be treated as a numeric value")
74 end
75 end
76
77 RSpec.describe "expect(actual).to be_within(delta).percent_of(expected)" do
78 it "passes when actual is within the given percent variance" do
79 expect(9.0).to be_within(10).percent_of(10.0)
80 expect(10.0).to be_within(10).percent_of(10.0)
81 expect(11.0).to be_within(10).percent_of(10.0)
82 end
83
84 it "fails when actual is outside the given percent variance" do
85 expect {
86 expect(8.9).to be_within(10).percent_of(10.0)
87 }.to fail_with("expected 8.9 to be within 10% of 10.0")
88
89 expect {
90 expect(11.1).to be_within(10).percent_of(10.0)
91 }.to fail_with("expected 11.1 to be within 10% of 10.0")
92 end
93
94 it "provides a description" do
95 matcher = be_within(0.5).percent_of(5.0)
96 matcher.matches?(5.1)
97 expect(matcher.description).to eq "be within 0.5% of 5.0"
98 end
99
100 it "works with custom measure objects" do
101 weight_class = Struct.new(:val) do
102 include Comparable
103 def <=>(other); val <=> other.val; end
104 def -(other); self.class.new(val - other.val); end
105 def abs; self.class.new(val.abs); end
106 def *(numeric); self.class.new(val * numeric); end
107 def /(numeric); self.class.new(val / numeric); end
108 end
109
110 expect(weight_class.new(99)).to be_within(2).percent_of(weight_class.new(100))
111 expect {
112 expect(weight_class.new(90)).to be_within(2).percent_of(weight_class.new(100))
113 }.to fail_with(/expected #<struct.*val=90> to be within 2% of #<struct.*val=100>/)
114 end
115 end
116
117 RSpec.describe "expect(actual).not_to be_within(delta).of(expected)" do
118 it "passes when actual < (expected - delta)" do
119 expect(4.49).not_to be_within(0.5).of(5.0)
120 end
121
122 it "passes when actual > (expected + delta)" do
123 expect(5.51).not_to be_within(0.5).of(5.0)
124 end
125
126 it "fails when actual == expected" do
127 expect {
128 expect(5.0).not_to be_within(0.5).of(5.0)
129 }.to fail_with("expected 5.0 not to be within 0.5 of 5.0")
130 end
131
132 it "fails when actual < (expected + delta)" do
133 expect {
134 expect(5.49).not_to be_within(0.5).of(5.0)
135 }.to fail_with("expected 5.49 not to be within 0.5 of 5.0")
136 end
137
138 it "fails when actual > (expected - delta)" do
139 expect {
140 expect(4.51).not_to be_within(0.5).of(5.0)
141 }.to fail_with("expected 4.51 not to be within 0.5 of 5.0")
142 end
143
144 it "fails when actual == (expected - delta)" do
145 expect {
146 expect(4.5).not_to be_within(0.5).of(5.0)
147 }.to fail_with("expected 4.5 not to be within 0.5 of 5.0")
148 end
149
150 it "fails when actual == (expected + delta)" do
151 expect {
152 expect(5.5).not_to be_within(0.5).of(5.0)
153 }.to fail_with("expected 5.5 not to be within 0.5 of 5.0")
154 end
155
156 it "passes if the actual is not numeric" do
157 expect(nil).not_to be_within(0.1).of(0)
158 end
159 end
160 end
161 end