aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/memoizable_test.rb
blob: 1b6cec2b2f535bbb2450d6b9b9c63ff41ed1dc29 (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
require 'abstract_unit'

uses_mocha 'Memoizable' do
  class MemoizableTest < Test::Unit::TestCase
    class Person
      include ActiveSupport::Memoizable

      def name
        fetch_name_from_floppy
      end
      memoize :name

      def age
        nil
      end
      memoize :age

      private
        def fetch_name_from_floppy
          "Josh"
        end
    end

    def test_memoization
      person = Person.new
      assert_equal "Josh", person.name

      person.expects(:fetch_name_from_floppy).never
      2.times { assert_equal "Josh", person.name }
    end

    def test_memoized_methods_are_frozen
      person = Person.new
      person.freeze
      assert_equal "Josh", person.name
      assert_equal true, person.name.frozen?
    end

    def test_memoization_frozen_with_nil_value
      person = Person.new
      person.freeze
      assert_equal nil, person.age
    end
  end
end