main 98594d0

fromjson_runner_test.go RawHistoryFolder

1// SPDX-License-Identifier: MIT
2
3package expr
4
5import (
6	"encoding/json"
7	"os"
8	"strings"
9	"testing"
10)
11
12type fromJSONCase struct {
13	In  string `json:"in"`
14	OK  bool   `json:"ok"`
15	Out string `json:"out"`
16}
17
18func loadFromJSONCases(tb testing.TB) []fromJSONCase {
19	tb.Helper()
20	data, err := os.ReadFile("testdata/fromjson_cases.json")
21	if err != nil {
22		tb.Fatal(err)
23	}
24	var cs []fromJSONCase
25	if err := json.Unmarshal(data, &cs); err != nil {
26		tb.Fatal(err)
27	}
28	return cs
29}
30
31// unportedJSON reports whether the input uses something of Json.NET that is not
32// ported: comments and constructors. The runner accepts them, this package
33// fails on them (see PORTING.md).
34func unportedJSON(s string) bool {
35	return strings.Contains(s, "/*") || strings.Contains(s, "//") || strings.Contains(s, "new ") || strings.Contains(s, "/")
36}
37
38// TestFromJSONMatchesRunner replays testdata/fromjson_cases.json, the answers
39// of the runner's fromJSON (gen/runner/fromjson_cases.mjs) on hand-picked and
40// mutated JSON-like texts: it must accept exactly the same texts and give the
41// same toJSON, except where the text could hold a comment or constructor.
42func TestFromJSONMatchesRunner(t *testing.T) {
43	cases := loadFromJSONCases(t)
44	bad, skipped := 0, 0
45	for _, c := range cases {
46		v, err := FromJSON([]byte(c.In))
47		if err == nil && strings.TrimFunc(c.In, isNetSpace) == "" {
48			err = os.ErrInvalid // fromJSON() rejects blank input before reading
49		}
50		gotOK := err == nil
51		if gotOK != c.OK || (gotOK && ToJSON(v) != c.Out) {
52			if unportedJSON(c.In) {
53				skipped++
54				continue
55			}
56			bad++
57			if bad <= 25 {
58				t.Errorf("FromJSON(%q): ok=%v out=%q (%v); runner ok=%v out=%q", c.In, gotOK, func() string {
59					if v != nil {
60						return ToJSON(v)
61					}
62					return ""
63				}(), err, c.OK, c.Out)
64			}
65		}
66	}
67	t.Logf("%d cases, %d with comments, slashes or constructors skipped, %d mismatches", len(cases), skipped, bad)
68}

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