1 31 package org.jruby.test; 32 33 import org.jruby.Ruby; 34 35 38 public class TestRubyHash extends TestRubyBase { 39 40 private String result; 41 42 public TestRubyHash(String name) { 43 super(name); 44 } 45 46 public void setUp() throws Exception { 47 if (runtime == null) { 48 runtime = Ruby.getDefaultInstance(); 49 } 50 eval("$h = {'foo' => 'bar'}"); 51 } 52 53 public void tearDown() { 54 super.tearDown(); 55 } 56 57 61 public void testConstructors() throws Exception { 62 result = eval("hash = {'a', 100}; p hash"); 63 assertEquals("{\"a\"=>100}", result); 64 result = eval("hash = Hash['b', 200]; p hash"); 65 assertEquals("{\"b\"=>200}", result); 66 result = eval("hash = Hash.new(); p hash['test']"); 67 assertEquals("nil", result); 68 result = eval("hash = Hash.new('default'); p hash['test']"); 69 assertEquals("\"default\"", result); 70 } 71 72 76 public void testLookups() throws Exception { 77 result = eval("key = 'a'; hash = {key => 'one'}; hash.store('a', 'two'); puts hash[key]"); 79 assertEquals("two", result); 80 result = eval("key = [1,2]; hash = {key => 'one'}; hash[[1,2]] = 'two'; puts hash[key]"); 81 assertEquals("two", result); 82 result = eval("key = :a; hash = {key => 'one'}; hash[:a] = 'two'; puts hash[key]"); 83 assertEquals("two", result); 84 result = eval("key = 1234; hash = {key => 'one'}; hash[1234] = 'two'; puts hash[key]"); 85 assertEquals("two", result); 86 result = eval("key = 12.4; hash = {key => 'one'}; hash[12.4] = 'two'; puts hash[key]"); 87 assertEquals("two", result); 88 result = eval("key = 19223372036854775807; hash = {key => 'one'}; hash[19223372036854775807] = 'two'; puts hash[key]"); 89 assertEquals("two", result); 90 result = eval("key = /a/; hash = {key => 'one'}; hash[/a/] = 'two'; puts hash[key]"); 92 assertEquals("two", result); 93 result = eval("key = (1..3); hash = {key => 'one'}; hash[(1..3)] = 'two'; puts hash[key]"); 94 assertEquals("two", result); 95 } 96 97 100 public void testConversions() throws Exception { 101 result = eval("p $h.to_s"); 102 assertEquals("\"foobar\"", result); 103 result = eval("p $h.to_a"); 104 assertEquals("[[\"foo\", \"bar\"]]", result); 105 result = eval("p $h.to_hash"); 106 assertEquals("{\"foo\"=>\"bar\"}", result); 107 } 108 109 112 public void testSizeRelated() throws Exception { 113 assertEquals("1", eval("p $h.size")); 114 assertEquals("1", eval("p $h.length")); 115 assertEquals("false", eval("p $h.empty?")); 116 assertEquals("true", eval("p Hash.new().empty?")); 117 } 118 119 122 public void testIterating() throws Exception { 123 assertEquals("[\"foo\", \"bar\"]", eval("$h.each {|pair| p pair}")); 124 assertEquals("{\"foo\"=>\"bar\"}", eval("p $h.each {|pair| }")); 125 assertTrue(eval("$h.each_pair {|pair| p pair}").indexOf("[\"foo\", \"bar\"]") != -1); 126 assertTrue(eval("p $h.each_pair {|pair| }").indexOf("{\"foo\"=>\"bar\"}") != -1); 127 128 assertEquals("\"foo\"", eval("$h.each_key {|k| p k}")); 129 assertEquals("{\"foo\"=>\"bar\"}", eval("p $h.each_key {|k| }")); 130 131 assertEquals("\"bar\"", eval("$h.each_value {|v| p v}")); 132 assertEquals("{\"foo\"=>\"bar\"}", eval("p $h.each_value {|v| }")); 133 } 134 135 138 public void testDeleting() throws Exception { 139 eval("$delete_h = {1=>2,3=>4}"); 140 assertEquals("2", eval("p $delete_h.delete(1)")); 141 assertEquals("{3=>4}", eval("p $delete_h")); 142 assertEquals("nil", eval("p $delete_h.delete(100)")); 143 assertEquals("100", eval("$delete_h.delete(100) {|x| p x }")); 144 145 eval("$delete_h = {1=>2,3=>4,5=>6}"); 146 assertEquals("{1=>2}", eval("p $delete_h.delete_if {|k,v| k >= 3}")); 147 assertEquals("{1=>2}", eval("p $delete_h")); 148 149 eval("$delete_h.clear"); 150 assertEquals("{}", eval("p $delete_h")); 151 152 eval("$delete_h = {1=>2,3=>4,5=>6}"); 153 assertEquals("{1=>2}", eval("p $delete_h.reject {|k,v| k >= 3}")); 154 assertEquals("3", eval("p $delete_h.size")); 155 156 eval("$delete_h = {1=>2,3=>4,5=>6}"); 157 eval("p $delete_h"); 158 159 assertEquals("{1=>2}", eval("p $delete_h.reject! {|k,v| k >= 3}")); 160 assertEquals("1", eval("p $delete_h.size")); 161 assertEquals("nil", eval("p $delete_h.reject! {|k,v| false}")); 162 } 163 164 167 public void testDefault() throws Exception { 168 assertEquals("nil", eval("p $h['njet']")); 169 assertEquals("nil", eval("p $h.default")); 170 eval("$h.default = 'missing'"); 171 assertEquals("\"missing\"", eval("p $h['njet']")); 172 assertEquals("\"missing\"", eval("p $h.default")); 173 } 174 175 178 public void testRestructuring() throws Exception { 179 eval("$h_sort = {\"a\"=>20,\"b\"=>30,\"c\"=>10}"); 180 assertEquals("[[\"a\", 20], [\"b\", 30], [\"c\", 10]]", 181 eval("p $h_sort.sort")); 182 assertEquals("[[\"c\", 10], [\"a\", 20], [\"b\", 30]]", 183 eval("p $h_sort.sort {|a,b| a[1]<=>b[1]}")); 184 185 eval("$h_invert = {\"n\"=>100,\"y\"=>300,\"d\"=>200,\"a\"=>0}"); 186 assertEquals("[[0, \"a\"], [100, \"n\"], [200, \"d\"], [300, \"y\"]]", 187 eval("p $h_invert.invert.sort")); 188 } 189 } 190 | Popular Tags |