Skip to content

Commit bc77a11

Browse files
committed
Add basic namespace tests
1 parent 5153a2d commit bc77a11

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

test/ruby/test_namespace.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,76 @@ def test_root_and_main_methods
695695
pat = "_ruby_ns_*."+RbConfig::CONFIG["DLEXT"]
696696
File.unlink(*Dir.glob(pat, base: tmp).map {|so| "#{tmp}/#{so}"})
697697
end
698+
699+
def test_basic_namespace_detections
700+
assert_separately([ENV_ENABLE_NAMESPACE], __FILE__, __LINE__, "#{<<~"begin;"}\n#{<<~'end;'}", ignore_stderr: true)
701+
begin;
702+
ns = Namespace.new
703+
code = <<~EOC
704+
NS1 = Namespace.current
705+
class Foo
706+
NS2 = Namespace.current
707+
NS2_proc = ->(){ NS2 }
708+
NS3_proc = ->(){ Namespace.current }
709+
710+
def ns4 = Namespace.current
711+
def self.ns5 = NS2
712+
def self.ns6 = Namespace.current
713+
def self.ns6_proc = ->(){ Namespace.current }
714+
715+
def self.yield_block = yield
716+
def self.call_block(&b) = b.call
717+
end
718+
NS9 = Foo.new.ns4
719+
EOC
720+
ns.eval(code)
721+
outer = Namespace.current
722+
assert_equal ns, ns::NS1 # on TOP frame
723+
assert_equal ns, ns::Foo::NS2 # on CLASS frame
724+
assert_equal ns, ns::Foo::NS2_proc.call # proc -> a const on CLASS
725+
assert_equal ns, ns::Foo::NS3_proc.call # proc -> the current
726+
assert_equal ns, ns::Foo.new.ns4 # instance method -> the current
727+
assert_equal ns, ns::Foo.ns5 # singleton method -> a const on CLASS
728+
assert_equal ns, ns::Foo.ns6 # singleton method -> the current
729+
assert_equal ns, ns::Foo.ns6_proc.call # method returns a proc -> the current
730+
731+
assert_equal outer, ns::Foo.yield_block{ Namespace.current } # method yields
732+
assert_equal outer, ns::Foo.call_block{ Namespace.current } # method calls a block
733+
assert_equal ns, ns::NS9 # on TOP frame, referring a class in the current
734+
end;
735+
end
736+
737+
def test_loading_extension_libs_in_main_namespace
738+
assert_separately([ENV_ENABLE_NAMESPACE], __FILE__, __LINE__, "#{<<~"begin;"}\n#{<<~'end;'}", ignore_stderr: true)
739+
begin;
740+
require "prism"
741+
require "optparse"
742+
require "date"
743+
require "time"
744+
require "delegate"
745+
require "singleton"
746+
require "pp"
747+
require "fileutils"
748+
require "tempfile"
749+
require "tmpdir"
750+
require "json"
751+
require "psych"
752+
require "yaml"
753+
require "zlib"
754+
require "open3"
755+
require "ipaddr"
756+
require "net/http"
757+
require "openssl"
758+
require "socket"
759+
require "uri"
760+
require "digest"
761+
require "erb"
762+
require "stringio"
763+
require "monitor"
764+
require "timeout"
765+
require "securerandom"
766+
expected = 1
767+
assert_equal expected, 1
768+
end;
769+
end
698770
end

0 commit comments

Comments
 (0)