From 36e44fc3e07320c3cede099f4719430dd54cebd5 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 2 Apr 2024 18:29:27 +0200 Subject: [PATCH] Add a test for Zeitwerk::NameError --- test/lib/zeitwerk/test_exceptions.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/lib/zeitwerk/test_exceptions.rb b/test/lib/zeitwerk/test_exceptions.rb index 8617b08..6734970 100644 --- a/test/lib/zeitwerk/test_exceptions.rb +++ b/test/lib/zeitwerk/test_exceptions.rb @@ -34,6 +34,22 @@ def assert_error_message(message, error) end end + test "raises NameError if the expected constant is not defined (namespace, overridden name)" do + files = [["m.rb", <<~RUBY], ["m/typo.rb", "M::TyPo = 1"]] + module M + def self.name + "OVERRIDDEN" + end + end + RUBY + with_setup(files) do + typo_rb = File.expand_path("m/typo.rb") + error = assert_raises(Zeitwerk::NameError) { M::Typo } + assert_error_message "expected #{typo_rb} to define Typo in the M namespace", error + assert_equal :Typo, error.name + end + end + test "eager loading raises NameError if files do not define the expected constants (top-level)" do files = [["x.rb", ""]] with_setup(files) do