/test/extensions_test.rb

http://github.com/drewblas/aws-ses · Ruby · 111 lines · 93 code · 17 blank · 1 comment · 1 complexity · 444bf0a423d9ae6bd37b90e45a87f5c8 MD5 · raw file

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