aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/testing/constant_lookup_test.rb
blob: c56c032cdef1fa730993c825842f55f51ac2c139 (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
require 'abstract_unit'

class Foo; end
class Bar < Foo;
  def index; end
  def self.index; end
end
class Baz < Bar; end
module FooBar; end

class ConstantLookupTest < ActiveSupport::TestCase
  include ActiveSupport::Testing::ConstantLookup

  def find_foo(name)
    self.class.determine_constant_from_test_name(name) do |constant|
      Class === constant && constant < Foo
    end
  end

  def find_module(name)
    self.class.determine_constant_from_test_name(name) do |constant|
      Module === constant
    end
  end

  def test_find_bar_from_foo
    assert_equal Bar, find_foo("Bar")
    assert_equal Bar, find_foo("Bar::index")
    assert_equal Bar, find_foo("Bar::index::authenticated")
    assert_equal Bar, find_foo("BarTest")
    assert_equal Bar, find_foo("BarTest::index")
    assert_equal Bar, find_foo("BarTest::index::authenticated")
  end

  def test_find_module
    assert_equal FooBar, find_module("FooBar")
    assert_equal FooBar, find_module("FooBar::index")
    assert_equal FooBar, find_module("FooBar::index::authenticated")
    assert_equal FooBar, find_module("FooBarTest")
    assert_equal FooBar, find_module("FooBarTest::index")
    assert_equal FooBar, find_module("FooBarTest::index::authenticated")
  end

  def test_returns_nil_when_cant_find_foo
    assert_nil find_foo("DoesntExist")
    assert_nil find_foo("DoesntExistTest")
    assert_nil find_foo("DoesntExist::Nadda")
    assert_nil find_foo("DoesntExist::Nadda::Nope")
    assert_nil find_foo("DoesntExist::Nadda::Nope::NotHere")
  end

  def test_returns_nil_when_cant_find_module
    assert_nil find_module("DoesntExist")
    assert_nil find_module("DoesntExistTest")
    assert_nil find_module("DoesntExist::Nadda")
    assert_nil find_module("DoesntExist::Nadda::Nope")
    assert_nil find_module("DoesntExist::Nadda::Nope::NotHere")
  end
end