1 28 package org.jruby.test; 29 30 import org.jruby.Ruby; 31 import org.jruby.RubyArray; 32 33 36 public class TestRubyArray extends TestRubyBase { 37 private String result; 38 39 public TestRubyArray(final String name) { 40 super(name); 41 } 42 43 public void setUp() throws Exception { 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 57 public void testConstructors() throws Exception { 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 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"); assertEquals("[\"a\", \"a\", \"a\"]", result); 74 result = eval("arr = Array.new(5) {|i| i*i}; p arr"); assertEquals("[0, 1, 4, 9, 16]", result); 76 result = eval("arr = Array.new(Array.new(3, 'a')); p arr"); assertEquals("[\"a\", \"a\", \"a\"]", result); 78 } 79 80 83 public void testLookups() throws Exception { 84 } 88 89 92 public void testConversions() throws Exception { 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 102 public void testSizeRelated() throws Exception { 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 112 public void testIterating() throws Exception { 113 } 116 117 120 public void testToArray() throws Exception { 121 final RubyArray arr = (RubyArray)runtime.evalScript("$h = ['foo','bar']"); 122 final String val1 = "foo"; 123 final String val2 = "bar"; 124 final Object [] 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 [] outp2 = (String [])arr.toArray(new String [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 [] outp3 = (String [])arr.toArray(new String [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 |