File: syntax_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 (139 lines) | stat: -rw-r--r-- 4,845 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
138
139
require 'spec_helper'

module RSpec
  module Expectations
    describe Syntax do
      context "when passing a message to an expectation" do
        let(:warner) { ::Kernel }

        let(:string_like_object) do
          Struct.new(:to_str, :to_s).new(*(["Ceci n'est pas une Chaine."]*2))
        end

        let(:insufficiently_string_like_object) do
          Struct.new(:to_s).new("Ceci n'est pas une Chaine.")
        end

        let(:callable_object) do
          Struct.new(:call).new("Ceci n'est pas une Chaine.")
        end

        describe "expect(...).to" do
          it "prints a warning when the message object isn't a String" do
            warner.should_receive(:warn).with(/ignoring.*message/)
            expect(3).to eq(3), :not_a_string
          end

          it "doesn't print a warning when message is a String" do
            warner.should_not_receive(:warn)
            expect(3).to eq(3), "a string"
          end

          it "doesn't print a warning when message responds to to_str" do
            warner.should_not_receive(:warn)
            expect(3).to eq(3), string_like_object
          end

          it "prints a warning when the message object handles to_s but not to_str" do
            warner.should_receive(:warn).with(/ignoring.*message/)
            expect(3).to eq(3), insufficiently_string_like_object
          end

          it "doesn't print a warning when message responds to call" do
            warner.should_not_receive(:warn)
            expect(3).to eq(3), callable_object
          end
        end

        describe "expect(...).not_to" do
          it "prints a warning when the message object isn't a String" do
            warner.should_receive(:warn).with(/ignoring.*message/)
            expect(3).not_to eq(4), :not_a_string
          end

          it "doesn't print a warning when message is a String" do
            warner.should_not_receive(:warn)
            expect(3).not_to eq(4), "a string"
          end

          it "doesn't print a warning when message responds to to_str" do
            warner.should_not_receive(:warn)
            expect(3).not_to eq(4), string_like_object
          end

          it "prints a warning when the message object handles to_s but not to_str" do
            warner.should_receive(:warn).with(/ignoring.*message/)
            expect(3).not_to eq(4), insufficiently_string_like_object
          end

          it "doesn't print a warning when message responds to call" do
            warner.should_not_receive(:warn)
            expect(3).not_to eq(4), callable_object
          end
        end
      end

      describe "expression generation" do
        let(:target) { "foo" }
        let(:expectation) { "eq('bar')" }
        let(:positive_expect_example) { "expect(foo).to eq('bar')" }
        let(:positive_should_example) { "foo.should eq('bar')" }
        let(:negative_expect_example) { "expect(foo).not_to eq('bar')" }
        let(:negative_should_example) { "foo.should_not eq('bar')" }

        def positive_expression
          Syntax.positive_expression(target, expectation)
        end

        def negative_expression
          Syntax.negative_expression(target, expectation)
        end

        context "when only :expect is enabled" do
          before do
            expect(Syntax.should_enabled?).to be_false
            expect(Syntax.expect_enabled?).to be_true
          end

          it 'generates a positive expression using the expect syntax' do
            expect(positive_expression).to eq(positive_expect_example)
          end

          it 'generates a negative expression using the expect syntax' do
            expect(negative_expression).to eq(negative_expect_example)
          end
        end

        context "when both :should and :expect are enabled", :uses_should do
          before do
            expect(Syntax.should_enabled?).to be_true
            expect(Syntax.expect_enabled?).to be_true
          end

          it 'generates a positive expression using the expect syntax' do
            expect(positive_expression).to eq(positive_expect_example)
          end

          it 'generates a negative expression using the expect syntax' do
            expect(negative_expression).to eq(negative_expect_example)
          end
        end

        context "when only :should is enabled", :uses_only_should do
          before do
            Syntax.should_enabled?.should be_true
            Syntax.expect_enabled?.should be_false
          end

          it 'generates a positive expression using the expect syntax' do
            positive_expression.should eq(positive_should_example)
          end

          it 'generates a negative expression using the expect syntax' do
            negative_expression.should eq(negative_should_example)
          end
        end
      end
    end
  end
end