test/test_characterizable.rb in characterizable-0.0.1 vs test/test_characterizable.rb in characterizable-0.0.2

- old
+ new

@@ -51,59 +51,79 @@ end end end end + should "survive as a certain kind of hash" do + a = SimpleAutomobile.new + + assert_equal Characterizable::SurvivorHash, SimpleAutomobile.characteristics.class + assert_equal Characterizable::SurvivorHash, SimpleAutomobile.characteristics.select { false }.class + assert_equal Characterizable::SurvivorHash, SimpleAutomobile.characteristics.slice(:hello).class + assert_equal Characterizable::SurvivorHash, SimpleAutomobile.characteristics.merge({:hi => 'there'}).class + + assert_equal Characterizable::SurvivorHash, a.characteristics.known.class + assert_equal Characterizable::SurvivorHash, a.characteristics.known.select { false }.class + assert_equal Characterizable::SurvivorHash, a.characteristics.known.slice(:hello).class + assert_equal Characterizable::SurvivorHash, a.characteristics.known.merge({:hi => 'there'}).class + + assert_equal Characterizable::Snapshot, a.characteristics.class + assert_equal Characterizable::Snapshot, a.characteristics.select { false }.class + assert_equal Characterizable::Snapshot, a.characteristics.slice(:hello).class + assert_equal Characterizable::Snapshot, a.characteristics.merge({:hi => 'there'}).class + end + should "tell you what characteristics are known" do a = SimpleAutomobile.new a.make = 'Ford' - assert_equal [:make], a.known_characteristics.map(&:name) + assert_equal [:make], a.characteristics.known.keys end should "tell you what characteristics are unknown" do a = SimpleAutomobile.new a.make = 'Ford' - assert_equal [:model, :variant], a.unknown_characteristics.map(&:name) + assert_equal [:model, :variant], a.characteristics.unknown.keys end should "present a concise set of known characteristics by getting rid of those that have been trumped" do a = SimpleAutomobile.new a.make = 'Ford' a.model = 'Taurus' a.variant = 'Taurus V6 DOHC' - assert_equal [:make, :variant], a.known_characteristics.map(&:name) + assert_equal [:make, :variant], a.characteristics.known.keys end should "not mention a characteristic as unknown if, in fact, it has been trumped" do a = SimpleAutomobile.new a.make = 'Ford' a.variant = 'Taurus V6 DOHC' - assert_equal [], a.unknown_characteristics.map(&:name) + assert_equal [], a.characteristics.unknown.keys end should "not mention a characteristic as unknown if it is waiting on something else to be revealed" do a = Automobile.new - assert !a.unknown_characteristics.map(&:name).include?(:model_year) + assert !a.characteristics.unknown.keys.include?(:model_year) end should "make sure that trumping works even within revealed characteristics" do a = Automobile.new - assert a.unknown_characteristics.map(&:name).include?(:size_class) + assert a.characteristics.unknown.keys.include?(:size_class) a.make = 'Ford' a.model_year = 1999 a.model = 'Taurus' a.size_class = 'mid-size' - assert_equal [:model, :model_year, :make], a.known_characteristics.map(&:name) - assert !a.unknown_characteristics.map(&:name).include?(:size_class) + assert_equal [:make, :model_year, :model], a.characteristics.known.keys + assert !a.characteristics.unknown.keys.include?(:size_class) end - should "enforce prerequisites by using an object's setter" do + should "not enforce prerequisites by using an object's setter" do a = Automobile.new a.make = 'Ford' a.model_year = 1999 a.make = nil - assert_equal nil, a.model_year + assert_equal 1999, a.model_year + assert_equal nil, a.characteristics.known[:model_year] end should "keep user-defined options on a characteristic" do assert_equal :length, Automobile.characteristics[:daily_distance_estimate].options[:measures] end @@ -112,52 +132,72 @@ assert !Automobile.characteristics[:daily_distance_estimate].options.has_key?(:trumps) end should "know which characteristics are 'visible'" do a = Automobile.new - assert a.unknown_characteristics.map(&:name).include?(:record_creation_date) - assert !a.visible_unknown_characteristics.map(&:name).include?(:record_creation_date) + assert a.characteristics.unknown.keys.include?(:record_creation_date) + assert !a.characteristics.visible_unknown.keys.include?(:record_creation_date) a.record_creation_date = 'yesterday' - assert a.known_characteristics.map(&:name).include?(:record_creation_date) - assert !a.visible_known_characteristics.map(&:name).include?(:record_creation_date) + assert a.characteristics.known.keys.include?(:record_creation_date) + assert !a.characteristics.visible_known.keys.include?(:record_creation_date) end - + should "be able to access values" do a = Automobile.new a.make = 'Ford' - b = Automobile.new - b.make = 'Pontiac' - assert_equal 'Ford', Automobile.characteristics[:make].value(a) - assert_equal 'Pontiac', Automobile.characteristics[:make].value(b) + assert_equal 'Ford', a.characteristics[:make] end - should "give back characteristics with values when accessed from an instance" do + should "know what is known on a snapshot" do a = Automobile.new a.make = 'Ford' - assert_equal 'Ford', a.characteristics[:make].value + assert_equal [:make], a.characteristics.known.keys end - should "not allow treating [unbound] characteristics like bound ones" do + should "know what is unknown on a snapshot" do a = Automobile.new a.make = 'Ford' - assert_raises(Characterizable::Characteristic::TreatedUnboundAsBound) do - Automobile.characteristics[:make].value - end + assert a.characteristics.unknown.keys.include?(:model_year) end - should "not allow treating bound characteristics like unbound ones" do + should "not reveal unknown characteristics in snapshots" do a = Automobile.new + a.model_year = 1999 + assert_equal [], a.characteristics.known.keys + assert_equal nil, a.characteristics[:model_year] + end + + should "not reveal unknown characteristics in snapshots, even if it was previously revealed" do + a = Automobile.new a.make = 'Ford' - b = Automobile.new - b.make = 'Pontiac' - assert_raises(Characterizable::Characteristic::TreatedBoundAsUnbound) do - a.characteristics[:make].value :anything - end + a.model_year = 1999 + assert_equal [:make, :model_year], a.characteristics.known.keys + a.make = nil + assert_equal [], a.characteristics.known.keys end - should "eagely populate bound characteristics" do + should "keep snapshots separately" do a = Automobile.new a.make = 'Ford' - assert_equal ['Ford'], a.known_characteristics.map(&:value) - assert_equal [:make], a.known_characteristics.map(&:name) + a.model_year = 1999 + snapshot = a.characteristics + assert_equal [:make, :model_year], snapshot.known.keys + a.make = nil + assert_equal [], a.characteristics.known.keys + assert_equal [:make, :model_year], snapshot.known.keys + end + + should "work when passed around as a snapshot" do + a = Automobile.new + a.make = 'Ford' + snapshot = a.characteristics + assert_equal [:make], snapshot.known.keys + assert snapshot.unknown.keys.include?(:model_year) + snapshot[:model_year] = 1999 + assert_equal 1999, snapshot[:model_year] + assert_equal [:make, :model_year], snapshot.known.keys + assert !snapshot.unknown.keys.include?(:model_year) + assert_equal nil, a.model_year + assert_equal nil, a.characteristics[:model_year] + assert_equal 1999, snapshot[:model_year] end end