main
1require "spec_helper"
2
3module Nasty
4 describe IdentityMap do
5 let(:sut) { IdentityMap.new }
6
7 context "when an item is added" do
8 let(:item) { Item.new(:id => 187) }
9
10 before { sut.add(item) }
11
12 it "indicates that an item with that id is available" do
13 sut.has_item_for?(item.id).should be_true
14 end
15
16 it "loads the item" do
17 sut.item_for(item.id).should == item
18 end
19
20 context "when an item is evicted" do
21 before { sut.evict(item) }
22
23 it "indicates that it does not have an item for the evicted id" do
24 sut.has_item_for?(item.id).should be_false
25 end
26
27 it "returns nothing" do
28 sut.item_for(item.id).should be_nil
29 end
30 end
31 end
32
33 context "when no items have been added" do
34 it "indicates that it does not have any items for any id" do
35 (0..10).each do |i|
36 sut.has_item_for?(i).should be_false
37 end
38 end
39 end
40
41 class Item
42 attr_reader :id
43
44 def initialize(attributes)
45 @id = attributes[:id]
46 end
47 end
48 end
49end