1// SPDX-License-Identifier: MIT
2//
3// Writes the golden files of TestToJSONGolden: for each input testdata/tojson/NAME.json
4// it writes NAME.golden, which is what the reference implementation
5// (@actions/expressions) prints for toJSON(fromJSON(input)), that is
6// JSON.stringify(JSON.parse(input, reviver), replacer, " ").
7//
8// node gen/golden.mjs /path/to/@actions/expressions/dist testdata/tojson
9//
10// New inputs are added by hand to that directory (and listed in INPUTS below
11// when they are generated rather than written out).
12
13import { pathToFileURL } from "node:url";
14import path from "node:path";
15import fs from "node:fs";
16
17const dist = path.resolve(process.argv[2]);
18const dir = process.argv[3];
19const { data } = await import(pathToFileURL(path.join(dist, "index.js")).href);
20
21const INPUTS = {
22 null: "null",
23 true: "true",
24 false: "false",
25 zero: "0",
26 negzero: "-0",
27 int: "42",
28 negative: "-17",
29 float: "1.5",
30 exp_small: "1e-7",
31 exp_large: "1e21",
32 exp_20: "1e20",
33 exp_pos: "1E2",
34 precision: "0.30000000000000004",
35 big_int: "12345678901234567890",
36 max_double: "1.7976931348623157e308",
37 min_double: "5e-324",
38 overflow: "1e999",
39 string_empty: '""',
40 string_plain: '"hello"',
41 string_escapes: '"quote \\" backslash \\\\ slash \\/ nl \\n cr \\r tab \\t bs \\b ff \\f"',
42 string_control: '"\\u0000\\u0001\\u001f\\u007f\\u0080"',
43 string_unicode: '"h\\u00e9llo \\u4e16\\u754c \\ud83d\\ude00 \\u2028\\u2029"',
44 string_lone_surrogate: '"\\ud800"',
45 array_empty: "[]",
46 array_flat: "[1,2,3]",
47 array_mixed: '[null,true,"a",1.5,[],{}]',
48 array_nested: "[[1,[2,[3,[]]]],[]]",
49 object_empty: "{}",
50 object_flat: '{"a":1,"b":"two","c":null}',
51 object_nested: '{"a":{"b":{"c":{"d":[1,{"e":2}]}}},"f":[]}',
52 object_order: '{"b":1,"a":2,"c":3}',
53 object_numeric_keys: '{"b":1,"10":2,"2":3,"a":4,"01":5,"1":6,"4294967294":7,"4294967295":8,"-1":9}',
54 object_case_dup: '{"a":1,"A":2,"b":3,"B":4}',
55 object_dup_key: '{"a":1,"b":2,"a":3}',
56 object_empty_key: '{"":1,"a":2}',
57 object_key_escapes: '{"quote\\"key":1,"new\\nline":2,"\\u00e9":3}',
58 github_event: '{"action":"opened","number":1,"pull_request":{"id":1,"draft":false,"labels":[{"name":"bug","color":"d73a4a"},{"name":"docs","color":null}],"head":{"ref":"feat/x","sha":"abc123"}},"repository":{"full_name":"octo/repo","private":true,"topics":[]}}',
59};
60
61fs.mkdirSync(dir, { recursive: true });
62for (const [name, text] of Object.entries(INPUTS)) {
63 fs.writeFileSync(path.join(dir, name + ".json"), text + "\n");
64}
65for (const f of fs.readdirSync(dir).filter(f => f.endsWith(".json")).sort()) {
66 const text = fs.readFileSync(path.join(dir, f), "utf8");
67 const value = JSON.parse(text, data.reviver);
68 fs.writeFileSync(path.join(dir, f.replace(/\.json$/, ".golden")), JSON.stringify(value, data.replacer, " "));
69}