Testing array.push(array)

D

Daniel Berger

Hi all,

I recently tried to write an assertion like so:

def test_array_infinity
array = [1,2,3]
assert_equal([1,2,3,"[...]"], array.push(array))
end

But, it appears that "[...]" is not a literal string. However, I can't
just remove the quotes, because then I get a syntax error.

How do I test this case?

Thanks,

Dan
 
E

Eric Hodel

Hi all,

I recently tried to write an assertion like so:

def test_array_infinity
array = [1,2,3]
assert_equal([1,2,3,"[...]"], array.push(array))
end

But, it appears that "[...]" is not a literal string. However, I
can't
just remove the quotes, because then I get a syntax error.

How do I test this case?

I don't think you can do it so easily:

irb(main):001:0> expected = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> expected << expected
=> [1, 2, 3, [...]]
irb(main):003:0> array = [1,2,3]
=> [1, 2, 3]
irb(main):004:0> array.push array
=> [1, 2, 3, [...]]
irb(main):005:0> array == expected
SystemStackError: stack level too deep
from (irb):5:in `=='
from (irb):5
from :0

Try:

irb(main):007:0> array[0] == expected[0]
=> true
irb(main):008:0> array[1] == expected[1]
=> true
irb(main):009:0> array[2] == expected[2]
=> true
irb(main):011:0> array[3] == array
=> true
 
D

Daniel Berger

Eric said:
Hi all,

I recently tried to write an assertion like so:

def test_array_infinity
array = [1,2,3]
assert_equal([1,2,3,"[...]"], array.push(array))
end

But, it appears that "[...]" is not a literal string. However, I
can't
just remove the quotes, because then I get a syntax error.

How do I test this case?

I don't think you can do it so easily:

irb(main):001:0> expected = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> expected << expected
=> [1, 2, 3, [...]]
irb(main):003:0> array = [1,2,3]
=> [1, 2, 3]
irb(main):004:0> array.push array
=> [1, 2, 3, [...]]
irb(main):005:0> array == expected
SystemStackError: stack level too deep
from (irb):5:in `=='
from (irb):5
from :0

Try:

irb(main):007:0> array[0] == expected[0]
=> true
irb(main):008:0> array[1] == expected[1]
=> true
irb(main):009:0> array[2] == expected[2]
=> true
irb(main):011:0> array[3] == array
=> true

Ah, that will work fine, thanks.

Dan
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top