Skip to content

Commit

Permalink
A few fixes to support before/after/around hooks in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 10, 2013
1 parent 5f7467e commit bc4680f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
require 'opal-rspec'

describe "Adam" do
before do
puts "a"
@bar = 200
end

it "should eat" do
1.should == 1
@bar.should == 201
end
end

Expand Down
56 changes: 49 additions & 7 deletions opal/opal-rspec/fixes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,60 @@ module Kernel
# Module#include should also include constants (as should class subclassing)
RSpec::Core::ExampleGroup::AllHookMemoizedHash = RSpec::Core::MemoizedHelpers::AllHookMemoizedHash

# bad.. something is going wrong inside hooks - so set hooks to empty, for now
# or is it a problem with Array.public_instance_methods(false) adding all array
# methods to this class and thus breaking things like `self.class.new`
# These two methods break because of instance_variables(). That method should ignore
# private variables added by opal. This breaks as we copy ._klass which makes these
# collections think they are arrays as we copy the _klass property from an array
class RSpec::Core::Hooks::HookCollection
def for(a)
RSpec::Core::Hooks::HookCollection.new.with(a)
def for(example_or_group)
RSpec::Core::Hooks::HookCollection.
new(hooks.select {|hook| hook.options_apply?(example_or_group)}).
with(example_or_group)
end
end

class RSpec::Core::Hooks::AroundHookCollection
def for(a)
RSpec::Core::Hooks::AroundHookCollection.new.with(a)
def for(example, initial_procsy=nil)
RSpec::Core::Hooks::AroundHookCollection.new(hooks.select {|hook| hook.options_apply?(example)}).
with(example, initial_procsy)
end
end

class RSpec::Core::Hooks::HookCollection
`def.$send = Opal.Kernel.$send`
`def.$__send__ = Opal.Kernel.$__send__`
`def.$class = Opal.Kernel.$class`
end

class Array
def flatten(level = undefined)
%x{
var result = [];
for (var i = 0, length = #{self}.length, item; i < length; i++) {
item = #{self}[i];
if (!item._isArray && #{`item`.respond_to?(:to_ary)}) {
item = item.$to_ary();
}
if (item._isArray) {
if (level == null) {
result = result.concat(#{`item`.flatten});
}
else if (level === 0) {
result.push(item);
}
else {
result = result.concat(#{`item`.flatten(`level - 1`)});
}
}
else {
result.push(item);
}
}
return result;
}
end
end

0 comments on commit bc4680f

Please sign in to comment.