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 156 157 158 159 160 161
|
module RSpec
module Matchers
RSpec.describe "expect(actual).to be_within(delta).of(expected)" do
it_behaves_like "an RSpec value matcher", :valid_value => 5, :invalid_value => -5 do
let(:matcher) { be_within(2).of(4.0) }
end
it "passes when actual == expected" do
expect(5.0).to be_within(0.5).of(5.0)
end
it "passes when actual < (expected + delta)" do
expect(5.49).to be_within(0.5).of(5.0)
end
it "passes when actual > (expected - delta)" do
expect(4.51).to be_within(0.5).of(5.0)
end
it "passes when actual == (expected - delta)" do
expect(4.5).to be_within(0.5).of(5.0)
end
it "passes when actual == (expected + delta)" do
expect(5.5).to be_within(0.5).of(5.0)
end
it "passes with integer arguments that are near each other" do
expect(1.0001).to be_within(5).percent_of(1)
end
it "passes with negative arguments" do
expect(-1.0001).to be_within(5).percent_of(-1)
end
it "fails when actual < (expected - delta)" do
expect {
expect(4.49).to be_within(0.5).of(5.0)
}.to fail_with("expected 4.49 to be within 0.5 of 5.0")
end
it "fails when actual > (expected + delta)" do
expect {
expect(5.51).to be_within(0.5).of(5.0)
}.to fail_with("expected 5.51 to be within 0.5 of 5.0")
end
it "works with Time" do
expect(Time.now).to be_within(0.1).of(Time.now)
end
it "provides a description" do
matcher = be_within(0.5).of(5.0)
matcher.matches?(5.1)
expect(matcher.description).to eq "be within 0.5 of 5.0"
end
it "formats expected within description" do
klass = Class.new { def inspect; "5"; end }
matcher = be_within(0.5).of(klass.new)
expect(matcher.description).to eq "be within 0.5 of 5"
end
it "raises an error if no expected value is given" do
expect {
expect(5.1).to be_within(0.5)
}.to raise_error(ArgumentError, /must set an expected value using #of/)
end
it "fails if the actual is not numeric" do
expect {
expect(nil).to be_within(0.1).of(0)
}.to fail_with("expected nil to be within 0.1 of 0, but it could not be treated as a numeric value")
end
end
RSpec.describe "expect(actual).to be_within(delta).percent_of(expected)" do
it "passes when actual is within the given percent variance" do
expect(9.0).to be_within(10).percent_of(10.0)
expect(10.0).to be_within(10).percent_of(10.0)
expect(11.0).to be_within(10).percent_of(10.0)
end
it "fails when actual is outside the given percent variance" do
expect {
expect(8.9).to be_within(10).percent_of(10.0)
}.to fail_with("expected 8.9 to be within 10% of 10.0")
expect {
expect(11.1).to be_within(10).percent_of(10.0)
}.to fail_with("expected 11.1 to be within 10% of 10.0")
end
it "provides a description" do
matcher = be_within(0.5).percent_of(5.0)
matcher.matches?(5.1)
expect(matcher.description).to eq "be within 0.5% of 5.0"
end
it "works with custom measure objects" do
weight_class = Struct.new(:val) do
include Comparable
def <=>(other); val <=> other.val; end
def -(other); self.class.new(val - other.val); end
def abs; self.class.new(val.abs); end
def *(numeric); self.class.new(val * numeric); end
def /(numeric); self.class.new(val / numeric); end
end
expect(weight_class.new(99)).to be_within(2).percent_of(weight_class.new(100))
expect {
expect(weight_class.new(90)).to be_within(2).percent_of(weight_class.new(100))
}.to fail_with(/expected #<struct.*val=90> to be within 2% of #<struct.*val=100>/)
end
end
RSpec.describe "expect(actual).not_to be_within(delta).of(expected)" do
it "passes when actual < (expected - delta)" do
expect(4.49).not_to be_within(0.5).of(5.0)
end
it "passes when actual > (expected + delta)" do
expect(5.51).not_to be_within(0.5).of(5.0)
end
it "fails when actual == expected" do
expect {
expect(5.0).not_to be_within(0.5).of(5.0)
}.to fail_with("expected 5.0 not to be within 0.5 of 5.0")
end
it "fails when actual < (expected + delta)" do
expect {
expect(5.49).not_to be_within(0.5).of(5.0)
}.to fail_with("expected 5.49 not to be within 0.5 of 5.0")
end
it "fails when actual > (expected - delta)" do
expect {
expect(4.51).not_to be_within(0.5).of(5.0)
}.to fail_with("expected 4.51 not to be within 0.5 of 5.0")
end
it "fails when actual == (expected - delta)" do
expect {
expect(4.5).not_to be_within(0.5).of(5.0)
}.to fail_with("expected 4.5 not to be within 0.5 of 5.0")
end
it "fails when actual == (expected + delta)" do
expect {
expect(5.5).not_to be_within(0.5).of(5.0)
}.to fail_with("expected 5.5 not to be within 0.5 of 5.0")
end
it "passes if the actual is not numeric" do
expect(nil).not_to be_within(0.1).of(0)
end
end
end
end
|