Skip to content

Commit a466f8b

Browse files
committedOct 29, 2013
Cleanup spec_helper
1 parent 055b851 commit a466f8b

File tree

4 files changed

+211
-209
lines changed

4 files changed

+211
-209
lines changed
 

Diff for: ‎spec/ospec/mock_install_method_patch.rb renamed to ‎spec/ospec/mspec_fixes.rb

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
require 'mspec/mocks/mock'
2+
3+
# 1. Opal does not support mutable strings
4+
class ExceptionState
5+
def initialize(state, location, exception)
6+
@exception = exception
7+
8+
@description = location ? ["An exception occurred during: #{location}"] : []
9+
if state
10+
@description << "\n" unless @description.empty?
11+
@description << state.description
12+
@describe = state.describe
13+
@it = state.it
14+
@description = @description.join ""
15+
else
16+
@describe = @it = ""
17+
end
18+
end
19+
end
20+
21+
# 2. class_eval() doesnt except string parameter
222
def Mock.install_method(obj, sym, type=nil)
323
meta = obj.singleton_class
424

Diff for: ‎spec/ospec/runner.rb

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
class OSpecFilter
2+
def self.main
3+
@main ||= self.new
4+
end
5+
6+
def initialize
7+
@filters = Set.new
8+
end
9+
10+
def register
11+
MSpec.register :exclude, self
12+
end
13+
14+
def ===(description)
15+
@filters.include? description
16+
end
17+
18+
def register_filters(description, block)
19+
instance_eval(&block)
20+
end
21+
22+
def fails(description)
23+
@filters << description
24+
end
25+
end
26+
27+
class Object
28+
def opal_filter(description, &block)
29+
OSpecFilter.main.register_filters(description, block)
30+
end
31+
end
32+
33+
class BrowserFormatter
34+
def initialize(out=nil)
35+
@exception = @failure = false
36+
@exceptions = []
37+
@count = 0
38+
@examples = 0
39+
40+
@current_state = nil
41+
end
42+
43+
def register
44+
MSpec.register :exception, self
45+
MSpec.register :before, self
46+
MSpec.register :after, self
47+
MSpec.register :start, self
48+
MSpec.register :finish, self
49+
MSpec.register :abort, self
50+
MSpec.register :enter, self
51+
end
52+
53+
def green(str)
54+
`console.info(str)`
55+
end
56+
57+
def red(str)
58+
`console.error(str)`
59+
end
60+
61+
def log(str)
62+
`console.log(str)`
63+
end
64+
65+
def exception?
66+
@exception
67+
end
68+
69+
def failure?
70+
@failure
71+
end
72+
73+
def enter(describe); end
74+
75+
def before(state=nil)
76+
@current_state = nil
77+
@failure = @exception = false
78+
end
79+
80+
def exception(exception)
81+
@count += 1
82+
@failure = @exception ? @failure && exception.failure? : exception.failure?
83+
@exception = true
84+
@exceptions << exception
85+
end
86+
87+
def after(state = nil)
88+
@current_state = nil
89+
@examples += 1
90+
end
91+
92+
def start
93+
@start_time = Time.now.to_f
94+
end
95+
96+
def finish
97+
time = Time.now.to_f - @start_time
98+
99+
if @exceptions.empty?
100+
log "\nFinished"
101+
green "#{@examples} examples, #{@count} failures (time taken: #{time})"
102+
103+
finish_with_code 0
104+
else
105+
log "\nFailures:"
106+
107+
@exceptions.each_with_index do |exception, idx|
108+
log "\n #{idx + 1}. #{exception.description}"
109+
red "\n #{exception.message}"
110+
end
111+
112+
log "\nFinished"
113+
red "#{@examples} examples, #{@count} failures (time taken: #{time})"
114+
115+
finish_with_code(1)
116+
end
117+
end
118+
119+
def finish_with_code(code)
120+
`window.OPAL_SPEC_CODE = code;`
121+
end
122+
end
123+
124+
class PhantomFormatter < BrowserFormatter
125+
def green(str)
126+
`console.log('\\033[32m' + str + '\\033[0m')`
127+
end
128+
129+
def red(str)
130+
`console.log('\\033[31m' + str + '\\033[0m')`
131+
end
132+
133+
def log(str)
134+
`console.log(str)`
135+
end
136+
end
137+
138+
module MSpec
139+
def self.opal_runner
140+
@env = Object.new
141+
@env.extend MSpec
142+
end
143+
end
144+
145+
class OSpecRunner
146+
def self.main(formatter_class = BrowserFormatter)
147+
@main ||= self.new formatter_class
148+
end
149+
150+
def initialize(formatter_class)
151+
@formatter_class = formatter_class
152+
register
153+
run
154+
end
155+
156+
def register
157+
formatter = @formatter_class.new
158+
formatter.register
159+
160+
OSpecFilter.main.register
161+
end
162+
163+
def run
164+
MSpec.opal_runner
165+
end
166+
167+
def will_start
168+
MSpec.actions :start
169+
end
170+
171+
def did_finish
172+
MSpec.actions :finish
173+
end
174+
end
175+
176+
module OutputSilencer
177+
def silence_stdout
178+
original_stdout = $stdout
179+
new_stdout = Object.new
180+
`#{new_stdout}.$puts = function(){}`
181+
begin
182+
$stdout = new_stdout
183+
yield
184+
ensure
185+
$stdout = original_stdout
186+
end
187+
end
188+
end

Diff for: ‎spec/spec_helper.rb

+2-208
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
require 'set'
44
require 'opal-parser'
55
require 'mspec'
6-
require 'ospec/mock_install_method_patch'
6+
require 'ospec/mspec_fixes'
7+
require 'ospec/runner'
78

89
ENV['MSPEC_RUNNER'] = true
910

@@ -17,214 +18,7 @@ def eval_js(javascript)
1718
end
1819
end
1920

20-
class OSpecFilter
21-
def self.main
22-
@main ||= self.new
23-
end
24-
25-
def initialize
26-
@filters = Set.new
27-
end
28-
29-
def register
30-
MSpec.register :exclude, self
31-
end
32-
33-
def ===(description)
34-
@filters.include? description
35-
end
36-
37-
def register_filters(description, block)
38-
instance_eval(&block)
39-
end
40-
41-
def fails(description)
42-
@filters << description
43-
end
44-
end
45-
46-
class Object
47-
def opal_filter(description, &block)
48-
OSpecFilter.main.register_filters(description, block)
49-
end
50-
end
51-
52-
class BrowserFormatter
53-
def initialize(out=nil)
54-
@exception = @failure = false
55-
@exceptions = []
56-
@count = 0
57-
@examples = 0
58-
59-
@current_state = nil
60-
end
61-
62-
def register
63-
MSpec.register :exception, self
64-
MSpec.register :before, self
65-
MSpec.register :after, self
66-
MSpec.register :start, self
67-
MSpec.register :finish, self
68-
MSpec.register :abort, self
69-
MSpec.register :enter, self
70-
end
71-
72-
def green(str)
73-
`console.info(str)`
74-
end
75-
76-
def red(str)
77-
`console.error(str)`
78-
end
79-
80-
def log(str)
81-
`console.log(str)`
82-
end
83-
84-
def exception?
85-
@exception
86-
end
87-
88-
def failure?
89-
@failure
90-
end
91-
92-
def enter(describe); end
93-
94-
def before(state=nil)
95-
@current_state = nil
96-
@failure = @exception = false
97-
end
98-
99-
def exception(exception)
100-
@count += 1
101-
@failure = @exception ? @failure && exception.failure? : exception.failure?
102-
@exception = true
103-
@exceptions << exception
104-
end
105-
106-
def after(state = nil)
107-
@current_state = nil
108-
@examples += 1
109-
end
110-
111-
def start
112-
@start_time = Time.now.to_f
113-
end
114-
115-
def finish
116-
time = Time.now.to_f - @start_time
117-
118-
if @exceptions.empty?
119-
log "\nFinished"
120-
green "#{@examples} examples, #{@count} failures (time taken: #{time})"
121-
122-
finish_with_code 0
123-
else
124-
log "\nFailures:"
125-
126-
@exceptions.each_with_index do |exception, idx|
127-
log "\n #{idx + 1}. #{exception.description}"
128-
red "\n #{exception.message}"
129-
end
130-
131-
log "\nFinished"
132-
red "#{@examples} examples, #{@count} failures (time taken: #{time})"
133-
134-
finish_with_code(1)
135-
end
136-
end
137-
138-
def finish_with_code(code)
139-
`window.OPAL_SPEC_CODE = code;`
140-
end
141-
end
142-
143-
class PhantomFormatter < BrowserFormatter
144-
def green(str)
145-
`console.log('\\033[32m' + str + '\\033[0m')`
146-
end
147-
148-
def red(str)
149-
`console.log('\\033[31m' + str + '\\033[0m')`
150-
end
151-
152-
def log(str)
153-
`console.log(str)`
154-
end
155-
end
156-
157-
class ExceptionState
158-
def initialize(state, location, exception)
159-
@exception = exception
160-
161-
@description = location ? ["An exception occurred during: #{location}"] : []
162-
if state
163-
@description << "\n" unless @description.empty?
164-
@description << state.description
165-
@describe = state.describe
166-
@it = state.it
167-
@description = @description.join ""
168-
else
169-
@describe = @it = ""
170-
end
171-
end
172-
end
173-
174-
module MSpec
175-
def self.opal_runner
176-
@env = Object.new
177-
@env.extend MSpec
178-
end
179-
end
180-
181-
class OSpecRunner
182-
def self.main(formatter_class = BrowserFormatter)
183-
@main ||= self.new formatter_class
184-
end
185-
186-
def initialize(formatter_class)
187-
@formatter_class = formatter_class
188-
register
189-
run
190-
end
191-
192-
def register
193-
formatter = @formatter_class.new
194-
formatter.register
195-
196-
OSpecFilter.main.register
197-
end
198-
199-
def run
200-
MSpec.opal_runner
201-
end
202-
203-
def will_start
204-
MSpec.actions :start
205-
end
206-
207-
def did_finish
208-
MSpec.actions :finish
209-
end
210-
end
211-
212-
module OutputSilencer
213-
def silence_stdout
214-
original_stdout = $stdout
215-
new_stdout = Object.new
216-
`#{new_stdout}.$puts = function(){}`
217-
begin
218-
$stdout = new_stdout
219-
yield
220-
ensure
221-
$stdout = original_stdout
222-
end
223-
end
224-
end
225-
22621
formatter_class = `!!window.OPAL_SPEC_PHANTOM` ? PhantomFormatter : BrowserFormatter
22722

22823
# As soon as this file loads, tell the runner the specs are starting
22924
OSpecRunner.main(formatter_class).will_start
230-

Diff for: ‎tasks/mspec.rake

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ end
6464
class RunSpec
6565
def initialize(file=nil)
6666
Opal::Processor.arity_check_enabled = true
67-
Opal::Processor.dynamic_require_severity = :warning
67+
Opal::Processor.dynamic_require_severity = :ignore
6868

6969
ENV['OPAL_SPEC'] = self.specs_to_run(file).join(',')
7070

0 commit comments

Comments
 (0)
Please sign in to comment.