// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com). // All rights reserved. // t_dict_test.go package expr import ( "errors" "testing" "git.portale-stac.it/go-pkg/expr/kern" "git.portale-stac.it/go-pkg/expr/scan" ) func TestDictParser(t *testing.T) { section := "Dict" inputs := []inputType{ /* 1 */ {`{}`, kern.NewDict(nil), nil}, /* 2 */ {`{123}`, nil, errors.New("[1:6] expected `:`, got `}`")}, /* 3 */ {`{1:"one",2:"two",3:"three"}`, kern.NewDict(map[any]any{int64(1): "one", int64(2): "two", int64(3): "three"}), nil}, /* 4 */ {`{1:"one",2:"two",3:"three"}[3]`, "three", nil}, /* 5 */ {`#{1:"one",2:"two",3:"three"}`, int64(3), nil}, /* 6 */ {`{1:"one"} + {2:"two"}`, kern.NewDict(map[any]any{int64(1): "one", int64(2): "two"}), nil}, /* 7 */ {`2 in {1:"one", 2:"two"}`, true, nil}, /* 8 */ {`D={"a":1, "b":2}; D["a"]=9; D`, kern.NewDict(map[any]any{"a": int64(9), "b": int64(2)}), nil}, /* 9 */ {`D={"a":1, "b":2}; D["z"]=9; D`, kern.NewDict(map[any]any{"z": int64(9), "a": int64(1), "b": int64(2)}), nil}, /* 10 */ {`D={"a":1, "b":2}; D[nil]=9`, nil, errors.New(`[1:21] index/key is nil`)}, /* 11 */ {`D={"a":1, "b":2}; D["a"]`, int64(1), nil}, /* 12 */ {`m={ "a":1, //"b":2, "c":3 }`, kern.NewDict(map[any]any{"a": int64(1), "c": int64(3)}), nil}, /* 13 */ {`D={"a":1, "b":2}; D."a"`, int64(1), nil}, /* 14 */ {`D={"a":1, "b":2}; D.a`, int64(1), nil}, /* 15 */ {`D={1:"a", 2:"b", 3:"c"}; D.(1+2)`, "c", nil}, /* 16 */ {`D={"a":1, "b":2}; D.a`, int64(1), nil}, } runTestSuiteSpec(t, section, inputs, 16) // runTestSuite(t, section, inputs) } func TestDictToStringMultiLine(t *testing.T) { var good bool section := "dict-ToString-ML" want := `{ "first": 1 }` args := map[any]any{ "first": newLiteralTerm(scan.NewValueToken(0, 0, scan.SymInteger, "1", 1)), } dict := kern.NewDict(args) got := dict.ToString(kern.MultiLine) // fmt.Printf("got=%q\n", got) if good = got == want; !good { t.Errorf("ToString(MultiLine): got = %q, want %q", got, want) } if good { t.Logf("%s -- succeeded", section) } else { t.Logf("%s -- failed", section) } } func TestDictToString(t *testing.T) { var good bool section := "dict-ToString-SL" want := `{"first": 1}` args := map[any]any{ "first": newLiteralTerm(scan.NewValueToken(0, 0, scan.SymInteger, "1", 1)), } dict := kern.NewDict(args) got := dict.ToString(0) // fmt.Printf("got=%q\n", got) if good = got == want; !good { t.Errorf("ToString(0): got = %q, want %q", got, want) } if good { t.Logf("%s -- succeeded", section) } else { t.Logf("%s -- failed", section) } }