-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
147 lines (139 loc) · 4.71 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const assert = require('assert');
const { check_yaml } = require('./dist/check_yaml.js');
const { yaml_to_json_array } = require('./dist/yaml_to_json_array.js');
const { yaml_to_json_string } = require('./dist/yaml_to_json_string.js');
const { atob } = require('./src/base64.js');
function check_yaml_string(yaml) {
const message = check_yaml(new TextEncoder("utf-8").encode(yaml));
if (message) {
return new TextDecoder("utf-8").decode(message);
} else {
return null;
}
}
function yaml_to_json_binary(yaml) {
return new TextDecoder("utf-8").decode(
yaml_to_json_array(
new TextEncoder("utf-8").encode(yaml)
)
);
}
// "árvíztűrő tükörfúrógép" stored as UTF-8, prints with multi-byte characters
assert.strictEqual(
new TextDecoder("utf-8").decode(
Uint8Array.from(
atob('w6FydsOtenTFsXLFkSB0w7xrw7ZyZsO6csOzZ8OpcA=='),
c => c.charCodeAt(0)
)
),
"árvíztűrő tükörfúrógép"
);
// a simple YAML string
assert.strictEqual(check_yaml_string('{foo: 1, bar: [2, 3], john: doe}'), null);
assert.strictEqual(yaml_to_json_string('{foo: 1, bar: [2, 3], john: doe}'), '{"foo": 1,"bar": [2,3],"john": "doe"}');
// an invalid YAML string
assert.notStrictEqual(check_yaml_string('{}{}'), null);
assert.strictEqual(yaml_to_json_string('{}{}'), null);
// a YAML string with wrong encoding
assert.notStrictEqual(check_yaml_string(String.raw`"árvíztűrő \x97 türökfúrógép"`), null);
assert.strictEqual(yaml_to_json_string(String.raw`"árvíztűrő \x97 türökfúrógép"`), null);
// a complex YAML string
const yaml = String.raw`
en: Planet (Gas)
fr: Planète (Gazeuse)
ru: Планета (Газ)
ja: 惑星(ガス)
zh: 行星(气体)
# UTF8 decoding only happens in double-quoted strings,
# as per the YAML standard
decode this: "\u263A \xE2\x98\xBA"
and this as well: "\u2705 \U0001D11E"
not decoded: '\u263A \xE2\x98\xBA'
neither this: '\u2705 \U0001D11E'
`;
const json = {
"en": "Planet (Gas)",
"fr": "Planète (Gazeuse)",
"ru": "Планета (Газ)",
"ja": "惑星(ガス)",
"zh": "行星(气体)",
"decode this": "☺ ☺",
"and this as well": "✅ 𝄞",
"not decoded": "\\u263A \\xE2\\x98\\xBA",
"neither this": "\\u2705 \\U0001D11E"
};
assert.deepStrictEqual(JSON.parse(yaml_to_json_string(yaml)), json);
assert.deepStrictEqual(JSON.parse(yaml_to_json_binary(yaml)), json);
// a complex YAML string with the document start marker and non-specific tags (!ruby)
const y = `--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
id:
regrade_option: ''
points_possible: 1.0
correct_comments: ''
incorrect_comments: ''
neutral_comments: ''
correct_comments_html: ''
incorrect_comments_html: ''
neutral_comments_html: ''
question_type: multiple_choice_question
question_name: Question
name: Question
question_text: "<p>Inca used an innovative farming method to overcome difficulties
presented by living in the mountains. This technique was called:</p>"
answers:
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
id: 1000
text: terraced agriculture
html: ''
comments: ''
comments_html: ''
weight: 100.0
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
id: 2000
text: slash & burn agriculture
html: ''
comments: ''
comments_html: ''
weight: 0.0
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
id: 3000
text: chinampas farming
html: ''
comments: ''
comments_html: ''
weight: 0.0
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
id: 4000
text: crop rotation
html: ''
comments: ''
comments_html: ''
weight: 0.0
text_after_answers: ''
assessment_question_id:
`;
const j = {
"id": null,
"regrade_option": "",
"points_possible": 1.0,
"correct_comments": "",
"incorrect_comments": "",
"neutral_comments": "",
"correct_comments_html": "",
"incorrect_comments_html": "",
"neutral_comments_html": "",
"question_type": "multiple_choice_question",
"question_name": "Question",
"name": "Question",
"question_text": "<p>Inca used an innovative farming method to overcome difficulties presented by living in the mountains. This technique was called:</p>",
"answers": [
{ "id": 1000, "text": "terraced agriculture", "html": "", "comments": "", "comments_html": "", "weight": 100.0 },
{ "id": 2000, "text": "slash & burn agriculture", "html": "", "comments": "", "comments_html": "", "weight": 0.0 },
{ "id": 3000, "text": "chinampas farming", "html": "", "comments": "", "comments_html": "", "weight": 0.0 },
{ "id": 4000, "text": "crop rotation", "html": "", "comments": "", "comments_html": "", "weight": 0.0 }
],
"text_after_answers": "",
"assessment_question_id": null
};
assert.deepStrictEqual(JSON.parse(yaml_to_json_string(y)), j);
assert.deepStrictEqual(JSON.parse(yaml_to_json_binary(y)), j);