Skip to content

Commit 915b962

Browse files
committedOct 15, 2014
Tidy up some specs
1 parent a190d22 commit 915b962

File tree

6 files changed

+64
-41
lines changed

6 files changed

+64
-41
lines changed
 

‎opal/opal/spec/expectations.rb

+8-7
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,26 @@ def failure_message_for_should_not
8484
end
8585

8686
matcher :raise_error do
87-
def initialize(&block)
88-
@block = block
89-
end
90-
9187
def match expected, actual
88+
@expected = expected || Exception
9289
ok = true
9390

9491
begin
95-
@block.call
92+
actual.call
9693
ok = false
9794
rescue => e
98-
@error = e
95+
@expected = @error = e
9996
end
10097

10198
ok
10299
end
103100

104101
def failure_message_for_should
105-
"expected #{actual} to be raised, but nothing was."
102+
"expected #@expected to be raised, but nothing was."
103+
end
104+
105+
def failure_message_for_should_not
106+
"did not expect an error, but #{@expected.class} was raised"
106107
end
107108
end
108109

‎opal/opal/spec/object.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
class Object
2-
def describe desc, &block
3-
OpalSpec::Example.create desc, block
4-
end
1+
def self.describe desc, &block
2+
OpalSpec::Example.create(desc, block)
3+
end
54

6-
def should matcher = nil
5+
class Object
6+
def should(matcher = nil)
77
if matcher
88
matcher.positive_match? self
99
else
1010
OpalSpec::PositiveOperatorMatcher.new self
1111
end
1212
end
1313

14-
def should_not matcher = nil
14+
def should_not(matcher = nil)
1515
if matcher
1616
matcher.negative_match? self
1717
else

‎opal/opal/spec/spec.rb

+20-12
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ def self.create(desc, block)
3030
group
3131
end
3232

33+
def self.describe(desc, &block)
34+
OpalSpec::Example.create(desc, block)
35+
end
36+
3337
def self.description
3438
@parent ? "#{@parent.description} #{@desc}" : @desc
3539
end
3640

37-
def self.it desc, &block
41+
def self.it(desc, &block)
3842
@examples << [desc, block]
3943
end
4044

@@ -66,7 +70,7 @@ def self.after(type = nil, &block)
6670
@after_hooks << block
6771
end
6872

69-
def self.run runner
73+
def self.run(runner)
7074
@runner = runner
7175
@runner.example_group_started self
7276

@@ -82,16 +86,16 @@ def self.run_next_example
8286
end
8387
end
8488

85-
def self.example_started example
89+
def self.example_started(example)
8690
@runner.example_started example
8791
end
8892

89-
def self.example_passed example
93+
def self.example_passed(example)
9094
@runner.example_passed example
9195
run_next_example
9296
end
9397

94-
def self.example_failed example
98+
def self.example_failed(example)
9599
@runner.example_failed example
96100
run_next_example
97101
end
@@ -106,7 +110,7 @@ def self.before_hooks
106110

107111
attr_reader :example_group, :exception
108112

109-
def initialize info
113+
def initialize(info)
110114
@description = info[0]
111115
@__block__ = info[1]
112116
@example_group = self.class
@@ -120,7 +124,7 @@ def async!
120124
@asynchronous = true
121125
end
122126

123-
def run runner
127+
def run(runner)
124128
@runner = runner
125129
begin
126130
@example_group.example_started self
@@ -165,7 +169,7 @@ def run_after_hooks
165169
end
166170
end
167171

168-
def run_async(&block)
172+
def async(&block)
169173
begin
170174
block.call
171175
rescue => e
@@ -177,8 +181,8 @@ def run_async(&block)
177181
finish_running
178182
end
179183

180-
def set_timeout(duration, &block)
181-
`setTimeout(#{block}, #{duration})`
184+
def delay(duration, &block)
185+
`setTimeout(#{block}, #{duration * 1000})`
182186
self
183187
end
184188

@@ -192,8 +196,12 @@ def initialize(value)
192196
@value = value
193197
end
194198

195-
def to(*args)
196-
@value.should(*args)
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)
197205
end
198206

199207
def to_not(*args)

‎spec/async_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
describe 'Async helpers' do
2+
let(:foo) { 100 }
3+
4+
before { @model = 200 }
5+
6+
async 'can run examples async' do
7+
async { 1.should == 1 }
8+
end
9+
10+
async 'can access let() and before() helpers' do
11+
async {
12+
foo.should eq(100)
13+
@model.should eq(200)
14+
}
15+
end
16+
17+
async 'can finish running after a long delay' do
18+
obj = [1, 2, 3, 4]
19+
20+
delay(1) {
21+
async { obj.should == [1, 2, 3, 4] }
22+
}
23+
end
24+
end

‎spec/expect_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@
66
it 'works with simple to_not matchers' do
77
expect(1).to_not eq(2)
88
end
9+
10+
it 'does not allow operator matchers' do
11+
proc {
12+
expect(2).to == 2
13+
}.should raise_error
14+
end
915
end

‎spec/specs_spec.rb

-16
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,6 @@
4949
false.should be_false
5050
nil.should be_nil
5151
end
52-
53-
async 'this should pass (in 0.1 second time)' do
54-
set_timeout(100) do
55-
run_async {
56-
1.should == 1
57-
}
58-
end
59-
end
60-
61-
async 'this should fail (in 0.1 second time)' do
62-
set_timeout(100) do
63-
run_async {
64-
lambda { 1.should == 5 }.should raise_error(Exception)
65-
}
66-
end
67-
end
6852
end
6953

7054
describe "let" do

0 commit comments

Comments
 (0)
Please sign in to comment.