1// SPDX-License-Identifier: MIT
2
3package expr
4
5import (
6 "context"
7 "encoding/json"
8 "errors"
9 "os"
10 "strings"
11 "testing"
12)
13
14// unportedMessage reports whether the runner's message is about something this
15// package does not read: comments, constructors ("new Date(1)"), and the
16// stray-bracket check of JContainer.
17func unportedMessage(in, msg string) bool {
18 return strings.Contains(in, "/") || strings.Contains(in, "new ") ||
19 strings.HasPrefix(msg, "Unexpected content while parsing JSON") || strings.Contains(msg, "constructor") ||
20 strings.HasPrefix(msg, "JsonToken ") && !strings.Contains(msg, "closing JsonType None")
21}
22
23// TestFromJSONErrorsMatchRunner replays testdata/fromjson_errors.json, the
24// messages of the runner's JsonReaderException for the inputs fromJSON rejects
25// (gen/runner/fromjson_errors.mjs). GitHub prints them in the job log.
26func TestFromJSONErrorsMatchRunner(t *testing.T) {
27 data, err := os.ReadFile("testdata/fromjson_errors.json")
28 if err != nil {
29 t.Fatal(err)
30 }
31 var cs []struct{ In, Msg string }
32 if err := json.Unmarshal(data, &cs); err != nil {
33 t.Fatal(err)
34 }
35 bad, skipped := 0, 0
36 for _, c := range cs {
37 _, err := FromJSON([]byte(c.In))
38 if err != nil && err.Error() == c.Msg {
39 continue
40 }
41 if unportedMessage(c.In, c.Msg) {
42 skipped++
43 continue
44 }
45 bad++
46 if bad <= 25 {
47 t.Errorf("FromJSON(%q): %v\n runner: %s", c.In, err, c.Msg)
48 }
49 }
50 t.Logf("%d cases, %d unported (comments, constructors), %d mismatches", len(cs), skipped, bad)
51 if skipped > len(cs)/10 {
52 t.Errorf("too many cases skipped: %d", skipped)
53 }
54}
55
56// The forms of the conformance corpus (expr-runtime-errors): fromJSON of
57// "not json", the same text the runner fails on.
58func TestFromJSONErrorText(t *testing.T) {
59 for _, tc := range []struct{ in, want string }{
60 {"not json", "Unexpected character encountered while parsing value: n. Path '', line 0, position 0."},
61 {"", "Error reading JToken from JsonReader. Path '', line 0, position 0."},
62 {" \n ", "Error reading JToken from JsonReader. Path '', line 2, position 1."},
63 {"[1,x]", "Unexpected character encountered while parsing value: x. Path '[0]', line 1, position 3."},
64 {`{"a":{"b":x}}`, "Unexpected character encountered while parsing value: x. Path 'a.b', line 1, position 10."},
65 {"{", "Error reading JObject from JsonReader. Path '', line 1, position 1."},
66 {"[1,2", "Unexpected end of content while loading JArray. Path '[1]', line 1, position 4."},
67 {`"abc`, "Unterminated string. Expected delimiter: \". Path '', line 1, position 4."},
68 {"tru", "Unexpected end when reading JSON. Path '', line 1, position 3."},
69 {"[nul]", "Error parsing null value. Path '', line 1, position 4."},
70 {"08", "Input string '08' is not a valid number. Path '', line 1, position 2."},
71 {"[1\n,\n x]", "Unexpected character encountered while parsing value: x. Path '[0]', line 3, position 1."},
72 } {
73 _, err := FromJSON([]byte(tc.in))
74 if err == nil || err.Error() != tc.want {
75 t.Errorf("FromJSON(%q) = %v, want %s", tc.in, err, tc.want)
76 }
77 var je *JSONError
78 if !errors.As(err, &je) || je.ExceptionClass() != "Newtonsoft.Json.JsonReaderException" {
79 t.Errorf("FromJSON(%q): not a *JSONError: %T", tc.in, err)
80 }
81 }
82}
83
84// fromJSON() fails with the reader's message, and the error unwraps to it.
85func TestFromJSONFunctionError(t *testing.T) {
86 node, err := Parse("fromJSON(env.x)", []string{"env"}, nil)
87 if err != nil {
88 t.Fatal(err)
89 }
90 _, err = Evaluate(context.Background(), node, EvalContext{Contexts: map[string]Value{"env": func() Value {
91 d := NewDictionary()
92 d.Add("x", String("not json"))
93 return d
94 }()}})
95 want := "Unexpected character encountered while parsing value: n. Path '', line 0, position 0."
96 var ee *EvalError
97 var je *JSONError
98 if !errors.As(err, &ee) || ee.Message != want || !errors.As(err, &je) {
99 t.Fatalf("%v", err)
100 }
101}