Created
January 29, 2009 15:27
-
-
Save thinkerbot/54582 to your computer and use it in GitHub Desktop.
MiniTest: A curious case of include (1.8 vs 1.9)
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
require 'rubygems' | |
require 'minitest/spec' | |
module A | |
module B | |
end | |
end | |
class DirectInclude | |
include A | |
end | |
class EvalInclude | |
end | |
EvalInclude.class_eval "include A" | |
class BlockInclude | |
end | |
block = lambda { include A } | |
BlockInclude.class_eval(&block) | |
class MethodEvalInclude | |
class << self | |
def class_include(&block) | |
class_eval(&block) | |
end | |
end | |
end | |
MethodEvalInclude.class_include { include A } | |
describe "Manual Includes" do | |
it "should show direct inclusion works" do | |
DirectInclude::B.must_equal A::B | |
end | |
it "should show eval inclusion works" do | |
EvalInclude::B.must_equal A::B | |
end | |
it "should show block inclusion works" do | |
BlockInclude::B.must_equal A::B | |
end | |
it "should show method inclusion works" do | |
MethodEvalInclude::B.must_equal A::B | |
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
require 'rubygems' | |
require 'minitest/spec' | |
# | |
# Including a module in a spec does not make constants | |
# available under ruby 1.8 but it does under ruby 1.9. | |
# | |
# This script requires the minitest gem to be installed: | |
# | |
# % gem install minitest | |
# | |
# See: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/a8a516f8732150db | |
module A | |
module B | |
end | |
end | |
# This passes on ruby 1.9, but not on 1.8. | |
describe "MiniTest Includes" do | |
include A | |
it "should show inclusion works" do | |
B.must_equal A::B | |
end | |
end | |
# This passes on both. What's more, if you uncomment this | |
# spec, the assignment TRANSLATES to the "MiniTest Includes" | |
# spec and that spec will pass! It indicates some pollution | |
# of one spec with another. | |
# | |
# describe "MiniTest Assigns" do | |
# B = A::B | |
# | |
# it "should show assignment works" do | |
# B.must_equal A::B | |
# end | |
# end | |
# | |
# NOTE: it's actually pollution of the enclosing namespace! | |
# You can show this: | |
# | |
# puts B # => 'A::B' on 1.8, error on 1.9 | |
# | |
# Here is an example showing the assignment case works. | |
module X | |
module Y | |
end | |
end | |
describe "MiniTest Assigns X::Y" do | |
Y = X::Y | |
it "should show constant assignment works" do | |
Y.must_equal X::Y | |
end | |
end | |
# Here are some workarounds... | |
module M | |
module N | |
end | |
describe "Nested" do | |
it "should show nesting works" do | |
N.must_equal M::N | |
end | |
end | |
end | |
describe "Full Constant Name" do | |
it "should show full names works" do | |
M::N.must_equal M::N | |
end | |
end | |
MiniTest::Unit.autorun |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment