spec/woyo/world/attributes_spec.rb in woyo-world-0.0.8 vs spec/woyo/world/attributes_spec.rb in woyo-world-0.0.9

- old
+ new

@@ -221,10 +221,68 @@ end end + context 'listeners' do + + let(:lat) { AttrTest.new } + let(:listener_class) do + class AttrListenerTest + attr_reader :changed_attribute, :changed_value + def notify attr,value + @changed_attribute = attr + @changed_value = value + end + end + AttrListenerTest + end + + before :each do + lat.attribute say: "Hello" + end + + it 'are notified of attribute changes' do + expect(lat.say).to eq "Hello" + listener = listener_class.new + lat.attributes.add_listener :say, listener + lat.say "Bye" + expect(listener.changed_attribute).to eq :say + end + + it 'are not notified if value does not change' do + expect(lat.say).to eq "Hello" + listener = listener_class.new + lat.attributes.add_listener :say, listener + lat.say "Hello" + expect(listener.changed_attribute).to be_nil + end + + it 'are notified with attribute and value' do + expect(lat.say).to eq "Hello" + listener = listener_class.new + lat.attributes.add_listener :say, listener + lat.say "Bye" + expect(listener.changed_attribute).to eq :say + expect(listener.changed_value).to eq "Bye" + end + + it 'may be multiple' do + expect(lat.say).to eq "Hello" + listener1 = listener_class.new + lat.attributes.add_listener :say, listener1 + listener2 = listener_class.new + lat.attributes.add_listener :say, listener2 + lat.say "Bye" + expect(listener1.changed_attribute).to eq :say + expect(listener1.changed_value).to eq "Bye" + expect(listener2.changed_attribute).to eq :say + expect(listener2.changed_value).to eq "Bye" + end + + end + context 'that are boolean have convenient instance accessors' do let(:bat) { class BooleanAttrTest; include Woyo::Attributes; end.new } before :each do @@ -414,9 +472,56 @@ end it 'returns empty list if no keys are truthy attributes' do hat.reaction hot: 'Sweat', warm: 'Relax', cool: 'Huddle', cold: 'Shiver' expect(hat.reaction).to eq [ ] + end + + end + + context 'changes' do + + let(:cat) { AttrTest.new } + + before :each do + cat.attributes :one, :two, :three + end + + it 'can be tracked' do + expect(cat.changes).to eq nil + cat.track_changes + expect(cat.changes).to be_instance_of Woyo::Attributes::ChangesHash + expect(cat.changes).to be_empty + cat.one = 1 + cat.two = 2 + expect(cat.changes.count).to eq 2 + expect(cat.changes[:one]).to eq 1 + expect(cat.changes[:two]).to eq 2 + end + + it 'tracking can be cleared' do + cat.track_changes + cat.three = 3 + expect(cat.changes[:three]).to eq 3 + cat.clear_changes + expect(cat.changes).to be_empty + cat.one = 1 + expect(cat.changes[:one]).to eq 1 + end + + it 'tracking includes dependent attributes (hashes with attribute keys)' do + cat.attributes :reaction, :hot, :warm, :cool, :cold + cat.hot = true + cat.reaction hot: 'Sweat', warm: 'Relax', cool: 'Huddle', cold: 'Shiver' + cat.track_changes + cat.cold = true + cat.hot = false + changes = cat.changes + expect(changes.keys).to include :reaction + expect(changes[:reaction]).to eq "Shiver" + expect(changes[:cold]).to eq true + expect(changes[:hot]).to eq false + expect(changes.count).to eq 3 end end end