main 98594d0

fromjson_errors_test.go RawHistoryFolder

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}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 3

↑↓ moveenter openesc close