File size: 5,908 Bytes
37a34fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Targeted grammar cases, one per test, using small inline strings rather
// than real files. Each case asserts both the round-trip property (the
// non-negotiable invariant) and that the specific token kinds/values that
// matter for that case came out right.

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { tokenize } from '../src/lexer.mjs';
import { encodeTextByCodeUnit } from '../src/codec.mjs';
import { decodeText } from '../../eu4_han_convert/src/codec.js';

function assertRoundTrip(text) {
  const tokens = tokenize(text);
  assert.equal(tokens.map((t) => t.raw).join(''), text);
  return tokens;
}

function kindsOf(tokens) {
  return tokens.filter((t) => t.kind !== 'whitespace').map((t) => t.kind);
}

test('comment containing a quote character is not treated as a string start', () => {
  const text = 'monarch_names = { \t# Most Monarchs were named with "nori"\n\tname1 = 1\n}\n';
  const tokens = assertRoundTrip(text);
  const comment = tokens.find((t) => t.kind === 'comment');
  assert.ok(comment, 'expected a comment token');
  assert.equal(comment.value, ' Most Monarchs were named with "nori"');
  // The quotes inside the comment must not have started a string token that
  // swallows the rest of the file.
  const strings = tokens.filter((t) => t.kind === 'string');
  assert.equal(strings.length, 0);
  const bareValues = tokens.filter((t) => t.kind === 'bare').map((t) => t.value);
  assert.ok(bareValues.includes('name1'));
  assert.ok(bareValues.includes('1'));
});

test('a string value that itself starts with # is not a comment (Silesia.txt case)', () => {
  const text = 'leader_names = { "#LIST#|leader_names|Hoppe" }\n';
  const tokens = assertRoundTrip(text);
  const strings = tokens.filter((t) => t.kind === 'string');
  assert.equal(strings.length, 1);
  assert.equal(strings[0].value, '#LIST#|leader_names|Hoppe');
  assert.equal(strings[0].raw, '"#LIST#|leader_names|Hoppe"');
  const comments = tokens.filter((t) => t.kind === 'comment');
  assert.equal(comments.length, 0);
});

test('multiple quoted values on one line are lexed as separate string tokens', () => {
  const text = 'a = "one" b = "two" c = "three"\n';
  const tokens = assertRoundTrip(text);
  const strings = tokens.filter((t) => t.kind === 'string').map((t) => t.value);
  assert.deepEqual(strings, ['one', 'two', 'three']);
});

test('bare token values: identifiers, numbers, dates, tags', () => {
  const text = 'tag = CAS\nyear = 1444\ndate = 1444.11.11\n';
  const tokens = assertRoundTrip(text);
  const bare = tokens.filter((t) => t.kind === 'bare').map((t) => t.value);
  assert.deepEqual(bare, ['tag', 'CAS', 'year', '1444', 'date', '1444.11.11']);
});

test('$PROVINCE$-style template token inside a string is preserved verbatim', () => {
  const text = 'title = "$PROVINCE$ has been conquered"\n';
  const tokens = assertRoundTrip(text);
  const strings = tokens.filter((t) => t.kind === 'string');
  assert.equal(strings.length, 1);
  assert.equal(strings[0].value, '$PROVINCE$ has been conquered');
});

test('date key block', () => {
  const text = '1444.11.11 = {\n\towner = CAS\n}\n';
  const tokens = assertRoundTrip(text);
  const bare = tokens.filter((t) => t.kind === 'bare').map((t) => t.value);
  assert.equal(bare[0], '1444.11.11');
  assert.deepEqual(kindsOf(tokens), ['bare', 'operator', 'lbrace', 'bare', 'operator', 'bare', 'rbrace']);
});

test('duplicate keys in one scope round-trip and both appear in the token stream', () => {
  const text = 'add_core = CAS\nadd_core = ARA\n';
  const tokens = assertRoundTrip(text);
  const addCoreKeys = tokens.filter((t) => t.kind === 'bare' && t.value === 'add_core');
  assert.equal(addCoreKeys.length, 2);
});

test('duplicate date keys (e.g. multiple 1444.11.11 blocks) round-trip and both appear', () => {
  const text = '1444.11.11 = {\n\towner = CAS\n}\n1444.11.11 = {\n\treligion = catholic\n}\n';
  const tokens = assertRoundTrip(text);
  const dateKeys = tokens.filter((t) => t.kind === 'bare' && t.value === '1444.11.11');
  assert.equal(dateKeys.length, 2);
});

test('empty block { }', () => {
  const text = 'chance = { }\n';
  const tokens = assertRoundTrip(text);
  assert.deepEqual(kindsOf(tokens), ['bare', 'operator', 'lbrace', 'rbrace']);
});

test('RGB triple', () => {
  const text = 'color = { 20  50  210 }\n';
  const tokens = assertRoundTrip(text);
  const bare = tokens.filter((t) => t.kind === 'bare').map((t) => t.value);
  assert.deepEqual(bare, ['color', '20', '50', '210']);
});

test('depth-2 nesting', () => {
  const text = 'outer = {\n\tinner = {\n\t\tvalue = 1\n\t}\n}\n';
  const tokens = assertRoundTrip(text);
  const lbraces = tokens.filter((t) => t.kind === 'lbrace');
  const rbraces = tokens.filter((t) => t.kind === 'rbrace');
  assert.equal(lbraces.length, 2);
  assert.equal(rbraces.length, 2);
});

test('tab-vs-space indentation is preserved exactly in whitespace tokens', () => {
  const text = 'a = {\n\tb = 1\n}\nc = {\n  d = 2\n}\n';
  const tokens = assertRoundTrip(text);
  const whitespaceRuns = tokens.filter((t) => t.kind === 'whitespace').map((t) => t.raw);
  assert.ok(whitespaceRuns.includes('\n\t'));
  assert.ok(whitespaceRuns.includes('\n  '));
});

test('CRLF line endings round-trip', () => {
  const text = 'a = 1\r\nb = 2\r\n';
  assertRoundTrip(text);
});

test('LF line endings round-trip', () => {
  const text = 'a = 1\nb = 2\n';
  assertRoundTrip(text);
});

test('a file with no trailing newline round-trips', () => {
  const text = 'a = 1\nb = 2';
  const tokens = assertRoundTrip(text);
  assert.equal(tokens.at(-1).raw, '2');
});

test('encodeText round-trips an astral character via code-unit splitting (U+20C18 bug fix)', () => {
  const text = 'test \u{20C18} test';
  const encoded = encodeTextByCodeUnit(text, { profile: 'legacy' });
  const decoded = decodeText(encoded);
  assert.equal(decoded, text);
});