File: test_shared_namespaces.rb

package info (click to toggle)
ruby-zeitwerk 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 752 kB
  • sloc: ruby: 6,467; makefile: 4
file content (81 lines) | stat: -rw-r--r-- 1,691 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

require "test_helper"

class TestSharedNamespaces < LoaderTest
  test "autoloads from a shared implicit namespace" do
    on_teardown { remove_const :M }

    mod = Module.new
    ::M = mod

    files = [["m/x.rb", "M::X = true"]]
    with_setup(files) do
      assert M::X
      loader.reload
      assert_same mod, M
      assert M::X
    end
  end

  test "autoloads from a shared implicit namespace managed by another loader" do
    files = [["a/m/x.rb", "M::X = true"], ["b/m/y.rb", "M::Y = true"]]
    with_files(files) do
      new_loader(dirs: "a")
      loader = new_loader(dirs: "b")

      mod = M

      assert M::X
      assert M::Y
      loader.reload
      assert_same mod, M
      assert M::X
      assert M::Y
    end
  end

  test "autoloads from a shared explicit namespace" do
    on_teardown { remove_const :M }

    mod = Module.new
    ::M = mod

    files = [
      ["m.rb", "class M; end"],
      ["m/x.rb", "M::X = true"]
    ]
    with_setup(files) do
      assert M::X
      loader.reload
      assert_same mod, M
      assert M::X
    end
  end

  test "autoloads from a shared explicit namespace (autoload)" do
    on_teardown do
      remove_const :M
      delete_loaded_feature "a/m.rb"
    end

    files = [
      ["a/m.rb", "class M; end"],
      ["b/m/x.rb", "M::X = true"]
    ]
    with_files(files) do
      # External code has an autoload defined, could be another loader or not,
      # does not matter.
      Object.autoload(:M, File.expand_path("a/m.rb"))

      loader.push_dir("b")
      loader.setup

      mod = M
      assert M::X
      loader.reload
      assert_same mod, M
      assert M::X
    end
  end
end