# This is a bunch of test cases for the 'diff' method. Since that method is currently # commented out, so too are the test cases. =begin # # = test/tc_debug.rb # # Unit tests for the debugging utilities contained in dev-utils/debug.rb. # # These can be run directly, as in ruby -Ilib lib/dev-utils/test/debug-test.rb, or # through the Rakefile, as in rake test. # # It's a little confusing to have dev-utils/test.rb as a library file and # dev-utils/test/* as test cases. That's a coincidence in this case; most projects # would not have a library file called test.rb. I'm trying out an approach to unit # tests whereby they are placed in the lib directory, thus ensuring that they are # accessed via the same $LOAD_PATH as the library itself. All other ways of # organising unit tests I have found annoying, because careful load-path management is needed # just to run them, which I find frustrating. # # It may turn out that I can find a really good alternative and promote that instead. # require 'dev-utils/debug' require 'test/unit' class TC_Debug < Test::Unit::TestCase Address = Struct.new(:number, :road, :city) class Person attr_reader :name, :age, :address def initialize(name, age, address) @name, @age, @address = name, age, address @unimportant = rand(100) end def ==(person) self.name == person.name and self.age == person.age and self.address == person.address end end # # Test between some fundamental types. These do not have fields to compare between: they # are immediate values. # def test_diff_0 assert_equal(nil, diff('abc', 'abc')) assert_equal('Immediate values differ', diff('abc', 'def')) end # # Test between a few simple Address structs. Structs don't have instance variables so # they're a special case. # def test_diff_1 # No difference between objects. a1 = Address.new(4, "Cumberland", "Sydney") a2 = Address.new(4, "Cumberland", "Sydney") assert_equal(nil, diff(a1, a2)) assert_equal(nil, diff(a2, a1)) # One difference between objects. a3 = Address.new(4, "Cumberlond", "Sydney") expected = ['road: "Cumberland"', 'road: "Cumberlond"'] assert_equal(expected, diff(a1, a3)) assert_equal(expected, diff(a2, a3)) expected = ['road: "Cumberlond"', 'road: "Cumberland"'] assert_equal(expected, diff(a3, a1)) assert_equal(expected, diff(a3, a2)) end # # This continues the work of test_diff_1, but is seperated out because it's testing # different stuff. Objects with multiple differences are compared. Differences should be # reported in alphabetical order, since no fundamental order is guaranteed. # def test_diff_2 a1 = Address.new(4, "Cumberland", "Sydney") # Two differences between objects. a4 = Address.new(4, "Cumberlond", "Melbourne") # Unless asked, only report the first difference. expected = ['city: "Sydney"', 'city: "Melbourne"'] assert_equal(expected, diff(a1, a4)) # If asked, report the first or second difference. assert_equal(expected, diff(a1, a4, 1)) expected = ['road: "Cumberland"', 'road: "Cumberlond"'] assert_equal(expected, diff(a1, a4, 2)) # If asked, report the third or greater difference, of which there is none. assert_equal(nil, diff(a1, a4, 3)) assert_equal(nil, diff(a1, a4, 100)) # If asked, report all differences. expected = [] expected << ['city: "Sydney"', 'city: "Melbourne"'] expected << ['road: "Cumberland"', 'road: "Cumberlond"'] assert_equal(expected, diff(a1, a4, :all)) # If asked, report number of differences. assert_equal(2, diff(a1, a4, :n)) end # # Test between a few simple Person objects. They have an instance variable that is not an # attribute, so it shouldn't influence the comparison. # # test_diff_2 did some testing of the advanced features, so we don't need to do those # here. # def test_diff_3 p1 = Person.new('John Smith', 31, nil) p2 = Person.new('John Smith', 31, nil) # same as p1 p3 = Person.new('John Brown', 31, nil) # name different from p1 p4 = Person.new('John Smith', 84, nil) # age different from p1 p5 = Person.new('Joan Brown', 84, nil) # name and age different from p1 assert_equal(nil, diff(p1, p2)) assert_equal(['name: "John Brown"', 'name: "John Smith"'], diff(p3, p1)) assert_equal(['name: "John Smith"', 'name: "John Brown"'], diff(p1, p3)) assert_equal(['age: 84', 'age: 31'], diff(p4, p1)) assert_equal(['age: 31', 'age: 84'], diff(p1, p4)) assert_equal(['age: 84', 'age: 31'], diff(p5, p1)) assert_equal(['name: "Joan Brown"', 'name: "John Smith"'], diff(p5, p1, 2)) assert_equal(2, diff(p5, p1, :n)) end # # Now we get recursive, and compare Person objects that have Address objects within them. # def test_diff_4 p1 = Person.new('John Smith', 31, Address.new(4, 'Holmes', 'Sydney')) p2 = Person.new('John Smith', 31, Address.new(4, 'Holmes', 'Sydney')) # == p1 p3 = Person.new('John Smith', 31, Address.new(4, 'Hawke', 'Sydney')) assert_equal(p1, p2) assert_equal(nil, diff(p1, p2)) assert_equal(nil, diff(p2, p1)) assert_equal(['address.road: "Hawke"', 'address.road: "Holmes"'], diff(p3, p1)) assert_equal(['address.road: "Hawke"', 'address.road: "Holmes"'], diff(p3, p1, 1)) assert_equal([['address.road: "Hawke"', 'address.road: "Holmes"']], diff(p3, p1, :all)) assert_equal(1, diff(p3, p1, :n)) # TODO: this should have more testing, but if that test passes, it's probably good enough # for me to use now. end def test_topology fail "Not implemented" end end =end