-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit for supporting async specs under rspec
- v1.1.0.alpha3
- v1.1.0.alpha2
- v1.1.0.alpha1
- v1.0.0
- v1.0.0.alpha1
- v0.8.0
- v0.8.0.alpha3
- v0.8.0.alpha2
- v0.8.0.alpha1
- v0.7.1
- v0.7.0
- v0.7.0.rc.2
- v0.7.0.rc.1
- v0.6.2
- v0.6.1
- v0.6.0
- v0.6.0.beta1
- v0.5.0
- v0.5.0.beta3
- v0.5.0.beta2
- v0.5.0.beta1
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.4.0.beta4
- v0.4.0.beta3
- v0.4.0.beta2
- v0.3.0.beta3
- v0.2.1
- v0.2.0
- v0.1.0
- v0.0.1.beta2
1 parent
31045e5
commit a95cd0b
Showing
4 changed files
with
190 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
module Opal | ||
module RSpec | ||
module AsyncDefinitions | ||
def async(desc, *args, &block) | ||
options = ::RSpec::Core::Metadata.build_hash_from(args) | ||
Opal::RSpec::AsyncExample.register(self, desc, options, block) | ||
end | ||
end | ||
|
||
module AsyncHelpers | ||
def run_async(&block) | ||
::RSpec.current_example.continue_async(block) | ||
end | ||
|
||
def set_timeout(duration, &block) | ||
`setTimeout(block, duration)` | ||
self | ||
end | ||
end | ||
|
||
class AsyncRunner | ||
def initialize(runner, reporter, finish_block) | ||
@runner = runner | ||
@reporter = reporter | ||
@finish_block = finish_block | ||
end | ||
|
||
def run | ||
@examples = AsyncExample.examples.clone | ||
run_next_example | ||
end | ||
|
||
def run_next_example | ||
if @examples.empty? | ||
finish | ||
else | ||
run_example @examples.pop | ||
end | ||
end | ||
|
||
def run_example(example) | ||
example_group = example.example_group | ||
|
||
@reporter.example_group_started example_group | ||
instance = example_group.new | ||
|
||
example.run(instance, @reporter) do | ||
example_finished example | ||
end | ||
end | ||
|
||
def example_finished(example) | ||
@reporter.example_group_finished example.example_group | ||
run_next_example | ||
end | ||
|
||
# Called once all examples have finished. Just calls back to main | ||
# runner to inform it | ||
def finish | ||
@finish_block.call | ||
end | ||
end | ||
|
||
class AsyncExample < ::RSpec::Core::Example | ||
def self.register(*args) | ||
examples << new(*args) | ||
end | ||
|
||
def self.examples | ||
@examples ||= [] | ||
end | ||
|
||
def run(example_group_instance, reporter, &after_run_block) | ||
@example_group_instance = example_group_instance | ||
@reporter = reporter | ||
@after_run_block = after_run_block | ||
|
||
should_wait = true | ||
|
||
::RSpec.current_example = self | ||
|
||
start(reporter) | ||
|
||
begin | ||
run_before_each | ||
@example_group_instance.instance_exec(self, &@example_block) | ||
rescue Exception => e | ||
set_exception(e) | ||
should_wait = false | ||
end | ||
|
||
async_example_finished unless should_wait | ||
end | ||
|
||
def continue_async(block) | ||
begin | ||
block.call | ||
rescue Exception => e | ||
set_exception(e) | ||
end | ||
|
||
async_example_finished | ||
end | ||
|
||
def async_example_finished | ||
begin | ||
run_after_each | ||
rescue Exception => e | ||
set_exception(e) | ||
ensure | ||
@example_group_instance.instance_variables.each do |ivar| | ||
@example_group_instance.instance_variable_set(ivar, nil) | ||
end | ||
@example_group_instance = nil | ||
|
||
begin | ||
assign_generated_description | ||
rescue Exception => e | ||
set_exception(e, "while assigning the example description") | ||
end | ||
end | ||
|
||
finish(@reporter) | ||
::RSpec.current_example = nil | ||
@after_run_block.call | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
describe "Asynchronous helpers" do | ||
|
||
let(:foo) { 100 } | ||
|
||
before do | ||
@model = Object.new | ||
end | ||
|
||
async "can run examples async" do | ||
run_async do | ||
1.should == 1 | ||
end | ||
end | ||
|
||
async "can access let() helpers and before() helpers" do | ||
run_async do | ||
foo.should eq(100) | ||
@model.should be_kind_of(Object) | ||
end | ||
end | ||
|
||
async "can finish running after a long delay" do | ||
obj = [1, 2, 3, 4] | ||
|
||
set_timeout 100 do | ||
run_async { obj.should == [1, 2, 3, 4] } | ||
end | ||
end | ||
end |