2008-07-14 20:50:32 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
uses_mocha 'Memoizable' do
|
|
|
|
class MemoizableTest < Test::Unit::TestCase
|
|
|
|
class Person
|
|
|
|
include ActiveSupport::Memoizable
|
|
|
|
|
|
|
|
def name
|
|
|
|
fetch_name_from_floppy
|
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :name
|
2008-07-14 20:50:32 -04:00
|
|
|
|
|
|
|
def age
|
|
|
|
nil
|
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :age
|
2008-07-14 20:50:32 -04:00
|
|
|
|
|
|
|
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
|
2008-07-14 21:23:23 -04:00
|
|
|
|
|
|
|
def test_double_memoization
|
|
|
|
assert_raise(RuntimeError) { Person.memoize :name }
|
|
|
|
end
|
2008-07-14 20:50:32 -04:00
|
|
|
end
|
|
|
|
end
|