opal-spec 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- gem "opal", :git => 'git://github/opal/opal.git'
1
+ gem "opal", :git => 'git://github.com/opal/opal.git'
2
2
  gem "rake"
data/README.md CHANGED
@@ -5,6 +5,69 @@ opal-spec is a minimal spec lib for opal, inspired by RSpec and MSpec.
5
5
  It is designed to run on [opal](http://opalrb.org), and provides the
6
6
  bare minimum to get specs running.
7
7
 
8
+ ## Writing Specs
9
+
10
+ Your specs should go into the `/spec` directory of your app. They take
11
+ the same form as rspec/mspec:
12
+
13
+ ```ruby
14
+ describe MyClass do
15
+ it 'should do some feature' do
16
+ 1.should == 1
17
+ end
18
+
19
+ it 'does something else' do
20
+ nil.should be_nil
21
+ false.should be_false
22
+ true.should be_true
23
+ end
24
+ end
25
+ ```
26
+
27
+ ### Running specs
28
+
29
+ Loading these specs in a browser won't just work. You need to run them.
30
+ The best place to do this is inside `spec/spec_helper.rb`, and with a
31
+ simple call:
32
+
33
+ ```ruby
34
+ OpalSpec::Runner.autorun
35
+ ```
36
+
37
+ ### Async examples
38
+
39
+ Examples can be async, and need to be defined as so:
40
+
41
+ ```ruby
42
+ describe 'MyClass' do
43
+ # normal, sync example
44
+ it 'does something' do
45
+ # ...
46
+ end
47
+
48
+ # async example
49
+ async 'does something else too' do
50
+ # ...
51
+ end
52
+ end
53
+ ```
54
+
55
+ This just marks the example as being async. To actually handle the async
56
+ result, you also need to use a `run_async` call inside some future handler:
57
+
58
+ ```ruby
59
+ async 'HTTP requests should work' do
60
+ HTTP.get('users/1.json') do |response|
61
+ run\_async {
62
+ response.ok?.should be\_true
63
+ }
64
+ end
65
+ end
66
+ ```
67
+
68
+ The end of the block passed to `run_async` informs opal-spec that you are
69
+ done with this test, so it can move on.
70
+
8
71
  Change Log
9
72
  ----------
10
73
 
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
- require 'opal'
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'opal/rake_task'
5
+
6
+ Opal::RakeTask.new do |t|
7
+ t.name = 'opal-spec'
8
+ end
@@ -138,7 +138,7 @@ module OpalSpec
138
138
  when OpalSpec::ExpectationNotMetError
139
139
  output = exception.message
140
140
  else
141
- output = "#{exception.class}: #{exception.message}\n"
141
+ output = "#{exception.class.name}: #{exception.message}\n"
142
142
  output += " #{exception.backtrace.join "\n "}\n"
143
143
  end
144
144
 
@@ -1,6 +1,7 @@
1
1
  module OpalSpec
2
2
  class Example
3
3
  attr_reader :description, :example_group, :exception
4
+ attr_accessor :asynchronous
4
5
 
5
6
  def initialize(group, desc, block)
6
7
  @example_group = group
@@ -8,38 +9,68 @@ module OpalSpec
8
9
  @__block__ = block
9
10
  end
10
11
 
11
- def run_before_hooks
12
- @example_group.before_hooks.each do |before|
13
- instance_eval &before
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
14
35
  end
15
36
  end
16
37
 
17
38
  def run_after_hooks
18
- @example_group.after_hooks.each do |after|
39
+ begin
40
+ @example_group.after_hooks.each do |after|
19
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
20
51
  end
21
52
  end
22
53
 
23
- def run runner
54
+ def run_async(&block)
24
55
  begin
25
- runner.example_started self
26
- run_before_hooks
27
- instance_eval &@__block__
56
+ block.call
28
57
  rescue => e
29
58
  @exception = e
30
59
  ensure
31
- begin
32
- run_after_hooks
33
- rescue => e
34
- @exception = e
35
- end
60
+ run_after_hooks
36
61
  end
37
62
 
38
- if @exception
39
- runner.example_failed self
40
- else
41
- runner.example_passed self
42
- end
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
43
74
  end
44
75
  end
45
- end
76
+ end
@@ -24,10 +24,16 @@ module OpalSpec
24
24
  @after_hooks = []
25
25
  end
26
26
 
27
- def it desc, &block
27
+ def it(desc, &block)
28
28
  @examples << Example.new(self, desc, block)
29
29
  end
30
30
 
31
+ def async(desc, &block)
32
+ example = Example.new(self, desc, block)
33
+ example.asynchronous = true
34
+ @examples << example
35
+ end
36
+
31
37
  def it_behaves_like(*objs)
32
38
  end
33
39
 
@@ -49,10 +55,34 @@ module OpalSpec
49
55
  @parent ? [].concat(@parent.after_hooks).concat(@after_hooks) : @after_hooks
50
56
  end
51
57
 
52
- def run runner
53
- runner.example_group_started self
54
- @examples.each { |example| example.run runner }
55
- runner.example_group_finished self
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
56
86
  end
57
87
 
58
88
  def description
@@ -24,7 +24,7 @@ module OpalSpec
24
24
  if @failed_examples.empty?
25
25
  log "\nFinished"
26
26
  log_green "#{example_count} examples, 0 failures"
27
- `phantom.exit(0)`
27
+ finish_with_code(0)
28
28
  else
29
29
  log "\nFailures:"
30
30
  @failed_examples.each_with_index do |example, idx|
@@ -35,7 +35,7 @@ module OpalSpec
35
35
  when OpalSpec::ExpectationNotMetError
36
36
  output = exception.message
37
37
  else
38
- output = "#{exception.class}: #{exception.message}\n"
38
+ output = "#{exception.class.name}: #{exception.message}\n"
39
39
  output += " #{exception.backtrace.join "\n "}\n"
40
40
  end
41
41
  log_red " #{output}"
@@ -43,9 +43,20 @@ module OpalSpec
43
43
 
44
44
  log "\nFinished"
45
45
  log_red "#{example_count} examples, #{@failed_examples.size} failures"
46
- `phantom.exit(1)`
46
+ finish_with_code(1)
47
47
  end
48
48
  end
49
+
50
+ def finish_with_code(code)
51
+ %x{
52
+ if (typeof(phantom) !== 'undefined') {
53
+ return phantom.exit(code);
54
+ }
55
+ else {
56
+ window.OPAL_SPEC_CODE = code;
57
+ }
58
+ }
59
+ end
49
60
 
50
61
  def example_group_started group
51
62
  @example_group = group
@@ -12,7 +12,7 @@ module OpalSpec
12
12
 
13
13
  def self.in_phantom?
14
14
  %x{
15
- if (typeof(phantom) !== 'undefined' && phantom.exit) {
15
+ if (typeof(phantom) !== 'undefined' || typeof(OPAL_SPEC_PHANTOM) !== 'undefined') {
16
16
  return true;
17
17
  }
18
18
 
@@ -41,10 +41,17 @@ module OpalSpec
41
41
  end
42
42
 
43
43
  def run
44
- groups = ExampleGroup.example_groups
44
+ @groups = ExampleGroup.example_groups.dup
45
45
  @formatter.start
46
- groups.each { |group| group.run self }
47
- @formatter.finish
46
+ run_next_group
47
+ end
48
+
49
+ def run_next_group
50
+ if @groups.empty?
51
+ @formatter.finish
52
+ else
53
+ @groups.shift.run self
54
+ end
48
55
  end
49
56
 
50
57
  def example_group_started group
@@ -53,6 +60,7 @@ module OpalSpec
53
60
 
54
61
  def example_group_finished group
55
62
  @formatter.example_group_finished group
63
+ run_next_group
56
64
  end
57
65
 
58
66
  def example_started example
@@ -1,3 +1,3 @@
1
1
  module OpalSpec
2
- VERSION = "0.2.0"
2
+ VERSION = '0.2.1'
3
3
  end
data/spec/index.html ADDED
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <title>opal-spec tests</title>
6
+ </head>
7
+ <body>
8
+ <script src="../build/opal.js"></script>
9
+ <script src="../build/opal-spec.js"></script>
10
+ <script src="../build/specs.js"></script>
11
+ </body>
12
+ </html>
data/spec/test.rb ADDED
@@ -0,0 +1,56 @@
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 'Another group' do
23
+ it 'this should pass' do
24
+ 1.should == 1
25
+ end
26
+
27
+ it 'this should fail' do
28
+ raise "whatever error you like"
29
+ end
30
+
31
+ it 'this should pass' do
32
+ true.should be_true
33
+ false.should be_false
34
+ nil.should be_nil
35
+ end
36
+
37
+ async 'this should pass (in 1 second time)' do
38
+ set_timeout(1000) do
39
+ run_async {
40
+ 1.should == 1
41
+ }
42
+ end
43
+ end
44
+
45
+ async 'this should fail (in 1 second time)' do
46
+ set_timeout(1000) do
47
+ run_async {
48
+ 1.should == 5
49
+ }
50
+ end
51
+ end
52
+ end
53
+
54
+ OpalSpec::Runner.autorun
55
+
56
+ puts "Assertions: #{@passed + @failures.length}, Passed: #{@passed}, Failures: #{@failures.length}"
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.0
4
+ version: 0.2.1
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: 2012-09-11 00:00:00.000000000Z
12
+ date: 2012-09-17 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Opal compatible spec library
15
15
  email: adam@adambeynon.com
@@ -33,6 +33,8 @@ files:
33
33
  - lib/opal-spec/scratch_pad.rb
34
34
  - lib/opal-spec/version.rb
35
35
  - opal-spec.gemspec
36
+ - spec/index.html
37
+ - spec/test.rb
36
38
  homepage: http://opalrb.org
37
39
  licenses: []
38
40
  post_install_message: