require 'test/unit' require 'envelope' class TC_Envelope < Test::Unit::TestCase def test_count base = BaseEnvelope.new assert(base.count == 0) end def test_empty envelope = Envelope.new assert(envelope.count == 0) assert_raise(EnvelopeException) { envelope.high } assert_raise(EnvelopeException) { envelope.low } end def test_add_numbers envelope = Envelope.new envelope.add 1 assert(envelope.high == 1) assert(envelope.low == 1) assert(envelope.count == 1) envelope.add 2 envelope.add(-3) envelope.add 4.5 assert(envelope.high == 4.5) assert(envelope.low == -3) assert(envelope.count == 4) envelope.add envelope assert(envelope.high == 4.5) assert(envelope.low == -3) assert(envelope.count == 8) assert_raise(EnvelopeException) { envelope.add "bad" } assert(envelope.count == 8) envelope = Envelope.new end def test_add_letters envelope = Envelope.new('c').add('m').add('e') assert(envelope.low == 'c') assert(envelope.high == 'm') assert(envelope.contains?('d')) assert(!(envelope.contains?('a'))) assert(!(envelope.contains?('r'))) assert(envelope.contains?(envelope)) assert(envelope.contains?(Envelope.new('e').add('h'))) assert(!(envelope.contains?(Envelope.new('a').add('h')))) assert(!(envelope.contains?(Envelope.new('e').add('s')))) assert(envelope === 'd') assert(!(envelope === 'a')) assert(!(envelope === 'r')) assert(envelope === envelope) assert(envelope === Envelope.new('e').add('h')) assert(!(envelope === Envelope.new('a').add('h'))) assert(!(envelope === Envelope.new('e').add('s'))) range = envelope.to_range assert(range === 'd') assert(!(range === 'a')) assert(!(range === 'r')) end def test_add_strings envelope = Envelope.new("carl").add("marie").add("edward") assert(envelope.low == "carl") assert(envelope.high == "marie") assert(envelope.contains?("dennis")) assert(!(envelope.contains?("aaron"))) assert(!(envelope.contains?("ralph"))) assert(envelope.contains?(envelope)) assert(envelope.contains?(Envelope.new("edward").add("harry"))) assert(!(envelope.contains?(Envelope.new("aaron").add("harry")))) assert(!(envelope.contains?(Envelope.new("edward").add("sally")))) range = envelope.to_range assert(range === "dennis") assert(!(range === "aaron")) assert(!(range === "ralph")) end def test_contains envelope = Envelope.new(0.75).add(3) assert(envelope.contains?(1)) assert(envelope.contains?(3)) assert(envelope.contains?(2)) assert(!(envelope.contains?(0))) assert(!(envelope.contains?(4))) assert(envelope.contains?(envelope)) assert(envelope.contains?(Envelope.new(1.5).add(2.5))) assert(!(envelope.contains?(Envelope.new(0.5).add(2.5)))) assert(!(envelope.contains?(Envelope.new(1.5).add(3.5)))) assert_raise(EnvelopeException) { envelope.add "bad" } range = envelope.to_range assert(range === 2.5) assert(!(range === 0)) assert(!(range === 4)) end end