Permalink
Browse files

The Person tests don't rely on instance variables

allows us to test freezing more properly
  • Loading branch information...
matthewrudy committed Mar 20, 2013
1 parent 5f9fc0a commit 2e947e6e922c6f7d0e2e7aab3ddcc2aff9cec8dc
Showing with 44 additions and 12 deletions.
  1. +44 −12 test/memoist_test.rb
View
@@ -2,26 +2,58 @@
require 'memoist'
class MemoistTest < Test::Unit::TestCase
+
+ class CallCounter
+
+ def initialize
+ @calls = {}
+ end
+
+ def call(method_name)
+ @calls[method_name] ||= 0
+ @calls[method_name] += 1
+ end
+
+ def count(method_name)
+ @calls[method_name] ||= 0
+ end
+
+ end
+
class Person
extend Memoist
- attr_reader :name_calls, :student_name_calls, :age_calls, :is_developer_calls, :name_query_calls
-
def initialize
- @name_calls = 0
- @student_name_calls = 0
- @age_calls = 0
- @is_developer_calls = 0
- @name_query_calls = 0
+ @counter = CallCounter.new
+ end
+
+ def name_calls
+ @counter.count(:name)
+ end
+
+ def student_name_calls
+ @counter.count(:student_name)
+ end
+
+ def name_query_calls
+ @counter.count(:name?)
+ end
+
+ def is_developer_calls
+ @counter.count(:is_developer?)
+ end
+
+ def age_calls
+ @counter.count(:age)
end
def name
- @name_calls += 1
+ @counter.call(:name)
"Josh"
end
def name?
- @name_query_calls += 1
+ @counter.call(:name?)
true
end
memoize :name?
@@ -32,7 +64,7 @@ def update(name)
memoize :update
def age
- @age_calls += 1
+ @counter.call(:age)
nil
end
@@ -48,15 +80,15 @@ def memoize_protected_test
private
def is_developer?
- @is_developer_calls += 1
+ @counter.call(:is_developer?)
"Yes"
end
memoize :is_developer?
end
class Student < Person
def name
- @student_name_calls += 1
+ @counter.call(:student_name)
"Student #{super}"
end
memoize :name, :identifier => :student

0 comments on commit 2e947e6

Please sign in to comment.