Best Gauge code snippet using lang.getFileName
hash_test.go
Source:hash_test.go
1package vm2import (3 "encoding/json"4 "reflect"5 "sort"6 "testing"7)8func TestHashClassSuperclass(t *testing.T) {9 tests := []struct {10 input string11 expected string12 }{13 {`Hash.class.name`, "Class"},14 {`Hash.superclass.name`, "Object"},15 }16 for i, tt := range tests {17 v := initTestVM()18 evaluated := v.testEval(t, tt.input, getFilename())19 VerifyExpected(t, i, evaluated, tt.expected)20 v.checkCFP(t, i, 0)21 v.checkSP(t, i, 1)22 }23}24func TestEvalHashExpression(t *testing.T) {25 input := `26 { foo: 123, bar: "test", Baz: true }27 `28 v := initTestVM()29 evaluated := v.testEval(t, input, getFilename())30 h, ok := evaluated.(*HashObject)31 if !ok {32 t.Fatalf("Expect evaluated value to be a hash. got: %T", evaluated)33 }34 for key, value := range h.Pairs {35 switch key {36 case "foo":37 verifyIntegerObject(t, 0, value, 123)38 case "bar":39 verifyStringObject(t, 0, value, "test")40 case "Baz":41 verifyBooleanObject(t, 0, value, true)42 }43 }44 v.checkCFP(t, 0, 0)45 v.checkSP(t, 0, 1)46}47func TestHashAccessOperation(t *testing.T) {48 tests := []struct {49 input string50 expected interface{}51 }{52 {`53 {}[:foo]54 `, nil},55 {`56 {}[:foo123]57 `, nil},58 {`59 { foo123: 100 }[:foo123]60 `, 100},61 {`62 {}["foo"]63 `, nil},64 {`65 { bar: "foo" }[:bar]66 `, "foo"},67 {`68 { bar: "foo" }["bar"]69 `, "foo"},70 {`71 { foo: 2, bar: "foo" }[:foo]72 `, 2},73 {`74 { foo: 2, bar: "foo" }["foo"]75 `, 2},76 {`77 h = { bar: "Foo" }78 h["bar"]79 `, "Foo"},80 {`81 h = { bar: 1, foo: 2 }82 h["foo"] = h["bar"]83 h["foo"]84 `, 1},85 {`86 h = {}87 h["foo"] = 10088 h["foo"]89 `, 100},90 {`91 h = {}92 h["foo"] = { bar: 100 }93 h["foo"]["bar"]94 `, 100},95 {`96 h = { foo: { bar: [1, 2, 3] }}97 h["foo"]["bar"][0] + h["foo"]["bar"][1]98 `, 3},99 {`100 h = {}101 h["foo"] = 100102 h["bar"]103 `, nil},104 {`105 h = { foo: 1, bar: 5, baz: 10 }106 h["foo"] = h["bar"] * h["baz"]107 h["foo"]108 `, 50},109 }110 for i, tt := range tests {111 v := initTestVM()112 evaluated := v.testEval(t, tt.input, getFilename())113 VerifyExpected(t, i, evaluated, tt.expected)114 v.checkCFP(t, i, 0)115 v.checkSP(t, i, 1)116 }117}118func TestHashAccessWithDefaultOperation(t *testing.T) {119 valueTests := []struct {120 input string121 expected interface{}122 }{123 {`124 h = {}125 h.default = 0126 h['c']127 `, 0},128 {`129 h = {}130 h.default = 0131 h['d'] += 2132 h['d']133 `, 2},134 }135 for i, tt := range valueTests {136 v := initTestVM()137 evaluated := v.testEval(t, tt.input, getFilename())138 VerifyExpected(t, i, evaluated, tt.expected)139 v.checkCFP(t, i, 0)140 v.checkSP(t, i, 1)141 }142 hashTests := []struct {143 input string144 expected map[string]interface{}145 }{146 {`147 h = {}148 h.default = 0149 h150 `, map[string]interface{}{}},151 {`152 h = {}153 h.default = 0154 h['d'] += 2155 h156 `, map[string]interface{}{"d": 2}},157 }158 for i, tt := range hashTests {159 v := initTestVM()160 evaluated := v.testEval(t, tt.input, getFilename())161 verifyHashObject(t, i, evaluated, tt.expected)162 v.checkCFP(t, i, 0)163 v.checkSP(t, i, 1)164 }165}166func TestHashAccessOperationFail(t *testing.T) {167 testsFail := []errorTestCase{168 {`{ a: 1, b: 2 }[]`, "ArgumentError: Expect 1 argument(s). got: 0", 1},169 {`{ a: 1, b: 2 }[true]`, "TypeError: Expect argument to be String. got: Boolean", 1},170 {`{ a: 1, b: 2 }[true] = 1`, "TypeError: Expect argument to be String. got: Boolean", 1},171 {`{ a: 1, b: 2 }["a", "b"]`, "ArgumentError: Expect 1 argument(s). got: 2", 1},172 {`{ a: 1, b: 2 }["a", "b"] = 123`, "ArgumentError: Expect 2 argument(s). got: 3", 1},173 }174 for i, tt := range testsFail {175 v := initTestVM()176 evaluated := v.testEval(t, tt.input, getFilename())177 checkErrorMsg(t, i, evaluated, tt.expected)178 v.checkCFP(t, i, tt.expectedCFP)179 v.checkSP(t, i, 1)180 }181}182func TestHashComparisonOperation(t *testing.T) {183 tests := []struct {184 input string185 expected interface{}186 }{187 {`{ a: 1, b: 2 } == 123`, false},188 {`{ a: 1, b: 2 } == "123"`, false},189 {`{ a: 1, b: 2 } == "124"`, false},190 {`{ a: 1, b: 2 } == (1..3)`, false},191 {`{ a: 1, b: 2 } == { a: 1, b: 2 }`, true},192 {`{ b: 2, a: 1 } == { a: 1, b: 2 }`, true}, // Hash has no order issue193 {`{ a: 1, b: 2 } == { a: 2, b: 1 }`, false},194 {`{ a: 1, b: 2 } == { b: 1, a: 2 }`, false},195 {`{ a: 1, b: 2 } == { a: 1, b: 2, c: 3 }`, false},196 {`{ a: 1, b: 2 } == { a: 2, b: 2, a: 1 }`, true}, // Hash front key will be overwritten if duplicated197 {`{ a: [1, 2, 3], b: 2 } == { a: [1, 2, 3], b: 2 }`, true},198 {`{ a: [1, 2, 3], b: 2 } == { a: [3, 2, 1], b: 2 }`, false}, // Hash of array has order issue199 {`{ a: 1, b: 2 } == [1, "String", true, 2..5]`, false},200 {`{ a: 1, b: 2 } == Integer`, false},201 {`{ a: 1, b: 2 } != 123`, true},202 {`{ a: 1, b: 2 } != "123"`, true},203 {`{ a: 1, b: 2 } != "124"`, true},204 {`{ a: 1, b: 2 } != (1..3)`, true},205 {`{ a: 1, b: 2 } != { a: 1, b: 2 }`, false},206 {`{ b: 2, a: 1 } != { a: 1, b: 2 }`, false}, // Hash has no order issue207 {`{ a: 1, b: 2 } != { a: 2, b: 1 }`, true},208 {`{ a: 1, b: 2 } != { b: 1, a: 2 }`, true},209 {`{ a: 1, b: 2 } != { a: 1, b: 2, c: 3 }`, true},210 {`{ a: 1, b: 2 } != { a: 2, b: 2, a: 1 }`, false}, // Hash front key will be overwritten if duplicated211 {`{ a: [1, 2, 3], b: 2 } != { a: [1, 2, 3], b: 2 }`, false},212 {`{ a: [1, 2, 3], b: 2 } != { a: [3, 2, 1], b: 2 }`, true}, // Hash of array has order issue213 {`{ a: 1, b: 2 } != [1, "String", true, 2..5]`, true},214 {`{ a: 1, b: 2 } != Integer`, true},215 }216 for i, tt := range tests {217 v := initTestVM()218 evaluated := v.testEval(t, tt.input, getFilename())219 VerifyExpected(t, i, evaluated, tt.expected)220 v.checkCFP(t, i, 0)221 v.checkSP(t, i, 1)222 }223}224// Method test225func TestHashAnyMethod(t *testing.T) {226 tests := []struct {227 input string228 expected interface{}229 }{230 {`231 { a: 1, b: 2 }.any? do |k, v|232 v == 2233 end234 `, true},235 {`236 { a: 1, b: 2 }.any? do |k, v|237 v238 end239 `, true},240 {`241 { a: 1, b: 2 }.any? do |k, v|242 v == 5243 end244 `, false},245 {`246 { a: 1, b: 2 }.any? do |k, v|247 nil248 end249 `, false},250 {`251 { }.any? do |k, v|252 true253 end254 `, false},255 // cases for providing an empty block256 {`257 { a: 1, b: 2 }.any? do; end258 `, false},259 {`260 { a: 1, b: 2 }.any? do |i|; end261 `, false},262 {`263 {}.any? do; end264 `, false},265 {`266 {}.any? do |i|; end267 `, false},268 {`269 { key: "foo", bar: "baz" }.any? do |k, v|270 true271 break272 end273 `, nil},274 }275 for i, tt := range tests {276 v := initTestVM()277 evaluated := v.testEval(t, tt.input, getFilename())278 VerifyExpected(t, i, evaluated, tt.expected)279 v.checkCFP(t, i, 0)280 v.checkSP(t, i, 1)281 }282}283func TestHashAnyMethodFail(t *testing.T) {284 testsFail := []errorTestCase{285 {`{ }.any?(123) do end`, "ArgumentError: Expect 0 argument(s). got: 1", 1},286 {`{ }.any?`, "InternalError: Can't yield without a block", 1},287 }288 for i, tt := range testsFail {289 v := initTestVM()290 evaluated := v.testEval(t, tt.input, getFilename())291 checkErrorMsg(t, i, evaluated, tt.expected)292 v.checkCFP(t, i, tt.expectedCFP)293 v.checkSP(t, i, 1)294 }295}296func TestHashClearMethod(t *testing.T) {297 tests := []struct {298 input string299 expected map[string]interface{}300 }{301 // object modification302 {`303 hash = { foo: 123, bar: "test" }304 hash.clear305 hash306 `, map[string]interface{}{}},307 // return value308 {`309 { foo: 123, bar: "test" }.clear310 `, map[string]interface{}{}},311 }312 for i, tt := range tests {313 v := initTestVM()314 evaluated := v.testEval(t, tt.input, getFilename())315 verifyHashObject(t, i, evaluated, tt.expected)316 v.checkCFP(t, i, 0)317 v.checkSP(t, i, 1)318 }319}320func TestHashClearMethodFail(t *testing.T) {321 testsFail := []errorTestCase{322 {`{ a: 1, b: 2 }.clear(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},323 {`{ a: 1, b: 2 }.clear(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},324 }325 for i, tt := range testsFail {326 v := initTestVM()327 evaluated := v.testEval(t, tt.input, getFilename())328 checkErrorMsg(t, i, evaluated, tt.expected)329 v.checkCFP(t, i, tt.expectedCFP)330 v.checkSP(t, i, 1)331 }332}333func TestHashDefaultOperation(t *testing.T) {334 tests := []struct {335 input string336 expected interface{}337 }{338 {`339 h = {}340 h.default341 `, nil},342 {`343 h = {}344 h.default = 0345 h.default346 `, 0},347 }348 for i, tt := range tests {349 v := initTestVM()350 evaluated := v.testEval(t, tt.input, getFilename())351 VerifyExpected(t, i, evaluated, tt.expected)352 v.checkCFP(t, i, 0)353 v.checkSP(t, i, 1)354 }355}356func TestHashDefaultSetOperationFail(t *testing.T) {357 testsFail := []errorTestCase{358 {`{ }.default = *[1, 2]`, "ArgumentError: Expect 1 argument(s). got: 2", 1},359 {`{ }.default = []`, "ArgumentError: Arrays and Hashes are not accepted as default values", 1},360 {`{ }.default = {}`, "ArgumentError: Arrays and Hashes are not accepted as default values", 1},361 }362 for i, tt := range testsFail {363 v := initTestVM()364 evaluated := v.testEval(t, tt.input, getFilename())365 checkErrorMsg(t, i, evaluated, tt.expected)366 v.checkCFP(t, i, tt.expectedCFP)367 v.checkSP(t, i, 1)368 }369}370func TestHashDeleteMethod(t *testing.T) {371 tests := []struct {372 input string373 expected interface{}374 }{375 {`376 h = { a: 1, b: "Hello", c: true }.delete("a")377 h["a"]378 `, nil},379 {`380 h = { a: 1, b: "Hello", c: true }.delete("a")381 h["b"]382 `, "Hello"},383 {`384 h = { a: 1, b: "Hello", c: true }.delete("a")385 h["c"]386 `, true},387 {`388 h = { a: 1, b: "Hello", c: true }.delete("b")389 h["a"]390 `, 1},391 {`392 h = { a: 1, b: "Hello", c: true }.delete("b")393 h["b"]394 `, nil},395 {`396 h = { a: 1, b: "Hello", c: true }.delete("b")397 h["c"]398 `, true},399 {`400 h = { a: 1, b: "Hello", c: true }.delete("c")401 h["a"]402 `, 1},403 {`404 h = { a: 1, b: "Hello", c: true }.delete("c")405 h["b"]406 `, "Hello"},407 {`408 h = { a: 1, b: "Hello", c: true }.delete("c")409 h["c"]410 `, nil},411 }412 for i, tt := range tests {413 v := initTestVM()414 evaluated := v.testEval(t, tt.input, getFilename())415 VerifyExpected(t, i, evaluated, tt.expected)416 v.checkCFP(t, i, 0)417 v.checkSP(t, i, 1)418 }419}420func TestHashDeleteMethodFail(t *testing.T) {421 testsFail := []errorTestCase{422 {`{ a: 1, b: "Hello", c: true }.delete`, "ArgumentError: Expect 1 argument(s). got: 0", 1},423 {`{ a: 1, b: "Hello", c: true }.delete("a", "b")`, "ArgumentError: Expect 1 argument(s). got: 2", 1},424 {`{ a: 1, b: "Hello", c: true }.delete(123)`, "TypeError: Expect argument to be String. got: Integer", 1},425 {`{ a: 1, b: "Hello", c: true }.delete(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},426 }427 for i, tt := range testsFail {428 v := initTestVM()429 evaluated := v.testEval(t, tt.input, getFilename())430 checkErrorMsg(t, i, evaluated, tt.expected)431 v.checkCFP(t, i, tt.expectedCFP)432 v.checkSP(t, i, 1)433 }434}435func TestHashDeleteIfMethod(t *testing.T) {436 tests := []struct {437 input string438 expected map[string]interface{}439 }{440 // Since the method returns the hash itself, for compactness we perform the441 // tests on the return value, but we still make sure, with the first test,442 // that the hash itself is modified.443 {`444 hash = { a: 1, b: 2 }445 hash.delete_if do |k, v| v == 1 end446 hash447 `, map[string]interface{}{"b": 2}},448 {`449 { a: 1, b: 2 }.delete_if do |k, v| v == 1 end450 `, map[string]interface{}{"b": 2}},451 {`452 { a: 1, b: 2 }.delete_if do |k, v| 5 end453 `, map[string]interface{}{}},454 {`455 { a: 1, b: 2 }.delete_if do |k, v| false end456 `, map[string]interface{}{"a": 1, "b": 2}},457 {`458 { a: 1, b: 2 }.delete_if do |k, v| nil end459 `, map[string]interface{}{"a": 1, "b": 2}},460 {`461 { }.delete_if do |k, v| true end462 `, map[string]interface{}{}},463 // cases for providing an empty block464 {`465 { a: 1, b: 2 }.delete_if do; end466 `, map[string]interface{}{"a": 1, "b": 2}},467 {`468 { a: 1, b: 2 }.delete_if do |i|; end469 `, map[string]interface{}{"a": 1, "b": 2}},470 {`471 {}.delete_if do; end472 `, map[string]interface{}{}},473 {`474 {}.delete_if do |i|; end475 `, map[string]interface{}{}},476 }477 for i, tt := range tests {478 v := initTestVM()479 evaluated := v.testEval(t, tt.input, getFilename())480 verifyHashObject(t, i, evaluated, tt.expected)481 v.checkCFP(t, i, 0)482 v.checkSP(t, i, 1)483 }484}485func TestHashDeleteIfMethodFail(t *testing.T) {486 testsFail := []errorTestCase{487 {`{ }.delete_if(123) do end`, "ArgumentError: Expect 0 argument(s). got: 1", 1},488 {`{ }.delete_if`, "InternalError: Can't yield without a block", 1},489 }490 for i, tt := range testsFail {491 v := initTestVM()492 evaluated := v.testEval(t, tt.input, getFilename())493 checkErrorMsg(t, i, evaluated, tt.expected)494 v.checkCFP(t, i, tt.expectedCFP)495 v.checkSP(t, i, 1)496 }497}498func TestHashDigMethod(t *testing.T) {499 tests := []struct {500 input string501 expected interface{}502 }{503 {`504 { a: 1, b: 2 }.dig(:a)505 `, 1},506 {`507 { a: {}, b: 2 }.dig(:a, :b)508 `, nil},509 {`510 { a: {}, b: 2 }.dig(:a, :b, :c)511 `, nil},512 }513 for i, tt := range tests {514 v := initTestVM()515 evaluated := v.testEval(t, tt.input, getFilename())516 VerifyExpected(t, i, evaluated, tt.expected)517 v.checkCFP(t, i, 0)518 v.checkSP(t, i, 1)519 }520}521func TestHashDigMethodFail(t *testing.T) {522 testsFail := []errorTestCase{523 {`{ a: [], b: 2 }.dig`, "ArgumentError: Expect 1 or more argument(s). got: 0", 1},524 {`{ a: 1, b: 2 }.dig(:a, :b)`, "TypeError: Expect target to be Diggable, got Integer", 1},525 }526 for i, tt := range testsFail {527 v := initTestVM()528 evaluated := v.testEval(t, tt.input, getFilename())529 checkErrorMsg(t, i, evaluated, tt.expected)530 v.checkCFP(t, i, tt.expectedCFP)531 v.checkSP(t, i, 1)532 }533}534func TestHashEachMethod(t *testing.T) {535 tests := []struct {536 input string537 expected map[string]interface{}538 }{539 // return value540 {`541 { b: "2", a: 1 }.each do end542 `, map[string]interface{}{"a": 1, "b": "2"}},543 // empty hash544 {`545 { }.each do end546 `, map[string]interface{}{}},547 }548 for i, tt := range tests {549 v := initTestVM()550 evaluated := v.testEval(t, tt.input, getFilename())551 verifyHashObject(t, i, evaluated, tt.expected)552 v.checkCFP(t, i, 0)553 v.checkSP(t, i, 1)554 }555 tests2 := []struct {556 input string557 expected [][]interface{}558 }{559 // block yielding560 {`561 output = []562 h = { b: "2", a: 1 }563 h.each do |k, v|564 output.push([k, v])565 end566 output567 `, [][]interface{}{{"a", 1}, {"b", "2"}}},568 }569 for i, tt := range tests2 {570 v := initTestVM()571 evaluated := v.testEval(t, tt.input, getFilename())572 verifyBidimensionalArrayObject(t, i, evaluated, tt.expected)573 v.checkCFP(t, i, 0)574 v.checkSP(t, i, 1)575 }576}577func TestHashEachMethodFail(t *testing.T) {578 testsFail := []errorTestCase{579 {`{ a: 1, b: 2}.each("Hello") do end580 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},581 {`{ a: 1, b: 2}.each`, "InternalError: Can't yield without a block", 1},582 }583 for i, tt := range testsFail {584 v := initTestVM()585 evaluated := v.testEval(t, tt.input, getFilename())586 checkErrorMsg(t, i, evaluated, tt.expected)587 v.checkCFP(t, i, tt.expectedCFP)588 v.checkSP(t, i, 1)589 }590}591func TestHashEachKeyMethod(t *testing.T) {592 tests := []struct {593 input string594 expected []interface{}595 }{596 {`597 { b: "Hello", c: "World", a: "Goby" }.each_key do end598 `, []interface{}{"a", "b", "c"}},599 {`600 { a: "Hello", b: "World", c: "Goby" }.each_key do |key|601 # Empty Block602 end603 `, []interface{}{"a", "b", "c"}},604 {`605 { b: "Hello", c: "World", a: "Goby" }.each_key do606 # Empty Block607 end608 `, []interface{}{"a", "b", "c"}},609 {`610 { b: "Hello", c: "World", b: "Goby" }.each_key do |key|611 # Empty Block612 end613 `, []interface{}{"b", "c"}},614 {`615 arr = []616 { a: "Hello", b: "World", c: "Goby" }.each_key do |key|617 arr.push(key)618 end619 arr620 `, []interface{}{"a", "b", "c"}},621 {`622 arr = []623 {}.each_key do |key|624 arr.push(key)625 end626 arr627 `, []interface{}{}},628 }629 for i, tt := range tests {630 v := initTestVM()631 evaluated := v.testEval(t, tt.input, getFilename())632 verifyArrayObject(t, i, evaluated, tt.expected)633 v.checkCFP(t, i, 0)634 v.checkSP(t, i, 1)635 }636}637func TestHashEachKeyMethodFail(t *testing.T) {638 testsFail := []errorTestCase{639 {`{ a: 1, b: 2, c: 3 }.each_key("Hello") do |key|640 puts key641 end642 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},643 {`{ a: 1, b: 2, c: 3 }.each_key`, "InternalError: Can't yield without a block", 1},644 }645 for i, tt := range testsFail {646 v := initTestVM()647 evaluated := v.testEval(t, tt.input, getFilename())648 checkErrorMsg(t, i, evaluated, tt.expected)649 v.checkCFP(t, i, tt.expectedCFP)650 v.checkSP(t, i, 1)651 }652}653func TestHashEachValueMethod(t *testing.T) {654 hashTests := []struct {655 input string656 expected []interface{}657 }{658 {`659 { a: "Hello", b: 123, c: true }.each_value do end660 `, []interface{}{"Hello", 123, true}},661 {`662 { a: "Hello", b: 123, c: true }.each_value do |v|663 # Empty Block664 end665 `, []interface{}{"Hello", 123, true}},666 {`667 { b: "Hello", c: 123, a: true }.each_value do |v|668 # Empty Block669 end670 `, []interface{}{true, "Hello", 123}},671 {`672 { a: "Hello", b: 123, a: true }.each_value do |v|673 # Empty Block674 end675 `, []interface{}{true, 123}},676 {`677 {}.each_value do |v|678 # Empty Block679 end680 `, []interface{}{}},681 }682 for i, tt := range hashTests {683 v := initTestVM()684 evaluated := v.testEval(t, tt.input, getFilename())685 verifyArrayObject(t, i, evaluated, tt.expected)686 v.checkCFP(t, i, 0)687 v.checkSP(t, i, 1)688 }689 normalTests := []struct {690 input string691 expected interface{}692 }{693 {`694 sum = 0695 { a: 1, b: 2, c: 3, d: 4, e: 5 }.each_value do |v|696 sum = sum + v697 end698 sum699 `, 15},700 {`701 sum = 0702 { a: 1, b: 2, a: 3, b: 4, a: 5 }.each_value do |v|703 sum = sum + v704 end705 sum706 `, 9},707 {`708 string = ""709 { a: "Hello", b: "World", c: "Goby", d: "Lang" }.each_value do |v|710 string = string + v + " "711 end712 string713 `, "Hello World Goby Lang "},714 {`715 string = ""716 {}.each_value do |v|717 string = string + v + " "718 end719 string720 `, ""},721 }722 for i, tt := range normalTests {723 v := initTestVM()724 evaluated := v.testEval(t, tt.input, getFilename())725 VerifyExpected(t, i, evaluated, tt.expected)726 v.checkCFP(t, i, 0)727 v.checkSP(t, i, 1)728 }729}730func TestHashEachValueMethodFail(t *testing.T) {731 testsFail := []errorTestCase{732 {`{ a: 1, b: 2, c: 3 }.each_value("Hello") do |value|733 puts value734 end735 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},736 {`{ a: 1, b: 2, c: 3 }.each_value`, "InternalError: Can't yield without a block", 1},737 }738 for i, tt := range testsFail {739 v := initTestVM()740 evaluated := v.testEval(t, tt.input, getFilename())741 checkErrorMsg(t, i, evaluated, tt.expected)742 v.checkCFP(t, i, tt.expectedCFP)743 v.checkSP(t, i, 1)744 }745}746func TestHashEmptyMethod(t *testing.T) {747 tests := []struct {748 input string749 expected interface{}750 }{751 {`{}.empty?`, true},752 {`{ a: "Hello" }.empty?`, false},753 }754 for i, tt := range tests {755 v := initTestVM()756 evaluated := v.testEval(t, tt.input, getFilename())757 VerifyExpected(t, i, evaluated, tt.expected)758 v.checkCFP(t, i, 0)759 v.checkSP(t, i, 1)760 }761}762func TestHashEmptyMethodFail(t *testing.T) {763 testsFail := []errorTestCase{764 {`{ a: 1, b: 2 }.empty?(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},765 {`{ a: 1, b: 2 }.empty?(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},766 }767 for i, tt := range testsFail {768 v := initTestVM()769 evaluated := v.testEval(t, tt.input, getFilename())770 checkErrorMsg(t, i, evaluated, tt.expected)771 v.checkCFP(t, i, tt.expectedCFP)772 v.checkSP(t, i, 1)773 }774}775func TestHashEqualMethod(t *testing.T) {776 tests := []struct {777 input string778 expected interface{}779 }{780 {`{ a: 1 }.eql?({ a: 1 })`, true},781 {`{ a: 1 }.eql?({ a: 1, b: 2 })`, false},782 {`{ a: 1, b: 2 }.eql?({ a: 1, b: 2 })`, true},783 {`{ a: 1, b: 2 }.eql?({ b: 2, a: 1 })`, true},784 {`{ a: 1, b: 2 }.eql?({ a: 2, b: 1 })`, false},785 {`{ a: 1, b: 2 }.eql?({ a: 2, b: 2, a: 1 })`, true},786 {`{ a: 1, b: 2 }.eql?({ a: 1, b: 2, a: 2 })`, false},787 {`{ a: [1, 2, 3], b: { hello: "World" } }.eql?({ a: [1, 2, 3], b: { hello: "World"} })`, true},788 {`{ a: [1, 2, 3], b: { hello: "World" } }.eql?({ a: [3, 2, 1], b: { hello: "World"} })`, false},789 {`{ b: { hello: "World", lang: "Goby" } }.eql?({ b: { lang: "Goby", hello: "World"} })`, true},790 {`{ number: 1, boolean: true, string: "Goby", array: [1, "2", true], hash: { hello: "World", lang: "Goby" }, range: 2..5, null: nil }.eql?({ number: 1, boolean: true, string: "Goby", array: [1, "2", true], hash: { hello: "World", lang: "Goby" }, range: 2..5, null: nil })`, true},791 {`{ number: 1, boolean: true, string: "Goby", array: [1, "2", true], hash: { lang: "Goby", hello: "World" }, range: 2..5, null: nil }.eql?({ range: 2..5, null: nil, string: "Goby", number: 1, array: [1, "2", true], boolean: true, hash: { hello: "World", lang: "Goby" } })`, true},792 }793 for i, tt := range tests {794 v := initTestVM()795 evaluated := v.testEval(t, tt.input, getFilename())796 VerifyExpected(t, i, evaluated, tt.expected)797 v.checkCFP(t, i, 0)798 v.checkSP(t, i, 1)799 }800}801func TestHashEqualMethodFail(t *testing.T) {802 testsFail := []errorTestCase{803 {`{ a: 1, b: 2 }.eql?`, "ArgumentError: Expect 1 argument(s). got: 0", 1},804 {`{ a: 1, b: 2 }.eql?(true, { hello: "World" })`, "ArgumentError: Expect 1 argument(s). got: 2", 1},805 }806 for i, tt := range testsFail {807 v := initTestVM()808 evaluated := v.testEval(t, tt.input, getFilename())809 checkErrorMsg(t, i, evaluated, tt.expected)810 v.checkCFP(t, i, tt.expectedCFP)811 v.checkSP(t, i, 1)812 }813}814func TestHashFetchMethod(t *testing.T) {815 tests := []struct {816 input string817 expected string818 }{819 {`820 { spaghetti: "eat" }.fetch("spaghetti")821 `, "eat"},822 {`823 { spaghetti: "eat" }.fetch("pizza", "not eat")824 `, "not eat"},825 {`826 { spaghetti: "eat" }.fetch("pizza") do |el| "eat " + el end827 `, "eat pizza"},828 }829 for i, tt := range tests {830 v := initTestVM()831 evaluated := v.testEval(t, tt.input, getFilename())832 VerifyExpected(t, i, evaluated, tt.expected)833 v.checkCFP(t, i, 0)834 v.checkSP(t, i, 1)835 }836}837func TestHashFetchMethodFail(t *testing.T) {838 testsFail := []errorTestCase{839 {`{ spaghetti: "eat" }.fetch()`, "ArgumentError: Expect 1 to 2 argument(s). got: 0", 1},840 {`{ spaghetti: "eat" }.fetch("a", "b", "c")`, "ArgumentError: Expect 1 to 2 argument(s). got: 3", 1},841 {`{ spaghetti: "eat" }.fetch("a", "b") do end`, "ArgumentError: The default argument can't be passed along with a block", 1},842 {`{ spaghetti: "eat" }.fetch("pizza")`, "ArgumentError: The value was not found, and no block has been provided", 1},843 }844 for i, tt := range testsFail {845 v := initTestVM()846 evaluated := v.testEval(t, tt.input, getFilename())847 checkErrorMsg(t, i, evaluated, tt.expected)848 v.checkCFP(t, i, tt.expectedCFP)849 v.checkSP(t, i, 1)850 }851}852func TestHashFetchValuesMethod(t *testing.T) {853 tests := []struct {854 input string855 expected []interface{}856 }{857 {`858 { cat: "feline", dog: "canine", cow: "bovine" }.fetch_values("cow", "cat")859 `, []interface{}{"bovine", "feline"}},860 {`861 { cat: "feline", dog: "canine", cow: "bovine" }.fetch_values("cow", "bird") do |k| k.upcase end862 `, []interface{}{"bovine", "BIRD"}},863 }864 for i, tt := range tests {865 v := initTestVM()866 evaluated := v.testEval(t, tt.input, getFilename())867 verifyArrayObject(t, i, evaluated, tt.expected)868 v.checkCFP(t, i, 0)869 v.checkSP(t, i, 1)870 }871}872func TestHashFetchValuesMethodFail(t *testing.T) {873 testsFail := []errorTestCase{874 {`{ cat: "feline" }.fetch_values()`, "ArgumentError: Expect 1 or more argument(s). got: 0", 1},875 {`{ cat: "feline" }.fetch_values(1)`, "TypeError: Expect argument to be String. got: Integer", 1},876 {`{ cat: "feline" }.fetch_values("dog")`, "ArgumentError: There is no value for the key `dog`, and no block has been provided", 1},877 }878 for i, tt := range testsFail {879 v := initTestVM()880 evaluated := v.testEval(t, tt.input, getFilename())881 checkErrorMsg(t, i, evaluated, tt.expected)882 v.checkCFP(t, i, 1)883 v.checkSP(t, i, 1)884 }885}886func TestHashHasKeyMethod(t *testing.T) {887 tests := []struct {888 input string889 expected interface{}890 }{891 {`{ a: "Hello", b: 123, c: true }.has_key?("a")`, true},892 {`{ a: "Hello", b: 123, c: true }.has_key?("d")`, false},893 {`{ a: "Hello", b: 123, c: true }.has_key?(:a)`, true},894 {`{ a: "Hello", b: 123, c: true }.has_key?(:d)`, false},895 }896 for i, tt := range tests {897 v := initTestVM()898 evaluated := v.testEval(t, tt.input, getFilename())899 VerifyExpected(t, i, evaluated, tt.expected)900 v.checkCFP(t, i, 0)901 v.checkSP(t, i, 1)902 }903}904func TestHashHasKeyMethodFail(t *testing.T) {905 testsFail := []errorTestCase{906 {`{ a: 1, b: 2 }.has_key?`, "ArgumentError: Expect 1 argument(s). got: 0", 1},907 {`{ a: 1, b: 2 }.has_key?(true, { hello: "World" })`, "ArgumentError: Expect 1 argument(s). got: 2", 1},908 {`{ a: 1, b: 2 }.has_key?(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},909 {`{ a: 1, b: 2 }.has_key?(123)`, "TypeError: Expect argument to be String. got: Integer", 1},910 }911 for i, tt := range testsFail {912 v := initTestVM()913 evaluated := v.testEval(t, tt.input, getFilename())914 checkErrorMsg(t, i, evaluated, tt.expected)915 v.checkCFP(t, i, tt.expectedCFP)916 v.checkSP(t, i, 1)917 }918}919func TestHashHasValueMethod(t *testing.T) {920 tests := []struct {921 input string922 expected interface{}923 }{924 {`{ a: "Hello", b: 123, c: true }.has_value?("Hello")`, true},925 {`{ a: "Hello", b: 123, c: true }.has_value?("World")`, false},926 {`{ a: "Hello", b: 123, c: true }.has_value?(123)`, true},927 {`{ a: "Hello", b: 123, c: true }.has_value?(false)`, false},928 {`{ a: "Hello", b: { lang: "Goby", arr: [3, 1, 2] }, c: true }.has_value?({ lang: "Goby", arr: [3, 1, 2] })`, true},929 {`{ a: "Hello", b: { lang: "Goby", arr: [3, 1, 2] }, c: true }.has_value?({ lang: "Goby", arr: [1, 2, 3] })`, false},930 {`{ a: "Hello", b: { lang: "Goby", arr: [3, 1, 2] }, c: true }.has_value?({ arr: [3, 1, 2], lang: "Goby" })`, true},931 }932 for i, tt := range tests {933 v := initTestVM()934 evaluated := v.testEval(t, tt.input, getFilename())935 VerifyExpected(t, i, evaluated, tt.expected)936 v.checkCFP(t, i, 0)937 v.checkSP(t, i, 1)938 }939}940func TestHashHasValueMethodFail(t *testing.T) {941 testsFail := []errorTestCase{942 {`{ a: 1, b: 2 }.has_value?`, "ArgumentError: Expect 1 argument(s). got: 0", 1},943 {`{ a: 1, b: 2 }.has_value?(true, { hello: "World" })`, "ArgumentError: Expect 1 argument(s). got: 2", 1},944 }945 for i, tt := range testsFail {946 v := initTestVM()947 evaluated := v.testEval(t, tt.input, getFilename())948 checkErrorMsg(t, i, evaluated, tt.expected)949 v.checkCFP(t, i, tt.expectedCFP)950 v.checkSP(t, i, 1)951 }952}953func TestHashKeysMethod(t *testing.T) {954 input := `955 { foo: 123, bar: "test", baz: true }.keys956 `957 v := initTestVM()958 evaluated := v.testEval(t, input, getFilename())959 arr, ok := evaluated.(*ArrayObject)960 if !ok {961 t.Fatalf("Expect evaluated value to be Array. got: %T", evaluated)962 } else if arr.Len() != 3 {963 t.Fatalf("Expect evaluated array length to be 3. got: %d", arr.Len())964 }965 var evaluatedArr []string966 for _, k := range arr.Elements {967 evaluatedArr = append(evaluatedArr, k.(*StringObject).value)968 }969 sort.Strings(evaluatedArr)970 if !reflect.DeepEqual(evaluatedArr, []string{"bar", "baz", "foo"}) {971 t.Fatalf("Expect evaluated array to be [\"bar\", \"baz\", \"foo\". got: %v", evaluatedArr)972 }973 v.checkCFP(t, 0, 0)974 v.checkSP(t, 0, 1)975}976func TestHashKeysMethodFail(t *testing.T) {977 testsFail := []errorTestCase{978 {`{ a: 1, b: 2 }.keys(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},979 {`{ a: 1, b: 2 }.keys(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},980 }981 for i, tt := range testsFail {982 v := initTestVM()983 evaluated := v.testEval(t, tt.input, getFilename())984 checkErrorMsg(t, i, evaluated, tt.expected)985 v.checkCFP(t, i, tt.expectedCFP)986 v.checkSP(t, i, 1)987 }988}989func TestHashLengthMethod(t *testing.T) {990 tests := []struct {991 input string992 expected interface{}993 }{994 {`995 { a: 1, b: 2 }.length996 `, 2},997 {`998 {}.length999 `, 0},1000 }1001 for i, tt := range tests {1002 v := initTestVM()1003 evaluated := v.testEval(t, tt.input, getFilename())1004 VerifyExpected(t, i, evaluated, tt.expected)1005 v.checkCFP(t, i, 0)1006 v.checkSP(t, i, 1)1007 }1008}1009func TestHashLengthMethodFail(t *testing.T) {1010 testsFail := []errorTestCase{1011 {`{ a: 1, b: 2 }.length(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1012 {`{ a: 1, b: 2 }.length(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},1013 }1014 for i, tt := range testsFail {1015 v := initTestVM()1016 evaluated := v.testEval(t, tt.input, getFilename())1017 checkErrorMsg(t, i, evaluated, tt.expected)1018 v.checkCFP(t, i, tt.expectedCFP)1019 v.checkSP(t, i, 1)1020 }1021}1022func TestHashMapValuesMethod(t *testing.T) {1023 tests := []struct {1024 input string1025 expected interface{}1026 }{1027 {`1028 result = { a: 1, b: 2, c: 3 }.map_values do end1029 result["a"] + result["b"] + result["c"]1030 `, 6},1031 {`1032 result = { a: 1, b: 2, c: 3 }.map_values do |v| end1033 result["a"] + result["b"] + result["c"]1034 `, 6},1035 {`1036 h = { a: 1, b: 2, c: 3 }1037 result = h.map_values do |v|1038 v * 31039 end1040 h["a"]1041 `, 1},1042 {`1043 h = { a: 1, b: 2, c: 3 }1044 result = h.map_values do |v|1045 v * 31046 end1047 h["b"]1048 `, 2},1049 {`1050 h = { a: 1, b: 2, c: 3 }1051 result = h.map_values do |v|1052 v * 31053 end1054 h["c"]1055 `, 3},1056 {`1057 h = { a: 1, b: 2, c: 3 }1058 result = h.map_values do |v|1059 v * 31060 end1061 result["a"]1062 `, 3},1063 {`1064 h = { a: 1, b: 2, c: 3 }1065 result = h.map_values do |v|1066 v * 31067 end1068 result["b"]1069 `, 6},1070 {`1071 h = { a: 1, b: 2, c: 3 }1072 result = h.map_values do |v|1073 v * 31074 end1075 result["c"]1076 `, 9},1077 {`1078 h = {}1079 result = h.map_values do |v|1080 v * 31081 end1082 result["c"]1083 `, nil},1084 }1085 for i, tt := range tests {1086 v := initTestVM()1087 evaluated := v.testEval(t, tt.input, getFilename())1088 VerifyExpected(t, i, evaluated, tt.expected)1089 v.checkCFP(t, i, 0)1090 v.checkSP(t, i, 1)1091 }1092}1093func TestHashMapValuesMethodFail(t *testing.T) {1094 testsFail := []errorTestCase{1095 {`{ a: 1, b: 2, c: 3 }.map_values("Hello") do |value|1096 value * 31097 end1098 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},1099 {`{ a: 1, b: 2, c: 3 }.map_values`, "InternalError: Can't yield without a block", 1},1100 }1101 for i, tt := range testsFail {1102 v := initTestVM()1103 evaluated := v.testEval(t, tt.input, getFilename())1104 checkErrorMsg(t, i, evaluated, tt.expected)1105 v.checkCFP(t, i, tt.expectedCFP)1106 v.checkSP(t, i, 1)1107 }1108}1109func TestHashMergeMethod(t *testing.T) {1110 input := []string{1111 `{ a: "Hello", b: 2..5 }.merge({ b: true, c: 123, d: ["World", 456, false] })`,1112 `{ b: 123, d: false }.merge({ a: "Hello", c: 123 }, { b: true, d: ["World"] }, { d: ["World", 456, false] })`,1113 }1114 for i, value := range input {1115 v := initTestVM()1116 evaluated := v.testEval(t, value, getFilename())1117 h, ok := evaluated.(*HashObject)1118 if !ok {1119 t.Fatalf("Expect evaluated value to be a hash. got: %T", evaluated)1120 }1121 for key, value := range h.Pairs {1122 switch key {1123 case "a":1124 verifyStringObject(t, i, value, "Hello")1125 case "b":1126 verifyBooleanObject(t, i, value, true)1127 case "c":1128 verifyIntegerObject(t, i, value, 123)1129 case "d":1130 verifyArrayObject(t, i, value, []interface{}{"World", 456, false})1131 }1132 }1133 v.checkCFP(t, i, 0)1134 v.checkSP(t, i, 1)1135 }1136}1137func TestHashMergeMethodFail(t *testing.T) {1138 testsFail := []errorTestCase{1139 {`{ a: 1, b: 2 }.merge`, "ArgumentError: Expect 1 or more argument(s). got: 0", 1},1140 {`{ a: 1, b: 2 }.merge(true, { hello: "World" })`, "TypeError: Expect argument to be Hash. got: Boolean", 1},1141 {`{ a: 1, b: 2 }.merge({ hello: "World" }, 123, "Hello")`, "TypeError: Expect argument to be Hash. got: Integer", 1},1142 }1143 for i, tt := range testsFail {1144 v := initTestVM()1145 evaluated := v.testEval(t, tt.input, getFilename())1146 checkErrorMsg(t, i, evaluated, tt.expected)1147 v.checkCFP(t, i, tt.expectedCFP)1148 v.checkSP(t, i, 1)1149 }1150}1151func TestHashSelectMethod(t *testing.T) {1152 testsSortedArray := []struct {1153 input string1154 expected map[string]interface{}1155 }{1156 {`1157 { a: 1, b: 2 }.select do |k, v|1158 v == 21159 end1160 `, map[string]interface{}{"b": 2}},1161 {`1162 { a: 1, b: 2 }.select do |k, v|1163 51164 end1165 `, map[string]interface{}{"a": 1, "b": 2}},1166 {`1167 { a: 1, b: 2 }.select do |k, v|1168 nil1169 end1170 `, map[string]interface{}{}},1171 {`1172 { a: 1, b: 2 }.select do |k, v|1173 false1174 end1175 `, map[string]interface{}{}},1176 {`1177 { }.select do end1178 `, map[string]interface{}{}},1179 // non-destructivity specification1180 {`1181 source = { a: 1, b: 2 }1182 source.select do |k, v| true end1183 source1184 `, map[string]interface{}{"a": 1, "b": 2}},1185 // cases for providing an empty block1186 {`1187 { a: 1, b: 2 }.select do end1188 `, map[string]interface{}{}},1189 {`1190 { a: 1, b: 2 }.select do |k| end1191 `, map[string]interface{}{}},1192 {`1193 { }.select do |k| end1194 `, map[string]interface{}{}},1195 }1196 for i, tt := range testsSortedArray {1197 v := initTestVM()1198 evaluated := v.testEval(t, tt.input, getFilename())1199 verifyHashObject(t, i, evaluated, tt.expected)1200 v.checkCFP(t, i, 0)1201 v.checkSP(t, i, 1)1202 }1203}1204func TestHashSelectMethodFail(t *testing.T) {1205 testsFail := []errorTestCase{1206 {`{ }.select(123) do end`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1207 {`{ }.select`, "InternalError: Can't yield without a block", 1},1208 }1209 for i, tt := range testsFail {1210 v := initTestVM()1211 evaluated := v.testEval(t, tt.input, getFilename())1212 checkErrorMsg(t, i, evaluated, tt.expected)1213 v.checkCFP(t, i, tt.expectedCFP)1214 v.checkSP(t, i, 1)1215 }1216}1217func TestHashSortedKeysMethod(t *testing.T) {1218 tests := []struct {1219 input string1220 expected []interface{}1221 }{1222 {`{ a: 1, b: 2, c: 3 }.sorted_keys`, []interface{}{"a", "b", "c"}},1223 {`{ c: 1, b: 2, a: 3 }.sorted_keys`, []interface{}{"a", "b", "c"}},1224 {`{ b: 1, a: 2, c: 3 }.sorted_keys`, []interface{}{"a", "b", "c"}},1225 {`{ b: 1, a: 2, b: 3 }.sorted_keys`, []interface{}{"a", "b"}},1226 {`{ c: 1, a: 2, a: 3 }.sorted_keys`, []interface{}{"a", "c"}},1227 }1228 for i, tt := range tests {1229 v := initTestVM()1230 evaluated := v.testEval(t, tt.input, getFilename())1231 verifyArrayObject(t, i, evaluated, tt.expected)1232 v.checkCFP(t, i, 0)1233 v.checkSP(t, i, 1)1234 }1235}1236func TestHashSortedKeysMethodFail(t *testing.T) {1237 testsFail := []errorTestCase{1238 {`{ a: 1, b: 2 }.sorted_keys(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1239 {`{ a: 1, b: 2 }.sorted_keys(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},1240 }1241 for i, tt := range testsFail {1242 v := initTestVM()1243 evaluated := v.testEval(t, tt.input, getFilename())1244 checkErrorMsg(t, i, evaluated, tt.expected)1245 v.checkCFP(t, i, tt.expectedCFP)1246 v.checkSP(t, i, 1)1247 }1248}1249func TestHashToArrayMethod(t *testing.T) {1250 testsSortedArray := []struct {1251 input string1252 expected []interface{}1253 }{1254 {`{ a: 1, b: 2, c: 3 }.to_a(true)[0]`, []interface{}{"a", 1}},1255 {`{ a: 1, b: 2, c: 3 }.to_a(true)[1]`, []interface{}{"b", 2}},1256 {`{ a: 1, b: 2, c: 3 }.to_a(true)[2]`, []interface{}{"c", 3}},1257 {`{ b: 1, c: 2, a: 3 }.to_a(true)[0]`, []interface{}{"a", 3}},1258 {`{ b: 1, c: 2, a: 3 }.to_a(true)[1]`, []interface{}{"b", 1}},1259 {`{ b: 1, c: 2, a: 3 }.to_a(true)[2]`, []interface{}{"c", 2}},1260 }1261 for i, tt := range testsSortedArray {1262 v := initTestVM()1263 evaluated := v.testEval(t, tt.input, getFilename())1264 verifyArrayObject(t, i, evaluated, tt.expected)1265 v.checkCFP(t, i, 0)1266 v.checkSP(t, i, 1)1267 }1268 input := `1269 { a: 123, b: "test", c: true, d: [1, "Goby", false] }.to_a1270 `1271 v := initTestVM()1272 evaluated := v.testEval(t, input, getFilename())1273 arr, ok := evaluated.(*ArrayObject)1274 if !ok {1275 t.Fatalf("Expect evaluated value to be Array. got: %T", evaluated)1276 } else if arr.Len() != 4 {1277 t.Fatalf("Expect evaluated array length to be 4. got: %d", arr.Len())1278 }1279 evaluatedArr := make(map[string]Object)1280 for _, p := range arr.Elements {1281 pair := p.(*ArrayObject)1282 evaluatedArr[pair.Elements[0].(*StringObject).value] = pair.Elements[1]1283 }1284 for k, v := range evaluatedArr {1285 switch k {1286 case "a":1287 verifyIntegerObject(t, 0, v, 123)1288 case "b":1289 verifyStringObject(t, 0, v, "test")1290 case "c":1291 verifyBooleanObject(t, 0, v, true)1292 case "d":1293 verifyArrayObject(t, 0, v, []interface{}{1, "Goby", false})1294 }1295 }1296 v.checkCFP(t, 0, 0)1297}1298func TestHashToArrayMethodFail(t *testing.T) {1299 testsFail := []errorTestCase{1300 {`{ a: 1, b: 2 }.to_a(true, { hello: "World" })`, "ArgumentError: Expect 1 or less argument(s). got: 2", 1},1301 {`{ a: 1, b: 2 }.to_a(123)`, "TypeError: Expect argument to be Boolean. got: Integer", 1},1302 }1303 for i, tt := range testsFail {1304 v := initTestVM()1305 evaluated := v.testEval(t, tt.input, getFilename())1306 checkErrorMsg(t, i, evaluated, tt.expected)1307 v.checkCFP(t, i, tt.expectedCFP)1308 v.checkSP(t, i, 1)1309 }1310}1311func TestHashToJSONMethodWithArray(t *testing.T) {1312 tests := []struct {1313 input string1314 expected interface{}1315 }{1316 {`1317 { a: 1, b: [1, "2", true]}.to_json1318 `, struct {1319 A int `json:"a"`1320 B []interface{} `json:"b"`1321 }{1322 A: 1,1323 B: []interface{}{1, "2", true},1324 }},1325 {`1326 { a: 1, b: [1, "2", [4, 5, nil], { foo: "bar" }]}.to_json1327 `, struct {1328 A int `json:"a"`1329 B []interface{} `json:"b"`1330 }{1331 A: 1,1332 B: []interface{}{1333 1, "2", []interface{}{4, 5, nil}, struct {1334 Foo string `json:"foo"`1335 }{1336 "bar",1337 },1338 },1339 }},1340 }1341 for i, tt := range tests {1342 v := initTestVM()1343 evaluated := v.testEval(t, tt.input, getFilename())1344 compareJSONResult(t, evaluated, tt.expected)1345 v.checkCFP(t, i, 0)1346 v.checkSP(t, i, 1)1347 }1348}1349func TestHashToJSONMethodWithNestedHash(t *testing.T) {1350 tests := []struct {1351 input string1352 expected interface{}1353 }{1354 {`1355 { a: 1, b: { c: 2 }}.to_json1356 `, struct {1357 A int `json:"a"`1358 B struct {1359 C int `json:"c"`1360 } `json:"b"`1361 }{1362 1,1363 struct {1364 C int `json:"c"`1365 }{C: 2},1366 }},1367 {`1368 { a: 1, b: { c: 2, d: { e: "foo" }}}.to_json1369 `, struct {1370 A int `json:"a"`1371 B struct {1372 C int `json:"c"`1373 D struct {1374 E string `json:"e"`1375 } `json:"d"`1376 } `json:"b"`1377 }{1378 1,1379 struct {1380 C int `json:"c"`1381 D struct {1382 E string `json:"e"`1383 } `json:"d"`1384 }{C: 2, D: struct {1385 E string `json:"e"`1386 }{E: "foo"}},1387 }},1388 }1389 for i, tt := range tests {1390 v := initTestVM()1391 evaluated := v.testEval(t, tt.input, getFilename())1392 compareJSONResult(t, evaluated, tt.expected)1393 v.checkCFP(t, i, 0)1394 v.checkSP(t, i, 1)1395 }1396}1397func TestHashToJSONMethodWithCustomType(t *testing.T) {1398 tests := []struct {1399 input string1400 expected interface{}1401 }{1402 {`1403 class Person1404 def initialize(name, age)1405 @name = name1406 @age = age1407 end1408 def to_json1409 { name: @name, age: @age }.to_json1410 end1411 end1412 stan = Person.new("Stan", 23)1413 h = { a: 1, person: stan }.to_json1414 `, struct {1415 A int `json:"a"`1416 Person struct {1417 Name string `json:"name"`1418 Age int `json:"age"`1419 } `json:"person"`1420 }{1421 1,1422 struct {1423 Name string `json:"name"`1424 Age int `json:"age"`1425 }{1426 Name: "Stan",1427 Age: 23,1428 },1429 }},1430 {`1431 class JobTitle1432 def initialize(name)1433 @name = name1434 end1435 def to_json1436 { title: @name }.to_json1437 end1438 end1439 class Person1440 def initialize(name, age)1441 @name = name1442 @age = age1443 @job = JobTitle.new("software engineer")1444 end1445 def to_json1446 { name: @name, age: @age, job: @job }.to_json1447 end1448 end1449 stan = Person.new("Stan", 23)1450 stan.to_json1451 `, struct {1452 Name string `json:"name"`1453 Age int `json:"age"`1454 Job struct {1455 Title string `json:"title"`1456 } `json:"job"`1457 }{1458 "Stan",1459 23,1460 struct {1461 Title string `json:"title"`1462 }{1463 Title: "software engineer",1464 },1465 }},1466 }1467 for i, tt := range tests {1468 v := initTestVM()1469 evaluated := v.testEval(t, tt.input, getFilename())1470 compareJSONResult(t, evaluated, tt.expected)1471 v.checkCFP(t, i, 0)1472 v.checkSP(t, i, 1)1473 }1474}1475func TestHashToJSONMethodWithBasicTypes(t *testing.T) {1476 tests := []struct {1477 input string1478 expected interface{}1479 }{1480 {`1481 {}.to_json1482 `, struct{}{}},1483 {`1484 { a: 1, b: 2 }.to_json1485 `, struct {1486 A int `json:"a"`1487 B int `json:"b"`1488 }{1489 1,1490 2,1491 }},1492 {`1493 { foo: "bar", b: 2 }.to_json1494 `, struct {1495 Foo string `json:"foo"`1496 B int `json:"b"`1497 }{1498 "bar",1499 2,1500 }},1501 {`1502 { foo: "bar", b: 2, boolean: true }.to_json1503 `, struct {1504 Foo string `json:"foo"`1505 B int `json:"b"`1506 Boolean bool `json:"boolean"`1507 }{1508 "bar",1509 2,1510 true,1511 }},1512 {`1513 { foo: "bar", b: 2, boolean: true, nothing: nil }.to_json1514 `, struct {1515 Foo string `json:"foo"`1516 B int `json:"b"`1517 Boolean bool `json:"boolean"`1518 Nothing interface{} `json:"nothing"`1519 }{1520 "bar",1521 2,1522 true,1523 nil,1524 }},1525 }1526 for i, tt := range tests {1527 v := initTestVM()1528 evaluated := v.testEval(t, tt.input, getFilename())1529 compareJSONResult(t, evaluated, tt.expected)1530 v.checkCFP(t, i, 0)1531 v.checkSP(t, i, 1)1532 }1533}1534func TestHashToJSONMethodFail(t *testing.T) {1535 testsFail := []errorTestCase{1536 {`{ a: 1, b: 2 }.to_json(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1537 {`{ a: 1, b: 2 }.to_json(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},1538 }1539 for i, tt := range testsFail {1540 v := initTestVM()1541 evaluated := v.testEval(t, tt.input, getFilename())1542 checkErrorMsg(t, i, evaluated, tt.expected)1543 v.checkCFP(t, i, tt.expectedCFP)1544 v.checkSP(t, i, 1)1545 }1546}1547func TestHashToStringMethod(t *testing.T) {1548 tests := []struct {1549 input string1550 expected interface{}1551 }{1552 {`{ a: 1 }.to_s`, "{ a: 1 }"},1553 {`{ a: 1, b: "Hello" }.to_s`, "{ a: 1, b: \"Hello\" }"},1554 {`{ a: 1, b: [1, true, "Hello", 1..2], c: { lang: "Goby" } }.to_s`, "{ a: 1, b: [1, true, \"Hello\", (1..2)], c: { lang: \"Goby\" } }"},1555 }1556 for i, tt := range tests {1557 v := initTestVM()1558 evaluated := v.testEval(t, tt.input, getFilename())1559 VerifyExpected(t, i, evaluated, tt.expected)1560 v.checkCFP(t, i, 0)1561 v.checkSP(t, i, 1)1562 }1563}1564func TestHashToStringMethodFail(t *testing.T) {1565 testsFail := []errorTestCase{1566 {`{ a: 1, b: 2 }.to_s(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1567 {`{ a: 1, b: 2 }.to_s(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},1568 }1569 for i, tt := range testsFail {1570 v := initTestVM()1571 evaluated := v.testEval(t, tt.input, getFilename())1572 checkErrorMsg(t, i, evaluated, tt.expected)1573 v.checkCFP(t, i, tt.expectedCFP)1574 v.checkSP(t, i, 1)1575 }1576}1577func TestHashTransformValuesMethod(t *testing.T) {1578 tests := []struct {1579 input string1580 expected interface{}1581 }{1582 {`1583 h = { a: 1, b: 2, c: 3 }1584 result = h.transform_values do |v|1585 v * 31586 end1587 h["a"]1588 `, 1},1589 {`1590 h = { a: 1, b: 2, c: 3 }1591 result = h.transform_values do |v|1592 v * 31593 end1594 h["b"]1595 `, 2},1596 {`1597 h = { a: 1, b: 2, c: 3 }1598 result = h.transform_values do |v|1599 v * 31600 end1601 h["c"]1602 `, 3},1603 {`1604 h = { a: 1, b: 2, c: 3 }1605 result = h.transform_values do |v|1606 v * 31607 end1608 result["a"]1609 `, 3},1610 {`1611 h = { a: 1, b: 2, c: 3 }1612 result = h.transform_values do |v|1613 v * 31614 end1615 result["b"]1616 `, 6},1617 {`1618 h = { a: 1, b: 2, c: 3 }1619 result = h.transform_values do |v|1620 v * 31621 end1622 result["c"]1623 `, 9},1624 {`1625 h = {}1626 result = h.transform_values do |v|1627 v * 31628 end1629 result["c"]1630 `, nil},1631 }1632 for i, tt := range tests {1633 v := initTestVM()1634 evaluated := v.testEval(t, tt.input, getFilename())1635 VerifyExpected(t, i, evaluated, tt.expected)1636 v.checkCFP(t, i, 0)1637 v.checkSP(t, i, 1)1638 }1639}1640func TestHashTransformValuesMethodFail(t *testing.T) {1641 testsFail := []errorTestCase{1642 {`{ a: 1, b: 2, c: 3 }.transform_values("Hello") do |value|1643 value * 31644 end1645 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},1646 {`{ a: 1, b: 2, c: 3 }.transform_values`, "InternalError: Can't yield without a block", 1},1647 }1648 for i, tt := range testsFail {1649 v := initTestVM()1650 evaluated := v.testEval(t, tt.input, getFilename())1651 checkErrorMsg(t, i, evaluated, tt.expected)1652 v.checkCFP(t, i, tt.expectedCFP)1653 v.checkSP(t, i, 1)1654 }1655}1656func TestHashValuesMethod(t *testing.T) {1657 input := `1658 { a: 123, b: "test", c: true, d: [1, "Goby", false] }.values1659 `1660 v := initTestVM()1661 evaluated := v.testEval(t, input, getFilename())1662 arr, ok := evaluated.(*ArrayObject)1663 if !ok {1664 t.Fatalf("Expect evaluated value to be Array. got: %T", evaluated)1665 } else if arr.Len() != 4 {1666 t.Fatalf("Expect evaluated array length to be 4. got: %d", arr.Len())1667 }1668 for _, v := range arr.Elements {1669 switch value := v.(type) {1670 case *IntegerObject:1671 verifyIntegerObject(t, 0, value, 123)1672 case *StringObject:1673 verifyStringObject(t, 0, v, "test")1674 case *BooleanObject:1675 verifyBooleanObject(t, 0, v, true)1676 case *ArrayObject:1677 verifyArrayObject(t, 0, v, []interface{}{1, "Goby", false})1678 }1679 }1680 v.checkCFP(t, 0, 0)1681 v.checkSP(t, 0, 1)1682}1683func TestHashValuesMethodFail(t *testing.T) {1684 testsFail := []errorTestCase{1685 {`{ a: 1, b: 2 }.values(123)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1686 {`{ a: 1, b: 2 }.values(true, { hello: "World" })`, "ArgumentError: Expect 0 argument(s). got: 2", 1},1687 }1688 for i, tt := range testsFail {1689 v := initTestVM()1690 evaluated := v.testEval(t, tt.input, getFilename())1691 checkErrorMsg(t, i, evaluated, tt.expected)1692 v.checkCFP(t, i, tt.expectedCFP)1693 v.checkSP(t, i, 1)1694 }1695}1696func TestHashValuesAtMethod(t *testing.T) {1697 tests := []struct {1698 input string1699 expected []interface{}1700 }{1701 {`1702 { a: 1, b: "2" }.values_at("a", "c")1703 `, []interface{}{1, nil}},1704 {`1705 { a: 1, b: "2" }.values_at()1706 `, []interface{}{}},1707 {`1708 {}.values_at("a")1709 `, []interface{}{nil}},1710 }1711 for i, tt := range tests {1712 v := initTestVM()1713 evaluated := v.testEval(t, tt.input, getFilename())1714 verifyArrayObject(t, i, evaluated, tt.expected)1715 v.checkCFP(t, i, 0)1716 v.checkSP(t, i, 1)1717 }1718}1719func TestHashValuesAtMethodFail(t *testing.T) {1720 testsFail := []errorTestCase{1721 {`{ a: 1, b: 2 }.values_at(123)`, "TypeError: Expect argument to be String. got: Integer", 1},1722 }1723 for i, tt := range testsFail {1724 v := initTestVM()1725 evaluated := v.testEval(t, tt.input, getFilename())1726 checkErrorMsg(t, i, evaluated, tt.expected)1727 v.checkCFP(t, i, tt.expectedCFP)1728 v.checkSP(t, i, 1)1729 }1730}1731// Test helpers1732func JSONBytesEqual(a, b []byte) (bool, error) {1733 var j, j2 interface{}1734 if err := json.Unmarshal(a, &j); err != nil {1735 return false, err1736 }1737 if err := json.Unmarshal(b, &j2); err != nil {1738 return false, err1739 }1740 return reflect.DeepEqual(j2, j), nil1741}1742// We can't equalTo string directly because the key/value's order might change and we can't control it.1743func compareJSONResult(t *testing.T, evaluated Object, exp interface{}) {1744 t.Helper()1745 expected, err := json.Marshal(exp)1746 if err != nil {1747 t.Fatal(err.Error())1748 }1749 s := evaluated.(*StringObject).value1750 r, err := JSONBytesEqual([]byte(s), expected)1751 if err != nil {1752 t.Fatal(err.Error())1753 }1754 if !r {1755 t.Fatalf("Expect json:\n%s \n\n got: %s", string(expected), s)1756 }1757}1758func TestHashInspectCallsToString(t *testing.T) {1759 input := `h = { a: 1, b: "234", c: true, d: nil, e: Class.new}; h.to_s == h.inspect`1760 expected := true1761 vm := initTestVM()1762 evaluated := vm.testEval(t, input, getFilename())1763 VerifyExpected(t, i, evaluated, expected)1764 vm.checkCFP(t, i, 0)1765 vm.checkSP(t, i, 1)1766}1767func TestHashInspectCallsChildElementToString(t *testing.T) {1768 input := `1769 a = nil1770 b = '234'1771 c = true1772 d = Class.new1773 e = [1,2,3]1774 { a: a, b: b, c: c, d: d, e: e }.inspect1775 `1776 expected := `{ a: nil, b: "234", c: true, d: #<Class:##OBJECTID## >, e: [1, 2, 3] }`1777 vm := initTestVM()1778 evaluated := vm.testEval(t, input, getFilename())1779 VerifyExpected(t, i, evaluated, expected)1780 vm.checkCFP(t, i, 0)1781 vm.checkSP(t, i, 1)1782}1783func TestHashDupMethod(t *testing.T) {1784 tests := []struct {1785 input string1786 expected map[string]interface{}1787 }{1788 {`{foo: "bar"}.dup`, map[string]interface{}{"foo": "bar"}},1789 {`1790a = {foo: "bar"}1791b = a.dup1792a["foo"] = 101793b1794`, map[string]interface{}{"foo": "bar"}},1795 }1796 for i, tt := range tests {1797 v := initTestVM()1798 evaluated := v.testEval(t, tt.input, getFilename())1799 verifyHashObject(t, i, evaluated, tt.expected)1800 v.checkCFP(t, i, 0)1801 v.checkSP(t, i, 1)1802 }1803}...
string_test.go
Source:string_test.go
1package vm2import (3 "fmt"4 "testing"5)6func TestStringClassSuperclass(t *testing.T) {7 tests := []struct {8 input string9 expected string10 }{11 {`String.class.name`, "Class"},12 {`String.superclass.name`, "Object"},13 }14 for i, tt := range tests {15 v := initTestVM()16 evaluated := v.testEval(t, tt.input, getFilename())17 VerifyExpected(t, i, evaluated, tt.expected)18 v.checkCFP(t, i, 0)19 v.checkSP(t, i, 1)20 }21}22func TestStringFmtMethod(t *testing.T) {23 tests := []struct {24 input string25 expected interface{}26 }{27 {`String.fmt("This is %s", "goby")`, "This is goby"},28 {`String.fmt("This is %slang", "goby")`, "This is gobylang"},29 {`String.fmt("This is %s %s", "goby", "ruby")`, "This is goby ruby"},30 {` String.fmt("Hello! %s", 1)`, "Hello! 1"},31 {` String.fmt("Hello! %s", 1.1)`, "Hello! 1.1"},32 {` String.fmt("Hello! %s", "1.1".to_d)`, "Hello! 1.1"},33 {` String.fmt("Hello! %s", "1.1".to_d.fraction)`, "Hello! 11/10"},34 {` String.fmt("Hello! %s", :symbol)`, "Hello! symbol"},35 {` String.fmt("Hello! %s", [:array])`, `Hello! ["array"]`},36 {` String.fmt("Hello! %s", {key: :value})`, `Hello! { key: "value" }`},37 }38 for i, tt := range tests {39 v := initTestVM()40 evaluated := v.testEval(t, tt.input, getFilename())41 VerifyExpected(t, i, evaluated, tt.expected)42 v.checkCFP(t, i, 0)43 v.checkSP(t, i, 1)44 }45}46func TestStringFmtMethodFail(t *testing.T) {47 testsFail := []errorTestCase{48 {`String.fmt()`, "ArgumentError: Expect 1 or more argument(s). got: 0", 1},49 {`String.fmt(1)`, "TypeError: Expect argument to be String. got: Integer", 1},50 {`String.fmt("Hello! %s Lang!")`, "ArgumentError: Expect 1 additional string(s) to insert. got: 0", 1},51 {`String.fmt("Hello! %s Lang!", "Goby", "Ruby")`, "ArgumentError: Expect 1 additional string(s) to insert. got: 2", 1},52 }53 for i, tt := range testsFail {54 v := initTestVM()55 evaluated := v.testEval(t, tt.input, getFilename())56 checkErrorMsg(t, i, evaluated, tt.expected)57 v.checkCFP(t, i, tt.expectedCFP)58 v.checkSP(t, i, 1)59 }60}61func TestEvalStringExpression(t *testing.T) {62 tests := []struct {63 input string64 expected string65 }{66 {`"st0012"`, "st0012"},67 {`'Monkey'`, "Monkey"},68 {`"\"Maxwell\""`, "\"Maxwell\""},69 {`"'Alexius'"`, "'Alexius'"},70 {`"\'Maxwell\'"`, "'Maxwell'"},71 {`'\'Alexius\''`, "'Alexius'"},72 {`"Maxwell\nAlexius"`, "Maxwell\nAlexius"},73 {`'Maxwell\nAlexius'`, "Maxwell\\nAlexius"},74 }75 for i, tt := range tests {76 v := initTestVM()77 evaluated := v.testEval(t, tt.input, getFilename())78 VerifyExpected(t, i, evaluated, tt.expected)79 v.checkCFP(t, i, 0)80 v.checkSP(t, i, 1)81 }82}83func TestStringComparison(t *testing.T) {84 tests := []struct {85 input string86 expected interface{}87 }{88 {`"123" == "123"`, true},89 {`"123" == "124"`, false},90 {`"123" == 123`, false},91 {`"123" == (1..3)`, false},92 {`"123" == { a: 1, b: 2 }`, false},93 {`"123" == [1, "String", true, 2..5]`, false},94 {`"\"Maxwell\"" == '"Maxwell"'`, true},95 {`"\'Maxwell\'" == '\'Maxwell\''`, true},96 {`"\"Maxwell\"" == '\"Maxwell\"'`, false},97 {`"123" != "123"`, false},98 {`"123" != "124"`, true},99 {`"123" != 123`, true},100 {`"123" != (1..3)`, true},101 {`"123" != { a: 1, b: 2 }`, true},102 {`"123" != [1, "String", true, 2..5]`, true},103 {`"123" != String`, true},104 {`"1234" > "123"`, true},105 {`"123" > "1235"`, false},106 {`"1234" < "123"`, false},107 {`"1234" < "12jdkfj3"`, true},108 {`"1234" <=> "1234"`, 0},109 {`"1234" <=> "4"`, -1},110 {`"abcdef" <=> "abcde"`, 1},111 {`"ä¸" <=> "ä¸"`, 0},112 {`"äº" <=> "ä¸"`, 1},113 {`"ä¸" <=> "äº"`, -1},114 {`"ð£" <=> "ð£"`, 0},115 {`"ð£" <=> "ä¸"`, 1},116 {`"ä¸" <=> "ð£"`, -1},117 {`"ðº" <=> "ð£"`, 1},118 {`"ð£" <=> "ðº"`, -1},119 }120 for i, tt := range tests {121 v := initTestVM()122 evaluated := v.testEval(t, tt.input, getFilename())123 VerifyExpected(t, i, evaluated, tt.expected)124 v.checkCFP(t, i, 0)125 v.checkSP(t, i, 1)126 }127}128func TestStringComparisonFail(t *testing.T) {129 testsFail := []errorTestCase{130 {`"a" < 1`, "TypeError: Expect argument to be String. got: Integer", 1},131 {`"a" > 1`, "TypeError: Expect argument to be String. got: Integer", 1},132 {`"a" <=> 1`, "TypeError: Expect argument to be String. got: Integer", 1},133 }134 for i, tt := range testsFail {135 v := initTestVM()136 evaluated := v.testEval(t, tt.input, getFilename())137 checkErrorMsg(t, i, evaluated, tt.expected)138 v.checkCFP(t, i, tt.expectedCFP)139 v.checkSP(t, i, 1)140 }141}142func TestMatchMethod(t *testing.T) {143 tests := []struct {144 input string145 expected interface{}146 }{147 {`"abc".match? Regexp.new("bc")`, 1},148 {`"abc".match? Regexp.new("d")`, nil},149 }150 for i, tt := range tests {151 v := initTestVM()152 evaluated := v.testEval(t, tt.input, getFilename())153 VerifyExpected(t, i, evaluated, tt.expected)154 v.checkCFP(t, i, 0)155 v.checkSP(t, i, 1)156 }157}158func TestMatchMethodFail(t *testing.T) {159 testsFail := []errorTestCase{160 {`"abc".match?(*[1, 2])`, "ArgumentError: Expect 1 argument(s). got: 2", 1},161 {`"abc".match?('a')`, "TypeError: Expect argument to be Regexp. got: String", 1},162 }163 for i, tt := range testsFail {164 v := initTestVM()165 evaluated := v.testEval(t, tt.input, getFilename())166 checkErrorMsg(t, i, evaluated, tt.expected)167 v.checkCFP(t, i, tt.expectedCFP)168 v.checkSP(t, i, 1)169 }170}171func TestStringOperation(t *testing.T) {172 tests := []struct {173 input string174 expected interface{}175 }{176 {`"Stan " + "Lo"`, "Stan Lo"},177 {`"Dog" + "&" + "Cat"`, "Dog&Cat"},178 {`"Three " * 3`, "Three Three Three "},179 {`"Zero" * 0`, ""},180 {`"Minus" * 1`, "Minus"},181 {`"Hello"[1]`, "e"},182 {`"Hello"[5]`, nil},183 {`"Hello"[-1]`, "o"},184 {`"Hello"[-6]`, nil},185 {`"Helloð£"[5]`, "ð£"},186 {`"Helloð£"[-1]`, "ð£"},187 {`"Hello"[1..3]`, "ell"},188 {`"Hello"[1..5]`, "ello"},189 {`"Hello"[-3..-1]`, "llo"},190 {`"Hello"[-6..-1]`, nil},191 {`"Helloð£"[-3..-3]`, "l"},192 {`"Helloð£"[1..-1]`, "elloð£"},193 {`"Hello\nWorld"[5]`, "\n"},194 {`"\"Maxwell\""[0]`, "\""},195 {`"\"Maxwell\""[-1]`, "\""},196 {`"\'Maxwell\'"[0]`, "'"},197 {`"\'Maxwell\'"[-1]`, "'"},198 {`'\"Maxwell\"'[0]`, "\\"},199 {`'\"Maxwell\"'[1]`, "\""},200 {`'\"Maxwell\"'[-1]`, "\""},201 {`'\"Maxwell\"'[-2]`, "\\"},202 {`'\'Maxwell\''[0]`, "'"},203 {`'\'Maxwell\''[-1]`, "'"},204 {`"Ruby"[1] = "oo"`, "Rooby"},205 {`"Go"[2] = "by"`, "Goby"},206 {`"Ruby"[-3] = "oo"`, "Rooby"},207 {`"Hello"[-5] = "Tr"`, "Trello"},208 {`"Hello\nWorld"[5] = " "`, "Hello World"},209 {`"Helloð£"[5] = "ðº"`, "Helloðº"},210 {`"Helloð£"[1] = "ðº"`, "Hðºlloð£"},211 {`"Helloð£"[-1] = "ðº"`, "Helloðº"},212 }213 for i, tt := range tests {214 v := initTestVM()215 evaluated := v.testEval(t, tt.input, getFilename())216 VerifyExpected(t, i, evaluated, tt.expected)217 v.checkCFP(t, i, 0)218 v.checkSP(t, i, 1)219 }220}221func TestStringOperationFail(t *testing.T) {222 testsFail := []errorTestCase{223 {`"Taipei" + 101`, "TypeError: Expect argument to be String. got: Integer", 1},224 {`"Taipei" * "101"`, "TypeError: Expect argument to be Integer. got: String", 1},225 {`"Taipei" * (-101)`, "ArgumentError: Expect second argument to be positive value. got: -101", 1},226 {`"Taipei"[1] = 1`, "TypeError: Expect argument to be String. got: Integer", 1},227 {`"Taipei"[1] = true`, "TypeError: Expect argument to be String. got: Boolean", 1},228 {`"Taipei"[]`, "ArgumentError: Expect 1 argument(s). got: 0", 1},229 {`"Taipei"[true] = 101`, "TypeError: Expect argument to be Integer. got: Boolean", 1},230 {`"Taipei"[20] = "10"`, "ArgumentError: Index value out of range. got: 20", 1},231 {`"Taipei"[-20] = "a"`, "ArgumentError: Index value out of range. got: -20", 1},232 }233 for i, tt := range testsFail {234 v := initTestVM()235 evaluated := v.testEval(t, tt.input, getFilename())236 checkErrorMsg(t, i, evaluated, tt.expected)237 v.checkCFP(t, i, tt.expectedCFP)238 v.checkSP(t, i, 1)239 }240}241// Method test242func TestStringCapitalizeMethod(t *testing.T) {243 tests := []struct {244 input string245 expected interface{}246 }{247 {`"cat".capitalize`, "Cat"},248 {`"HELLO".capitalize`, "Hello"},249 {`"123word".capitalize`, "123word"},250 {`"Two Words".capitalize`, "Two words"},251 {`"first Lower".capitalize`, "First lower"},252 {`"all lower".capitalize`, "All lower"},253 {`"heLlo\nWoRLd".capitalize`, "Hello\nworld"},254 {`"ð£HeLlOðº".capitalize`, "ð£helloðº"},255 {`"ð£HeLlOðº".capitalize`, "ð£helloðº"},256 {`"ÃÃÃÃÃÃ
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÅÅ ÃÃÃÃß".capitalize`, "ÃáâãäåæçèéêëìÃîïñòóôõöøÅšùúûüýÿ"},257 {`"ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎΠΡΣΤΥΦΧΨΩ".capitalize`, "ÎβγδεζηθικλμνξοÏÏÏÏÏ
ÏÏÏÏ"},258 }259 for i, tt := range tests {260 v := initTestVM()261 evaluated := v.testEval(t, tt.input, getFilename())262 VerifyExpected(t, i, evaluated, tt.expected)263 v.checkCFP(t, i, 0)264 v.checkSP(t, i, 1)265 }266}267func TestStringChopMethod(t *testing.T) {268 tests := []struct {269 input string270 expected interface{}271 }{272 {`"Hello".chop`, "Hell"},273 {`"Hello\n".chop`, "Hello"},274 {`"Helloð£".chop`, "Hello"},275 }276 for i, tt := range tests {277 v := initTestVM()278 evaluated := v.testEval(t, tt.input, getFilename())279 VerifyExpected(t, i, evaluated, tt.expected)280 v.checkCFP(t, i, 0)281 v.checkSP(t, i, 1)282 }283}284func TestStringConcatenateMethod(t *testing.T) {285 tests := []struct {286 input string287 expected interface{}288 }{289 {`"Hello ".concat("World")`, "Hello World"},290 {`"Hello World".concat("ð£")`, "Hello Worldð£"},291 }292 for i, tt := range tests {293 v := initTestVM()294 evaluated := v.testEval(t, tt.input, getFilename())295 VerifyExpected(t, i, evaluated, tt.expected)296 v.checkCFP(t, i, 0)297 v.checkSP(t, i, 1)298 }299}300func TestStringConcatenateMethodFail(t *testing.T) {301 testsFail := []errorTestCase{302 {`"a".concat`, "ArgumentError: Expect 1 argument(s). got: 0", 1},303 {`"a".concat("Hello", "World")`, "ArgumentError: Expect 1 argument(s). got: 2", 1},304 {`"a".concat(1)`, "TypeError: Expect argument to be String. got: Integer", 1},305 {`"a".concat(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},306 {`"a".concat(nil)`, "TypeError: Expect argument to be String. got: Null", 1},307 }308 for i, tt := range testsFail {309 v := initTestVM()310 evaluated := v.testEval(t, tt.input, getFilename())311 checkErrorMsg(t, i, evaluated, tt.expected)312 v.checkCFP(t, i, tt.expectedCFP)313 v.checkSP(t, i, 1)314 }315}316func TestStringCountMethod(t *testing.T) {317 tests := []struct {318 input string319 expected interface{}320 }{321 {`"abcde".count`, 5},322 {`"ååï¼ä¸çï¼".count`, 6},323 {`"Hello\nWorld".count`, 11},324 {`"Hello\nWorldð£".count`, 12},325 }326 for i, tt := range tests {327 v := initTestVM()328 evaluated := v.testEval(t, tt.input, getFilename())329 VerifyExpected(t, i, evaluated, tt.expected)330 v.checkCFP(t, i, 0)331 v.checkSP(t, i, 1)332 }333}334func TestStringDeleteMethod(t *testing.T) {335 tests := []struct {336 input string337 expected interface{}338 }{339 {`"Hello hello HeLlo".delete("el")`, "Hlo hlo HeLlo"},340 {`"Hello ð£ Hello ð£ Hello".delete("ð£")`, "Hello Hello Hello"},341 }342 for i, tt := range tests {343 v := initTestVM()344 evaluated := v.testEval(t, tt.input, getFilename())345 VerifyExpected(t, i, evaluated, tt.expected)346 v.checkCFP(t, i, 0)347 v.checkSP(t, i, 1)348 }349}350func TestStringDeleteMethodFail(t *testing.T) {351 testsFail := []errorTestCase{352 {`"Hello hello HeLlo".delete`, "ArgumentError: Expect 1 argument(s). got: 0", 1},353 {`"Hello hello HeLlo".delete(1)`, "TypeError: Expect argument to be String. got: Integer", 1},354 {`"Hello hello HeLlo".delete(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},355 {`"Hello hello HeLlo".delete(nil)`, "TypeError: Expect argument to be String. got: Null", 1},356 }357 for i, tt := range testsFail {358 v := initTestVM()359 evaluated := v.testEval(t, tt.input, getFilename())360 checkErrorMsg(t, i, evaluated, tt.expected)361 v.checkCFP(t, i, tt.expectedCFP)362 v.checkSP(t, i, 1)363 }364}365func TestStringDowncaseMethod(t *testing.T) {366 tests := []struct {367 input string368 expected interface{}369 }{370 {`"hEllO".downcase`, "hello"},371 {`"MORE wOrds".downcase`, "more words"},372 {`"HeLlO\tWorLD".downcase`, "hello\tworld"},373 {`"ð£HeLlOðº".downcase`, "ð£helloðº"},374 {`"ÃÃÃÃÃÃ
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÅÅ ÃÃÃÃß".downcase`, "à áâãäåæçèéêëìÃîïñòóôõöøÅšùúûüýÿ"},375 {`"ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎΠΡΣΤΥΦΧΨΩ".downcase`, "αβγδεζηθικλμνξοÏÏÏÏÏ
ÏÏÏÏ"},376 }377 for i, tt := range tests {378 v := initTestVM()379 evaluated := v.testEval(t, tt.input, getFilename())380 VerifyExpected(t, i, evaluated, tt.expected)381 v.checkCFP(t, i, 0)382 v.checkSP(t, i, 1)383 }384}385func TestStringEachByteMethod(t *testing.T) {386 tests := []struct {387 input string388 expected []interface{}389 }{390 {`391 arr = []392 "Hello\nWorld".each_byte do |byte|393 arr.push(byte)394 end395 arr396 `, []interface{}{72, 101, 108, 108, 111, 10, 87, 111, 114, 108, 100}},397 {`398 arr = []399 "Sushi ð£".each_byte do |byte|400 arr.push(byte)401 end402 arr403 `, []interface{}{83, 117, 115, 104, 105, 32, 240, 159, 141, 163}},404 // cases for providing an empty block405 {`406 a = "Sushi ð£".each_byte do; end407 a.to_a408 `, []interface{}{"S", "u", "s", "h", "i", " ", "ð£"}},409 {`410 a = "Sushi ð£".each_byte do |i|; end411 a.to_a412 `, []interface{}{"S", "u", "s", "h", "i", " ", "ð£"}},413 {`414 a = "".each_byte do; end415 a.to_a416 `, []interface{}{}},417 {`418 a = "".each_byte do |i|; end419 a.to_a420 `, []interface{}{}},421 }422 for i, tt := range tests {423 v := initTestVM()424 evaluated := v.testEval(t, tt.input, getFilename())425 verifyArrayObject(t, i, evaluated, tt.expected)426 v.checkCFP(t, i, 0)427 v.checkSP(t, i, 1)428 }429}430func TestStringEachByteMethodFail(t *testing.T) {431 testsFail := []errorTestCase{432 {`433 "Taipei".each_byte(101) do |byte|434 puts byte435 end436 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},437 {`"Taipei".each_byte`, "InternalError: Can't yield without a block", 1},438 }439 for i, tt := range testsFail {440 v := initTestVM()441 evaluated := v.testEval(t, tt.input, getFilename())442 checkErrorMsg(t, i, evaluated, tt.expected)443 v.checkCFP(t, i, tt.expectedCFP)444 v.checkSP(t, i, 1)445 }446}447func TestStringEachCharMethod(t *testing.T) {448 tests := []struct {449 input string450 expected []interface{}451 }{452 {`453 arr = []454 "Hello\nWorld".each_char do |char|455 arr.push(char)456 end457 arr458 `, []interface{}{"H", "e", "l", "l", "o", "\n", "W", "o", "r", "l", "d"}},459 // cases for providing an empty block460 {`461 a = "Sushi ð£".each_char do; end; a.to_a462 `, []interface{}{"S", "u", "s", "h", "i", " ", "ð£"}},463 {`464 a = "Sushi ð£".each_char do |i|; end; a.to_a465 `, []interface{}{"S", "u", "s", "h", "i", " ", "ð£"}},466 {`467 a = "".each_char do; end468 a.to_a469 `, []interface{}{}},470 {`471 a = "".each_char do |i|; end472 a.to_a473 `, []interface{}{}},474 }475 for i, tt := range tests {476 v := initTestVM()477 evaluated := v.testEval(t, tt.input, getFilename())478 verifyArrayObject(t, i, evaluated, tt.expected)479 v.checkCFP(t, i, 0)480 v.checkSP(t, i, 1)481 }482}483func TestStringEachCharMethodFail(t *testing.T) {484 testsFail := []errorTestCase{485 {`486 "Taipei".each_char(101) do |char|487 puts char488 end489 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},490 {`"Taipei".each_char`, "InternalError: Can't yield without a block", 1},491 }492 for i, tt := range testsFail {493 v := initTestVM()494 evaluated := v.testEval(t, tt.input, getFilename())495 checkErrorMsg(t, i, evaluated, tt.expected)496 v.checkCFP(t, i, tt.expectedCFP)497 v.checkSP(t, i, 1)498 }499}500func TestStringEachLineMethod(t *testing.T) {501 tests := []struct {502 input string503 expected []interface{}504 }{505 {`506 arr = []507 "Hello\nWorld\nGoby".each_line do |line|508 arr.push(line)509 end510 arr511 `, []interface{}{"Hello", "World", "Goby"}},512 {`513 arr = []514 "Max\vwell\nAlex\fius".each_line do |line|515 arr.push(line)516 end517 arr518 `, []interface{}{"Max\vwell", "Alex\fius"}},519 // cases for providing an empty block520 {`521 a = "Max\vwell\nAlex\fius".each_line do; end; a.to_a522 `, []interface{}{"M", "a", "x", "\v", "w", "e", "l", "l", "\n", "A", "l", "e", "x", "\f", "i", "u", "s"}},523 {`524 a = "Max\vwell\nAlex\fius".each_line do |i|; end; a.to_a525 `, []interface{}{"M", "a", "x", "\v", "w", "e", "l", "l", "\n", "A", "l", "e", "x", "\f", "i", "u", "s"}},526 {`527 a = "".each_line do; end; a.to_a528 `, []interface{}{}},529 {`530 a = "".each_line do |i|; end; a.to_a531 `, []interface{}{}},532 }533 for i, tt := range tests {534 v := initTestVM()535 evaluated := v.testEval(t, tt.input, getFilename())536 verifyArrayObject(t, i, evaluated, tt.expected)537 v.checkCFP(t, i, 0)538 v.checkSP(t, i, 1)539 }540}541func TestStringEachLineMethodFail(t *testing.T) {542 testsFail := []errorTestCase{543 {`544 "Taipei".each_line(101) do |line|545 puts line546 end547 `, "ArgumentError: Expect 0 argument(s). got: 1", 1},548 {`"Taipei".each_line`, "InternalError: Can't yield without a block", 1},549 }550 for i, tt := range testsFail {551 v := initTestVM()552 evaluated := v.testEval(t, tt.input, getFilename())553 checkErrorMsg(t, i, evaluated, tt.expected)554 v.checkCFP(t, i, tt.expectedCFP)555 v.checkSP(t, i, 1)556 }557}558func TestStringEndWithMethod(t *testing.T) {559 tests := []struct {560 input string561 expected interface{}562 }{563 {`"Hello".end_with?("llo")`, true},564 {`"Hello".end_with?("Hello")`, true},565 {`"Hello".end_with?("Hello ")`, false},566 {`"ååï¼ä¸çï¼".end_with?("ä¸çï¼")`, true},567 {`"Hello".end_with?("ell")`, false},568 {`"ååï¼ä¸çï¼".end_with?("ååï¼")`, false},569 {`"ð£Helloðº".end_with?("ðº")`, true},570 {`"ð£Helloðº".end_with?("ð£")`, false},571 }572 for i, tt := range tests {573 v := initTestVM()574 evaluated := v.testEval(t, tt.input, getFilename())575 VerifyExpected(t, i, evaluated, tt.expected)576 v.checkCFP(t, i, 0)577 v.checkSP(t, i, 1)578 }579}580func TestStringEndWithMethodFail(t *testing.T) {581 testsFail := []errorTestCase{582 {`"Taipei".end_with?("1", "0", "1")`, "ArgumentError: Expect 1 argument(s). got: 3", 1},583 {`"Taipei".end_with?(101)`, "TypeError: Expect argument to be String. got: Integer", 1},584 {`"Hello".end_with?(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},585 {`"Hello".end_with?(1..5)`, "TypeError: Expect argument to be String. got: Range", 1},586 }587 for i, tt := range testsFail {588 v := initTestVM()589 evaluated := v.testEval(t, tt.input, getFilename())590 checkErrorMsg(t, i, evaluated, tt.expected)591 v.checkCFP(t, i, tt.expectedCFP)592 v.checkSP(t, i, 1)593 }594}595func TestStringEmptyMethod(t *testing.T) {596 tests := []struct {597 input string598 expected interface{}599 }{600 {`"".empty?`, true},601 {`"Hello".empty?`, false},602 }603 for i, tt := range tests {604 v := initTestVM()605 evaluated := v.testEval(t, tt.input, getFilename())606 VerifyExpected(t, i, evaluated, tt.expected)607 v.checkCFP(t, i, 0)608 v.checkSP(t, i, 1)609 }610}611func TestStringEqualMethod(t *testing.T) {612 tests := []struct {613 input string614 expected interface{}615 }{616 {`"Hello".eql?("Hello")`, true},617 {`"Hello\nWorld".eql?("Hello\nWorld")`, true},618 {`"Hello".eql?("World")`, false},619 {`"Hello".eql?(1)`, false},620 {`"Hello".eql?(true)`, false},621 {`"Hello".eql?(2..4)`, false},622 {`"Helloð£".eql?("Helloð£")`, true},623 }624 for i, tt := range tests {625 v := initTestVM()626 evaluated := v.testEval(t, tt.input, getFilename())627 VerifyExpected(t, i, evaluated, tt.expected)628 v.checkCFP(t, i, 0)629 v.checkSP(t, i, 1)630 }631}632func TestStringEqualMethodFail(t *testing.T) {633 testsFail := []errorTestCase{634 {`"Hello".eql?`, "ArgumentError: Expect 1 argument(s). got: 0", 1},635 {`"Hello".eql?("Hello", "World")`, "ArgumentError: Expect 1 argument(s). got: 2", 1},636 }637 for i, tt := range testsFail {638 v := initTestVM()639 evaluated := v.testEval(t, tt.input, getFilename())640 checkErrorMsg(t, i, evaluated, tt.expected)641 v.checkCFP(t, i, tt.expectedCFP)642 v.checkSP(t, i, 1)643 }644}645func TestStringIncludeMethod(t *testing.T) {646 tests := []struct {647 input string648 expected interface{}649 }{650 {`"Hello\nWorld".include?("Hello")`, true},651 {`"Hello\nWorld".include?("Hello\nWorld")`, true},652 {`"Hello\nWorld".include?("Hello World")`, false},653 {`"Hello\nWorld".include?("Hello\nWorld!")`, false},654 {`"Hello\nWorld".include?("\n")`, true},655 {`"Hello\nWorld".include?("\r")`, false},656 {`"Helloð£".include?("ð£")`, true},657 }658 for i, tt := range tests {659 v := initTestVM()660 evaluated := v.testEval(t, tt.input, getFilename())661 VerifyExpected(t, i, evaluated, tt.expected)662 v.checkCFP(t, i, 0)663 v.checkSP(t, i, 1)664 }665}666func TestStringIncludeMethodFail(t *testing.T) {667 testsFail := []errorTestCase{668 {`"Goby".include?`, "ArgumentError: Expect 1 argument(s). got: 0", 1},669 {`"Goby".include?("Ruby", "Lang")`, "ArgumentError: Expect 1 argument(s). got: 2", 1},670 {`"Goby".include?(2)`, "TypeError: Expect argument to be String. got: Integer", 1},671 {`"Goby".include?(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},672 {`"Goby".include?(nil)`, "TypeError: Expect argument to be String. got: Null", 1},673 }674 for i, tt := range testsFail {675 v := initTestVM()676 evaluated := v.testEval(t, tt.input, getFilename())677 checkErrorMsg(t, i, evaluated, tt.expected)678 v.checkCFP(t, i, tt.expectedCFP)679 v.checkSP(t, i, 1)680 }681}682func TestStringInsertMethod(t *testing.T) {683 tests := []struct {684 input string685 expected interface{}686 }{687 {`"Hello".insert(0, "X")`, "XHello"},688 {`"Hello".insert(2, "X")`, "HeXllo"},689 {`"Hello".insert(5, "X")`, "HelloX"},690 {`"Hello".insert(-2, "X")`, "HelXlo"},691 {`"Hello".insert(-6, "X")`, "XHello"},692 {`"Hello".insert(0, "ð£")`, "ð£Hello"},693 {`"Hello".insert(2, "ð£")`, "Heð£llo"},694 {`"Hello".insert(5, "ð£")`, "Helloð£"},695 {`"Hello".insert(-2, "ð£")`, "Helð£lo"},696 {`"Hello".insert(-6, "ð£")`, "ð£Hello"},697 }698 for i, tt := range tests {699 v := initTestVM()700 evaluated := v.testEval(t, tt.input, getFilename())701 VerifyExpected(t, i, evaluated, tt.expected)702 v.checkCFP(t, i, 0)703 v.checkSP(t, i, 1)704 }705}706func TestStringInsertMethodFail(t *testing.T) {707 testsFail := []errorTestCase{708 {`"Goby Lang".insert`, "ArgumentError: Expect 2 argument(s). got: 0", 1},709 {`"Taipei".insert(6, " ", "101")`, "ArgumentError: Expect 2 argument(s). got: 3", 1},710 {`"Taipei".insert("6", " 101")`, "TypeError: Expect argument #1 to be Integer. got: String", 1},711 {`"Taipei".insert(6, 101)`, "TypeError: Expect argument #2 to be String. got: Integer", 1},712 {`"Taipei".insert(-8, "101")`, "ArgumentError: Index value out of range. got: -8", 1},713 {`"Taipei".insert(7, "101")`, "ArgumentError: Index value out of range. got: 7", 1},714 }715 for i, tt := range testsFail {716 v := initTestVM()717 evaluated := v.testEval(t, tt.input, getFilename())718 checkErrorMsg(t, i, evaluated, tt.expected)719 v.checkCFP(t, i, tt.expectedCFP)720 v.checkSP(t, i, 1)721 }722}723func TestStringLeftJustifyMethod(t *testing.T) {724 tests := []struct {725 input string726 expected interface{}727 }{728 {`"Hello".ljust(2)`, "Hello"},729 {`"Hello".ljust(7)`, "Hello "},730 {`"Hello".ljust(10, "xo")`, "Helloxoxox"},731 {`"Hello".ljust(10, "ð£ðº")`, "Helloð£ðºð£ðºð£"},732 }733 for i, tt := range tests {734 v := initTestVM()735 evaluated := v.testEval(t, tt.input, getFilename())736 VerifyExpected(t, i, evaluated, tt.expected)737 v.checkCFP(t, i, 0)738 v.checkSP(t, i, 1)739 }740}741func TestStringLeftJustifyMethodFail(t *testing.T) {742 testsFail := []errorTestCase{743 {`"Hello".ljust`, "ArgumentError: Expect 1 to 2 argument(s). got: 0", 1},744 {`"Hello".ljust(1, 2, 3, 4, 5)`, "ArgumentError: Expect 1 to 2 argument(s). got: 5", 1},745 {`"Hello".ljust(true)`, "TypeError: Expect argument #1 to be Integer. got: Boolean", 1},746 {`"Hello".ljust("World")`, "TypeError: Expect argument #1 to be Integer. got: String", 1},747 {`"Hello".ljust(2..5)`, "TypeError: Expect argument #1 to be Integer. got: Range", 1},748 {`"Hello".ljust(10, 10)`, "TypeError: Expect argument #2 to be String. got: Integer", 1},749 {`"Hello".ljust(10, 2..5)`, "TypeError: Expect argument #2 to be String. got: Range", 1},750 {`"Hello".ljust(10, true)`, "TypeError: Expect argument #2 to be String. got: Boolean", 1},751 }752 for i, tt := range testsFail {753 v := initTestVM()754 evaluated := v.testEval(t, tt.input, getFilename())755 checkErrorMsg(t, i, evaluated, tt.expected)756 v.checkCFP(t, i, tt.expectedCFP)757 v.checkSP(t, i, 1)758 }759}760func TestStringLengthMethod(t *testing.T) {761 tests := []struct {762 input string763 expected interface{}764 }{765 {`"New method".length`, 10},766 {`" ".length`, 1},767 {`"ð£ð£ð£".length`, 3},768 }769 for i, tt := range tests {770 v := initTestVM()771 evaluated := v.testEval(t, tt.input, getFilename())772 VerifyExpected(t, i, evaluated, tt.expected)773 v.checkCFP(t, i, 0)774 v.checkSP(t, i, 1)775 }776}777func TestStringMatch(t *testing.T) {778 tests := []struct {779 input string780 expected interface{}781 }{782 {`"Goby!!".match(Regexp.new("G(o)b(y)")).to_s`, "#<MatchData 0:\"Goby\" 1:\"o\" 2:\"y\">"},783 {`"Ruby".match(Regexp.new("G(o)b(y)"))`, nil},784 }785 for i, tt := range tests {786 vm := initTestVM()787 evaluated := vm.testEval(t, tt.input, getFilename())788 VerifyExpected(t, i, evaluated, tt.expected)789 vm.checkCFP(t, i, 0)790 vm.checkSP(t, i, 1)791 }792}793func TestStringMatchFail(t *testing.T) {794 testsFail := []errorTestCase{795 {`'a'.match(Regexp.new("abc"), 1)`, "ArgumentError: Expect 1 argument(s). got: 2", 1},796 {`'a'.match(1)`, "TypeError: Expect argument to be Regexp. got: Integer", 1},797 }798 for i, tt := range testsFail {799 v := initTestVM()800 evaluated := v.testEval(t, tt.input, getFilename())801 checkErrorMsg(t, i, evaluated, tt.expected)802 v.checkCFP(t, i, tt.expectedCFP)803 v.checkSP(t, i, 1)804 }805}806func TestStringReplaceMethod(t *testing.T) {807 tests := []struct {808 input string809 expected interface{}810 }{811 {`"Ruby Lang Ruby Ruby".replace("Ru", "Go")`, "Goby Lang Goby Goby"},812 {`"ð£Rubyð£Lang".replace("Ru", "Go")`, "ð£Gobyð£Lang"},813 {`re = Regexp.new("(Ru|ru)");"Ruby Lang ruby lang".replace(re, "Go")`, "Goby Lang Goby lang"},814 }815 for i, tt := range tests {816 v := initTestVM()817 evaluated := v.testEval(t, tt.input, getFilename())818 VerifyExpected(t, i, evaluated, tt.expected)819 v.checkCFP(t, i, 0)820 v.checkSP(t, i, 1)821 }822}823func TestStringReplaceMethodFail(t *testing.T) {824 testsFail := []errorTestCase{825 {`"Invalid".replace`, "ArgumentError: Expect 2 argument(s). got: 0", 1},826 {`"Invalid".replace("string")`, "ArgumentError: Expect 2 argument(s). got: 1", 1},827 {`"Invalid".replace("string", "replace", true)`, "ArgumentError: Expect 2 argument(s). got: 3", 1},828 {`"Invalid".replace(true, "replacement")`, "TypeError: Expect argument #1 to be String or Regexp. got: Boolean", 1},829 {`"Invalid".replace("pattern", true)`, "TypeError: Expect argument #2 to be String. got: Boolean", 1},830 }831 for i, tt := range testsFail {832 v := initTestVM()833 evaluated := v.testEval(t, tt.input, getFilename())834 checkErrorMsg(t, i, evaluated, tt.expected)835 v.checkCFP(t, i, tt.expectedCFP)836 v.checkSP(t, i, 1)837 }838}839func TestStringReplaceOnceMethod(t *testing.T) {840 tests := []struct {841 input string842 expected interface{}843 }{844 {`"Ruby Lang Ruby Ruby".replace_once("Ru", "Go")`, "Goby Lang Ruby Ruby"},845 {`"ð£Rubyð£Lang Ruby".replace_once("Ru", "Go")`, "ð£Gobyð£Lang Ruby"},846 {`re = Regexp.new("(Ru|ru)");"Ruby Lang ruby lang".replace_once(re, "Go")`, "Goby Lang ruby lang"},847 }848 for i, tt := range tests {849 v := initTestVM()850 evaluated := v.testEval(t, tt.input, getFilename())851 VerifyExpected(t, i, evaluated, tt.expected)852 v.checkCFP(t, i, 0)853 v.checkSP(t, i, 1)854 }855}856func TestStringReplaceOnceMethodFail(t *testing.T) {857 testsFail := []errorTestCase{858 {`"Invalid".replace_once`, "ArgumentError: Expect 2 argument(s). got: 0", 1},859 {`"Invalid".replace_once("string")`, "ArgumentError: Expect 2 argument(s). got: 1", 1},860 {`"Invalid".replace_once("string", "replace", true)`, "ArgumentError: Expect 2 argument(s). got: 3", 1},861 {`"Invalid".replace_once(true, "replacement")`, "TypeError: Expect argument #1 to be String or Regexp. got: Boolean", 1},862 {`"Invalid".replace_once("pattern", true)`, "TypeError: Expect argument #2 to be String. got: Boolean", 1},863 }864 for i, tt := range testsFail {865 v := initTestVM()866 evaluated := v.testEval(t, tt.input, getFilename())867 checkErrorMsg(t, i, evaluated, tt.expected)868 v.checkCFP(t, i, tt.expectedCFP)869 v.checkSP(t, i, 1)870 }871}872func TestStringReverseMethod(t *testing.T) {873 tests := []struct {874 input string875 expected interface{}876 }{877 {`"Reverse Rooby-lang".reverse`, "gnal-ybooR esreveR"},878 {`" ".reverse`, " "},879 {`"-123".reverse`, "321-"},880 {`"Hello\nWorld".reverse`, "dlroW\nolleH"},881 {`"Hello ð£ðº World".reverse`, "dlroW ðºð£ olleH"},882 }883 for i, tt := range tests {884 v := initTestVM()885 evaluated := v.testEval(t, tt.input, getFilename())886 VerifyExpected(t, i, evaluated, tt.expected)887 v.checkCFP(t, i, 0)888 v.checkSP(t, i, 1)889 }890}891func TestStringRightJustifyMethod(t *testing.T) {892 tests := []struct {893 input string894 expected interface{}895 }{896 {`"Hello".rjust(2)`, "Hello"},897 {`"Hello".rjust(7)`, " Hello"},898 {`"Hello".rjust(10, "xo")`, "xoxoxHello"},899 {`"Hello".rjust(10, "ð£ðº")`, "ð£ðºð£ðºð£Hello"},900 }901 for i, tt := range tests {902 v := initTestVM()903 evaluated := v.testEval(t, tt.input, getFilename())904 VerifyExpected(t, i, evaluated, tt.expected)905 v.checkCFP(t, i, 0)906 v.checkSP(t, i, 1)907 }908}909func TestStringRightJustifyFail(t *testing.T) {910 testsFail := []errorTestCase{911 {`"Hello".rjust`, "ArgumentError: Expect 1 to 2 argument(s). got: 0", 1},912 {`"Hello".rjust(1, 2, 3, 4, 5)`, "ArgumentError: Expect 1 to 2 argument(s). got: 5", 1},913 {`"Hello".rjust(true)`, "TypeError: Expect argument #1 to be Integer. got: Boolean", 1},914 {`"Hello".rjust("World")`, "TypeError: Expect argument #1 to be Integer. got: String", 1},915 {`"Hello".rjust(2..5)`, "TypeError: Expect argument #1 to be Integer. got: Range", 1},916 {`"Hello".rjust(10, 10)`, "TypeError: Expect argument #2 to be String. got: Integer", 1},917 {`"Hello".rjust(10, 2..5)`, "TypeError: Expect argument #2 to be String. got: Range", 1},918 {`"Hello".rjust(10, true)`, "TypeError: Expect argument #2 to be String. got: Boolean", 1},919 }920 for i, tt := range testsFail {921 v := initTestVM()922 evaluated := v.testEval(t, tt.input, getFilename())923 checkErrorMsg(t, i, evaluated, tt.expected)924 v.checkCFP(t, i, tt.expectedCFP)925 v.checkSP(t, i, 1)926 }927}928func TestStringSizeMethod(t *testing.T) {929 tests := []struct {930 input string931 expected interface{}932 }{933 {`"Rooby".size`, 5},934 {`" ".size`, 1},935 {`"ð£ðºðºð£".size`, 4},936 }937 for i, tt := range tests {938 v := initTestVM()939 evaluated := v.testEval(t, tt.input, getFilename())940 VerifyExpected(t, i, evaluated, tt.expected)941 v.checkCFP(t, i, 0)942 v.checkSP(t, i, 1)943 }944}945func TestStringSliceMethod(t *testing.T) {946 tests := []struct {947 input string948 expected interface{}949 }{950 {`"Hello World".slice(1..6)`, "ello W"},951 {`"1234567890".slice(6..1)`, ""},952 {`"1234567890".slice(11..1)`, nil},953 {`"1234567890".slice(11..-1)`, nil},954 {`"1234567890".slice(-10..1)`, "12"},955 {`"1234567890".slice(-5..1)`, ""},956 {`"1234567890".slice(-10..-1)`, "1234567890"},957 {`"1234567890".slice(-10..-11)`, ""},958 {`"1234567890".slice(1..-1)`, "234567890"},959 {`"1234567890".slice(1..-1234)`, ""},960 {`"1234567890".slice(-10..-5)`, "123456"},961 {`"1234567890".slice(-5..-10)`, ""},962 {`"1234567890".slice(-11..5)`, nil},963 {`"1234567890".slice(-10..-12)`, ""},964 {`"1234567890".slice(-11..-12)`, nil},965 {`"1234567890".slice(-11..-5)`, nil},966 {`"Hello ð£ðº World".slice(1..6)`, "ello ð£"},967 {`"Hello ð£ðº World".slice(-10..7)`, "o ð£ðº"},968 {`"Hello ð£ðº World".slice(1..-1)`, "ello ð£ðº World"},969 {`"Hello ð£ðº World".slice(-12..-5)`, "llo ð£ðº W"},970 {`"Hello World".slice(4)`, "o"},971 {`"Hello\nWorld".slice(5)`, "\n"},972 {`"Hello World".slice(-3)`, "r"},973 {`"Hello World".slice(-11)`, "H"},974 {`"Hello World".slice(-12)`, nil},975 {`"Hello World".slice(11)`, nil},976 {`"Hello ð£ðº World".slice(6)`, "ð£"},977 {`"Hello ð£ðº World".slice(-7)`, "ðº"},978 {`"Hello ð£ðº World".slice(-10)`, "o"},979 {`"Hello ð£ðº World".slice(-15)`, nil},980 {`"Hello ð£ðº World".slice(14)`, nil},981 }982 for i, tt := range tests {983 v := initTestVM()984 evaluated := v.testEval(t, tt.input, getFilename())985 VerifyExpected(t, i, evaluated, tt.expected)986 v.checkCFP(t, i, 0)987 v.checkSP(t, i, 1)988 }989}990func TestStringSliceMethodFail(t *testing.T) {991 testsFail := []errorTestCase{992 {`"Goby Lang".slice`, "ArgumentError: Expect 1 argument(s). got: 0", 1},993 {`"Goby Lang".slice("Hello")`, "TypeError: Expect argument to be Range or Integer. got: String", 1},994 {`"Goby Lang".slice(true)`, "TypeError: Expect argument to be Range or Integer. got: Boolean", 1},995 }996 for i, tt := range testsFail {997 v := initTestVM()998 evaluated := v.testEval(t, tt.input, getFilename())999 checkErrorMsg(t, i, evaluated, tt.expected)1000 v.checkCFP(t, i, tt.expectedCFP)1001 v.checkSP(t, i, 1)1002 }1003}1004func TestStringSplitMethod(t *testing.T) {1005 tests := []struct {1006 input string1007 expected interface{}1008 }{1009 {`1010 arr = "Hello World".split("o")1011 arr[0]1012 `, "Hell"},1013 {`1014 arr = "Hello World".split("o")1015 arr[1]1016 `, " W"},1017 {`1018 arr = "Hello World".split("o")1019 arr[2]1020 `, "rld"},1021 {`1022 arr = "Hello".split("")1023 arr[0]1024 `, "H"},1025 {`1026 arr = "Hello".split("")1027 arr[1]1028 `, "e"},1029 {`1030 arr = "Hello".split("")1031 arr[2]1032 `, "l"},1033 {`1034 arr = "Hello".split("")1035 arr[3]1036 `, "l"},1037 {`1038 arr = "Hello".split("")1039 arr[4]1040 `, "o"},1041 {`1042 arr = "Hello\nWorld\nGoby".split("\n")1043 arr[0]1044 `, "Hello"},1045 {`1046 arr = "Hello\nWorld\nGoby".split("\n")1047 arr[1]1048 `, "World"},1049 {`1050 arr = "Hello\nWorld\nGoby".split("\n")1051 arr[2]1052 `, "Goby"},1053 {`1054 arr = "HelloðºWorldðºGoby".split("ðº")1055 arr[0]1056 `, "Hello"},1057 {`1058 arr = "HelloðºWorldðºGoby".split("ðº")1059 arr[1]1060 `, "World"},1061 {`1062 arr = "HelloðºWorldðºGoby".split("ðº")1063 arr[2]1064 `, "Goby"},1065 {`1066 arr = "HelloðºWorldð£Goby".split("ðº")1067 arr[0]1068 `, "Hello"},1069 {`1070 arr = "HelloðºWorldð£Goby".split("ðº")1071 arr[1]1072 `, "Worldð£Goby"},1073 }1074 for i, tt := range tests {1075 v := initTestVM()1076 evaluated := v.testEval(t, tt.input, getFilename())1077 VerifyExpected(t, i, evaluated, tt.expected)1078 v.checkCFP(t, i, 0)1079 v.checkSP(t, i, 1)1080 }1081}1082func TestStringSplitMethodFail(t *testing.T) {1083 testsFail := []errorTestCase{1084 {`"Hello World".split`, "ArgumentError: Expect 1 argument(s). got: 0", 1},1085 {`"Hello World".split(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},1086 {`"Hello World".split(123)`, "TypeError: Expect argument to be String. got: Integer", 1},1087 {`"Hello World".split(1..2)`, "TypeError: Expect argument to be String. got: Range", 1},1088 }1089 for i, tt := range testsFail {1090 v := initTestVM()1091 evaluated := v.testEval(t, tt.input, getFilename())1092 checkErrorMsg(t, i, evaluated, tt.expected)1093 v.checkCFP(t, i, tt.expectedCFP)1094 v.checkSP(t, i, 1)1095 }1096}1097func TestStringStartWithMethod(t *testing.T) {1098 tests := []struct {1099 input string1100 expected interface{}1101 }{1102 {`"Hello".start_with("Hel")`, true},1103 {`"Hello".start_with("Hello")`, true},1104 {`"Hello".start_with("Hello ")`, false},1105 {`"ååï¼ä¸çï¼".start_with("ååï¼")`, true},1106 {`"Hello".start_with("hel")`, false},1107 {`"ååï¼ä¸ç".start_with("ä¸çï¼")`, false},1108 {`"ð£Helloðº".start_with("ð£")`, true},1109 {`"ð£Helloðº".start_with("ðº")`, false},1110 }1111 for i, tt := range tests {1112 v := initTestVM()1113 evaluated := v.testEval(t, tt.input, getFilename())1114 VerifyExpected(t, i, evaluated, tt.expected)1115 v.checkCFP(t, i, 0)1116 v.checkSP(t, i, 1)1117 }1118}1119func TestStringStartWithMethodFail(t *testing.T) {1120 testsFail := []errorTestCase{1121 {`"Taipei".start_with("1", "0", "1")`, "ArgumentError: Expect 1 argument(s). got: 3", 1},1122 {`"Taipei".start_with(101)`, "TypeError: Expect argument to be String. got: Integer", 1},1123 {`"Hello".start_with(true)`, "TypeError: Expect argument to be String. got: Boolean", 1},1124 {`"Hello".start_with(1..5)`, "TypeError: Expect argument to be String. got: Range", 1},1125 }1126 for i, tt := range testsFail {1127 v := initTestVM()1128 evaluated := v.testEval(t, tt.input, getFilename())1129 checkErrorMsg(t, i, evaluated, tt.expected)1130 v.checkCFP(t, i, tt.expectedCFP)1131 v.checkSP(t, i, 1)1132 }1133}1134func TestStringStripMethod(t *testing.T) {1135 tests := []struct {1136 input string1137 expected interface{}1138 }{1139 {`" Goby Lang ".strip`, "Goby Lang"},1140 {`"\nGoby Lang\r\t".strip`, "Goby Lang"},1141 {`" \t ð£ Goby Lang ðº \r\n ".strip`, "ð£ Goby Lang ðº"},1142 }1143 for i, tt := range tests {1144 v := initTestVM()1145 evaluated := v.testEval(t, tt.input, getFilename())1146 VerifyExpected(t, i, evaluated, tt.expected)1147 v.checkCFP(t, i, 0)1148 v.checkSP(t, i, 1)1149 }1150}1151func TestStringConversion(t *testing.T) {1152 tests := []struct {1153 input string1154 expected interface{}1155 }{1156 {`"string".to_s`, "string"},1157 {`"Maxwell\nAlexius".to_s`, "Maxwell\nAlexius"},1158 {`'Maxwell\nAlexius'.to_s`, "Maxwell\\nAlexius"},1159 {`"\"Maxwell\"".to_s`, "\"Maxwell\""},1160 {`'\"Maxwell\"'.to_s`, "\\\"Maxwell\\\""},1161 {`"\'Maxwell\'".to_s`, "'Maxwell'"},1162 {`'\'Maxwell\''.to_s`, "'Maxwell'"},1163 {`"123".to_i`, 123},1164 {`"string".to_i`, 0},1165 {`" \t123".to_i`, 123},1166 {`"123string123".to_i`, 123},1167 {`"string123".to_i`, 0},1168 {`"123.5".to_f`, 123.5},1169 {`".5".to_f`, 0.5},1170 {`" 123.5".to_f`, 123.5},1171 {`"3.5e2".to_f`, 350.0},1172 {`1173 arr = "Goby".to_a1174 arr[0]1175 `, "G"},1176 {`1177 arr = "Goby".to_a1178 arr[1]1179 `, "o"},1180 {`1181 arr = "Goby".to_a1182 arr[2]1183 `, "b"},1184 {`1185 arr = "Goby".to_a1186 arr[3]1187 `, "y"},1188 {`1189 arr = "ð£Gobyðº".to_a1190 arr[0]1191 `, "ð£"},1192 {`1193 arr = "ð£Gobyðº".to_a1194 arr[5]1195 `, "ðº"},1196 {`1197 arr = "Maxwell\nAlexius".to_a1198 arr[7]1199 `, "\n"},1200 {`1201 arr = 'Maxwell\nAlexius'.to_a1202 arr[7]1203 `, "\\"},1204 {`1205 arr = 'Maxwell\nAlexius'.to_a1206 arr[8]1207 `, "n"},1208 {`1209 arr = "\"Maxwell\"".to_a1210 arr[0]1211 `, "\""},1212 {`1213 arr = "\"Maxwell\"".to_a1214 arr[-1]1215 `, "\""},1216 {`1217 arr = "\'Maxwell\'".to_a1218 arr[0]1219 `, "'"},1220 {`1221 arr = "\'Maxwell\'".to_a1222 arr[-1]1223 `, "'"},1224 {`1225 arr = '\"Maxwell\"'.to_a1226 arr[0]1227 `, "\\"},1228 {`1229 arr = '\"Maxwell\"'.to_a1230 arr[1]1231 `, "\""},1232 {`1233 arr = '\"Maxwell\"'.to_a1234 arr[-1]1235 `, "\""},1236 {`1237 arr = '\"Maxwell\"'.to_a1238 arr[-2]1239 `, "\\"},1240 {`1241 arr = '\'Maxwell\''.to_a1242 arr[0]1243 `, "'"},1244 {`1245 arr = '\'Maxwell\''.to_a1246 arr[-1]1247 `, "'"},1248 {`"str".to_bytes.to_s.split(" ")[0]`, "<GoObject:"},1249 }1250 for i, tt := range tests {1251 v := initTestVM()1252 evaluated := v.testEval(t, tt.input, getFilename())1253 VerifyExpected(t, i, evaluated, tt.expected)1254 v.checkCFP(t, i, 0)1255 v.checkSP(t, i, 1)1256 }1257}1258func TestStringConversionFail(t *testing.T) {1259 testsFail := []errorTestCase{1260 {`"str".to_a(1)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1261 {`"str".to_d(1)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1262 {`"str".to_i(1)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1263 {`"str".to_f(1)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1264 {`"str".to_s(1)`, "ArgumentError: Expect 0 argument(s). got: 1", 1},1265 {`"1.1.1".to_f`, "ArgumentError: Invalid numeric string. got: 1.1.1", 1},1266 {`"3.5ef".to_f`, "ArgumentError: Invalid numeric string. got: 3.5ef", 1},1267 }1268 for i, tt := range testsFail {1269 v := initTestVM()1270 evaluated := v.testEval(t, tt.input, getFilename())1271 checkErrorMsg(t, i, evaluated, tt.expected)1272 v.checkCFP(t, i, tt.expectedCFP)1273 v.checkSP(t, i, 1)1274 }1275}1276func TestStringUpcaseMethod(t *testing.T) {1277 tests := []struct {1278 input string1279 expected interface{}1280 }{1281 {`"hEllO".upcase`, "HELLO"},1282 {`"MORE wOrds".upcase`, "MORE WORDS"},1283 {`"Hello\nWorld".upcase`, "HELLO\nWORLD"},1284 {`"ð£Helloðº".upcase`, "ð£HELLOðº"},1285 {`"à áâãäåæçèéêëìÃîïñòóôõöøÅšùúûüýÿ".upcase`, "ÃÃÃÃÃÃ
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÅÅ ÃÃÃÃß"},1286 {`"αβγδεζηθικλμνξοÏÏÏÏÏ
ÏÏÏÏ".upcase`, "ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎΠΡΣΤΥΦΧΨΩ"},1287 }1288 for i, tt := range tests {1289 v := initTestVM()1290 evaluated := v.testEval(t, tt.input, getFilename())1291 VerifyExpected(t, i, evaluated, tt.expected)1292 v.checkCFP(t, i, 0)1293 v.checkSP(t, i, 1)1294 }1295}1296// Other test1297func TestStringMethodChaining(t *testing.T) {1298 tests := []struct {1299 input string1300 expected interface{}1301 }{1302 {`"More test".reverse.upcase`, "TSET EROM"},1303 }1304 for i, tt := range tests {1305 v := initTestVM()1306 evaluated := v.testEval(t, tt.input, getFilename())1307 VerifyExpected(t, i, evaluated, tt.expected)1308 v.checkCFP(t, i, 0)1309 v.checkSP(t, i, 1)1310 }1311}1312func TestToStringInspect(t *testing.T) {1313 tests := []struct {1314 input string1315 expected string1316 }{1317 // simple string1318 {`"a".to_s`, `a`},1319 {`"a".inspect`, `"a"`},1320 {`'a'.to_s`, `a`},1321 {`'a'.inspect`, `"a"`},1322 // double quotes1323 {`'say "hello"'.to_s`, `say "hello"`},1324 {`'say "hello"'.inspect`, `"say \"hello\""`},1325 // newline1326 {`'a\nb'.to_s`, `a\nb`},1327 {`'a\nb'.inspect`, `"a\\nb"`},1328 {`"a\nb".to_s`, "a\nb"},1329 {`"a\nb".inspect`, `"a\nb"`},1330 // newline doublequotes and backslash1331 {`'\n\"\\'.to_s`, `\n\"\\`},1332 {`'\n\"\\'.inspect`, `"\\n\\\"\\\\"`},1333 {`"\n\"\\".to_s`, "\n\"\\"},1334 {`"\n\"\\".inspect`, `"\n\"\\"`},1335 }1336 for i, tt := range tests {1337 v := initTestVM()1338 evaluated := v.testEval(t, tt.input, getFilename())1339 VerifyExpected(t, i, evaluated, tt.expected)1340 v.checkCFP(t, i, 0)1341 v.checkSP(t, i, 1)1342 }1343}1344func TestStringInspectEvaluatesToOriginalString(t *testing.T) {1345 tests := []struct {1346 input string1347 }{1348 {`'a'`},1349 {`"a"`},1350 {`'say "hello"'`},1351 {`"say \"hello\""`},1352 {`'a\nb'`},1353 {`"a\nb"`},1354 {`'a\\nb'`},1355 {`"a\\nb"`},1356 {`'\n\"\\'`},1357 {`"\n\"\\"`},1358 {`'\\n\\\"\\\\'`},1359 {`"\\n\\\"\\\\"`},1360 {`'\n\"\\'`},1361 {`"\n\"\\"`},1362 }1363 for i, tt := range tests {1364 v := initTestVM()1365 evaluated := v.testEval(t, tt.input, getFilename())1366 evaluatedInspect := v.testEval(t, fmt.Sprintf("%s.inspect", tt.input), getFilename())1367 evaluatedInspectEvaluated := v.testEval(t, evaluatedInspect.ToString(), getFilename())1368 // evaluated string equals evaluated-inspected-evaluated string1369 VerifyExpected(t, i, evaluated, evaluatedInspectEvaluated.ToString())1370 v.checkCFP(t, i, 0)1371 v.checkSP(t, i, 3)1372 }1373}1374func TestStringDupMethod(t *testing.T) {1375 tests := []struct {1376 input string1377 expected interface{}1378 }{1379 {1380 `"Stan".dup`, "Stan"},1381 {`1382s = "Stan"1383ds = "Stan".dup1384ds += " Lo"1385[s, ds]1386`, []interface{}{"Stan", "Stan Lo"}},1387 }1388 for i, tt := range tests {1389 v := initTestVM()1390 evaluated := v.testEval(t, tt.input, getFilename())1391 VerifyExpected(t, i, evaluated, tt.expected)1392 v.checkCFP(t, i, 0)1393 v.checkSP(t, i, 1)1394 }1395}...
java_lang_StackTraceElement.go
Source:java_lang_StackTraceElement.go
1package javaparser2import "github.com/timob/javabind"3type JavaLangStackTraceElementInterface interface {4 JavaLangObjectInterface5 // public java.lang.String getFileName()6 GetFileName() string7 // public int getLineNumber()8 GetLineNumber() int9 // public java.lang.String getClassName()10 GetClassName() string11 // public java.lang.String getMethodName()12 GetMethodName() string13 // public boolean isNativeMethod()14 IsNativeMethod() bool15}16type JavaLangStackTraceElement struct {17 JavaLangObject18}19// public java.lang.StackTraceElement(java.lang.String, java.lang.String, java.lang.String, int)20func NewJavaLangStackTraceElement(a string, b string, c string, d int) (*JavaLangStackTraceElement) {21 conv_a := javabind.NewGoToJavaString()22 conv_b := javabind.NewGoToJavaString()23 conv_c := javabind.NewGoToJavaString()24 if err := conv_a.Convert(a); err != nil {25 panic(err)26 }27 if err := conv_b.Convert(b); err != nil {28 panic(err)29 }30 if err := conv_c.Convert(c); err != nil {31 panic(err)32 }33 obj, err := javabind.GetEnv().NewObject("java/lang/StackTraceElement", conv_a.Value().Cast("java/lang/String"), conv_b.Value().Cast("java/lang/String"), conv_c.Value().Cast("java/lang/String"), d)34 if err != nil {35 panic(err)36 }37 conv_a.CleanUp()38 conv_b.CleanUp()39 conv_c.CleanUp()40 x := &JavaLangStackTraceElement{}41 x.Callable = &javabind.Callable{obj}42 return x43}44// public java.lang.String getFileName()45func (jbobject *JavaLangStackTraceElement) GetFileName() string {46 jret, err := jbobject.CallMethod(javabind.GetEnv(), "getFileName", "java/lang/String")47 if err != nil {48 panic(err)49 }50 retconv := javabind.NewJavaToGoString()51 dst := new(string)52 retconv.Dest(dst)53 if err := retconv.Convert(javabind.ObjectRef(jret)); err != nil {54 panic(err)55 }56 retconv.CleanUp()57 return *dst58}59// public int getLineNumber()60func (jbobject *JavaLangStackTraceElement) GetLineNumber() int {...
getFileName
Using AI Code Generation
1import "fmt"2func main(){3 fmt.Println(lang.getFileName())4}5import "fmt"6func main(){7 fmt.Println(lang.getFileName())8}9import "fmt"10func main(){11 fmt.Println(lang.getFileName())12}13func getFileName() string{14}15import "fmt"16import "lang"17func main(){18 fmt.Println(lang.getFileName())19}20import "fmt"21import "lang"22func main(){23 fmt.Println(lang.getFileName())24}25import "fmt"26import "lang"27func main(){28 fmt.Println(lang.getFileName())29}30func getFileName() string{31}32import "fmt"33import "lang"34func main(){35 fmt.Println(lang.getFileName())36}37import "fmt"38import "lang"39func main(){40 fmt.Println(lang.getFileName())41}42import "
getFileName
Using AI Code Generation
1import (2func main() {3 fmt.Println(strings.Split("a,b,c", ","))4}5import (6func main() {7 fmt.Println(strings.Split("a,b,c", ","))8}9import (10func main() {11 fmt.Println(strings.Split("a,b,c", ","))12}13import (14func main() {15 fmt.Println(strings.Split("a,b,c", ","))16}17import (18func main() {19 fmt.Println(strings.Split("a,b,c", ","))20}21import (22func main() {23 fmt.Println(strings.Split("a,b,c", ","))24}25import (26func main() {27 fmt.Println(strings.Split("a,b,c", ","))28}29import (30func main() {31 fmt.Println(strings.Split("a,b,c", ","))32}33import (34func main() {35 fmt.Println(strings.Split("a,b,c", ","))36}
getFileName
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(lang.getFileName())4}5import "os"6func getFileName() string {7}8import "os"9func getFileName() string {10}11import "fmt"12import "lang"13func main() {14 fmt.Println(lang.getFileName())15}16import "os"17func getFileName() string {18}19func getDirName() string {20}21import "fmt"22import "lang"23func main() {24 fmt.Println(lang.getFileName())25 fmt.Println(lang.getDirName())26}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!