Forum: Ruby unit tests and string comparisons

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-12 01:57
(Received via mailing list)
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?
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-01-12 02:06
(Received via mailing list)
On Jan 11, 2006, at 4:57 PM, Joe Van Dyk wrote:

> strings?
Use unit_diff from ZenTest.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-12 02:12
(Received via mailing list)
On Thu, 12 Jan 2006, Joe Van Dyk wrote:

>
> 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-12 02:15
(Received via mailing list)
On 1/12/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> > know that they're different though, as doing "big ass long string".sum
>    def sdiff a, b
>
> 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.
D05623be23ac664b917ddd66bb3ed4ae?d=identicon&s=25 Joby Bednar (Guest)
on 2006-01-12 04:13
(Received via mailing list)
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[i]^s2[i].to_i==0?'-':'#'}

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

(above looks better in a fixed font display)

-Joby
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-01-12 06:20
(Received via mailing list)
On Jan 11, 2006, at 5:14 PM, Joe Van Dyk wrote:

>>> says that they're different.  And they look very similar to me.  I
>>    a = "foobar"
>>
> of a related problem, I guess.
uh, unit_diff, from ZenTest.

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

No, really.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-12 14:52
(Received via mailing list)
On Jan 11, 2006, at 11:17 PM, Eric Hodel wrote:

> uh, unit_diff, from ZenTest.
>
> http://rubyforge.org/frs/?group_id=419&release_id=3133
>
> No, really.

I have to agree.  This does what you all are building code to do.  :)

James Edward Gray II
This topic is locked and can not be replied to.