File: test_callbacks.rb

package info (click to toggle)
ruby-zeitwerk 2.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 732 kB
  • sloc: ruby: 6,240; makefile: 4
file content (111 lines) | stat: -rw-r--r-- 2,841 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require "test_helper"

class TestCallbacks < LoaderTest
  module Namespace; end

  test "autoloading a file triggers on_file_autoloaded (Object)" do
    def loader.__on_file_autoloaded(file)
      if file == File.expand_path("x.rb")
        $on_file_autoloaded_called = true
      end
      super
    end

    files = [["x.rb", "X = true"]]
    with_setup(files) do
      $on_file_autoloaded_called = false
      assert X
      assert $on_file_autoloaded_called
    end
  end

  test "autoloading a file triggers on_file_autoloaded (Namespace)" do
    def loader.__on_file_autoloaded(file)
      if file == File.expand_path("x.rb")
        $on_file_autoloaded_called = true
      end
      super
    end

    files = [["x.rb", "#{Namespace}::X = true"]]
    with_setup(files, namespace: Namespace) do
      $on_file_autoloaded_called = false
      assert Namespace::X
      assert $on_file_autoloaded_called
    end
  end

  test "requiring an autoloadable file triggers on_file_autoloaded (Object)" do
    def loader.__on_file_autoloaded(file)
      if file == File.expand_path("y.rb")
        $on_file_autoloaded_called = true
      end
      super
    end

    files = [
      ["x.rb", "X = true"],
      ["y.rb", "Y = X"]
    ]
    with_setup(files, load_path: ".") do
      $on_file_autoloaded_called = false
      require "y"
      assert Y
      assert $on_file_autoloaded_called
    end
  end

  test "requiring an autoloadable file triggers on_file_autoloaded (Namespace)" do
    def loader.__on_file_autoloaded(file)
      if file == File.expand_path("y.rb")
        $on_file_autoloaded_called = true
      end
      super
    end

    files = [
      ["x.rb", "#{Namespace}::X = true"],
      ["y.rb", "#{Namespace}::Y = #{Namespace}::X"]
    ]
    with_setup(files, namespace: Namespace, load_path: ".") do
      $on_file_autoloaded_called = false
      require "y"
      assert Namespace::Y
      assert $on_file_autoloaded_called
    end
  end

  test "autoloading a directory triggers on_dir_autoloaded (Object)" do
    def loader.__on_dir_autoloaded(dir)
      if dir == File.expand_path("m")
        $on_dir_autoloaded_called = true
      end
      super
    end

    files = [["m/x.rb", "M::X = true"]]
    with_setup(files) do
      $on_dir_autoloaded_called = false
      assert M::X
      assert $on_dir_autoloaded_called
    end
  end

  test "autoloading a directory triggers on_dir_autoloaded (Namespace)" do
    def loader.__on_dir_autoloaded(dir)
      if dir == File.expand_path("m")
        $on_dir_autoloaded_called = true
      end
      super
    end

    files = [["m/x.rb", "#{Namespace}::M::X = true"]]
    with_setup(files, namespace: Namespace) do
      $on_dir_autoloaded_called = false
      assert Namespace::M::X
      assert $on_dir_autoloaded_called
    end
  end
end