diff --git a/packages/encoding.json_test.go b/packages/encoding.json_test.go index e5f25098..d2ca80c2 100644 --- a/packages/encoding.json_test.go +++ b/packages/encoding.json_test.go @@ -9,10 +9,10 @@ func TestJson(t *testing.T) { os.Setenv("ANKO_DEBUG", "1") var toByteSlice = func(s string) []byte { return []byte(s) } tests := []testStruct{ - {script: "json = import(\"encoding/json\"); a = {\"b\": \"b\"}; c, err = json.Marshal(a); err", output: map[string]interface{}{"a": map[string]interface{}{"b": "b"}, "c": []byte("{\"b\":\"b\"}")}}, - {script: "json = import(\"encoding/json\"); b = 1; err = json.Unmarshal(a, &b); err", input: map[string]interface{}{"a": []byte("{\"b\": \"b\"}")}, output: map[string]interface{}{"a": []byte("{\"b\": \"b\"}"), "b": map[string]interface{}{"b": "b"}}}, - {script: "json = import(\"encoding/json\"); b = 1; err = json.Unmarshal(toByteSlice(a), &b); err", input: map[string]interface{}{"a": "{\"b\": \"b\"}", "toByteSlice": toByteSlice}, output: map[string]interface{}{"a": "{\"b\": \"b\"}", "b": map[string]interface{}{"b": "b"}}}, - {script: "json = import(\"encoding/json\"); b = 1; err = json.Unmarshal(toByteSlice(a), &b); err", input: map[string]interface{}{"a": "[[\"1\", \"2\"],[\"3\", \"4\"]]", "toByteSlice": toByteSlice}, output: map[string]interface{}{"a": "[[\"1\", \"2\"],[\"3\", \"4\"]]", "b": []interface{}{[]interface{}{"1", "2"}, []interface{}{"3", "4"}}}}, + {script: `json = import("encoding/json"); a = {"b": "b"}; c, err = json.Marshal(a); err`, output: map[string]interface{}{"a": map[string]interface{}{"b": "b"}, "c": []byte("{\"b\":\"b\"}")}}, + {script: `json = import("encoding/json"); b = 1; err = json.Unmarshal(a, &b); err`, input: map[string]interface{}{"a": []byte("{\"b\": \"b\"}")}, output: map[string]interface{}{"a": []byte("{\"b\": \"b\"}"), "b": map[string]interface{}{"b": "b"}}}, + {script: `json = import("encoding/json"); b = 1; err = json.Unmarshal(toByteSlice(a), &b); err`, input: map[string]interface{}{"a": "{\"b\": \"b\"}", "toByteSlice": toByteSlice}, output: map[string]interface{}{"a": "{\"b\": \"b\"}", "b": map[string]interface{}{"b": "b"}}}, + {script: `json = import("encoding/json"); b = 1; err = json.Unmarshal(toByteSlice(a), &b); err`, input: map[string]interface{}{"a": "[[\"1\", \"2\"],[\"3\", \"4\"]]", "toByteSlice": toByteSlice}, output: map[string]interface{}{"a": "[[\"1\", \"2\"],[\"3\", \"4\"]]", "b": []interface{}{[]interface{}{"1", "2"}, []interface{}{"3", "4"}}}}, } runTests(t, tests) } diff --git a/packages/packages_test.go b/packages/packages_test.go index 3b19dbae..c335f6e8 100644 --- a/packages/packages_test.go +++ b/packages/packages_test.go @@ -22,7 +22,7 @@ type testStruct struct { func TestDefineImport(t *testing.T) { env := vm.NewEnv() DefineImport(env) - value, err := env.Execute("strings = import(\"strings\"); strings.ToLower(\"TEST\")") + value, err := env.Execute(`strings = import("strings"); strings.ToLower("TEST")`) if err != nil { t.Errorf("Execute error - received: %v - expected: %v", err, nil) } @@ -35,7 +35,7 @@ func TestDefineImportPackageNotFound(t *testing.T) { os.Unsetenv("ANKO_DEBUG") env := vm.NewEnv() DefineImport(env) - value, err := env.Execute("a = import(\"a\")") + value, err := env.Execute(`a = import("a")`) expectedError := ("package 'a' not found") if err == nil { t.Errorf("DefineImportPackageNotFound error - received: %v - expected: %v", err, expectedError) diff --git a/packages/regexp_test.go b/packages/regexp_test.go index 07661f88..f64be6d0 100644 --- a/packages/regexp_test.go +++ b/packages/regexp_test.go @@ -8,25 +8,25 @@ import ( func TestRegexp(t *testing.T) { os.Setenv("ANKO_DEBUG", "1") tests := []testStruct{ - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^simple$\"); re.MatchString(\"simple\")", runOutput: true}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^simple$\"); re.MatchString(\"no match\")", runOutput: false}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^simple$"); re.MatchString("simple")`, runOutput: true}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^simple$"); re.MatchString("no match")`, runOutput: false}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^simple$", "b": "simple"}, runOutput: true, output: map[string]interface{}{"a": "^simple$", "b": "simple"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^simple$", "b": "no match"}, runOutput: false, output: map[string]interface{}{"a": "^simple$", "b": "no match"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^simple$", "b": "simple"}, runOutput: true, output: map[string]interface{}{"a": "^simple$", "b": "simple"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^simple$", "b": "no match"}, runOutput: false, output: map[string]interface{}{"a": "^simple$", "b": "no match"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^a\\\\.\\\\d+\\\\.b$\"); re.String()", runOutput: "^a\\.\\d+\\.b$"}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^a\\\\.\\\\d+\\\\.b$\"); re.MatchString(\"a.1.b\")", runOutput: true}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^a\\\\.\\\\d+\\\\.b$\"); re.MatchString(\"a.22.b\")", runOutput: true}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^a\\\\.\\\\d+\\\\.b$\"); re.MatchString(\"a.333.b\")", runOutput: true}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^a\\\\.\\\\d+\\\\.b$\"); re.MatchString(\"no match\")", runOutput: false}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(\"^a\\\\.\\\\d+\\\\.b$\"); re.MatchString(\"a+1+b\")", runOutput: false}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^a\\.\\d+\\.b$"); re.String()`, runOutput: "^a\\.\\d+\\.b$"}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^a\\.\\d+\\.b$"); re.MatchString("a.1.b")`, runOutput: true}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^a\\.\\d+\\.b$"); re.MatchString("a.22.b")`, runOutput: true}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^a\\.\\d+\\.b$"); re.MatchString("a.333.b")`, runOutput: true}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^a\\.\\d+\\.b$"); re.MatchString("no match")`, runOutput: false}, + {script: `regexp = import("regexp"); re = regexp.MustCompile("^a\\.\\d+\\.b$"); re.MatchString("a+1+b")`, runOutput: false}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.String()", input: map[string]interface{}{"a": "^a\\.\\d+\\.b$"}, runOutput: "^a\\.\\d+\\.b$", output: map[string]interface{}{"a": "^a\\.\\d+\\.b$"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.1.b"}, runOutput: true, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.1.b"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.22.b"}, runOutput: true, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.22.b"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.333.b"}, runOutput: true, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.333.b"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "no match"}, runOutput: false, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "no match"}}, - {script: "regexp = import(\"regexp\"); re = regexp.MustCompile(a); re.MatchString(b)", input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a+1+b"}, runOutput: false, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a+1+b"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.String()`, input: map[string]interface{}{"a": "^a\\.\\d+\\.b$"}, runOutput: "^a\\.\\d+\\.b$", output: map[string]interface{}{"a": "^a\\.\\d+\\.b$"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.1.b"}, runOutput: true, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.1.b"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.22.b"}, runOutput: true, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.22.b"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.333.b"}, runOutput: true, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a.333.b"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "no match"}, runOutput: false, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "no match"}}, + {script: `regexp = import("regexp"); re = regexp.MustCompile(a); re.MatchString(b)`, input: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a+1+b"}, runOutput: false, output: map[string]interface{}{"a": "^a\\.\\d+\\.b$", "b": "a+1+b"}}, } runTests(t, tests) } diff --git a/packages/sort_test.go b/packages/sort_test.go index 16ef1cda..e7cd7cbe 100644 --- a/packages/sort_test.go +++ b/packages/sort_test.go @@ -8,9 +8,9 @@ import ( func TestSort(t *testing.T) { os.Setenv("ANKO_DEBUG", "1") tests := []testStruct{ - {script: "sort = import(\"sort\"); a = make([]int); a += [5, 3, 1, 4, 2]; sort.Ints(a); a", runOutput: []int{1, 2, 3, 4, 5}, output: map[string]interface{}{"a": []int{1, 2, 3, 4, 5}}}, - {script: "sort = import(\"sort\"); a = make([]float64); a += [5.5, 3.3, 1.1, 4.4, 2.2]; sort.Float64s(a); a", runOutput: []float64{1.1, 2.2, 3.3, 4.4, 5.5}, output: map[string]interface{}{"a": []float64{1.1, 2.2, 3.3, 4.4, 5.5}}}, - {script: "sort = import(\"sort\"); a = make([]string); a += [\"e\", \"c\", \"a\", \"d\", \"b\"]; sort.Strings(a); a", runOutput: []string{"a", "b", "c", "d", "e"}, output: map[string]interface{}{"a": []string{"a", "b", "c", "d", "e"}}}, + {script: `sort = import("sort"); a = make([]int); a += [5, 3, 1, 4, 2]; sort.Ints(a); a`, runOutput: []int{1, 2, 3, 4, 5}, output: map[string]interface{}{"a": []int{1, 2, 3, 4, 5}}}, + {script: `sort = import("sort"); a = make([]float64); a += [5.5, 3.3, 1.1, 4.4, 2.2]; sort.Float64s(a); a`, runOutput: []float64{1.1, 2.2, 3.3, 4.4, 5.5}, output: map[string]interface{}{"a": []float64{1.1, 2.2, 3.3, 4.4, 5.5}}}, + {script: `sort = import("sort"); a = make([]string); a += ["e", "c", "a", "d", "b"]; sort.Strings(a); a`, runOutput: []string{"a", "b", "c", "d", "e"}, output: map[string]interface{}{"a": []string{"a", "b", "c", "d", "e"}}}, {script: ` sort = import("sort") a = [5, 1.1, 3, "f", "2", "4.4"] diff --git a/packages/strconv_test.go b/packages/strconv_test.go index 7c1df964..06b267ac 100644 --- a/packages/strconv_test.go +++ b/packages/strconv_test.go @@ -21,20 +21,20 @@ func TestStrconv(t *testing.T) { return fmt.Sprint(v) } tests := []testStruct{ - {script: "strconv = import(\"strconv\"); a = true; b = strconv.FormatBool(a)", runOutput: "true", output: map[string]interface{}{"a": true, "b": "true"}}, - {script: "strconv = import(\"strconv\"); a = 1.1; b = strconv.FormatFloat(a, toRune(\"f\"), -1, 64)", input: map[string]interface{}{"toRune": toRune}, runOutput: "1.1", output: map[string]interface{}{"a": float64(1.1), "b": "1.1"}}, - {script: "strconv = import(\"strconv\"); a = 1; b = strconv.FormatInt(a, 10)", runOutput: "1", output: map[string]interface{}{"a": int64(1), "b": "1"}}, - {script: "strconv = import(\"strconv\"); b = strconv.FormatInt(a, 10)", input: map[string]interface{}{"a": uint64(1)}, runOutput: "1", output: map[string]interface{}{"a": uint64(1), "b": "1"}}, + {script: `strconv = import("strconv"); a = true; b = strconv.FormatBool(a)`, runOutput: "true", output: map[string]interface{}{"a": true, "b": "true"}}, + {script: `strconv = import("strconv"); a = 1.1; b = strconv.FormatFloat(a, toRune("f"), -1, 64)`, input: map[string]interface{}{"toRune": toRune}, runOutput: "1.1", output: map[string]interface{}{"a": float64(1.1), "b": "1.1"}}, + {script: `strconv = import("strconv"); a = 1; b = strconv.FormatInt(a, 10)`, runOutput: "1", output: map[string]interface{}{"a": int64(1), "b": "1"}}, + {script: `strconv = import("strconv"); b = strconv.FormatInt(a, 10)`, input: map[string]interface{}{"a": uint64(1)}, runOutput: "1", output: map[string]interface{}{"a": uint64(1), "b": "1"}}, - {script: "strconv = import(\"strconv\"); a = \"true\"; b, err = strconv.ParseBool(a); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "true", "b": true, "err": ""}}, - {script: "strconv = import(\"strconv\"); a = \"2\"; b, err = strconv.ParseBool(a); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "strconv.ParseBool: parsing \"2\": invalid syntax", output: map[string]interface{}{"a": "2", "b": false, "err": "strconv.ParseBool: parsing \"2\": invalid syntax"}}, - {script: "strconv = import(\"strconv\"); a = \"1.1\"; b, err = strconv.ParseFloat(a, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "1.1", "b": float64(1.1), "err": ""}}, - {script: "strconv = import(\"strconv\"); a = \"a\"; b, err = strconv.ParseFloat(a, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "strconv.ParseFloat: parsing \"a\": invalid syntax", output: map[string]interface{}{"a": "a", "b": float64(0), "err": "strconv.ParseFloat: parsing \"a\": invalid syntax"}}, - {script: "strconv = import(\"strconv\"); a = \"1\"; b, err = strconv.ParseInt(a, 10, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "1", "b": int64(1), "err": ""}}, - {script: "strconv = import(\"strconv\"); a = \"1.1\"; b, err = strconv.ParseInt(a, 10, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "strconv.ParseInt: parsing \"1.1\": invalid syntax", output: map[string]interface{}{"a": "1.1", "b": int64(0), "err": "strconv.ParseInt: parsing \"1.1\": invalid syntax"}}, - {script: "strconv = import(\"strconv\"); a = \"a\"; b, err = strconv.ParseInt(a, 10, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "strconv.ParseInt: parsing \"a\": invalid syntax", output: map[string]interface{}{"a": "a", "b": int64(0), "err": "strconv.ParseInt: parsing \"a\": invalid syntax"}}, - {script: "strconv = import(\"strconv\"); a = \"1\"; b, err = strconv.ParseUint(a, 10, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "1", "b": uint64(1), "err": ""}}, - {script: "strconv = import(\"strconv\"); a = \"a\"; b, err = strconv.ParseUint(a, 10, 64); err = toString(err)", input: map[string]interface{}{"toString": toString}, runOutput: "strconv.ParseUint: parsing \"a\": invalid syntax", output: map[string]interface{}{"a": "a", "b": uint64(0), "err": "strconv.ParseUint: parsing \"a\": invalid syntax"}}, + {script: `strconv = import("strconv"); a = "true"; b, err = strconv.ParseBool(a); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "true", "b": true, "err": ""}}, + {script: `strconv = import("strconv"); a = "2"; b, err = strconv.ParseBool(a); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: `strconv.ParseBool: parsing "2": invalid syntax`, output: map[string]interface{}{"a": "2", "b": false, "err": `strconv.ParseBool: parsing "2": invalid syntax`}}, + {script: `strconv = import("strconv"); a = "1.1"; b, err = strconv.ParseFloat(a, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "1.1", "b": float64(1.1), "err": ""}}, + {script: `strconv = import("strconv"); a = "a"; b, err = strconv.ParseFloat(a, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: `strconv.ParseFloat: parsing "a": invalid syntax`, output: map[string]interface{}{"a": "a", "b": float64(0), "err": `strconv.ParseFloat: parsing "a": invalid syntax`}}, + {script: `strconv = import("strconv"); a = "1"; b, err = strconv.ParseInt(a, 10, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "1", "b": int64(1), "err": ""}}, + {script: `strconv = import("strconv"); a = "1.1"; b, err = strconv.ParseInt(a, 10, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: `strconv.ParseInt: parsing "1.1": invalid syntax`, output: map[string]interface{}{"a": "1.1", "b": int64(0), "err": `strconv.ParseInt: parsing "1.1": invalid syntax`}}, + {script: `strconv = import("strconv"); a = "a"; b, err = strconv.ParseInt(a, 10, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: `strconv.ParseInt: parsing "a": invalid syntax`, output: map[string]interface{}{"a": "a", "b": int64(0), "err": `strconv.ParseInt: parsing "a": invalid syntax`}}, + {script: `strconv = import("strconv"); a = "1"; b, err = strconv.ParseUint(a, 10, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: "", output: map[string]interface{}{"a": "1", "b": uint64(1), "err": ""}}, + {script: `strconv = import("strconv"); a = "a"; b, err = strconv.ParseUint(a, 10, 64); err = toString(err)`, input: map[string]interface{}{"toString": toString}, runOutput: `strconv.ParseUint: parsing "a": invalid syntax`, output: map[string]interface{}{"a": "a", "b": uint64(0), "err": `strconv.ParseUint: parsing "a": invalid syntax`}}, } runTests(t, tests) } diff --git a/packages/strings_test.go b/packages/strings_test.go index 85b27520..592b7b42 100644 --- a/packages/strings_test.go +++ b/packages/strings_test.go @@ -8,9 +8,9 @@ import ( func TestStrings(t *testing.T) { os.Setenv("ANKO_DEBUG", "1") tests := []testStruct{ - {script: "strings = import(\"strings\"); a = \" one two \"; b = strings.TrimSpace(a)", runOutput: "one two", output: map[string]interface{}{"a": " one two ", "b": "one two"}}, - {script: "strings = import(\"strings\"); a = \"a b c d\"; b = strings.Split(a, \" \")", runOutput: []string{"a", "b", "c", "d"}, output: map[string]interface{}{"a": "a b c d", "b": []string{"a", "b", "c", "d"}}}, - {script: "strings = import(\"strings\"); a = \"a b c d\"; b = strings.SplitN(a, \" \", 3)", runOutput: []string{"a", "b", "c d"}, output: map[string]interface{}{"a": "a b c d", "b": []string{"a", "b", "c d"}}}, + {script: `strings = import("strings"); a = " one two "; b = strings.TrimSpace(a)`, runOutput: "one two", output: map[string]interface{}{"a": " one two ", "b": "one two"}}, + {script: `strings = import("strings"); a = "a b c d"; b = strings.Split(a, " ")`, runOutput: []string{"a", "b", "c", "d"}, output: map[string]interface{}{"a": "a b c d", "b": []string{"a", "b", "c", "d"}}}, + {script: `strings = import("strings"); a = "a b c d"; b = strings.SplitN(a, " ", 3)`, runOutput: []string{"a", "b", "c d"}, output: map[string]interface{}{"a": "a b c d", "b": []string{"a", "b", "c d"}}}, } runTests(t, tests) }