-
-
Save sj26/6af6275fae229d93dc6b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby -I. | |
class A | |
end | |
class A::B | |
end | |
class B | |
end | |
class C | |
end | |
class D | |
autoload :B, "b" | |
end | |
class E | |
autoload :B, "f" | |
end | |
require "minitest/autorun" | |
class TestConstants < Minitest::Test | |
def test_toplevel_nested_reopen | |
# Defining nested constant with same name as | |
# toplevel constant doesn't reopen constant. | |
refute_equal B, A::B | |
end | |
def test_class_walk_toplevel | |
# Referencing nested constants which don't exist inside the nested context | |
# but do exist in the toplevel context will return the toplevel constant | |
# with a warning. | |
out, error = capture_io do | |
assert_equal B, C::B | |
end | |
assert_match /warning: toplevel constant B referenced by C::B\Z/, error | |
end | |
def test_constant_autoloaded | |
# Autoloaded constants don't output warnings, and don't reopen similarly | |
# named toplevel constants. | |
assert_equal "b", D.autoload?(:B) | |
out, error = capture_io do | |
assert D::B.is_a? Class | |
refute_equal B, D::B | |
end | |
assert_nil D.autoload?(:B) | |
assert_empty error | |
end | |
def test_constant_autoload_fails | |
# When autoloading a file fails to defined a declared autoload constant, we | |
# get a TypeError. | |
assert_raises TypeError do | |
E::B | |
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
class D::B | |
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
class E::F | |
end |
Author
sj26
commented
Dec 16, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment