KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > test > TestRubyArray


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006 Ola Bini <Ola.Bini@ki.se>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.test;
29
30 import org.jruby.Ruby;
31 import org.jruby.RubyArray;
32
33 /**
34  * Test case for functionality in RubyArray
35  */

36 public class TestRubyArray extends TestRubyBase {
37     private String JavaDoc result;
38
39     public TestRubyArray(final String JavaDoc name) {
40         super(name);
41     }
42
43     public void setUp() throws Exception JavaDoc {
44         if (runtime == null) {
45             runtime = Ruby.getDefaultInstance();
46         }
47         eval("$h = ['foo','bar']");
48     }
49
50     public void tearDown() {
51         super.tearDown();
52     }
53
54     /**
55      * Test literal constructor [], Array[], Array::[], and Array::new with all forms of parameters.
56      */

57     public void testConstructors() throws Exception JavaDoc {
58         result = eval("arr = ['a', 100]; p arr");
59         assertEquals("[\"a\", 100]", result);
60         result = eval("arr = Array['b', 200]; p arr");
61         assertEquals("[\"b\", 200]", result);
62         /*
63         result = eval("arr = Array::['c', 200]; p arr");
64         assertEquals("[\"c\", 200]", result);
65         result = eval("arr = Array.['d', 200]; p arr");
66         assertEquals("[\"d\", 200]", result);
67         */

68         result = eval("arr = Array.new(); p arr");
69         assertEquals("[]", result);
70         result = eval("arr = Array.new(2); p arr");
71         assertEquals("[nil, nil]", result);
72         result = eval("arr = Array.new(3, 'a'); p arr"); // Init
73
assertEquals("[\"a\", \"a\", \"a\"]", result);
74         result = eval("arr = Array.new(5) {|i| i*i}; p arr"); // Block
75
assertEquals("[0, 1, 4, 9, 16]", result);
76         result = eval("arr = Array.new(Array.new(3, 'a')); p arr"); // Copy constructor
77
assertEquals("[\"a\", \"a\", \"a\"]", result);
78     }
79
80     /**
81      * Test Array#[]= (store) and Array#[] (retrieve).
82      */

83     public void testLookups() throws Exception JavaDoc {
84         // value equality
85
//result = eval("key = 3; arr = []; arr[key] = 'one'; arr.store(3, 'two'); puts arr[key]");
86
//assertEquals("two", result);
87
}
88
89     /**
90      * Array#to_s, Array#to_a
91      */

92     public void testConversions() throws Exception JavaDoc {
93         result = eval("p $h.to_s");
94         assertEquals("\"foobar\"", result);
95         result = eval("p $h.to_a");
96         assertEquals("[\"foo\", \"bar\"]", result);
97     }
98
99     /**
100      * Array#size, Array#length, Array#empty?
101      */

102     public void testSizeRelated() throws Exception JavaDoc {
103         assertEquals("2", eval("p $h.size"));
104         assertEquals("2", eval("p $h.length"));
105         assertEquals("false", eval("p $h.empty?"));
106         assertEquals("true", eval("p Array.new().empty?"));
107     }
108
109     /**
110      * Array#each
111      */

112     public void testIterating() throws Exception JavaDoc {
113         //assertEquals("\"foo\"\n\"bar\"", eval("$h.each {|val| p val}"));
114
//assertEquals("[\"foo\", \"bar\"]", eval("p $h.each {|val| }"));
115
}
116
117     /**
118      * This tests toArray-functionality
119      */

120     public void testToArray() throws Exception JavaDoc {
121         final RubyArray arr = (RubyArray)runtime.evalScript("$h = ['foo','bar']");
122         final String JavaDoc val1 = "foo";
123         final String JavaDoc val2 = "bar";
124         final Object JavaDoc[] outp = arr.toArray();
125         assertTrue("toArray should not return null",null != outp);
126         assertTrue("toArray should not return empty array",0 != outp.length);
127         assertEquals("first element should be \"foo\"",val1,outp[0]);
128         assertEquals("second element should be \"bar\"",val2,outp[1]);
129         final String JavaDoc[] outp2 = (String JavaDoc[])arr.toArray(new String JavaDoc[0]);
130         assertTrue("toArray should not return null",null != outp2);
131         assertTrue("toArray should not return empty array",0 != outp2.length);
132         assertEquals("first element should be \"foo\"",val1,outp2[0]);
133         assertEquals("second element should be \"bar\"",val2,outp2[1]);
134         final String JavaDoc[] outp3 = (String JavaDoc[])arr.toArray(new String JavaDoc[arr.size()]);
135         assertTrue("toArray should not return null",null != outp3);
136         assertTrue("toArray should not return empty array",0 != outp3.length);
137         assertEquals("first element should be \"foo\"",val1,outp3[0]);
138         assertEquals("second element should be \"bar\"",val2,outp3[1]);
139     }
140 }
141
Popular Tags