Skip to content

Commit 68f749e

Browse files
committedOct 15, 2014
Improve dsl
1 parent 915b962 commit 68f749e

File tree

5 files changed

+136
-90
lines changed

5 files changed

+136
-90
lines changed
 

‎opal/opal/spec/expect.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module OpalSpec
2+
class ExpectationTarget
3+
def initialize(value)
4+
@value = value
5+
end
6+
7+
def to(matcher = nil, &block)
8+
no_matcher! unless matcher
9+
matcher.positive_match? @value
10+
end
11+
12+
def to_not(matcher = nil)
13+
no_matcher! unless matcher
14+
matcher.negative_match? @value
15+
end
16+
17+
def no_matcher!
18+
raise ArgumentError, 'The expect syntax requires a matcher'
19+
end
20+
end
21+
end

‎opal/opal/spec/expectations.rb

+65-46
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,82 @@
11
module OpalSpec
22
class ExpectationNotMetError < StandardError; end
33

4-
module Expectations; end
4+
module Expectations
5+
end
56

6-
def self.matcher name, &block
7+
def self.matcher(name, &block)
78
klass = Class.new(Matcher, &block)
89

9-
klass.define_method(:matcher_name) do
10-
name
11-
end
10+
klass.define_method(:matcher_name) { name }
1211

1312
Expectations.define_method(name) do |*args|
1413
klass.new(*args)
1514
end
1615
end
1716

1817
matcher :be_nil do
19-
def match expected, actual
20-
actual.nil?
18+
def match?
19+
@subject.nil?
2120
end
2221

23-
def failure_message_for_should
24-
"expected #{expected.inspect} to be nil."
22+
def failure_message
23+
"expected #{@subject.inspect} to be nil."
24+
end
25+
26+
def negative_failure_message
27+
"expected #{@subject.inspect} to not be nil."
2528
end
2629
end
2730

2831
matcher :be_true do
29-
def match expected, actual
30-
actual == true
32+
def match?
33+
@subject == true
3134
end
3235

33-
def failure_message_for_should
34-
"expected #{actual.inspect} to be true."
36+
def failure_message
37+
"expected #{@subject.inspect} to be true."
3538
end
3639
end
3740

3841
matcher :be_false do
39-
def match expected, actual
40-
actual == false
42+
def match?
43+
@subject == false
4144
end
4245

43-
def failure_message_for_should
44-
"expected #{actual.inspect} to be false."
46+
def failure_message
47+
"expected #{@subject.inspect} to be false."
4548
end
4649
end
4750

4851
matcher :be_kind_of do
49-
def match expected, actual
50-
actual.kind_of? expected
52+
def initialize(klass)
53+
@klass = klass
5154
end
5255

53-
def failure_message_for_should
54-
"expected #{actual.inspect} to be a kind of #{expected.name}, not #{actual.class.name}."
56+
def match?
57+
@subject.kind_of?(@klass)
58+
end
59+
60+
def failure_message
61+
"expected #{@subject.inspect} to be a kind of #{@klass.name}, not #{@subject.class.name}."
5562
end
5663
end
5764

5865
matcher :eq do
59-
def match expected, actual
60-
expected == actual
66+
def initialize(expected)
67+
@expected = expected
6168
end
6269

63-
def failure_message_for_should
64-
"expected #{expected.inspect}, got: #{actual.inspect} (using ==)."
70+
def match?
71+
@subject == @expected
6572
end
6673

67-
def failure_message_for_should_not
68-
"expected #{expected.inspect}, not to be: #{actual.inspect} (using ==)."
74+
def failure_message
75+
"expected #{@expected.inspect}, got: #{@subject.inspect} (using ==)."
76+
end
77+
78+
def negative_failure_message
79+
"expected #{@expected.inspect}, not to be: #{@subject.inspect} (using ==)."
6980
end
7081
end
7182

@@ -84,46 +95,54 @@ def failure_message_for_should_not
8495
end
8596

8697
matcher :raise_error do
87-
def match expected, actual
88-
@expected = expected || Exception
89-
ok = true
98+
def initialize(klass)
99+
@klass = klass
100+
end
101+
102+
def match?
103+
@klass ||= Exception
104+
passed = true
90105

91106
begin
92-
actual.call
93-
ok = false
107+
@subject.call
108+
passed = false
94109
rescue => e
95-
@expected = @error = e
110+
@error = e
96111
end
97112

98-
ok
113+
passed
99114
end
100115

101-
def failure_message_for_should
102-
"expected #@expected to be raised, but nothing was."
116+
def failure_message
117+
"expected #@klass to be raised, but nothing was."
103118
end
104119

105-
def failure_message_for_should_not
106-
"did not expect an error, but #{@expected.class} was raised"
120+
def negative_failure_message
121+
"did not expect an error, but #{@error.class} was raised"
107122
end
108123
end
109124

110125
matcher :be_empty do
111-
def match expected, actual
112-
actual.empty?
126+
def match?
127+
@subject.empty?
113128
end
114129

115-
def failure_message_for_should
116-
"expected #{actual.inspect} to be empty"
130+
def failure_message
131+
"expected #{@subject.inspect} to be empty"
117132
end
118133
end
119134

120135
matcher :respond_to do
121-
def match expected, actual
122-
actual.respond_to? expected
136+
def initialize(method_name)
137+
@method_name = method_name
123138
end
124139

125-
def failure_message_for_should
126-
"expected #{actual.inspect} (#{actual.class}) to respond to #{expected}."
140+
def match?
141+
@subject.respond_to? @method_name
142+
end
143+
144+
def failure_message
145+
"expected #{@subject.inspect} (#{@subject.class}) to respond to #{@method_name}."
127146
end
128147
end
129148
end

‎opal/opal/spec/matchers.rb

+43-25
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
11
module OpalSpec
22
class Matcher
3-
attr_reader :actual, :expected
3+
def initialize(*args)
4+
@args = args
5+
end
46

5-
def initialize expected = nil
6-
@expected = expected
7+
def inspect
8+
"#<#{matcher_name} matcher>"
79
end
810

9-
def positive_match? actual
10-
@actual = actual
11+
def positive_match?(subject)
12+
@subject = subject
1113

12-
unless match expected, actual
13-
raise OpalSpec::ExpectationNotMetError, failure_message_for_should
14+
unless match?
15+
raise OpalSpec::ExpectationNotMetError, failure_message
1416
end
1517
end
1618

17-
def negative_match? actual
18-
@actual = actual
19+
def negative_match?(subject)
20+
@subject = subject
1921

20-
if match expected, actual
21-
raise OpalSpec::ExpectationNotMetError, failure_message_for_should_not
22+
if match?
23+
raise OpalSpec::ExpectationNotMetError, negative_failure_message
2224
end
2325
end
2426

25-
def failure_message_for_should
26-
"expected: #{expected.inspect}, actual: #{actual.inspect} (#{matcher_name}) [should]."
27+
def failure_message
28+
"expected #{@args.first.inspect}, actual: #{@subject} [#{matcher_name}]"
2729
end
2830

29-
def failure_message_for_should_not
30-
"expected: #{expected.inspect}, actual: #{actual.inspect} (#{matcher_name}) [should_not]."
31+
def negative_failure_message
32+
"expected #{@args.first.inspect} to not match #{@subject} [#{matcher_name}]"
3133
end
3234
end
3335

3436
class PositiveOperatorMatcher < Matcher
35-
def == actual
37+
def initialize(subject)
38+
@subject = subject
39+
end
40+
41+
def matcher_name
42+
"positive operator"
43+
end
44+
45+
def ==(actual)
3646
@actual = actual
3747

38-
unless expected == actual
39-
raise Opal::Spec::ExpectationNotMetError, failure_message_for_should
48+
unless @subject == @actual
49+
raise Opal::Spec::ExpectationNotMetError, failure_message
4050
end
4151
end
4252

43-
def failure_message_for_should
44-
"expected #{actual.inspect}, but got: #{expected.inspect} (using ==)."
53+
def failure_message
54+
"expected #{@actual.inspect}, but got: #{@subject.inspect} (using ==)."
4555
end
4656
end
4757

4858
class NegativeOperatorMatcher < Matcher
49-
def == actual
59+
def initialize(subject)
60+
@subject = subject
61+
end
62+
63+
def matcher_name
64+
"negative operator"
65+
end
66+
67+
def ==(actual)
5068
@actual = actual
5169

52-
if expected == actual
53-
raise Opal::Spec::ExpectationNotMetError, failure_message_for_should_not
70+
if @subject == @actual
71+
raise Opal::Spec::ExpectationNotMetError, negative_failure_message
5472
end
5573
end
5674

57-
def failure_message_for_should_not
58-
"expected #{actual.inspect} not to be: #{expected.inspect} (using ==)."
75+
def negative_failure_message
76+
"expected #{@actual.inspect} not to be: #{@subject.inspect} (using ==)."
5977
end
6078
end
6179
end

‎opal/opal/spec/spec.rb

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'opal/spec/expect'
2+
13
module OpalSpec
24
class Example
35
include Expectations
@@ -187,25 +189,7 @@ def delay(duration, &block)
187189
end
188190

189191
def expect(value)
190-
ExpectTarget.new(value)
191-
end
192-
end
193-
194-
class ExpectTarget
195-
def initialize(value)
196-
@value = value
197-
end
198-
199-
def to(matcher = nil, &block)
200-
unless matcher
201-
raise ArgumentError, 'The expect syntax requires a matcher'
202-
end
203-
204-
@value.should(matcher)
205-
end
206-
207-
def to_not(*args)
208-
@value.should_not(*args)
192+
ExpectationTarget.new(value)
209193
end
210194
end
211195
end

‎spec/expect_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
proc {
1212
expect(2).to == 2
1313
}.should raise_error
14+
15+
proc {
16+
expect(2).to_not == 2
17+
}.should raise_error
1418
end
1519
end

0 commit comments

Comments
 (0)
Please sign in to comment.