Testing array.push(array)

Discussion in 'Ruby' started by Daniel Berger, Nov 9, 2005.

  1. 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
     
    Daniel Berger, Nov 9, 2005
    #1
    1. Advertising

  2. Daniel Berger

    Eric Hodel Guest

    On Nov 8, 2005, at 6:42 PM, Daniel Berger wrote:

    > 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

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
     
    Eric Hodel, Nov 9, 2005
    #2
    1. Advertising

  3. Eric Hodel wrote:
    > On Nov 8, 2005, at 6:42 PM, Daniel Berger wrote:
    >
    > > 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
     
    Daniel Berger, Nov 9, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. XyZaa
    Replies:
    0
    Views:
    581
    XyZaa
    Jul 19, 2007
  2. Replies:
    5
    Views:
    153
    YANAGAWA Kazuhisa
    Mar 8, 2006
  3. samppi
    Replies:
    27
    Views:
    515
    David A. Black
    Dec 5, 2007
  4. Amanda ..
    Replies:
    3
    Views:
    154
    Amanda ..
    Aug 25, 2008
  5. Mason Kelsey
    Replies:
    5
    Views:
    115
    Mason Kelsey
    Sep 27, 2009
Loading...

Share This Page