On 2002.08.29, Jim Freeze <jim / freeze.org> wrote: > On Thu, Aug 29, 2002 at 01:08:13PM +0900, Ryan King wrote: > > On 2002.08.29, Jim Freeze <jim / freeze.org> wrote: > > [...] http://www.rubygarden.org/ruby?CompareByValue > Concerning unit testing, I don't seem to use #=='s muuch > on classes. I think I usually test whether functions return > the right thing or not. What do you use to compare the return values to the expected values? If you use one of the built-in types (Strings, Arrays, Hashes, etc.), they all have decent .=='s for this purpose. However, the minute you move onto less primitive types, you end up defining the == method. Especially as a system grows, abstractions are built. You end up seeing fewer and fewer primitives... and hence define more and more .== methods. =) > However, I suppose that you would have to put some limitations > on testing the instance variables. For example, do you trace > through arrays? Arrays don't get special-cased. You just call Array#== the same as you'd call Burro#==. Fortunately, Array#== is appropriate for this case, which is that it compares by value. > If this was a tree class, it could be a mess. > > Do I understand this correctly? I'm pretty sure you understand correctly, because you know it will be a mess. =) However, it's normally what I mean, for [contrived] example: expected = ImaginarySqlStatementClass.new { :select => [ '*' ], :from => [ 'animals' ], :where => "name = 'burro'" } actual = "select * from animals where name = 'burro'".to_sql assert_equal expected, actual Yes, the member variables of expected/actual are trees... but I want to compare them by value. > Hmm...I wonder if one could sort the output of Marshall.dump > and compare.... Actually, using .inspect was Nathaniel's first thought. Then, we got tripped up by the way it puts its id into the string. Then, we worked around that, only to find out that the order of the member variables can get dumped in differing orders depending (IIRC) on which order they're initialized. The second attempt was to use Marshall.dump, but I ran into some minor glitch just like with .inspect. It might end up being worth hacking around these problems to get a concise implementation, though. - Ryan King