public
Rubygem
Description: Ultra lightweight gem/plugin to do in-memory caching of any Ruby method
Homepage: http://github.com/JackDanger/simple_memoize
Clone URL: git://github.com/JackDanger/simple_memoize.git
commit  33d0c969f1747f93acaf1bc5906ddc7d3ab2be4f
tree    344495f9769ebbaa42f59b833fc30765101fe2b3
parent  29ec74c4cd2e09938854fbb558b049fa16c8f926
simple_memoize / test / test_simple_memoize.rb
100644 115 lines (96 sloc) 2.646 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/simple_memoize'
require 'rubygems'
require 'mocha'
 
module Barks
  def growl
    'Grrrrr'
  end
  memoize :growl
  
  def protected_growl
    'Grrrrr'
  end
  protected :protected_growl
  memoize :protected_growl
  
  def private_growl
    'Grrrrr'
  end
  private :private_growl
  memoize :private_growl
  
  class << self
    def sounds
      ['woof', 'ruff']
    end
    memoize :sounds
  end
end
 
class Dog
  include Barks
  
  def drink
    'slurp'
  end
  memoize :drink
  
  class << self
    def breeds
      ['doberman', 'dalmatian']
    end
    memoize :breeds
  end
end
 
class SimpleMemoizeTest < Test::Unit::TestCase
  def test_module_method_only_calls_memoized_once
    dog = Dog.new
    dog.expects(:growl_without_memo).returns('Grrrrr').once
    4.times { dog.growl }
  end
 
  def test_module_method_calls_method_several_times
    dog = Dog.new
    dog.expects(:growl).returns('Grrrrr').times(4)
    4.times { dog.growl }
  end
 
  def test_module_class_method_only_calls_memoized_once
    sounds = Barks.sounds_without_memo
    Barks.expects(:sounds_without_memo).returns(sounds).once
    4.times { Barks.sounds }
  end
 
  def test_module_class_method_calls_method_several_times
    sounds = Barks.sounds_without_memo
    Barks.expects(:sounds).returns(sounds).times(4)
    4.times { Barks.sounds }
  end
  
  def test_object_method_calls_memoized_once
    dog = Dog.new
    drink = dog.drink_without_memo
    dog.expects(:drink_without_memo).returns(drink).once
    4.times { dog.drink }
  end
  
  def test_object_method_calls_method_several_times
    dog = Dog.new
    drink = dog.drink_without_memo
    dog.expects(:drink).returns(drink).times(4)
    4.times { dog.drink }
  end
  
  def test_class_method_calls_memoized_once
    breeds = Dog.breeds_without_memo
    Dog.expects(:breeds_without_memo).returns(breeds).once
    4.times { Dog.breeds }
  end
  
  def test_class_method_calls_method_several_times
    breeds = Dog.breeds_without_memo
    Dog.expects(:breeds).returns(breeds).times(4)
    4.times { Dog.breeds }
  end
  
  def test_protected_methods_remain_protected
    dog = Dog.new
    assert dog.protected_methods.include?('protected_growl_without_memo')
    assert dog.protected_methods.include?('protected_growl')
  end
  
  def test_private_methods_remain_private
    dog = Dog.new
    assert dog.private_methods.include?('private_growl_without_memo')
    assert dog.private_methods.include?('private_growl')
  end
  
  def test_cant_memoize_a_missing_method
    assert_raises(NoMethodError) { Barks.memoize :totally_bad_method_name }
  end
end