/test/extensions_test.rb
Ruby | 111 lines | 93 code | 17 blank | 1 comment | 1 complexity | 444bf0a423d9ae6bd37b90e45a87f5c8 MD5 | raw file
Possible License(s): JSON
1require File.expand_path('../helper', __FILE__) 2 3class KerneltExtensionsTest < Test::Unit::TestCase 4 class Foo 5 def foo 6 __method__ 7 end 8 9 def bar 10 foo 11 end 12 13 def baz 14 bar 15 end 16 end 17 18 class Bar 19 def foo 20 calling_method 21 end 22 23 def bar 24 calling_method 25 end 26 27 def calling_method 28 __method__(1) 29 end 30 end 31 32 def test___method___works_regardless_of_nesting 33 f = Foo.new 34 [:foo, :bar, :baz].each do |method| 35 assert_equal 'foo', f.send(method) 36 end 37 end 38 39 def test___method___depth 40 b = Bar.new 41 assert_equal 'foo', b.foo 42 assert_equal 'bar', b.bar 43 end 44end if RUBY_VERSION <= '1.8.7' 45 46class ModuleExtensionsTest < Test::Unit::TestCase 47 class Foo 48 def foo(reload = false) 49 expirable_memoize(reload) do 50 Time.now 51 end 52 end 53 54 def bar(reload = false) 55 expirable_memoize(reload, :baz) do 56 Time.now 57 end 58 end 59 60 def quux 61 Time.now 62 end 63 memoized :quux 64 end 65 66 def setup 67 @instance = Foo.new 68 end 69 70 def test_memoize 71 assert !instance_variables_of(@instance).include?('@foo') 72 cached_result = @instance.foo 73 assert_equal cached_result, @instance.foo 74 assert instance_variables_of(@instance).include?('@foo') 75 assert_equal cached_result, @instance.send(:instance_variable_get, :@foo) 76 assert_not_equal cached_result, new_cache = @instance.foo(:reload) 77 assert_equal new_cache, @instance.foo 78 assert_equal new_cache, @instance.send(:instance_variable_get, :@foo) 79 end 80 81 def test_customizing_memoize_storage 82 assert !instance_variables_of(@instance).include?('@bar') 83 assert !instance_variables_of(@instance).include?('@baz') 84 cached_result = @instance.bar 85 assert !instance_variables_of(@instance).include?('@bar') 86 assert instance_variables_of(@instance).include?('@baz') 87 assert_equal cached_result, @instance.bar 88 assert_equal cached_result, @instance.send(:instance_variable_get, :@baz) 89 assert_nil @instance.send(:instance_variable_get, :@bar) 90 end 91 92 def test_memoized 93 assert !instance_variables_of(@instance).include?('@quux') 94 cached_result = @instance.quux 95 assert_equal cached_result, @instance.quux 96 assert instance_variables_of(@instance).include?('@quux') 97 assert_equal cached_result, @instance.send(:instance_variable_get, :@quux) 98 assert_not_equal cached_result, new_cache = @instance.quux(:reload) 99 assert_equal new_cache, @instance.quux 100 assert_equal new_cache, @instance.send(:instance_variable_get, :@quux) 101 end 102 103 private 104 # For 1.9 compatibility 105 def instance_variables_of(object) 106 object.instance_variables.map do |instance_variable| 107 instance_variable.to_s 108 end 109 end 110 111end