Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add additional examples from a_matsuda's talk.

  • Loading branch information...
commit d9bd1363d29d73ee32c06ee73dd0afb2f3ddaf05 1 parent ee0539f
Charles Oliver Nutter headius authored
Showing with 49 additions and 12 deletions.
  1. +31 −12 core/kernel/using_spec.rb
  2. +18 −0 core/module/refine_spec.rb
43 core/kernel/using_spec.rb
View
@@ -1,13 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
ruby_version_is "2.0.0" do
- describe "Kernel#using" do
- before :each do
- @string_mod = Module.new do
- refine(String) {def foo; 'foo'; end}
- end
- end
+ require File.expand_path('../fixtures/string_refinement', __FILE__)
+ describe "Kernel#using" do
it "requires one or more Module arguments" do
lambda do
Module.new do
@@ -23,10 +19,8 @@
end
it "uses refinements from the given module for method calls in the target module" do
- string_mod = @string_mod
-
mod = Module.new do
- using string_mod
+ using StringRefinement
def self.go(str)
str.foo
end
@@ -36,10 +30,8 @@ def self.go(str)
end
it "uses refinements from the given module for method calls in subclasses" do
- string_mod = @string_mod
-
cls = Class.new do
- using string_mod
+ using StringRefinement
end
cls2 = Class.new(cls) do
def self.go(str)
@@ -127,5 +119,32 @@ def bar; 'quux'; end
mod2.call_bar(cls2.new).should == 'bar'
end
+
+ it "applies used refinements to module/class_eval blocks" do
+ mod = Module.new do
+ using StringRefinement
+ end
+
+ mod.module_eval {'hello'.foo}.should == 'foo'
+ mod.class_eval {'hello'.foo}.should == 'foo'
+ end
+
+ it "applies used refinements to lambda blocks" do

Isn't this just because the refinement actually gets applied to the toplevel scope? That's the spec we want to express.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ lambda do
+ using StringRefinement
+ 'hello'.foo
+ end.call.should == 'foo'
+ end
+
+ ruby_bug "in a_matsuda's slides but does not appear to work", "2.0.1" do
+ it "applies used refinements to nested closures inside module/class_eval" do
+ mod = Module.new do
+ using StringRefinement
+ end
+
+ mod.module_eval { lambda { 'hello'.say } }.call.should == 'foo'
+ mod.class_eval { lambda { 'hello'.say } }.call.should == 'foo'
+ end
+ end
end
end
18 core/module/refine_spec.rb
View
@@ -71,5 +71,23 @@ def blah
end
end.should raise_error(ArgumentError)
end
+
+ it "applies refinements to calls in the the containing module's body" do
+ result = nil
+ Module.new do
+ refine(String) {def foo; 'foo'; end}
+ result = 'hello'.foo
+ end
+
+ result.should == 'foo'
+ end
+
+ it "does not apply refinements to external scopes not using the module" do
+ Module.new do
+ refine(String) {def foo; 'foo'; end}
+ end
+
+ lambda {'hello'.foo}.should raise_error(NoMethodError)
+ end
end
end
John Firebaugh

Isn't this just because the refinement actually gets applied to the toplevel scope? That's the spec we want to express.

Please sign in to comment.
Something went wrong with that request. Please try again.