2008-07-14 20:50:32 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
2009-04-13 19:56:04 -04:00
|
|
|
class MemoizableTest < ActiveSupport::TestCase
|
2008-11-22 18:19:19 -05:00
|
|
|
class Person
|
2011-06-15 16:08:08 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
extend ActiveSupport::Memoizable
|
|
|
|
end
|
2008-07-14 20:50:32 -04:00
|
|
|
|
2009-09-04 17:28:36 -04:00
|
|
|
attr_reader :name_calls, :age_calls, :is_developer_calls, :name_query_calls
|
2009-03-06 09:40:54 -05:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def initialize
|
|
|
|
@name_calls = 0
|
|
|
|
@age_calls = 0
|
2009-03-06 09:40:54 -05:00
|
|
|
@is_developer_calls = 0
|
2009-09-04 17:28:36 -04:00
|
|
|
@name_query_calls = 0
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def name
|
|
|
|
@name_calls += 1
|
|
|
|
"Josh"
|
|
|
|
end
|
2008-08-07 15:55:14 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def name?
|
2009-09-04 17:28:36 -04:00
|
|
|
@name_query_calls += 1
|
2008-11-22 18:19:19 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
memoize :name?
|
2008-08-07 15:55:14 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def update(name)
|
|
|
|
"Joshua"
|
|
|
|
end
|
|
|
|
memoize :update
|
2008-07-18 12:18:16 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def age
|
|
|
|
@age_calls += 1
|
|
|
|
nil
|
2008-07-22 11:26:44 -04:00
|
|
|
end
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
memoize :name, :age
|
2009-03-06 09:40:54 -05:00
|
|
|
|
2010-09-23 17:48:35 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def memoize_protected_test
|
|
|
|
'protected'
|
|
|
|
end
|
|
|
|
memoize :memoize_protected_test
|
|
|
|
|
2009-03-06 09:40:54 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def is_developer?
|
|
|
|
@is_developer_calls += 1
|
|
|
|
"Yes"
|
|
|
|
end
|
|
|
|
memoize :is_developer?
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-07-18 12:18:16 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
class Company
|
|
|
|
attr_reader :name_calls
|
|
|
|
def initialize
|
|
|
|
@name_calls = 0
|
2008-07-22 11:26:44 -04:00
|
|
|
end
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def name
|
|
|
|
@name_calls += 1
|
|
|
|
"37signals"
|
2008-07-22 11:26:44 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
module Rates
|
2011-06-15 16:08:08 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
extend ActiveSupport::Memoizable
|
|
|
|
end
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
attr_reader :sales_tax_calls
|
|
|
|
def sales_tax(price)
|
|
|
|
@sales_tax_calls ||= 0
|
|
|
|
@sales_tax_calls += 1
|
|
|
|
price * 0.1025
|
|
|
|
end
|
|
|
|
memoize :sales_tax
|
|
|
|
end
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
class Calculator
|
2011-06-15 16:08:08 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
extend ActiveSupport::Memoizable
|
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
include Rates
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
attr_reader :fib_calls
|
|
|
|
def initialize
|
|
|
|
@fib_calls = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def fib(n)
|
|
|
|
@fib_calls += 1
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
if n == 0 || n == 1
|
|
|
|
n
|
|
|
|
else
|
|
|
|
fib(n - 1) + fib(n - 2)
|
2008-07-22 11:26:44 -04:00
|
|
|
end
|
2008-07-14 20:50:32 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
memoize :fib
|
2008-07-14 20:50:32 -04:00
|
|
|
|
2011-06-14 19:11:09 -04:00
|
|
|
def add_or_subtract(i, j, add)
|
|
|
|
if add
|
|
|
|
i + j
|
|
|
|
else
|
|
|
|
i - j
|
|
|
|
end
|
|
|
|
end
|
|
|
|
memoize :add_or_subtract
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def counter
|
|
|
|
@count ||= 0
|
|
|
|
@count += 1
|
2008-07-18 12:18:16 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
memoize :counter
|
|
|
|
end
|
2008-07-18 12:18:16 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def setup
|
|
|
|
@person = Person.new
|
|
|
|
@calculator = Calculator.new
|
|
|
|
end
|
2008-07-18 12:18:16 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoization
|
|
|
|
assert_equal "Josh", @person.name
|
|
|
|
assert_equal 1, @person.name_calls
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
3.times { assert_equal "Josh", @person.name }
|
|
|
|
assert_equal 1, @person.name_calls
|
|
|
|
end
|
2008-10-05 18:24:52 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoization_with_punctuation
|
|
|
|
assert_equal true, @person.name?
|
|
|
|
|
|
|
|
assert_nothing_raised(NameError) do
|
|
|
|
@person.memoize_all
|
|
|
|
@person.unmemoize_all
|
2008-08-07 15:55:14 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-08-07 15:55:14 -04:00
|
|
|
|
2009-09-04 17:28:36 -04:00
|
|
|
def test_memoization_flush_with_punctuation
|
|
|
|
assert_equal true, @person.name?
|
|
|
|
@person.flush_cache(:name?)
|
|
|
|
3.times { assert_equal true, @person.name? }
|
|
|
|
assert_equal 2, @person.name_query_calls
|
|
|
|
end
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoization_with_nil_value
|
|
|
|
assert_equal nil, @person.age
|
|
|
|
assert_equal 1, @person.age_calls
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
3.times { assert_equal nil, @person.age }
|
|
|
|
assert_equal 1, @person.age_calls
|
|
|
|
end
|
2008-07-14 20:50:32 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_reloadable
|
|
|
|
assert_equal 1, @calculator.counter
|
|
|
|
assert_equal 2, @calculator.counter(:reload)
|
|
|
|
assert_equal 2, @calculator.counter
|
|
|
|
assert_equal 3, @calculator.counter(true)
|
|
|
|
assert_equal 3, @calculator.counter
|
|
|
|
end
|
2008-07-14 20:50:32 -04:00
|
|
|
|
2008-12-03 11:57:08 -05:00
|
|
|
def test_flush_cache
|
|
|
|
assert_equal 1, @calculator.counter
|
|
|
|
|
|
|
|
assert @calculator.instance_variable_get(:@_memoized_counter).any?
|
|
|
|
@calculator.flush_cache(:counter)
|
|
|
|
assert @calculator.instance_variable_get(:@_memoized_counter).empty?
|
|
|
|
|
|
|
|
assert_equal 2, @calculator.counter
|
|
|
|
end
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_unmemoize_all
|
|
|
|
assert_equal 1, @calculator.counter
|
2008-08-13 20:22:38 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
assert @calculator.instance_variable_get(:@_memoized_counter).any?
|
|
|
|
@calculator.unmemoize_all
|
|
|
|
assert @calculator.instance_variable_get(:@_memoized_counter).empty?
|
2008-08-13 20:22:38 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_equal 2, @calculator.counter
|
|
|
|
end
|
2008-08-13 20:22:38 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoize_all
|
|
|
|
@calculator.memoize_all
|
|
|
|
assert @calculator.instance_variable_defined?(:@_memoized_counter)
|
|
|
|
end
|
2008-08-13 20:22:38 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoization_cache_is_different_for_each_instance
|
|
|
|
assert_equal 1, @calculator.counter
|
|
|
|
assert_equal 2, @calculator.counter(:reload)
|
|
|
|
assert_equal 1, Calculator.new.counter
|
|
|
|
end
|
2008-07-18 12:18:16 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoized_is_not_affected_by_freeze
|
|
|
|
@person.freeze
|
|
|
|
assert_equal "Josh", @person.name
|
|
|
|
assert_equal "Joshua", @person.update("Joshua")
|
|
|
|
end
|
2008-07-14 20:50:32 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoization_with_args
|
|
|
|
assert_equal 55, @calculator.fib(10)
|
|
|
|
assert_equal 11, @calculator.fib_calls
|
|
|
|
end
|
2008-07-14 21:23:23 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_reloadable_with_args
|
|
|
|
assert_equal 55, @calculator.fib(10)
|
|
|
|
assert_equal 11, @calculator.fib_calls
|
|
|
|
assert_equal 55, @calculator.fib(10, :reload)
|
|
|
|
assert_equal 12, @calculator.fib_calls
|
|
|
|
assert_equal 55, @calculator.fib(10, true)
|
|
|
|
assert_equal 13, @calculator.fib_calls
|
|
|
|
end
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2011-06-14 19:11:09 -04:00
|
|
|
def test_memoization_with_boolean_arg
|
|
|
|
assert_equal 4, @calculator.add_or_subtract(2, 2, true)
|
|
|
|
assert_equal 2, @calculator.add_or_subtract(4, 2, false)
|
|
|
|
end
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_object_memoization
|
|
|
|
[Company.new, Company.new, Company.new].each do |company|
|
2011-06-15 16:08:08 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
company.extend ActiveSupport::Memoizable
|
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
company.memoize :name
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_equal "37signals", company.name
|
|
|
|
assert_equal 1, company.name_calls
|
|
|
|
assert_equal "37signals", company.name
|
|
|
|
assert_equal 1, company.name_calls
|
2008-07-22 11:26:44 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_memoized_module_methods
|
|
|
|
assert_equal 1.025, @calculator.sales_tax(10)
|
|
|
|
assert_equal 1, @calculator.sales_tax_calls
|
|
|
|
assert_equal 1.025, @calculator.sales_tax(10)
|
|
|
|
assert_equal 1, @calculator.sales_tax_calls
|
|
|
|
assert_equal 2.5625, @calculator.sales_tax(25)
|
|
|
|
assert_equal 2, @calculator.sales_tax_calls
|
|
|
|
end
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_object_memoized_module_methods
|
|
|
|
company = Company.new
|
|
|
|
company.extend(Rates)
|
2008-07-22 11:26:44 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_equal 1.025, company.sales_tax(10)
|
|
|
|
assert_equal 1, company.sales_tax_calls
|
|
|
|
assert_equal 1.025, company.sales_tax(10)
|
|
|
|
assert_equal 1, company.sales_tax_calls
|
|
|
|
assert_equal 2.5625, company.sales_tax(25)
|
|
|
|
assert_equal 2, company.sales_tax_calls
|
|
|
|
end
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_double_memoization
|
|
|
|
assert_raise(RuntimeError) { Person.memoize :name }
|
|
|
|
person = Person.new
|
2011-06-15 16:08:08 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
person.extend ActiveSupport::Memoizable
|
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_raise(RuntimeError) { person.memoize :name }
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
company = Company.new
|
2011-06-15 16:08:08 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
company.extend ActiveSupport::Memoizable
|
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
company.memoize :name
|
|
|
|
assert_raise(RuntimeError) { company.memoize :name }
|
2008-07-14 20:50:32 -04:00
|
|
|
end
|
2009-03-06 09:40:54 -05:00
|
|
|
|
2010-09-23 17:48:35 -04:00
|
|
|
def test_protected_method_memoization
|
|
|
|
person = Person.new
|
|
|
|
|
|
|
|
assert_raise(NoMethodError) { person.memoize_protected_test }
|
|
|
|
assert_equal "protected", person.send(:memoize_protected_test)
|
|
|
|
end
|
|
|
|
|
2009-03-06 09:40:54 -05:00
|
|
|
def test_private_method_memoization
|
|
|
|
person = Person.new
|
|
|
|
|
|
|
|
assert_raise(NoMethodError) { person.is_developer? }
|
|
|
|
assert_equal "Yes", person.send(:is_developer?)
|
|
|
|
assert_equal 1, person.is_developer_calls
|
|
|
|
assert_equal "Yes", person.send(:is_developer?)
|
|
|
|
assert_equal 1, person.is_developer_calls
|
|
|
|
end
|
|
|
|
|
2008-07-14 20:50:32 -04:00
|
|
|
end
|