opal-spec 0.2.10 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +1 -5
  3. data/README.md +33 -41
  4. data/config.ru +5 -2
  5. data/example/Gemfile +4 -0
  6. data/example/README.md +8 -0
  7. data/example/Rakefile +10 -0
  8. data/example/opal/stack.rb +2 -0
  9. data/example/spec/spec_helper.rb +2 -0
  10. data/example/spec/stack_spec.rb +17 -0
  11. data/lib/opal/spec/rake_task.rb +17 -24
  12. data/lib/opal/spec/version.rb +1 -1
  13. data/lib/opal/spec.rb +1 -7
  14. data/opal/opal/spec/browser_formatter.rb +218 -0
  15. data/opal/opal/spec/expectations.rb +128 -0
  16. data/opal/opal/spec/matchers.rb +61 -0
  17. data/opal/opal/spec/object.rb +21 -0
  18. data/{lib/assets/javascripts → opal}/opal/spec/phantom_formatter.rb +6 -6
  19. data/{lib/assets/javascripts → opal}/opal/spec/runner.rb +6 -21
  20. data/opal/opal/spec/spec.rb +185 -0
  21. data/{lib/assets/javascripts → opal}/opal/spec/sprockets_runner.rb.erb +3 -3
  22. data/{lib/assets/javascripts → opal}/opal/spec.rb +3 -5
  23. data/opal-spec.gemspec +5 -0
  24. data/spec/matchers/be_empty_spec.rb +9 -0
  25. data/spec/matchers/respond_to_spec.rb +15 -0
  26. data/spec/specs_spec.rb +149 -0
  27. metadata +69 -25
  28. data/lib/assets/javascripts/opal/spec/browser_formatter.rb +0 -192
  29. data/lib/assets/javascripts/opal/spec/example.rb +0 -76
  30. data/lib/assets/javascripts/opal/spec/example_group.rb +0 -92
  31. data/lib/assets/javascripts/opal/spec/expectations.rb +0 -54
  32. data/lib/assets/javascripts/opal/spec/kernel.rb +0 -9
  33. data/lib/assets/javascripts/opal/spec/matchers.rb +0 -104
  34. data/lib/assets/javascripts/opal/spec/scratch_pad.rb +0 -17
  35. data/lib/opal/spec/server.rb +0 -63
  36. data/spec/specs.rb +0 -73
  37. data/vendor/spec_runner.html.erb +0 -9
  38. /data/{lib/assets/javascripts → opal}/opal-spec.rb +0 -0
@@ -1,192 +0,0 @@
1
- module Spec
2
- class BrowserFormatter
3
- CSS = <<-EOS
4
-
5
- body {
6
- font-size: 14px;
7
- font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
8
- }
9
-
10
- pre {
11
- font-family: "Bitstream Vera Sans Mono", Monaco, "Lucida Console", monospace;
12
- font-size: 12px;
13
- color: #444444;
14
- white-space: pre;
15
- padding: 3px 0px 3px 12px;
16
- margin: 0px 0px 8px;
17
-
18
- background: #FAFAFA;
19
- -webkit-box-shadow: rgba(0,0,0,0.07) 0 1px 2px inset;
20
- -webkit-border-radius: 3px;
21
- -moz-border-radius: 3px;
22
- border-radius: 3px;
23
- border: 1px solid #DDDDDD;
24
- }
25
-
26
- ul.example_groups {
27
- list-style-type: none;
28
- }
29
-
30
- li.group.passed .group_description {
31
- color: #597800;
32
- font-weight: bold;
33
- }
34
-
35
- li.group.failed .group_description {
36
- color: #FF000E;
37
- font-weight: bold;
38
- }
39
-
40
- li.example.passed {
41
- color: #597800;
42
- }
43
-
44
- li.example.failed {
45
- color: #FF000E;
46
- }
47
-
48
- .examples {
49
- list-style-type: none;
50
- }
51
- EOS
52
-
53
- def initialize
54
- @examples = []
55
- @failed_examples = []
56
- end
57
-
58
- def start
59
- %x{
60
- if (!document || !document.body) {
61
- #{ raise "Not running in browser." };
62
- }
63
-
64
- var summary_element = document.createElement('p');
65
- summary_element.className = 'summary';
66
- summary_element.innerHTML = "Running...";
67
-
68
- var groups_element = document.createElement('ul');
69
- groups_element.className = 'example_groups';
70
-
71
- var target = document.getElementById('opal-spec-output');
72
-
73
- if (!target) {
74
- target = document.body;
75
- }
76
-
77
- target.appendChild(summary_element);
78
- target.appendChild(groups_element);
79
-
80
- var styles = document.createElement('style');
81
- styles.type = 'text/css';
82
-
83
- if (styles.styleSheet) {
84
- styles.styleSheet.cssText = __scope.CSS;
85
- }
86
- else {
87
- styles.appendChild(document.createTextNode(__scope.CSS));
88
- }
89
-
90
- document.getElementsByTagName('head')[0].appendChild(styles);
91
- }
92
-
93
- @start_time = Time.now.to_f
94
- @groups_element = `groups_element`
95
- @summary_element = `summary_element`
96
- end
97
-
98
- def finish
99
- time = Time.now.to_f - @start_time
100
- text = "\n#{example_count} examples, #{@failed_examples.size} failures (time taken: #{time})"
101
- `#{@summary_element}.innerHTML = text`
102
- end
103
-
104
- def example_group_started group
105
- @example_group = group
106
- @example_group_failed = false
107
-
108
- %x{
109
- var group_element = document.createElement('li');
110
-
111
- var description = document.createElement('span');
112
- description.className = 'group_description';
113
- description.innerHTML = #{group.description.to_s};
114
- group_element.appendChild(description);
115
-
116
- var example_list = document.createElement('ul');
117
- example_list.className = 'examples';
118
- group_element.appendChild(example_list);
119
-
120
- #@groups_element.appendChild(group_element);
121
- }
122
-
123
- @group_element = `group_element`
124
- @example_list = `example_list`
125
- end
126
-
127
- def example_group_finished group
128
- if @example_group_failed
129
- `#@group_element.className = 'group failed';`
130
- else
131
- `#@group_element.className = 'group passed';`
132
- end
133
- end
134
-
135
- def example_started example
136
- @examples << example
137
- @example = example
138
- end
139
-
140
- def example_failed example
141
- @failed_examples << example
142
- @example_group_failed = true
143
-
144
- exception = example.exception
145
-
146
- case exception
147
- when Spec::ExpectationNotMetError
148
- output = exception.message
149
- else
150
- output = "#{exception.class.name}: #{exception.message}\n"
151
- output += " #{exception.backtrace.join "\n "}\n"
152
- end
153
-
154
- %x{
155
- var wrapper = document.createElement('li');
156
- wrapper.className = 'example failed';
157
-
158
- var description = document.createElement('span');
159
- description.className = 'example_description';
160
- description.innerHTML = #{example.description};
161
-
162
- var exception = document.createElement('pre');
163
- exception.className = 'exception';
164
- exception.innerHTML = output;
165
-
166
- wrapper.appendChild(description);
167
- wrapper.appendChild(exception);
168
-
169
- #@example_list.appendChild(wrapper);
170
- #@example_list.style.display = 'list-item';
171
- }
172
- end
173
-
174
- def example_passed example
175
- %x{
176
- var wrapper = document.createElement('li');
177
- wrapper.className = 'example passed';
178
-
179
- var description = document.createElement('span');
180
- description.className = 'example_description';
181
- description.innerHTML = #{example.description};
182
-
183
- wrapper.appendChild(description);
184
- #@example_list.appendChild(wrapper);
185
- }
186
- end
187
-
188
- def example_count
189
- @examples.size
190
- end
191
- end
192
- end
@@ -1,76 +0,0 @@
1
- module Spec
2
- class Example
3
- attr_reader :description, :example_group, :exception
4
- attr_accessor :asynchronous
5
-
6
- def initialize(group, desc, block)
7
- @example_group = group
8
- @description = desc
9
- @__block__ = block
10
- end
11
-
12
- def finish_running
13
- if @exception
14
- @example_group.example_failed self
15
- else
16
- @example_group.example_passed self
17
- end
18
- end
19
-
20
- def run
21
- begin
22
- @example_group.example_started self
23
- run_before_hooks
24
- instance_eval(&@__block__)
25
- rescue => e
26
- @exception = e
27
- ensure
28
- run_after_hooks unless @asynchronous
29
- end
30
-
31
- if @asynchronous
32
- # must wait ...
33
- else
34
- finish_running
35
- end
36
- end
37
-
38
- def run_after_hooks
39
- begin
40
- @example_group.after_hooks.each do |after|
41
- instance_eval &after
42
- end
43
- rescue => e
44
- @exception = e
45
- end
46
- end
47
-
48
- def run_before_hooks
49
- @example_group.before_hooks.each do |before|
50
- instance_eval &before
51
- end
52
- end
53
-
54
- def run_async(&block)
55
- begin
56
- block.call
57
- rescue => e
58
- @exception = e
59
- ensure
60
- run_after_hooks
61
- end
62
-
63
- finish_running
64
- end
65
-
66
- def set_timeout(duration, &block)
67
- %x{
68
- setTimeout(function() {
69
- #{ block.call };
70
- }, duration);
71
- }
72
-
73
- self
74
- end
75
- end
76
- end
@@ -1,92 +0,0 @@
1
- module Spec
2
- class ExampleGroup
3
- @example_groups = []
4
- def self.example_groups
5
- @example_groups
6
- end
7
-
8
- @stack = []
9
- def self.create desc, block
10
- group = self.new desc, @stack.last
11
- @example_groups << group
12
-
13
- @stack << group
14
- group.instance_eval &block
15
- @stack.pop
16
- end
17
-
18
- def initialize desc, parent
19
- @desc = desc.to_s
20
- @parent = parent
21
- @examples = []
22
-
23
- @before_hooks = []
24
- @after_hooks = []
25
- end
26
-
27
- def it(desc, &block)
28
- @examples << Example.new(self, desc, block)
29
- end
30
-
31
- def async(desc, &block)
32
- example = Example.new(self, desc, block)
33
- example.asynchronous = true
34
- @examples << example
35
- end
36
-
37
- def it_behaves_like(*objs)
38
- end
39
-
40
- def before type = :each, &block
41
- raise "unsupported before type: #{type}" unless type == :each
42
- @before_hooks << block
43
- end
44
-
45
- def after type = :each, &block
46
- raise "unsupported after type: #{type}" unless type == :each
47
- @after_hooks << block
48
- end
49
-
50
- def before_hooks
51
- @parent ? [].concat(@parent.before_hooks).concat(@before_hooks) : @before_hooks
52
- end
53
-
54
- def after_hooks
55
- @parent ? [].concat(@parent.after_hooks).concat(@after_hooks) : @after_hooks
56
- end
57
-
58
- def run(runner)
59
- @runner = runner
60
- @runner.example_group_started self
61
-
62
- @running_examples = @examples.dup
63
- run_next_example
64
- end
65
-
66
- def run_next_example
67
- if @running_examples.empty?
68
- @runner.example_group_finished self
69
- else
70
- @running_examples.shift.run
71
- end
72
- end
73
-
74
- def example_started(example)
75
- @runner.example_started(example)
76
- end
77
-
78
- def example_passed(example)
79
- @runner.example_passed(example)
80
- run_next_example
81
- end
82
-
83
- def example_failed(example)
84
- @runner.example_failed(example)
85
- run_next_example
86
- end
87
-
88
- def description
89
- @parent ? "#{@parent.description} #{@desc}" : @desc
90
- end
91
- end
92
- end
@@ -1,54 +0,0 @@
1
-
2
- module Spec
3
- class ExpectationNotMetError < StandardError; end
4
-
5
- module Expectations
6
- def should matcher = nil
7
- if matcher
8
- matcher.match self
9
- else
10
- Spec::PositiveOperatorMatcher.new self
11
- end
12
- end
13
-
14
- def should_not matcher = nil
15
- if matcher
16
- matcher.not_match self
17
- else
18
- Spec::NegativeOperatorMatcher.new self
19
- end
20
- end
21
-
22
- def be_kind_of expected
23
- Spec::BeKindOfMatcher.new expected
24
- end
25
-
26
- def be_nil
27
- Spec::BeNilMatcher.new nil
28
- end
29
-
30
- def be_true
31
- Spec::BeTrueMatcher.new true
32
- end
33
-
34
- def be_false
35
- Spec::BeFalseMatcher.new false
36
- end
37
-
38
- def eq(expected)
39
- Spec::EqlMatcher.new expected
40
- end
41
-
42
- def equal expected
43
- Spec::EqualMatcher.new expected
44
- end
45
-
46
- def raise_error expected
47
- Spec::RaiseErrorMatcher.new expected
48
- end
49
- end
50
- end
51
-
52
- class Object
53
- include Spec::Expectations
54
- end
@@ -1,9 +0,0 @@
1
- module Kernel
2
- def describe(desc, &block)
3
- Spec::ExampleGroup.create desc, block
4
- end
5
-
6
- def mock(obj)
7
- Object.new
8
- end
9
- end
@@ -1,104 +0,0 @@
1
- module Spec
2
- class Matcher
3
- def initialize(actual)
4
- @actual = actual
5
- end
6
-
7
- def failure(message)
8
- raise Spec::ExpectationNotMetError, message
9
- end
10
- end
11
-
12
- class PositiveOperatorMatcher < Matcher
13
- def == expected
14
- if @actual == expected
15
- true
16
- else
17
- failure "expected: #{expected.inspect}, got: #{@actual.inspect} (using ==)."
18
- end
19
- end
20
- end
21
-
22
- class NegativeOperatorMatcher < Matcher
23
- def == expected
24
- if @actual == expected
25
- failure "expected: #{expected.inspect} not to be #{@actual.inspect} (using ==)."
26
- end
27
- end
28
- end
29
-
30
- class BeKindOfMatcher < Matcher
31
- def match expected
32
- unless expected.kind_of? @actual
33
- failure "expected #{expected.inspect} to be a kind of #{@actual.name}, not #{expected.class.name}."
34
- end
35
- end
36
- end
37
-
38
- class BeNilMatcher < Matcher
39
- def match expected
40
- unless expected.nil?
41
- failure "expected #{expected.inspect} to be nil."
42
- end
43
- end
44
- end
45
-
46
- class BeTrueMatcher < Matcher
47
- def match expected
48
- unless expected == true
49
- failure "expected #{expected.inspect} to be true."
50
- end
51
- end
52
- end
53
-
54
- class BeFalseMatcher < Matcher
55
- def match expected
56
- unless expected == false
57
- failure "expected #{expected.inspect} to be false."
58
- end
59
- end
60
- end
61
-
62
- class EqlMatcher < Matcher
63
- def match(expected)
64
- unless expected == @actual
65
- failure "expected: #{expected.inspect}, got: #{@actual.inspect} (using ==)."
66
- end
67
- end
68
-
69
- def not_match(expected)
70
- if expected.equal? @actual
71
- failure "expected: #{expected.inspect} not to be #{@actual.inspect} (using ==)."
72
- end
73
- end
74
- end
75
-
76
- class EqualMatcher < Matcher
77
- def match expected
78
- unless expected.equal? @actual
79
- failure "expected #{@actual.inspect} to be the same as #{expected.inspect}."
80
- end
81
- end
82
-
83
- def not_match expected
84
- if expected.equal? @actual
85
- failure "expected #{@actual.inspect} not to be equal to #{expected.inspect}."
86
- end
87
- end
88
- end
89
-
90
- class RaiseErrorMatcher < Matcher
91
- def match block
92
- should_raise = false
93
- begin
94
- block.call
95
- should_raise = true
96
- rescue => e
97
- end
98
-
99
- if should_raise
100
- failure "expected #{@actual} to be raised, but nothing was."
101
- end
102
- end
103
- end
104
- end
@@ -1,17 +0,0 @@
1
- module ScratchPad
2
- def self.clear
3
- @record = nil
4
- end
5
-
6
- def self.record(arg)
7
- @record = arg
8
- end
9
-
10
- def self.<<(arg)
11
- @record << arg
12
- end
13
-
14
- def self.recorded
15
- @record
16
- end
17
- end
@@ -1,63 +0,0 @@
1
- require 'opal/spec'
2
- require 'erb'
3
-
4
- module Opal
5
- module Spec
6
- class Server
7
- class Index
8
- def initialize(app, server)
9
- @app = app
10
- @server = server
11
- end
12
-
13
- def call(env)
14
- if env['PATH_INFO'] == '/'
15
- [200, { 'Content-Type' => 'text/html' }, [self.html]]
16
- else
17
- @app.call env
18
- end
19
- end
20
-
21
- def html
22
- source = File.read File.join(VENDOR_PATH, 'spec_runner.html.erb')
23
- ERB.new(source).result binding
24
- end
25
-
26
- def javascript_include_tag(source)
27
- if @server.debug
28
- paths = sprockets[source].to_a.map { |d| "#{d.logical_path}?body=1" }
29
- tags = paths.map { |p| "<script src=\"/assets/#{p}\"></script>" }
30
- tags.join "\n"
31
- else
32
- "<script src=\"/assets/#{source}.js\"></script>"
33
- end
34
- end
35
-
36
- def sprockets
37
- @server.sprockets
38
- end
39
- end
40
-
41
- attr_reader :sprockets
42
- attr_reader :debug
43
-
44
- def initialize(debug = true)
45
- @debug = debug
46
- server = self
47
-
48
- @sprockets = sprockets = Opal::Environment.new
49
- @sprockets.append_path 'spec'
50
-
51
- @app = Rack::Builder.app do
52
- map('/assets') { run sprockets }
53
- use Index, server
54
- run Rack::Directory.new('spec')
55
- end
56
- end
57
-
58
- def call(env)
59
- @app.call env
60
- end
61
- end
62
- end
63
- end
data/spec/specs.rb DELETED
@@ -1,73 +0,0 @@
1
- @passed = 0
2
- @failures = []
3
-
4
- def assert(value, message='Not true')
5
- if value
6
- @passed += 1
7
- else
8
- @failures << message
9
- end
10
- end
11
-
12
- def assert_equal(got, expected, message='Not equal')
13
- assert(got == expected, message)
14
- end
15
-
16
- describe 'Normal group' do
17
- it 'this should fail' do
18
- 1.should == 2
19
- end
20
- end
21
-
22
- describe "New eql" do
23
- it "these should both pass" do
24
- 1.should eq(1)
25
- 1.should_not eq(2)
26
- end
27
-
28
- it "and this should fail" do
29
- 1.should eq(:adam)
30
- end
31
- end
32
-
33
- describe Object do
34
- it "should output a nice name for classes" do
35
- 1.should eq(1)
36
- end
37
- end
38
-
39
- describe 'Another group' do
40
- it 'this should pass' do
41
- 1.should == 1
42
- end
43
-
44
- it 'this should fail' do
45
- raise "whatever error you like"
46
- end
47
-
48
- it 'this should pass' do
49
- true.should be_true
50
- false.should be_false
51
- nil.should be_nil
52
- end
53
-
54
- async 'this should pass (in 0.1 second time)' do
55
- set_timeout(100) do
56
- run_async {
57
- 1.should == 1
58
- }
59
- end
60
- end
61
-
62
- async 'this should fail (in 0.1 second time)' do
63
- set_timeout(100) do
64
- run_async {
65
- 1.should == 5
66
- }
67
- end
68
- end
69
- end
70
-
71
- Opal::Spec::Runner.autorun
72
-
73
- puts "Assertions: #{@passed + @failures.length}, Passed: #{@passed}, Failures: #{@failures.length}"
@@ -1,9 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>opal-spec</title>
5
- </head>
6
- <body>
7
- <%= javascript_include_tag 'opal/spec/sprockets_runner' %>
8
- </body>
9
- </html>
File without changes