Skip to content

Instantly share code, notes, and snippets.

@kenglishhi
Created November 9, 2012 01:21
Show Gist options
  • Save kenglishhi/4043120 to your computer and use it in GitHub Desktop.
Save kenglishhi/4043120 to your computer and use it in GitHub Desktop.
Failing test shows memoize doesn't memoize a method overridden in a subclas
require 'test_helper'
require 'memoist'
class SubclassBugTest < Test::Unit::TestCase
class Person
extend Memoist
attr_reader :name_calls
def initialize
@name_calls = 0
end
def name
@name_calls += 1
"Person"
end
memoize :name
end
class Student < Person
attr_reader :name_calls
def initialize
@name_calls = 0
end
def name
@name_calls += 1
"Student"
end
end
def setup
@student = Student.new
end
def test_memoization_subclass
assert_equal "Student", @student.name
assert_equal 1, @student.name_calls
3.times { assert_equal "Student", @student.name }
assert_equal 1, @student.name_calls
assert @student.name.equal?(@person.name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment