unit tests and string comparisons

J

Joe Van Dyk

Hi,

I'm testing the equality of two rather large strings. They look the
same to me, but doing

assert_equal "big ass long string", "big ass long string"

says that they're different. And they look very similar to me. I
know that they're different though, as doing "big ass long string".sum
gives a different checksum than the other one.

Any ideas on how I can easily view the differences between two long strings=
?
 
E

Eric Hodel

I'm testing the equality of two rather large strings. They look the
same to me, but doing

assert_equal "big ass long string", "big ass long string"

says that they're different. And they look very similar to me. I
know that they're different though, as doing "big ass long string".sum
gives a different checksum than the other one.

Any ideas on how I can easily view the differences between two long
strings?

Use unit_diff from ZenTest.
 
A

ara.t.howard

Hi,

I'm testing the equality of two rather large strings. They look the
same to me, but doing

assert_equal "big ass long string", "big ass long string"

says that they're different. And they look very similar to me. I
know that they're different though, as doing "big ass long string".sum
gives a different checksum than the other one.

Any ideas on how I can easily view the differences between two long strings?

stupid - but works:

harp:~ > cat a.rb
a = "foobar"
b = "foobaz"

def sdiff a, b
a = a.split //
b = b.split //
a.zip(b).each_with_index{|cc,idx| printf "%d: %s\n", idx, cc.inspect unless cc.uniq.size == 1}
end

sdiff a, b


harp:~ > ruby a.rb
5: ["r", "z"]

or just dump them to two files using 'od -c' and then use 'diff -u'.

regards.

-a
 
J

Joe Van Dyk

Hi,

I'm testing the equality of two rather large strings. They look the
same to me, but doing

assert_equal "big ass long string", "big ass long string"

says that they're different. And they look very similar to me. I
know that they're different though, as doing "big ass long string".sum
gives a different checksum than the other one.

Any ideas on how I can easily view the differences between two long str=
ings?

stupid - but works:

harp:~ > cat a.rb
a =3D "foobar"
b =3D "foobaz"

def sdiff a, b
a =3D a.split //
b =3D b.split //
a.zip(b).each_with_index{|cc,idx| printf "%d: %s\n", idx, cc.inspect= unless cc.uniq.size =3D=3D 1}
end

sdiff a, b


harp:~ > ruby a.rb
5: ["r", "z"]

or just dump them to two files using 'od -c' and then use 'diff -u'.

Thanks, I ended up doing something similar. I thought I recalled
reading a post here (or maybe on the Rails mailing list) about some
guy who was comparing large hashes and was having difficulties seeing
the differences between the two in the unit test output. This is sort
of a related problem, I guess.
 
J

Joby Bednar

Just for fun... here's another quick visual method:

s1 = 'abcdefghijk1mnOpqrstuvwxyz'
s2 = 'abcdefghijklmn0pqrstuvwxyz'

puts s1, s2
# XOR the binary of each character, will be 0 if the same
s1.size.times {|i| print s1^s2.to_i==0?'-':'#'}

Results:
abcdefghijk1mnOpqrstuvwxyz
abcdefghijklmn0pqrstuvwxyz
-----------#--#-----------

(above looks better in a fixed font display)

-Joby
 
E

Eric Hodel

Hi,

I'm testing the equality of two rather large strings. They look the
same to me, but doing

assert_equal "big ass long string", "big ass long string"

says that they're different. And they look very similar to me. I
know that they're different though, as doing "big ass long
string".sum
gives a different checksum than the other one.

Any ideas on how I can easily view the differences between two
long strings?

stupid - but works:

harp:~ > cat a.rb
a = "foobar"
b = "foobaz"

def sdiff a, b
a = a.split //
b = b.split //
a.zip(b).each_with_index{|cc,idx| printf "%d: %s\n", idx,
cc.inspect unless cc.uniq.size == 1}
end

sdiff a, b


harp:~ > ruby a.rb
5: ["r", "z"]

or just dump them to two files using 'od -c' and then use 'diff -u'.

Thanks, I ended up doing something similar. I thought I recalled
reading a post here (or maybe on the Rails mailing list) about some
guy who was comparing large hashes and was having difficulties seeing
the differences between the two in the unit test output. This is sort
of a related problem, I guess.

uh, unit_diff, from ZenTest.

http://rubyforge.org/frs/?group_id=419&release_id=3133

No, really.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top