File: test_custom_inflector.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 (46 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download
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
# frozen_string_literal: true

require "test_helper"

class TestCustomInflector < LoaderTest
  test "raises TypeError if the inflector #camelize does not return a string" do
    with_files([["foo.rb", nil]]) do
      loader.inflector = Class.new(Zeitwerk::Inflector) do
        def camelize(_basename, _abspath)
          :Foo # this is wrong
        end
      end.new

      loader.push_dir(".")

      error = assert_raises TypeError do
        loader.setup
      end

      assert_includes error.message, "#camelize must return a String"
    end
  end

  test "raises if the returned constant names has ::" do
    with_files([["foo-bar.rb", nil]]) do
      loader.inflector = Class.new(Zeitwerk::Inflector) do
        def camelize(basename, _abspath)
          if basename == "foo-bar"
            "Foo::Bar" # this is wrong
          else
            super
          end
        end
      end.new

      loader.push_dir(".")

      error = assert_raises Zeitwerk::NameError do
        loader.setup
      end

      assert_includes error.message, "wrong constant name Foo::Bar"
      assert_includes error.message, "#camelize should return a simple constant name"
    end
  end
end