aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/class_cache_test.rb
blob: 1ef1939b4b122648e6bf4620efd63b9d69398bf5 (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
# frozen_string_literal: true

require "abstract_unit"
require "active_support/dependencies"

module ActiveSupport
  module Dependencies
    class ClassCacheTest < ActiveSupport::TestCase
      def setup
        @cache = ClassCache.new
      end

      def test_empty?
        assert_empty @cache
        @cache.store(ClassCacheTest)
        assert_not_empty @cache
      end

      def test_clear!
        assert_empty @cache
        @cache.store(ClassCacheTest)
        assert_not_empty @cache
        @cache.clear!
        assert_empty @cache
      end

      def test_set_key
        @cache.store(ClassCacheTest)
        assert @cache.key?(ClassCacheTest.name)
      end

      def test_get_with_class
        @cache.store(ClassCacheTest)
        assert_equal ClassCacheTest, @cache.get(ClassCacheTest)
      end

      def test_get_with_name
        @cache.store(ClassCacheTest)
        assert_equal ClassCacheTest, @cache.get(ClassCacheTest.name)
      end

      def test_get_constantizes
        assert_empty @cache
        assert_equal ClassCacheTest, @cache.get(ClassCacheTest.name)
      end

      def test_get_constantizes_fails_on_invalid_names
        assert_empty @cache
        assert_raise NameError do
          @cache.get("OmgTotallyInvalidConstantName")
        end
      end

      def test_get_alias
        assert_empty @cache
        assert_equal @cache[ClassCacheTest.name], @cache.get(ClassCacheTest.name)
      end

      def test_safe_get_constantizes
        assert_empty @cache
        assert_equal ClassCacheTest, @cache.safe_get(ClassCacheTest.name)
      end

      def test_safe_get_constantizes_doesnt_fail_on_invalid_names
        assert_empty @cache
        assert_nil @cache.safe_get("OmgTotallyInvalidConstantName")
      end

      def test_new_rejects_strings
        @cache.store ClassCacheTest.name
        assert_not @cache.key?(ClassCacheTest.name)
      end

      def test_store_returns_self
        x = @cache.store ClassCacheTest
        assert_equal @cache, x
      end
    end
  end
end