File: be_within_spec.rb

package info (click to toggle)
ruby-rspec-expectations 2.14.2-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 920 kB
  • sloc: ruby: 8,202; makefile: 4
file content (137 lines) | stat: -rw-r--r-- 4,365 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'

module RSpec
  module Matchers
    describe "expect(actual).to be_within(delta).of(expected)" do
      it_behaves_like "an RSpec 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 "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 "raises an error if the actual does not respond to :-" do
        expect {
          expect(nil).to be_within(0.1).of(0)
        }.to raise_error(ArgumentError, /The actual value \(nil\) must respond to `-`/)
      end
    end

    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
    end

    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
    end
  end
end