aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/constantize_test_cases.rb
blob: fa23fd6b6540878837f041e4ac9db47e340e9619 (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
require "dependencies_test_helpers"

module Ace
  module Base
    class Case
      class Dice
      end
    end
    class Fase < Case
    end
  end
  class Gas
    include Base
  end
end

class Object
  module AddtlGlobalConstants
    class Case
      class Dice
      end
    end
  end
  include AddtlGlobalConstants
end

module ConstantizeTestCases
  include DependenciesTestHelpers

  def run_constantize_tests_on
    assert_equal Ace::Base::Case, yield("Ace::Base::Case")
    assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
    assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
    assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Fase::Dice")
    assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")

    assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
    assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
    assert_equal Ace::Base::Case::Dice, yield("Ace::Gas::Case::Dice")

    assert_equal Case::Dice, yield("Case::Dice")
    assert_equal AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
    assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")

    assert_equal Case::Dice, yield("Object::Case::Dice")
    assert_equal AddtlGlobalConstants::Case::Dice, yield("Object::Case::Dice")
    assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")

    assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
    assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")

    assert_raises(NameError) { yield("UnknownClass") }
    assert_raises(NameError) { yield("UnknownClass::Ace") }
    assert_raises(NameError) { yield("UnknownClass::Ace::Base") }
    assert_raises(NameError) { yield("An invalid string") }
    assert_raises(NameError) { yield("InvalidClass\n") }
    assert_raises(NameError) { yield("Ace::ConstantizeTestCases") }
    assert_raises(NameError) { yield("Ace::Base::ConstantizeTestCases") }
    assert_raises(NameError) { yield("Ace::Gas::Base") }
    assert_raises(NameError) { yield("Ace::Gas::ConstantizeTestCases") }
    assert_raises(NameError) { yield("") }
    assert_raises(NameError) { yield("::") }
    assert_raises(NameError) { yield("Ace::gas") }

    assert_raises(NameError) do
      with_autoloading_fixtures do
        yield("RaisesNameError")
      end
    end

    assert_raises(NoMethodError) do
      with_autoloading_fixtures do
        yield("RaisesNoMethodError")
      end
    end

    with_autoloading_fixtures do
      yield("Prepend::SubClassConflict")
      assert_equal "constant", defined?(Prepend::SubClassConflict)
    end
  end

  def run_safe_constantize_tests_on
    assert_equal Ace::Base::Case, yield("Ace::Base::Case")
    assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
    assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
    assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
    assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
    assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
    assert_equal Case::Dice, yield("Case::Dice")
    assert_equal Case::Dice, yield("Object::Case::Dice")
    assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
    assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
    assert_nil yield("")
    assert_nil yield("::")
    assert_nil yield("UnknownClass")
    assert_nil yield("UnknownClass::Ace")
    assert_nil yield("UnknownClass::Ace::Base")
    assert_nil yield("An invalid string")
    assert_nil yield("InvalidClass\n")
    assert_nil yield("blargle")
    assert_nil yield("Ace::ConstantizeTestCases")
    assert_nil yield("Ace::Base::ConstantizeTestCases")
    assert_nil yield("Ace::Gas::Base")
    assert_nil yield("Ace::Gas::ConstantizeTestCases")
    assert_nil yield("#<Class:0x7b8b718b>::Nested_1")
    assert_nil yield("Ace::gas")
    assert_nil yield("Object::ABC")
    assert_nil yield("Object::Object::Object::ABC")
    assert_nil yield("A::Object::B")
    assert_nil yield("A::Object::Object::Object::B")

    assert_raises(NameError) do
      with_autoloading_fixtures do
        yield("RaisesNameError")
      end
    end

    assert_raises(NoMethodError) do
      with_autoloading_fixtures do
        yield("RaisesNoMethodError")
      end
    end
  end
end