Skip to content

Instantly share code, notes, and snippets.

@sj26
Created December 16, 2015 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sj26/6af6275fae229d93dc6b to your computer and use it in GitHub Desktop.
Save sj26/6af6275fae229d93dc6b to your computer and use it in GitHub Desktop.
#!/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
class D::B
end
class E::F
end
@sj26
Copy link
Author

sj26 commented Dec 16, 2015

$ ./a.rb
Run options: --seed 6615

# Running:

....

Finished in 0.002155s, 1855.9272 runs/s, 5103.7997 assertions/s.

4 runs, 11 assertions, 0 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment