Class: TestBlockCache
- Defined in:
- lib/block_cache.rb
Overview
Unit tests for the BlockCache class
This test suite verifies correct behavior of:
-
initialization (with and without initial cache)
-
finding objects by id
-
getting or creating objects via block or proc
-
adding objects (including replacing existing ones)
-
removing objects by id
-
clearing the cache
-
reporting cache size
Instance Method Summary collapse
- #setup ⇒ Object
- #test_add_appends_new_object ⇒ Object
- #test_add_raises_for_object_without_id_method ⇒ Object
- #test_add_replaces_existing_object_with_same_id ⇒ Object
- #test_clear_empties_the_cache ⇒ Object
- #test_find_by_id_returns_matching_object ⇒ Object
- #test_find_by_id_returns_nil_when_not_found ⇒ Object
- #test_get_or_create_creates_with_block ⇒ Object
- #test_get_or_create_creates_with_proc ⇒ Object
- #test_get_or_create_raises_without_block_or_proc ⇒ Object
- #test_get_or_create_returns_existing_object ⇒ Object
- #test_initialize_defaults_to_empty_array ⇒ Object
- #test_initialize_with_provided_array ⇒ Object
- #test_remove_deletes_object_by_id ⇒ Object
- #test_remove_nonexistent_id_leaves_cache_unchanged ⇒ Object
- #test_size_returns_number_of_cached_objects ⇒ Object
Instance Method Details
#setup ⇒ Object
97 98 99 100 101 102 |
# File 'lib/block_cache.rb', line 97 def setup @obj1 = DummyObject.new(1) @obj2 = DummyObject.new(2) @initial_cache = [@obj1] @cache = BlockCache.new(@initial_cache.dup) end |
#test_add_appends_new_object ⇒ Object
150 151 152 153 154 155 |
# File 'lib/block_cache.rb', line 150 def test_add_appends_new_object result = @cache.add(@obj2) assert_equal @obj2, result assert_equal 2, @cache.size assert_equal [@obj1, @obj2], @cache.cache end |
#test_add_raises_for_object_without_id_method ⇒ Object
165 166 167 168 169 170 |
# File 'lib/block_cache.rb', line 165 def test_add_raises_for_object_without_id_method invalid = Object.new assert_raises(ArgumentError) do @cache.add(invalid) end end |
#test_add_replaces_existing_object_with_same_id ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/block_cache.rb', line 157 def test_add_replaces_existing_object_with_same_id replacement = DummyObject.new(1) result = @cache.add(replacement) assert_equal replacement, result assert_equal 1, @cache.size assert_equal replacement, @cache.find_by_id(1) end |
#test_clear_empties_the_cache ⇒ Object
186 187 188 189 190 |
# File 'lib/block_cache.rb', line 186 def test_clear_empties_the_cache @cache.clear assert_equal [], @cache.cache assert_equal 0, @cache.size end |
#test_find_by_id_returns_matching_object ⇒ Object
113 114 115 |
# File 'lib/block_cache.rb', line 113 def test_find_by_id_returns_matching_object assert_equal @obj1, @cache.find_by_id(1) end |
#test_find_by_id_returns_nil_when_not_found ⇒ Object
117 118 119 |
# File 'lib/block_cache.rb', line 117 def test_find_by_id_returns_nil_when_not_found assert_nil @cache.find_by_id(999) end |
#test_get_or_create_creates_with_block ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/block_cache.rb', line 127 def test_get_or_create_creates_with_block new_obj = @cache.get_or_create(3) { |id| DummyObject.new(id) } assert_instance_of DummyObject, new_obj assert_equal 3, new_obj.id assert_includes @cache.cache, new_obj assert_equal 2, @cache.size end |
#test_get_or_create_creates_with_proc ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/block_cache.rb', line 135 def test_get_or_create_creates_with_proc factory = proc { |id| DummyObject.new(id * 10) } new_obj = @cache.get_or_create(5, factory) assert_instance_of DummyObject, new_obj assert_equal 50, new_obj.id assert_equal new_obj, @cache.find_by_id(50) assert_equal 2, @cache.size end |
#test_get_or_create_raises_without_block_or_proc ⇒ Object
144 145 146 147 148 |
# File 'lib/block_cache.rb', line 144 def test_get_or_create_raises_without_block_or_proc assert_raises(ArgumentError) do @cache.get_or_create(7) end end |
#test_get_or_create_returns_existing_object ⇒ Object
121 122 123 124 125 |
# File 'lib/block_cache.rb', line 121 def test_get_or_create_returns_existing_object found = @cache.get_or_create(1) { |id| DummyObject.new(id) } assert_equal @obj1, found assert_equal 1, @cache.size end |
#test_initialize_defaults_to_empty_array ⇒ Object
104 105 106 107 |
# File 'lib/block_cache.rb', line 104 def test_initialize_defaults_to_empty_array empty_cache = BlockCache.new assert_equal [], empty_cache.cache end |
#test_initialize_with_provided_array ⇒ Object
109 110 111 |
# File 'lib/block_cache.rb', line 109 def test_initialize_with_provided_array assert_equal @initial_cache, @cache.cache end |
#test_remove_deletes_object_by_id ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/block_cache.rb', line 172 def test_remove_deletes_object_by_id @cache.add(@obj2) @cache.remove(1) assert_nil @cache.find_by_id(1) assert_equal 1, @cache.size assert_equal [@obj2], @cache.cache end |
#test_remove_nonexistent_id_leaves_cache_unchanged ⇒ Object
180 181 182 183 184 |
# File 'lib/block_cache.rb', line 180 def test_remove_nonexistent_id_leaves_cache_unchanged original = @cache.cache.dup @cache.remove(999) assert_equal original, @cache.cache end |
#test_size_returns_number_of_cached_objects ⇒ Object
192 193 194 195 196 |
# File 'lib/block_cache.rb', line 192 def test_size_returns_number_of_cached_objects assert_equal 1, @cache.size @cache.add(@obj2) assert_equal 2, @cache.size end |