9f27b5aaa78295751fa402f9d3595c89a4a3bcb4
[ruby_koans.git] / koans / about_arrays.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutArrays < EdgeCase::Koan
4   def test_creating_arrays
5     empty_array = Array.new
6     assert_equal __, empty_array.class
7     assert_equal __, empty_array.size
8   end
9
10   def test_array_literals
11     array = Array.new
12     assert_equal [], array
13
14     array[0] = 1
15     assert_equal [1], array
16
17     array[1] = 2
18     assert_equal [1, __], array
19
20     array << 333
21     assert_equal __, array
22   end
23
24   def test_accessing_array_elements
25     array = [:peanut, :butter, :and, :jelly]
26
27     assert_equal __, array[0]
28     assert_equal __, array.first
29     assert_equal __, array[3]
30     assert_equal __, array.last
31     assert_equal __, array[-1]
32     assert_equal __, array[-3]
33   end
34
35   def test_slicing_arrays
36     array = [:peanut, :butter, :and, :jelly]
37
38     assert_equal __, array[0,1]
39     assert_equal __, array[0,2]
40     assert_equal __, array[2,2]
41     assert_equal __, array[2,20]
42     assert_equal __, array[4,0]
43     assert_equal __, array[4,100]
44     assert_equal __, array[5,0]
45   end
46
47   def test_arrays_and_ranges
48     assert_equal __, (1..5).class
49     assert_not_equal [1,2,3,4,5], (1..5)
50     assert_equal __, (1..5).to_a
51     assert_equal __, (1...5).to_a
52   end
53
54   def test_slicing_with_ranges
55     array = [:peanut, :butter, :and, :jelly]
56
57     assert_equal __, array[0..2]
58     assert_equal __, array[0...2]
59     assert_equal __, array[2..-1]
60   end
61
62   def test_pushing_and_popping_arrays
63     array = [1,2]
64     array.push(:last)
65
66     assert_equal __, array
67
68     popped_value = array.pop
69     assert_equal __, popped_value
70     assert_equal __, array
71   end
72
73   def test_shifting_arrays
74     array = [1,2]
75     array.unshift(:first)
76
77     assert_equal __, array
78
79     shifted_value = array.shift
80     assert_equal __, shifted_value
81     assert_equal __, array
82   end
83
84 end