opal-spec 0.2.15 → 0.2.16

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.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.16 2013-06-09
2
+
3
+ * Define matchers using OpalSpec.matcher() method.
4
+
5
+ * Remove old TestCase code, and more to pure spec based testing.
6
+
1
7
  ## 0.2.15 2013-05-2
2
8
 
3
9
  * Remove opal-sprockets dependency and use opal directly for sprockets
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source :rubygems
2
2
  gemspec
3
3
 
4
4
  gem 'rake'
5
- gem 'opal'
5
+ gem 'opal', :github => 'opal/opal'
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module Spec
3
- VERSION = '0.2.15'
3
+ VERSION = '0.2.16'
4
4
  end
5
5
  end
data/opal/opal/spec.rb CHANGED
@@ -1,15 +1,13 @@
1
1
  require 'opal'
2
2
 
3
- require 'opal/spec/test_case'
4
- require 'opal/spec/spec'
5
3
  require 'opal/spec/matchers'
6
4
  require 'opal/spec/runner'
7
- require 'opal/spec/scratch_pad'
8
5
  require 'opal/spec/expectations'
6
+ require 'opal/spec/spec'
9
7
  require 'opal/spec/browser_formatter'
10
8
  require 'opal/spec/phantom_formatter'
11
- require 'opal/spec/kernel'
9
+ require 'opal/spec/object'
12
10
 
13
11
  module Opal
14
- Spec = ::OpalTest # compatibility
12
+ Spec = OpalSpec # compatibility
15
13
  end
@@ -1,4 +1,4 @@
1
- module OpalTest
1
+ module OpalSpec
2
2
  class BrowserFormatter
3
3
  CSS = <<-CSS
4
4
 
@@ -56,79 +56,66 @@ module OpalTest
56
56
  end
57
57
 
58
58
  def start
59
- %x{
60
- if (!document || !document.body) {
61
- #{ raise "Not running in browser." };
62
- }
59
+ raise "Not running in browser" unless $global[:document]
63
60
 
64
- var summary_element = document.createElement('p');
65
- summary_element.className = 'summary';
66
- summary_element.innerHTML = "Running...";
61
+ @summary_element = $global.document.createElement 'p'
62
+ @summary_element.className = 'summary'
63
+ @summary_element.innerHTML = "Running..."
67
64
 
68
- var groups_element = document.createElement('ul');
69
- groups_element.className = 'example_groups';
65
+ @groups_element = $global.document.createElement 'ul'
66
+ @groups_element.className = 'example_groups'
70
67
 
71
- var target = document.getElementById('opal-spec-output');
68
+ target = $global.document.getElementById 'opal-spec-output'
72
69
 
73
- if (!target) {
74
- target = document.body;
75
- }
70
+ target ||= $global.document.body
76
71
 
77
- target.appendChild(summary_element);
78
- target.appendChild(groups_element);
72
+ target.appendChild @summary_element
73
+ target.appendChild @groups_element
79
74
 
80
- var styles = document.createElement('style');
81
- styles.type = 'text/css';
75
+ styles = $global.document.createElement 'style'
76
+ styles.type = "text/css"
82
77
 
83
- if (styles.styleSheet) {
84
- styles.styleSheet.cssText = __scope.CSS;
85
- }
86
- else {
87
- styles.appendChild(document.createTextNode(__scope.CSS));
88
- }
78
+ if styles[:styleSheet]
79
+ styles.styleSheet.cssText = CSS
80
+ else
81
+ styles.appendChild $global.document.createTextNode CSS
82
+ end
89
83
 
90
- document.getElementsByTagName('head')[0].appendChild(styles);
91
- }
84
+ $global.document.getElementsByTagName('head')[0].appendChild styles
92
85
 
93
86
  @start_time = Time.now.to_f
94
- @groups_element = `groups_element`
95
- @summary_element = `summary_element`
96
87
  end
97
88
 
98
89
  def finish
99
90
  time = Time.now.to_f - @start_time
100
91
  text = "\n#{example_count} examples, #{@failed_examples.size} failures (time taken: #{time})"
101
- `#{@summary_element}.innerHTML = text`
92
+
93
+ @summary_element.innerHTML = text
102
94
  end
103
95
 
104
96
  def example_group_started group
105
97
  @example_group = group
106
98
  @example_group_failed = false
107
99
 
108
- %x{
109
- var group_element = document.createElement('li');
100
+ @group_element = $global.document.createElement 'li'
110
101
 
111
- var description = document.createElement('span');
112
- description.className = 'group_description';
113
- description.innerHTML = #{group.description.to_s};
114
- group_element.appendChild(description);
102
+ description = $global.document.createElement 'span'
103
+ description.className = 'group_description'
104
+ description.innerHTML = group.description.to_s
105
+ @group_element.appendChild description
115
106
 
116
- var example_list = document.createElement('ul');
117
- example_list.className = 'examples';
118
- group_element.appendChild(example_list);
107
+ @example_list = $global.document.createElement 'ul'
108
+ @example_list.className = 'examples'
109
+ @group_element.appendChild @example_list
119
110
 
120
- #@groups_element.appendChild(group_element);
121
- }
122
-
123
- @group_element = `group_element`
124
- @example_list = `example_list`
111
+ @groups_element.appendChild @group_element
125
112
  end
126
113
 
127
114
  def example_group_finished group
128
115
  if @example_group_failed
129
- `#@group_element.className = 'group failed';`
116
+ @group_element.className = 'group failed'
130
117
  else
131
- `#@group_element.className = 'group passed';`
118
+ @group_element.className = 'group passed'
132
119
  end
133
120
  end
134
121
 
@@ -144,45 +131,41 @@ module OpalTest
144
131
  exception = example.exception
145
132
 
146
133
  case exception
147
- when Spec::ExpectationNotMetError
134
+ when OpalSpec::ExpectationNotMetError
148
135
  output = exception.message
149
136
  else
150
137
  output = "#{exception.class.name}: #{exception.message}\n"
151
138
  output += " #{exception.backtrace.join "\n "}\n"
152
139
  end
153
140
 
154
- %x{
155
- var wrapper = document.createElement('li');
156
- wrapper.className = 'example failed';
141
+ wrapper = $global.document.createElement 'li'
142
+ wrapper.className = 'example failed'
157
143
 
158
- var description = document.createElement('span');
159
- description.className = 'example_description';
160
- description.innerHTML = #{example.description};
144
+ description = $global.document.createElement 'span'
145
+ description.className = 'example_description'
146
+ description.innerHTML = example.description
161
147
 
162
- var exception = document.createElement('pre');
163
- exception.className = 'exception';
164
- exception.innerHTML = output;
148
+ exception = $global.document.createElement 'pre'
149
+ exception.className = 'exception'
150
+ exception.innerHTML = output
165
151
 
166
- wrapper.appendChild(description);
167
- wrapper.appendChild(exception);
152
+ wrapper.appendChild description
153
+ wrapper.appendChild exception
168
154
 
169
- #@example_list.appendChild(wrapper);
170
- #@example_list.style.display = 'list-item';
171
- }
155
+ @example_list.appendChild wrapper
156
+ @example_list.style.display = 'list-item'
172
157
  end
173
158
 
174
159
  def example_passed example
175
- %x{
176
- var wrapper = document.createElement('li');
177
- wrapper.className = 'example passed';
160
+ wrapper = $global.document.createElement 'li'
161
+ wrapper.className = 'example passed'
178
162
 
179
- var description = document.createElement('span');
180
- description.className = 'example_description';
181
- description.innerHTML = #{example.description};
163
+ description = $global.document.createElement 'span'
164
+ description.className = 'example_description'
165
+ description.innerHTML = example.description
182
166
 
183
- wrapper.appendChild(description);
184
- #@example_list.appendChild(wrapper);
185
- }
167
+ wrapper.appendChild description
168
+ @example_list.appendChild wrapper
186
169
  end
187
170
 
188
171
  def example_count
@@ -1,53 +1,128 @@
1
- module OpalTest
1
+ module OpalSpec
2
2
  class ExpectationNotMetError < StandardError; end
3
3
 
4
- module Expectations
5
- def should matcher = nil
6
- if matcher
7
- matcher.match self
8
- else
9
- PositiveOperatorMatcher.new self
10
- end
4
+ module Expectations; end
5
+
6
+ def self.matcher name, &block
7
+ klass = Class.new(Matcher, &block)
8
+
9
+ klass.define_method(:matcher_name) do
10
+ name
11
11
  end
12
12
 
13
- def should_not matcher = nil
14
- if matcher
15
- matcher.not_match self
16
- else
17
- NegativeOperatorMatcher.new self
18
- end
13
+ Expectations.define_method(name) do |*args|
14
+ klass.new(*args)
19
15
  end
16
+ end
20
17
 
21
- def be_kind_of expected
22
- BeKindOfMatcher.new expected
18
+ matcher :be_nil do
19
+ def match expected, actual
20
+ actual.nil?
23
21
  end
24
22
 
25
- def be_nil
26
- BeNilMatcher.new nil
23
+ def failure_message_for_should
24
+ "expected #{expected.inspect} to be nil."
27
25
  end
26
+ end
28
27
 
29
- def be_true
30
- BeTrueMatcher.new true
28
+ matcher :be_true do
29
+ def match expected, actual
30
+ actual == true
31
31
  end
32
32
 
33
- def be_false
34
- BeFalseMatcher.new false
33
+ def failure_message_for_should
34
+ "expected #{actual.inspect} to be true."
35
35
  end
36
+ end
36
37
 
37
- def eq(expected)
38
- EqlMatcher.new expected
38
+ matcher :be_false do
39
+ def match expected, actual
40
+ actual == false
39
41
  end
40
42
 
41
- def equal expected
42
- EqualMatcher.new expected
43
+ def failure_message_for_should
44
+ "expected #{actual.inspect} to be false."
43
45
  end
46
+ end
44
47
 
45
- def raise_error expected
46
- RaiseErrorMatcher.new expected
48
+ matcher :be_kind_of do
49
+ def match expected, actual
50
+ actual.kind_of? expected
51
+ end
52
+
53
+ def failure_message_for_should
54
+ "expected #{actual.inspect} to be a kind of #{expected.name}, not #{actual.class.name}."
47
55
  end
48
56
  end
49
- end
50
57
 
51
- class Object
52
- include OpalTest::Expectations
58
+ matcher :eq do
59
+ def match expected, actual
60
+ expected == actual
61
+ end
62
+
63
+ def failure_message_for_should
64
+ "expected #{expected.inspect}, got: #{actual.inspect} (using ==)."
65
+ end
66
+
67
+ def failure_message_for_should_not
68
+ "expected #{expected.inspect}, not to be: #{actual.inspect} (using ==)."
69
+ end
70
+ end
71
+
72
+ matcher :equal do
73
+ def match expected, actual
74
+ expected.equal? actual
75
+ end
76
+
77
+ def failure_message_for_should
78
+ "expected #{actual.inspect} to be the same as #{expected.inspect}."
79
+ end
80
+
81
+ def failure_message_for_should_not
82
+ "expected #{actual.inspect} to not be equal to #{expected.inspect}."
83
+ end
84
+ end
85
+
86
+ matcher :raise_error do
87
+ def initialize(&block)
88
+ @block = block
89
+ end
90
+
91
+ def match expected, actual
92
+ ok = true
93
+
94
+ begin
95
+ @block.call
96
+ ok = false
97
+ rescue => e
98
+ @error = e
99
+ end
100
+
101
+ ok
102
+ end
103
+
104
+ def failure_message_for_should
105
+ "expected #{actual} to be raised, but nothing was."
106
+ end
107
+ end
108
+
109
+ matcher :be_empty do
110
+ def match expected, actual
111
+ actual.empty?
112
+ end
113
+
114
+ def failure_message_for_should
115
+ "expected #{actual.inspect} to be empty"
116
+ end
117
+ end
118
+
119
+ matcher :respond_to do
120
+ def match expected, actual
121
+ actual.respond_to? expected
122
+ end
123
+
124
+ def failure_message_for_should
125
+ "expected #{actual.inspect} (#{actual.class}) to respond to #{expected}."
126
+ end
127
+ end
53
128
  end
@@ -1,98 +1,61 @@
1
- require 'opal/spec/matchers/base'
2
- require 'opal/spec/matchers/be_empty'
3
- require 'opal/spec/matchers/respond_to'
1
+ module OpalSpec
2
+ class Matcher
3
+ attr_reader :actual, :expected
4
4
 
5
- module OpalTest
6
- class PositiveOperatorMatcher < Matcher
7
- def == expected
8
- if @actual == expected
9
- true
10
- else
11
- failure "expected: #{expected.inspect}, got: #{@actual.inspect} (using ==)."
12
- end
5
+ def initialize expected = nil
6
+ @expected = expected
13
7
  end
14
- end
15
8
 
16
- class NegativeOperatorMatcher < Matcher
17
- def == expected
18
- if @actual == expected
19
- failure "expected: #{expected.inspect} not to be #{@actual.inspect} (using ==)."
20
- end
21
- end
22
- end
9
+ def positive_match? actual
10
+ @actual = actual
23
11
 
24
- class BeKindOfMatcher < Matcher
25
- def match expected
26
- unless expected.kind_of? @actual
27
- failure "expected #{expected.inspect} to be a kind of #{@actual.name}, not #{expected.class.name}."
12
+ unless match expected, actual
13
+ raise OpalSpec::ExpectationNotMetError, failure_message_for_should
28
14
  end
29
15
  end
30
- end
31
16
 
32
- class BeNilMatcher < Matcher
33
- def match expected
34
- unless expected.nil?
35
- failure "expected #{expected.inspect} to be nil."
17
+ def negative_match? actual
18
+ @actual = actual
19
+
20
+ if match expected, actual
21
+ raise OpalSpec::ExpectationNotMetError, failure_message_for_should_not
36
22
  end
37
23
  end
38
- end
39
24
 
40
- class BeTrueMatcher < Matcher
41
- def match expected
42
- unless expected == true
43
- failure "expected #{expected.inspect} to be true."
44
- end
25
+ def failure_message_for_should
26
+ "expected: #{expected.inspect}, actual: #{actual.inspect} (#{matcher_name}) [should]."
45
27
  end
46
- end
47
28
 
48
- class BeFalseMatcher < Matcher
49
- def match expected
50
- unless expected == false
51
- failure "expected #{expected.inspect} to be false."
52
- end
29
+ def failure_message_for_should_not
30
+ "expected: #{expected.inspect}, actual: #{actual.inspect} (#{matcher_name}) [should_not]."
53
31
  end
54
32
  end
55
33
 
56
- class EqlMatcher < Matcher
57
- def match(expected)
58
- unless expected == @actual
59
- failure "expected: #{@actual.inspect}, got: #{expected.inspect} (using ==)."
34
+ class PositiveOperatorMatcher < Matcher
35
+ def == actual
36
+ @actual = actual
37
+
38
+ unless expected == actual
39
+ raise Opal::Spec::ExpectationNotMetError, failure_message_for_should
60
40
  end
61
41
  end
62
42
 
63
- def not_match(expected)
64
- if expected.equal? @actual
65
- failure "expected: #{expected.inspect} not to be #{@actual.inspect} (using ==)."
66
- end
43
+ def failure_message_for_should
44
+ "expected #{expected.inspect}, but got: #{actual.inspect} (using ==)."
67
45
  end
68
46
  end
69
47
 
70
- class EqualMatcher < Matcher
71
- def match expected
72
- unless expected.equal? @actual
73
- failure "expected #{@actual.inspect} to be the same as #{expected.inspect}."
74
- end
75
- end
48
+ class NegativeOperatorMatcher < Matcher
49
+ def == actual
50
+ @actual = actual
76
51
 
77
- def not_match expected
78
- if expected.equal? @actual
79
- failure "expected #{@actual.inspect} not to be equal to #{expected.inspect}."
52
+ if expected == actual
53
+ raise Opal::Spec::ExpectationNotMetError, failure_message_for_should_not
80
54
  end
81
55
  end
82
- end
83
56
 
84
- class RaiseErrorMatcher < Matcher
85
- def match block
86
- should_raise = false
87
- begin
88
- block.call
89
- should_raise = true
90
- rescue => e
91
- end
92
-
93
- if should_raise
94
- failure "expected #{@actual} to be raised, but nothing was."
95
- end
57
+ def failure_message_for_should_not
58
+ "expected #{expected.inspect} not to be: #{actual.inspect} (using ==)."
96
59
  end
97
60
  end
98
61
  end
@@ -0,0 +1,21 @@
1
+ class Object
2
+ def describe desc, &block
3
+ OpalSpec::Example.create desc, block
4
+ end
5
+
6
+ def should matcher = nil
7
+ if matcher
8
+ matcher.positive_match? self
9
+ else
10
+ OpalSpec::PositiveOperatorMatcher.new self
11
+ end
12
+ end
13
+
14
+ def should_not matcher = nil
15
+ if matcher
16
+ matcher.negative_match? self
17
+ else
18
+ OpalSpec::NegativeOperatorMatcher.new self
19
+ end
20
+ end
21
+ end
@@ -1,4 +1,4 @@
1
- module OpalTest
1
+ module OpalSpec
2
2
  class PhantomFormatter
3
3
  def initialize
4
4
  @examples = []
@@ -34,7 +34,7 @@ module OpalTest
34
34
 
35
35
  exception = example.exception
36
36
  case exception
37
- when Spec::ExpectationNotMetError
37
+ when OpalSpec::ExpectationNotMetError
38
38
  output = exception.message
39
39
  else
40
40
  output = "#{exception.class.name}: #{exception.message}\n"
@@ -48,16 +48,13 @@ module OpalTest
48
48
  finish_with_code(1)
49
49
  end
50
50
  end
51
-
51
+
52
52
  def finish_with_code(code)
53
- %x{
54
- if (typeof(phantom) !== 'undefined') {
55
- return phantom.exit(code);
56
- }
57
- else {
58
- window.OPAL_SPEC_CODE = code;
59
- }
60
- }
53
+ if $global[:phantom]
54
+ $global[:phantom].exit code
55
+ else
56
+ $global.OPAL_SPEC_CODE = code
57
+ end
61
58
  end
62
59
 
63
60
  def example_group_started group
@@ -1,32 +1,16 @@
1
- module OpalTest
1
+ module OpalSpec
2
2
  class Runner
3
3
  def self.in_browser?
4
- %x{
5
- if (typeof(window) !== 'undefined' && typeof(document) !== 'undefined') {
6
- return true;
7
- }
8
-
9
- return false;
10
- }
4
+ $global[:document]
11
5
  end
12
6
 
13
7
  def self.in_phantom?
14
- %x{
15
- if (typeof(phantom) !== 'undefined' || typeof(OPAL_SPEC_PHANTOM) !== 'undefined') {
16
- return true;
17
- }
18
-
19
- return false;
20
- }
8
+ $global[:phantom] or $global[:OPAL_SPEC_PHANTOM]
21
9
  end
22
10
 
23
11
  def self.autorun
24
12
  if in_browser?
25
- %x{
26
- setTimeout(function() {
27
- #{ Runner.new.run };
28
- }, 0);
29
- }
13
+ $global.setTimeout -> { Runner.new.run }, 0
30
14
  else
31
15
  Runner.new.run
32
16
  end
@@ -41,7 +25,7 @@ module OpalTest
41
25
  end
42
26
 
43
27
  def run
44
- @groups = TestCase.test_cases.dup
28
+ @groups = Example.groups.dup
45
29
  @formatter.start
46
30
  run_next_group
47
31
  end
@@ -1,30 +1,41 @@
1
- module OpalTest
2
- class Spec < TestCase
1
+ module OpalSpec
2
+ class Example
3
+ include Expectations
4
+
5
+ def self.groups
6
+ @groups ||= []
7
+ end
3
8
 
4
9
  def self.stack
5
10
  @stack ||= []
6
11
  end
7
12
 
8
13
  def self.create(desc, block)
9
- parent = Spec.stack.last
14
+ stack = Example.stack
15
+ parent = stack.last
10
16
 
11
- Class.new(parent || Spec) do
17
+ group = Class.new(parent || Example) do
12
18
  @desc = desc
13
19
  @parent = parent
20
+ @examples = []
21
+ @before_hooks = []
22
+ @after_hooks = []
14
23
  end
15
- end
16
24
 
17
- def self.to_s
18
- "<OpalTest::Spec #{@desc.inspect}>"
25
+ Example.groups << group
26
+ stack << group
27
+ group.class_eval(&block)
28
+ stack.pop
29
+
30
+ group
19
31
  end
20
32
 
21
33
  def self.description
22
34
  @parent ? "#{@parent.description} #{@desc}" : @desc
23
35
  end
24
36
 
25
- def self.it(desc, &block)
26
- @specs ||= 0
27
- define_method("test_#{@specs += 1}_#{desc}", &block)
37
+ def self.it desc, &block
38
+ @examples << [desc, block]
28
39
  end
29
40
 
30
41
  def self.async(desc, &block)
@@ -34,10 +45,7 @@ module OpalTest
34
45
  end
35
46
  end
36
47
 
37
- def self.pending(desc, &block)
38
- @pending ||= []
39
- @pending << [desc, block]
40
- end
48
+ def self.pending(*); end
41
49
 
42
50
  def self.let(name, &block)
43
51
  define_method(name) do
@@ -50,17 +58,58 @@ module OpalTest
50
58
  let(:subject, &block)
51
59
  end
52
60
 
53
- def self.it_behaves_like(*objs)
54
- end
55
-
56
- # type is ignored (is always :each)
57
61
  def self.before(type = nil, &block)
58
- @setup_hooks << block
62
+ @before_hooks << block
59
63
  end
60
64
 
61
- # type is ignored (is always :each)
62
65
  def self.after(type = nil, &block)
63
- @teardown_hooks << block
66
+ @after_hooks << block
67
+ end
68
+
69
+ def self.run runner
70
+ @runner = runner
71
+ @runner.example_group_started self
72
+
73
+ @running_examples = @examples ? @examples.dup : []
74
+ run_next_example
75
+ end
76
+
77
+ def self.run_next_example
78
+ if @running_examples.empty?
79
+ @runner.example_group_finished self
80
+ else
81
+ new(@running_examples.shift).run @runner
82
+ end
83
+ end
84
+
85
+ def self.example_started example
86
+ @runner.example_started example
87
+ end
88
+
89
+ def self.example_passed example
90
+ @runner.example_passed example
91
+ run_next_example
92
+ end
93
+
94
+ def self.example_failed example
95
+ @runner.example_failed example
96
+ run_next_example
97
+ end
98
+
99
+ def self.after_hooks
100
+ @parent ? @parent.after_hooks + @after_hooks : @after_hooks
101
+ end
102
+
103
+ def self.before_hooks
104
+ @parent ? @parent.before_hooks + @before_hooks : @before_hooks
105
+ end
106
+
107
+ attr_reader :example_group, :exception
108
+
109
+ def initialize info
110
+ @description = info[0]
111
+ @__block__ = info[1]
112
+ @example_group = self.class
64
113
  end
65
114
 
66
115
  def description
@@ -71,6 +120,51 @@ module OpalTest
71
120
  @asynchronous = true
72
121
  end
73
122
 
123
+ def run runner
124
+ @runner = runner
125
+ begin
126
+ @example_group.example_started self
127
+ run_before_hooks
128
+ instance_eval(&@__block__)
129
+ rescue => e
130
+ @exception = e
131
+ ensure
132
+ unless @asynchronous
133
+ run_after_hooks
134
+ end
135
+ end
136
+
137
+ if @asynchronous
138
+ # must wait..
139
+ else
140
+ finish_running
141
+ end
142
+ end
143
+
144
+ def finish_running
145
+ if @exception
146
+ @example_group.example_failed self
147
+ else
148
+ @example_group.example_passed self
149
+ end
150
+ end
151
+
152
+ def run_before_hooks
153
+ @example_group.before_hooks.each do |before|
154
+ instance_eval(&before)
155
+ end
156
+ end
157
+
158
+ def run_after_hooks
159
+ begin
160
+ @example_group.after_hooks.each do |after|
161
+ instance_eval(&after)
162
+ end
163
+ rescue => e
164
+ @exception = e
165
+ end
166
+ end
167
+
74
168
  def run_async(&block)
75
169
  begin
76
170
  block.call
@@ -79,12 +173,12 @@ module OpalTest
79
173
  ensure
80
174
  run_after_hooks
81
175
  end
82
-
176
+
83
177
  finish_running
84
178
  end
85
179
 
86
180
  def set_timeout(duration, &block)
87
- `setTimeout(#{block}, #{duration})`
181
+ `setTimeout(#{block}, #{duration})`
88
182
  self
89
183
  end
90
184
  end
@@ -8,4 +8,4 @@
8
8
  <% require_asset s.sub(/^spec\//, '').sub(/\.(rb|opal)$/, '') %>
9
9
  <% end %>
10
10
 
11
- OpalTest::Runner.autorun
11
+ OpalSpec::Runner.autorun
data/spec/specs_spec.rb CHANGED
@@ -1,10 +1,20 @@
1
+ describe "PositiveOperatorMatcher" do
2
+ it "matches should operators" do
3
+ 1.should == 1
4
+ end
5
+
6
+ it "matches negative operators" do
7
+ 1.should_not == 2
8
+ end
9
+ end
10
+
1
11
  describe 'Normal group' do
2
12
  it 'exceptions can be thrown' do
3
13
  err = nil
4
14
 
5
15
  begin
6
16
  1.should == 2
7
- rescue => e
17
+ rescue Exception => e
8
18
  err = e
9
19
  end
10
20
 
@@ -115,3 +125,25 @@ describe "A nested group" do
115
125
  end
116
126
  end
117
127
  end
128
+
129
+ OpalSpec.matcher :custom_matcher do
130
+ def match expected
131
+ unless expected == 42
132
+ failure "foo"
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "Custom Matchers" do
138
+ it "is defined in spec scope" do
139
+ respond_to?(:custom_matcher).should be_true
140
+ end
141
+
142
+ it "passes the expected value to the matcher" do
143
+ 42.should custom_matcher
144
+ end
145
+
146
+ it "can raise error when not maching expectation" do
147
+ lambda { 43.should custom_matcher }.should raise_error(Exception)
148
+ end
149
+ end if false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.15
4
+ version: 0.2.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
12
+ date: 2013-06-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Opal compatible spec library
15
15
  email: adam.beynon@gmail.com
@@ -30,24 +30,17 @@ files:
30
30
  - opal-spec.gemspec
31
31
  - opal/opal-spec.rb
32
32
  - opal/opal/spec.rb
33
- - opal/opal/spec/assertions.rb
34
33
  - opal/opal/spec/browser_formatter.rb
35
34
  - opal/opal/spec/expectations.rb
36
- - opal/opal/spec/kernel.rb
37
35
  - opal/opal/spec/matchers.rb
38
- - opal/opal/spec/matchers/base.rb
39
- - opal/opal/spec/matchers/be_empty.rb
40
- - opal/opal/spec/matchers/respond_to.rb
36
+ - opal/opal/spec/object.rb
41
37
  - opal/opal/spec/phantom_formatter.rb
42
38
  - opal/opal/spec/runner.rb
43
- - opal/opal/spec/scratch_pad.rb
44
39
  - opal/opal/spec/spec.rb
45
40
  - opal/opal/spec/sprockets_runner.rb.erb
46
- - opal/opal/spec/test_case.rb
47
41
  - spec/matchers/be_empty_spec.rb
48
42
  - spec/matchers/respond_to_spec.rb
49
43
  - spec/specs_spec.rb
50
- - spec/tests_spec.rb
51
44
  - vendor/spec_runner.js
52
45
  homepage: http://opalrb.org
53
46
  licenses: []
@@ -69,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
62
  version: '0'
70
63
  requirements: []
71
64
  rubyforge_project:
72
- rubygems_version: 1.8.24
65
+ rubygems_version: 1.8.23
73
66
  signing_key:
74
67
  specification_version: 3
75
68
  summary: Opal compatible spec
@@ -1,12 +0,0 @@
1
- module OpalTest
2
- module Assertions
3
-
4
- def assert(test, msg = "Failed assertion, no message given.")
5
- unless test
6
- msg = msg.call if Proc === msg
7
- raise ExpectationNotMetError, msg
8
- end
9
- true
10
- end
11
- end
12
- end
@@ -1,14 +0,0 @@
1
- module Kernel
2
- def describe(desc, &block)
3
- group = OpalTest::Spec.create(desc, block)
4
-
5
- stack = OpalTest::Spec.stack
6
- stack << group
7
- group.class_eval &block
8
- stack.pop
9
- end
10
-
11
- def mock(obj)
12
- Object.new
13
- end
14
- end
@@ -1,11 +0,0 @@
1
- module OpalTest
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
- end
@@ -1,15 +0,0 @@
1
- module OpalTest
2
- class BeEmptyMatcher < Matcher
3
- def match(actual)
4
- unless actual.empty?
5
- failure "Expected #{actual.inspect} to be empty"
6
- end
7
- end
8
- end
9
- end
10
-
11
- class Object
12
- def be_empty
13
- OpalTest::BeEmptyMatcher.new
14
- end
15
- end
@@ -1,19 +0,0 @@
1
- module OpalTest
2
- class RespondToMatcher < Matcher
3
- def initialize(expected)
4
- @expected = expected
5
- end
6
-
7
- def match(actual)
8
- unless actual.respond_to?(@expected)
9
- failure "Expected #{actual.inspect} (#{actual.class}) to respond to #{@expected}"
10
- end
11
- end
12
- end
13
- end
14
-
15
- class Object
16
- def respond_to(expected)
17
- OpalTest::RespondToMatcher.new expected
18
- end
19
- 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,123 +0,0 @@
1
- require 'opal/spec/assertions'
2
-
3
- module OpalTest
4
- class TestCase
5
- include Assertions
6
-
7
- def self.test_cases
8
- @test_cases ||= []
9
- end
10
-
11
- def self.inherited(klass)
12
- TestCase.test_cases << klass
13
- klass.instance_eval { @setup_hooks = []; @teardown_hooks = [] }
14
- end
15
-
16
- def self.description
17
- to_s
18
- end
19
-
20
- def self.run(runner)
21
- @runner = runner
22
- @runner.example_group_started self
23
-
24
- @running_examples = self.instance_methods.grep(/^test_/)
25
- run_next_example
26
- end
27
-
28
- def self.run_next_example
29
- if @running_examples.empty?
30
- @runner.example_group_finished self
31
- else
32
- self.new(@running_examples.shift).run(@runner)
33
- end
34
- end
35
-
36
- def self.example_started(example)
37
- @runner.example_started(example)
38
- end
39
-
40
- def self.example_passed(example)
41
- @runner.example_passed(example)
42
- run_next_example
43
- end
44
-
45
- def self.example_failed(example)
46
- @runner.example_failed(example)
47
- run_next_example
48
- end
49
-
50
- def self.setup_hooks
51
- @parent ? [].concat(@parent.setup_hooks).concat(@setup_hooks) : @setup_hooks
52
- end
53
-
54
- def self.teardown_hooks
55
- @parent ? [].concat(@parent.teardown_hooks).concat(@teardown_hooks) : @teardown_hooks
56
- end
57
-
58
- def initialize(name)
59
- @__name__ = name
60
- @example_group = self.class
61
- @description = name.sub(/^test_(\d)+_/, '')
62
- end
63
-
64
- def description
65
- @__name__
66
- end
67
-
68
- attr_reader :example_group, :exception
69
-
70
- def finish_running
71
- if @exception
72
- @example_group.example_failed self
73
- else
74
- @example_group.example_passed self
75
- end
76
- end
77
-
78
- def run(runner)
79
- @runner = runner
80
- begin
81
- @example_group.example_started self
82
- run_before_hooks
83
- setup
84
- __send__ @__name__
85
- rescue => e
86
- @exception = e
87
- ensure
88
- unless @asynchronous
89
- teardown
90
- run_after_hooks
91
- end
92
- end
93
-
94
- if @asynchronous
95
- # must wait ...
96
- else
97
- finish_running
98
- end
99
- end
100
-
101
- def run_after_hooks
102
- begin
103
- @example_group.teardown_hooks.each do |after|
104
- instance_eval &after
105
- end
106
- rescue => e
107
- @exception = e
108
- end
109
- end
110
-
111
- def run_before_hooks
112
- @example_group.setup_hooks.each do |before|
113
- instance_eval &before
114
- end
115
- end
116
-
117
- def setup
118
- end
119
-
120
- def teardown
121
- end
122
- end
123
- end
data/spec/tests_spec.rb DELETED
@@ -1,17 +0,0 @@
1
- class SimpleTest < OpalTest::TestCase
2
- def test_this_should_be_tested
3
- :pass.should eq(:pass)
4
- end
5
-
6
- def test_should_also_pass
7
- 1.should == 1
8
- end
9
-
10
- def test_simple_assertion
11
- assert true
12
- assert 42
13
-
14
- lambda { assert nil }.should raise_error(Exception)
15
- lambda { assert false }.should raise_error(Exception)
16
- end
17
- end