1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutArrays < EdgeCase::Koan
4 def test_creating_arrays
5 empty_array = Array.new
6 assert_equal __(Array), empty_array.class
7 assert_equal __(0), empty_array.size
10 def test_array_literals
12 assert_equal [], array # __
15 assert_equal [1], array # __
18 assert_equal [1, __(2)], array
21 assert_equal __([1, 2, 333]), array
24 def test_accessing_array_elements
25 array = [:peanut, :butter, :and, :jelly]
27 assert_equal __(:peanut), array[0]
28 assert_equal __(:peanut), array.first
29 assert_equal __(:jelly), array[3]
30 assert_equal __(:jelly), array.last
31 assert_equal __(:jelly), array[-1]
32 assert_equal __(:butter), array[-3]
35 def test_slicing_arrays
36 array = [:peanut, :butter, :and, :jelly]
38 assert_equal __([:peanut]), array[0,1]
39 assert_equal __([:peanut, :butter]), array[0,2]
40 assert_equal __([:and, :jelly]), array[2,2]
41 assert_equal __([:and, :jelly]), array[2,20]
42 assert_equal __([]), array[4,0]
43 assert_equal __([]), array[4,100]
44 assert_equal __(nil), array[5,0]
47 def test_arrays_and_ranges
48 assert_equal __(Range), (1..5).class
49 assert_not_equal [1,2,3,4,5], (1..5) # __
50 assert_equal __([1,2,3,4,5]), (1..5).to_a
51 assert_equal __([1,2,3,4]), (1...5).to_a
54 def test_slicing_with_ranges
55 array = [:peanut, :butter, :and, :jelly]
57 assert_equal __([:peanut, :butter, :and]), array[0..2]
58 assert_equal __([:peanut, :butter]), array[0...2]
59 assert_equal __([:and, :jelly]), array[2..-1]
62 def test_pushing_and_popping_arrays
66 assert_equal __([1, 2, :last]), array
68 popped_value = array.pop
69 assert_equal __(:last), popped_value
70 assert_equal __([1, 2]), array
73 def test_shifting_arrays
77 assert_equal __([:first, 1, 2]), array
79 shifted_value = array.shift
80 assert_equal __(:first), shifted_value
81 assert_equal __([1, 2]), array