aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-11-19 12:04:51 -0800
committerSean Griffin <sean@thoughtbot.com>2014-11-19 12:11:26 -0800
commitf767ac22fa213df754e160c59189d28ed4f95568 (patch)
treeef999dd9c51aca766da16cd1f7cbb9b7ab8944ee /activerecord/test
parentcafed35b610d830049b7dffd924cbcdeedab72bb (diff)
downloadrails-f767ac22fa213df754e160c59189d28ed4f95568.tar.gz
rails-f767ac22fa213df754e160c59189d28ed4f95568.tar.bz2
rails-f767ac22fa213df754e160c59189d28ed4f95568.zip
Reintroduce cache with tests
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/type/type_map_test.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/activerecord/test/cases/type/type_map_test.rb b/activerecord/test/cases/type/type_map_test.rb
index 1e4eba1961..172c6dfc4c 100644
--- a/activerecord/test/cases/type/type_map_test.rb
+++ b/activerecord/test/cases/type/type_map_test.rb
@@ -139,6 +139,38 @@ module ActiveRecord
assert_equal "foo-1-2-3", mapping.fetch("foo", 1, 2, 3) { |*args| args.join("-") }
assert_equal "bar-1-2-3", mapping.fetch("bar", 1, 2, 3) { |*args| args.join("-") }
end
+
+ def test_fetch_memoizes
+ mapping = TypeMap.new
+
+ looked_up = false
+ mapping.register_type(1) do
+ fail if looked_up
+ looked_up = true
+ "string"
+ end
+
+ assert_equal "string", mapping.fetch(1)
+ assert_equal "string", mapping.fetch(1)
+ end
+
+ def test_fetch_memoizes_on_args
+ mapping = TypeMap.new
+ mapping.register_type("foo") { |*args| args.join("-") }
+
+ assert_equal "foo-1-2-3", mapping.fetch("foo", 1, 2, 3) { |*args| args.join("-") }
+ assert_equal "foo-2-3-4", mapping.fetch("foo", 2, 3, 4) { |*args| args.join("-") }
+ end
+
+ def test_register_clears_cache
+ mapping = TypeMap.new
+
+ mapping.register_type(1, "string")
+ mapping.lookup(1)
+ mapping.register_type(1, "int")
+
+ assert_equal "int", mapping.lookup(1)
+ end
end
end
end