[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Base#compare which ignores 'id'

Simon Strandgaard

10/26/2003 12:55:00 PM

When doing unittesting I ofthen need to compare some computed data against
what is expected. I have a class hierarchy derived from a base class.

How do one install a compare method in the base class, which ignores 'id' ?

--
Simon Strandgaard

def test_transform
a = mk_letter 'a'
b = mk_letter 'b'
alt = mk_alternation(a, b)
wild = mk_wild
c = mk_letter 'c'
rep = mk_repeat wild, 0, 1
seq = mk_sequence alt, rep, c

s = ScannerHierarchy
exp = [
s::Alternation.new(true),
s::Pattern.new(true),
s::Match.new(a),
s::Pattern.new(false),
s::Pattern.new(true),
s::Match.new(b),
s::Pattern.new(false),
s::Alternation.new(false),
s::Repeat.new(true),
s::Match.new(wild),
s::Repeat.new(false),
s::Match.new(c)
]
v = TransformVisitor.new
seq.accept(v)
assert_equal(exp, v.result)
end
2 Answers

Simon Strandgaard

10/26/2003 1:13:00 PM

0

On Sun, 26 Oct 2003 13:54:43 +0100, Simon Strandgaard wrote:

> When doing unittesting I ofthen need to compare some computed data against
> what is expected. I have a class hierarchy derived from a base class.
>
> How do one install a compare method in the base class, which ignores 'id' ?

What I am interested in is an #assert_equal_ignore_id method.
Is such thing possible ?

How do you do comparison of data structures when unittesting ?

--
Simon Strandgaard



Simon Strandgaard

10/26/2003 1:47:00 PM

0

On Sun, 26 Oct 2003 14:13:08 +0100, Simon Strandgaard wrote:

> On Sun, 26 Oct 2003 13:54:43 +0100, Simon Strandgaard wrote:
>
>> When doing unittesting I ofthen need to compare some computed data against
>> what is expected. I have a class hierarchy derived from a base class.
>>
>> How do one install a compare method in the base class, which ignores 'id' ?
>
> What I am interested in is an #assert_equal_ignore_id method.
> Is such thing possible ?
>
> How do you do comparison of data structures when unittesting ?

Solved.. compare the output generated Marshal.dump :-)

# assert_equal which ignore id
def assert_equal_marshal(expected, actual, message=nil)
full_message = build_message(message, expected, actual) do |arg1, arg2|
"<#{arg1}> expected but was\n" +
"<#{arg2}>"
end
assert_block(full_message) {
Marshal.dump(expected) == Marshal.dump(actual)
}
end

--
Simon Strandgaard