test/test_characterizable.rb in characterizable-0.0.6 vs test/test_characterizable.rb in characterizable-0.0.7
- old
+ new
@@ -45,11 +45,11 @@
has :variant, :trumps => :model
end
end
class TestCharacterizable < Test::Unit::TestCase
- should "let you define the relevant characteristics of a class" do
+ should "let you define the effective characteristics of a class" do
assert_nothing_raised do
class OnDemandAutomobile
include Characterizable
characterize do
has :make
@@ -65,71 +65,110 @@
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.relevant.class
- assert_equal Characterizable::SurvivorHash, a.characteristics.relevant.select { false }.class
- assert_equal Characterizable::SurvivorHash, a.characteristics.relevant.slice(:hello).class
- assert_equal Characterizable::SurvivorHash, a.characteristics.relevant.merge({:hi => 'there'}).class
+ assert_equal Characterizable::SurvivorHash, a.characteristics.effective.class
+ assert_equal Characterizable::SurvivorHash, a.characteristics.effective.select { false }.class
+ assert_equal Characterizable::SurvivorHash, a.characteristics.effective.slice(:hello).class
+ assert_equal Characterizable::SurvivorHash, a.characteristics.effective.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 relevant" do
+ should "tell you what characteristics are effective" do
a = SimpleAutomobile.new
a.make = 'Ford'
- assert_same_contents [:make], a.characteristics.relevant.keys
+ assert_same_contents [:make], a.characteristics.effective.keys
end
- should "tell you what characteristics are irrelevant" do
+ should "tell you what characteristics are potential" do
a = SimpleAutomobile.new
a.make = 'Ford'
- assert_same_contents [:model, :variant], a.characteristics.irrelevant.keys
+ assert_same_contents [:model, :variant], a.characteristics.potential.keys
end
- should "present a concise set of relevant characteristics by getting rid of those that have been trumped" do
+ should "present a concise set of effective 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_same_contents [:make, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :variant], a.characteristics.effective.keys
end
- should "not mention a characteristic as irrelevant if, in fact, it has been trumped" do
+ should "not mention a characteristic as potential if, in fact, it has been trumped" do
a = SimpleAutomobile.new
a.make = 'Ford'
a.variant = 'Taurus V6 DOHC'
- assert_same_contents [], a.characteristics.irrelevant.keys
+ assert_same_contents [], a.characteristics.potential.keys
end
+
+ should "know what characteristics are wasted and why" do
+ a = Automobile.new
+ a.hybridity = 'Electric'
+ a.variant = 'Taurus V6 DOHC'
+ assert_equal 'Electric', a.characteristics[:hybridity]
+ assert_equal nil, a.characteristics[:variant]
+ assert_same_contents [:hybridity], a.characteristics.effective.keys
+ assert_same_contents [:variant], a.characteristics.wasted.keys
+ assert_same_contents [:model], a.characteristics.lacking.keys
+ assert_same_contents [], a.characteristics.trumped.keys
+ a.model = 'Taurus'
+ assert_equal 'Electric', a.characteristics[:hybridity]
+ assert_equal nil, a.characteristics[:variant]
+ assert_equal nil, a.characteristics[:model]
+ assert_same_contents [:hybridity], a.characteristics.effective.keys
+ assert_same_contents [:variant, :model], a.characteristics.wasted.keys
+ assert_same_contents [:model_year], a.characteristics.lacking.keys
+ assert_same_contents [], a.characteristics.trumped.keys
+ a.model_year = '2006'
+ assert_equal 'Electric', a.characteristics[:hybridity]
+ assert_equal nil, a.characteristics[:variant]
+ assert_equal nil, a.characteristics[:model]
+ assert_equal nil, a.characteristics[:model_year]
+ assert_same_contents [:hybridity], a.characteristics.effective.keys
+ assert_same_contents [:variant, :model, :model_year], a.characteristics.wasted.keys
+ assert_same_contents [:make], a.characteristics.lacking.keys
+ assert_same_contents [], a.characteristics.trumped.keys
+ a.make = 'Ford'
+ assert_equal nil, a.characteristics[:hybridity]
+ assert_equal 'Taurus V6 DOHC', a.characteristics[:variant]
+ assert_equal 'Taurus', a.characteristics[:model]
+ assert_equal '2006', a.characteristics[:model_year]
+ assert_equal 'Ford', a.characteristics[:make]
+ assert_same_contents [:variant, :model, :model_year, :make], a.characteristics.effective.keys
+ assert_same_contents [], a.characteristics.wasted.keys
+ assert_same_contents [], a.characteristics.lacking.keys
+ assert_same_contents [:hybridity], a.characteristics.trumped.keys
+ end
- should "not mention a characteristic as irrelevant if it is waiting on something else to be revealed" do
+ should "not mention a characteristic as potential if it is waiting on something else to be revealed" do
a = Automobile.new
- assert !a.characteristics.irrelevant.keys.include?(:model_year)
+ assert !a.characteristics.potential.keys.include?(:model_year)
end
should "make sure that trumping works even within revealed characteristics" do
a = Automobile.new
- assert a.characteristics.irrelevant.keys.include?(:size_class)
+ assert a.characteristics.potential.keys.include?(:size_class)
a.make = 'Ford'
a.model_year = 1999
a.model = 'Taurus'
a.size_class = 'mid-size'
- assert_same_contents [:make, :model_year, :model], a.characteristics.relevant.keys
- assert !a.characteristics.irrelevant.keys.include?(:size_class)
+ assert_same_contents [:make, :model_year, :model], a.characteristics.effective.keys
+ assert !a.characteristics.potential.keys.include?(:size_class)
end
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 1999, a.model_year
- assert_equal nil, a.characteristics.relevant[:model_year]
+ assert_equal nil, a.characteristics.effective[:model_year]
end
should "keep user-defined options on a characteristic" do
assert_equal :length, Automobile.characteristics[:daily_distance_estimate].options[:measures]
end
@@ -147,71 +186,71 @@
a = Automobile.new
a.make = 'Ford'
assert_equal 'Ford', a.characteristics[:make]
end
- should "know what is relevant on a snapshot" do
+ should "know what is effective on a snapshot" do
a = Automobile.new
a.make = 'Ford'
- assert_same_contents [:make], a.characteristics.relevant.keys
+ assert_same_contents [:make], a.characteristics.effective.keys
end
- should "know what is irrelevant on a snapshot" do
+ should "know what is potential on a snapshot" do
a = Automobile.new
a.make = 'Ford'
- assert a.characteristics.irrelevant.keys.include?(:model_year)
+ assert a.characteristics.potential.keys.include?(:model_year)
end
- should "not reveal irrelevant characteristics in snapshots" do
+ should "not reveal potential characteristics in snapshots" do
a = Automobile.new
a.model_year = 1999
- assert_same_contents [], a.characteristics.relevant.keys
+ assert_same_contents [], a.characteristics.effective.keys
assert_equal nil, a.characteristics[:model_year]
end
- should "not reveal irrelevant characteristics in snapshots, even if it was previously revealed" do
+ should "not reveal potential characteristics in snapshots, even if it was previously revealed" do
a = Automobile.new
a.make = 'Ford'
a.model_year = 1999
- assert_same_contents [:make, :model_year], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year], a.characteristics.effective.keys
a.make = nil
- assert_same_contents [], a.characteristics.relevant.keys
+ assert_same_contents [], a.characteristics.effective.keys
end
should "keep snapshots separately" do
a = Automobile.new
a.make = 'Ford'
a.model_year = 1999
snapshot = a.characteristics
- assert_same_contents [:make, :model_year], snapshot.relevant.keys
+ assert_same_contents [:make, :model_year], snapshot.effective.keys
a.make = nil
- assert_same_contents [], a.characteristics.relevant.keys
- assert_same_contents [:make, :model_year], snapshot.relevant.keys
+ assert_same_contents [], a.characteristics.effective.keys
+ assert_same_contents [:make, :model_year], snapshot.effective.keys
end
should "work when passed around as a snapshot" do
a = Automobile.new
a.make = 'Ford'
snapshot = a.characteristics
- assert_same_contents [:make], snapshot.relevant.keys
- assert snapshot.irrelevant.keys.include?(:model_year)
+ assert_same_contents [:make], snapshot.effective.keys
+ assert snapshot.potential.keys.include?(:model_year)
snapshot[:model_year] = 1999
assert_equal 1999, snapshot[:model_year]
- assert_same_contents [:make, :model_year], snapshot.relevant.keys
- assert !snapshot.irrelevant.keys.include?(:model_year)
+ assert_same_contents [:make, :model_year], snapshot.effective.keys
+ assert !snapshot.potential.keys.include?(:model_year)
assert_equal nil, a.model_year
assert_equal nil, a.characteristics[:model_year]
assert_equal 1999, snapshot[:model_year]
end
should "appreciate that sometimes characteristics just magically appear" do
a = Automobile.new
a.daily_distance_estimate = 15
snapshot = a.characteristics
snapshot[:daily_distance_oracle_estimate] = 20
- assert_same_contents [:daily_distance_oracle_estimate], snapshot.relevant.keys
- assert_same_contents [:daily_distance_estimate], a.characteristics.relevant.keys
+ assert_same_contents [:daily_distance_oracle_estimate], snapshot.effective.keys
+ assert_same_contents [:daily_distance_estimate], a.characteristics.effective.keys
end
# has :make do |make|
# make.reveals :model_year do |model_year|
# model_year.reveals :model, :trumps => :size_class do |model|
@@ -220,43 +259,43 @@
a = Automobile.new
a.make = 'Ford'
a.model_year = 1999
a.model = 'Taurus'
a.variant = 'Taurus 1999'
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.make = nil
- assert_same_contents [], a.characteristics.relevant.keys
+ assert_same_contents [], a.characteristics.effective.keys
a.make = 'Ford'
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.model_year = nil
- assert_same_contents [:make], a.characteristics.relevant.keys
+ assert_same_contents [:make], a.characteristics.effective.keys
a.model_year = 1999
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.model = nil
- assert_same_contents [:make, :model_year], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year], a.characteristics.effective.keys
a.model = 'Taurus'
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.variant = nil
- assert_same_contents [:make, :model_year, :model], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model], a.characteristics.effective.keys
end
should "handle trumping on multiple levels" do
a = Automobile.new
a.size_class = 'small' # can be trumped by model
a.hybridity = 'no' # can be trumped by variant
a.make = 'Ford'
a.model_year = 1999
a.model = 'Taurus'
a.variant = 'Taurus 1999'
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.variant = nil
- assert_same_contents [:make, :model_year, :model, :hybridity], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :hybridity], a.characteristics.effective.keys
a.variant = 'Taurus 1999'
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.model = nil # which reveals size class, but also hybridity!
- assert_same_contents [:make, :model_year, :size_class, :hybridity], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :size_class, :hybridity], a.characteristics.effective.keys
a.model = 'Taurus'
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
a.make = nil
- assert_same_contents [:size_class, :hybridity], a.characteristics.relevant.keys
+ assert_same_contents [:size_class, :hybridity], a.characteristics.effective.keys
end
end