|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'test_helper' |
| 3 | +require 'tempfile' |
| 4 | + |
| 5 | +module CI |
| 6 | + module Queue |
| 7 | + class ClassProxyTest < Minitest::Test |
| 8 | + def setup |
| 9 | + # Clear loaded files tracking between tests |
| 10 | + ClassProxy.class_variable_set(:@@loaded_files, Set.new) |
| 11 | + end |
| 12 | + |
| 13 | + def test_acts_like_a_class |
| 14 | + proxy = ClassProxy.new('String') |
| 15 | + |
| 16 | + assert proxy.is_a?(Class) |
| 17 | + assert proxy == String, "ClassProxy should equal the actual class" |
| 18 | + assert_equal 'String', proxy.name |
| 19 | + end |
| 20 | + |
| 21 | + def test_delegates_method_calls |
| 22 | + proxy = ClassProxy.new('Array') |
| 23 | + |
| 24 | + assert_respond_to proxy, :new |
| 25 | + instance = proxy.new |
| 26 | + assert_kind_of Array, instance |
| 27 | + end |
| 28 | + |
| 29 | + def test_marshaling_without_file_path |
| 30 | + proxy = ClassProxy.new('Integer') |
| 31 | + |
| 32 | + marshaled = Marshal.dump(proxy) |
| 33 | + unmarshaled = Marshal.load(marshaled) |
| 34 | + |
| 35 | + assert_equal 'Integer', unmarshaled.name |
| 36 | + assert unmarshaled == Integer, "Unmarshaled proxy should equal Integer" |
| 37 | + end |
| 38 | + |
| 39 | + def test_marshaling_with_file_path |
| 40 | + proxy = ClassProxy.new('String', file_path: '/tmp/test.rb') |
| 41 | + |
| 42 | + marshaled = Marshal.dump(proxy) |
| 43 | + unmarshaled = Marshal.load(marshaled) |
| 44 | + |
| 45 | + assert_equal 'String', unmarshaled.name |
| 46 | + end |
| 47 | + |
| 48 | + def test_lazy_loads_file_when_constant_not_found |
| 49 | + test_file = create_test_file('LazyLoadTest', 'class LazyLoadTest; end') |
| 50 | + |
| 51 | + # Create proxy before class is loaded |
| 52 | + proxy = ClassProxy.new('LazyLoadTest', file_path: test_file.path) |
| 53 | + |
| 54 | + # Accessing proxy should trigger file load |
| 55 | + assert_equal 'LazyLoadTest', proxy.name |
| 56 | + |
| 57 | + cleanup_test_file(test_file) |
| 58 | + end |
| 59 | + |
| 60 | + def test_loads_file_only_once |
| 61 | + load_counter = 0 |
| 62 | + test_file = create_test_file('LoadOnceTest', <<~RUBY) |
| 63 | + class LoadOnceTest |
| 64 | + @@load_count ||= 0 |
| 65 | + @@load_count += 1 |
| 66 | + def self.load_count |
| 67 | + @@load_count |
| 68 | + end |
| 69 | + end |
| 70 | + RUBY |
| 71 | + |
| 72 | + # Create two proxies for same class |
| 73 | + proxy1 = ClassProxy.new('LoadOnceTest', file_path: test_file.path) |
| 74 | + proxy2 = ClassProxy.new('LoadOnceTest', file_path: test_file.path) |
| 75 | + |
| 76 | + # Access both |
| 77 | + count1 = proxy1.load_count |
| 78 | + count2 = proxy2.load_count |
| 79 | + |
| 80 | + # File should only be loaded once |
| 81 | + assert_equal 1, count2 |
| 82 | + |
| 83 | + cleanup_test_file(test_file) |
| 84 | + end |
| 85 | + |
| 86 | + def test_raises_load_error_for_missing_file |
| 87 | + proxy = ClassProxy.new('MissingClass', file_path: '/tmp/nonexistent_file_12345.rb') |
| 88 | + |
| 89 | + error = assert_raises(LoadError) do |
| 90 | + proxy.name |
| 91 | + end |
| 92 | + |
| 93 | + assert_match(/Test file not found/, error.message) |
| 94 | + assert_match(/nonexistent_file_12345\.rb/, error.message) |
| 95 | + end |
| 96 | + |
| 97 | + def test_raises_argument_error_for_invalid_file_extension |
| 98 | + proxy = ClassProxy.new('InvalidFile', file_path: '/tmp/test.txt') |
| 99 | + |
| 100 | + error = assert_raises(ArgumentError) do |
| 101 | + proxy.name |
| 102 | + end |
| 103 | + |
| 104 | + assert_match(/Invalid test file path/, error.message) |
| 105 | + assert_match(/must end with \.rb/, error.message) |
| 106 | + end |
| 107 | + |
| 108 | + def test_raises_helpful_error_when_class_not_defined_in_file |
| 109 | + test_file = create_test_file('WrongClass', 'class WrongClass; end') |
| 110 | + |
| 111 | + proxy = ClassProxy.new('ExpectedClass', file_path: test_file.path) |
| 112 | + |
| 113 | + error = assert_raises(NameError) do |
| 114 | + proxy.name |
| 115 | + end |
| 116 | + |
| 117 | + assert_match(/ExpectedClass not found after loading/, error.message) |
| 118 | + assert_match(/#{test_file.path}/, error.message) |
| 119 | + |
| 120 | + cleanup_test_file(test_file) |
| 121 | + end |
| 122 | + |
| 123 | + def test_thread_safety |
| 124 | + test_file = create_test_file('ThreadSafeClass', 'class ThreadSafeClass; end') |
| 125 | + |
| 126 | + threads = 10.times.map do |
| 127 | + Thread.new do |
| 128 | + proxy = ClassProxy.new('ThreadSafeClass', file_path: test_file.path) |
| 129 | + proxy.name |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + results = threads.map(&:value) |
| 134 | + |
| 135 | + assert_equal 10, results.size |
| 136 | + assert results.all? { |r| r == 'ThreadSafeClass' } |
| 137 | + |
| 138 | + cleanup_test_file(test_file) |
| 139 | + end |
| 140 | + |
| 141 | + def test_equality_with_actual_class |
| 142 | + proxy = ClassProxy.new('String') |
| 143 | + |
| 144 | + # ClassProxy#== handles comparison with actual class |
| 145 | + assert proxy == String, "ClassProxy should equal String" |
| 146 | + |
| 147 | + # Note: String#== doesn't know about ClassProxy, so String == proxy may not work |
| 148 | + # This is expected behavior - the proxy is transparent when used polymorphically |
| 149 | + end |
| 150 | + |
| 151 | + def test_equality_with_another_proxy |
| 152 | + proxy1 = ClassProxy.new('String') |
| 153 | + proxy2 = ClassProxy.new('String') |
| 154 | + |
| 155 | + # Both proxies should equal the same underlying class |
| 156 | + assert proxy1 == String, "proxy1 should equal String" |
| 157 | + assert proxy2 == String, "proxy2 should equal String" |
| 158 | + end |
| 159 | + |
| 160 | + def test_hash_delegation |
| 161 | + proxy = ClassProxy.new('Integer') |
| 162 | + |
| 163 | + assert_equal Integer.hash, proxy.hash |
| 164 | + end |
| 165 | + |
| 166 | + def test_inspect_delegation |
| 167 | + proxy = ClassProxy.new('Array') |
| 168 | + |
| 169 | + assert_equal Array.inspect, proxy.inspect |
| 170 | + end |
| 171 | + |
| 172 | + def test_to_s_returns_class_name |
| 173 | + proxy = ClassProxy.new('String') |
| 174 | + |
| 175 | + assert_equal 'String', proxy.to_s |
| 176 | + end |
| 177 | + |
| 178 | + def test_nested_constant_resolution |
| 179 | + proxy = ClassProxy.new('Minitest::Test') |
| 180 | + |
| 181 | + assert proxy == Minitest::Test, "ClassProxy should equal Minitest::Test" |
| 182 | + assert_equal 'Minitest::Test', proxy.name |
| 183 | + end |
| 184 | + |
| 185 | + def test_lazy_loads_nested_constant |
| 186 | + test_file = create_test_file('Nested::LazyClass', <<~RUBY) |
| 187 | + module Nested |
| 188 | + class LazyClass |
| 189 | + def self.test_method |
| 190 | + 'it works' |
| 191 | + end |
| 192 | + end |
| 193 | + end |
| 194 | + RUBY |
| 195 | + |
| 196 | + proxy = ClassProxy.new('Nested::LazyClass', file_path: test_file.path) |
| 197 | + |
| 198 | + assert_equal 'it works', proxy.test_method |
| 199 | + |
| 200 | + cleanup_test_file(test_file) |
| 201 | + end |
| 202 | + |
| 203 | + private |
| 204 | + |
| 205 | + def create_test_file(class_name, content) |
| 206 | + file = Tempfile.new(['test_class', '.rb']) |
| 207 | + file.write(content) |
| 208 | + file.close |
| 209 | + file |
| 210 | + end |
| 211 | + |
| 212 | + def cleanup_test_file(file) |
| 213 | + # Remove the constant if it was defined |
| 214 | + parts = file.path.match(/test_class.*\.rb/)[0].split('::') |
| 215 | + Object.send(:remove_const, parts.first.to_sym) if Object.const_defined?(parts.first) |
| 216 | + rescue |
| 217 | + # Ignore cleanup errors |
| 218 | + ensure |
| 219 | + file.unlink |
| 220 | + end |
| 221 | + end |
| 222 | + end |
| 223 | +end |
0 commit comments