-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-declaration-patterns.js
More file actions
206 lines (192 loc) Β· 7.09 KB
/
test-declaration-patterns.js
File metadata and controls
206 lines (192 loc) Β· 7.09 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Test tokenizer handling of declaration patterns discovered in ANTLR work
*/
const { ClarionTokenizer } = require('./out/server/src/ClarionTokenizer');
// Test cases based on ANTLR discoveries
const testCases = [
{
name: "1. Soft keywords as field names (FONT, FAMILY, SIZE, COLOR, PATTERN)",
code: `StyleQueueType QUEUE,TYPE
Font GROUP,Name('Font')
FontName STRING(100)
Family STRING(100)
Size LONG
Color STRING(7)
END
Interior GROUP
Color STRING(7)
Pattern STRING(100)
END
END`,
checks: [
{ desc: "Font as GROUP name", tokenValue: "Font", shouldNotBeType: "Structure" },
{ desc: "Family as field name", tokenValue: "Family", shouldNotBeType: "Property" },
{ desc: "Size as field name", tokenValue: "Size", shouldNotBeType: "Property" },
{ desc: "Color as field name", tokenValue: "Color", shouldNotBeType: "Property" },
{ desc: "Pattern as field name", tokenValue: "Pattern" }
]
},
{
name: "2. Anonymous/unnamed fields (padding)",
code: `MyGroup GROUP
STRING('\\')
ActualField LONG
END`,
checks: [
{ desc: "Should tokenize unnamed STRING field", tokenValue: "STRING", shouldBeType: "Type" }
]
},
{
name: "3. Reference variables with structure types",
code: `QRef &QUEUE
GRef &GROUP
FRef &FILE
CustomRef &MyQueueType`,
checks: [
{ desc: "&QUEUE reference", tokenValue: "&", shouldHaveNext: "QUEUE" },
{ desc: "&GROUP reference", tokenValue: "&", shouldHaveNext: "GROUP" },
{ desc: "&FILE reference", tokenValue: "&", shouldHaveNext: "FILE" }
]
},
{
name: "4. Optional comma before attributes",
code: `power1 LONG,AUTO
power2 LONG AUTO`,
checks: [
{ desc: "With comma", tokenValue: "AUTO", afterValue: "power1" },
{ desc: "Without comma", tokenValue: "AUTO", afterValue: "power2" }
]
},
{
name: "5. GROUP/QUEUE pre-initialization",
code: `g GROUP(sourceGroup)
q QUEUE(sourceQueue)
MyQueue QUEUE,TYPE
instance QUEUE(MyQueue)`,
checks: [
{ desc: "GROUP with source", tokenValue: "GROUP" },
{ desc: "QUEUE with source", tokenValue: "QUEUE" },
{ desc: "QUEUE TYPE definition", tokenValue: "TYPE" }
]
},
{
name: "6. Nested structure references with DOT",
code: `myVar.inner.field = 5
SELF.memberVar.subField = 10`,
checks: [
{ desc: "Multi-level dot notation", tokenValue: "myVar" }
]
},
{
name: "7. EQUATE declarations without values (ITEMIZE)",
code: `ITEMIZE
First EQUATE
Second EQUATE
Third EQUATE(100)
END`,
checks: [
{ desc: "ITEMIZE structure", tokenValue: "ITEMIZE", shouldBeType: "Structure" },
{ desc: "EQUATE without value", tokenValue: "EQUATE", line: 1 },
{ desc: "EQUATE with value", tokenValue: "EQUATE", line: 3 }
]
},
{
name: "8. One-line structure declarations with DOT",
code: `EmptyGroup GROUP.
EmptyQueue QUEUE,TYPE.`,
checks: [
{ desc: "GROUP with DOT terminator", tokenValue: "GROUP" },
{ desc: "QUEUE with DOT terminator", tokenValue: "QUEUE" },
{ desc: "DOT as terminator", tokenValue: "." }
]
},
{
name: "9. CLASS instantiation patterns",
code: `st StringTheory()
loc:class StringTheory()
myClass CLASS(BaseClass)`,
checks: [
{ desc: "Direct instantiation", tokenValue: "StringTheory" },
{ desc: "CLASS with inheritance", tokenValue: "CLASS", shouldBeType: "Structure" }
]
},
{
name: "10. Hybrid IF statement (inline + block)",
code: `IF ~hDC THEN message('failed')
WineventExtendedErr = ds_WinError()
ds_ErrorSet(err)
END`,
checks: [
{ desc: "IF keyword", tokenValue: "IF", shouldBeType: "Structure" },
{ desc: "THEN keyword", tokenValue: "THEN", shouldBeType: "Keyword" }
]
}
];
// Run tests
console.log('π§ͺ Testing Declaration Patterns from ANTLR Analysis\n');
console.log('='.repeat(70) + '\n');
let totalTests = 0;
let passedTests = 0;
let failedTests = 0;
testCases.forEach((testCase, idx) => {
console.log(`\n${idx + 1}. ${testCase.name}`);
console.log('-'.repeat(70));
const tokenizer = new ClarionTokenizer(testCase.code);
const tokens = tokenizer.tokenize();
console.log(` Code: ${testCase.code.split('\n')[0]}...`);
console.log(` Tokens generated: ${tokens.length}`);
if (testCase.checks) {
testCase.checks.forEach(check => {
totalTests++;
const matchingTokens = tokens.filter(t =>
t.value === check.tokenValue ||
t.value.toUpperCase() === check.tokenValue.toUpperCase()
);
if (matchingTokens.length === 0) {
console.log(` β ${check.desc}: Token '${check.tokenValue}' not found`);
failedTests++;
return;
}
const token = matchingTokens[0];
// Check type if specified
if (check.shouldBeType) {
if (token.type === check.shouldBeType) {
console.log(` β
${check.desc}: ${token.type}`);
passedTests++;
} else {
console.log(` β ${check.desc}: Expected ${check.shouldBeType}, got ${token.type}`);
failedTests++;
}
} else if (check.shouldNotBeType) {
if (token.type !== check.shouldNotBeType) {
console.log(` β
${check.desc}: ${token.type} (not ${check.shouldNotBeType})`);
passedTests++;
} else {
console.log(` β ${check.desc}: Should NOT be ${check.shouldNotBeType}`);
failedTests++;
}
} else if (check.shouldHaveNext) {
const tokenIdx = tokens.indexOf(token);
const nextToken = tokens[tokenIdx + 1];
if (nextToken && (nextToken.value === check.shouldHaveNext ||
nextToken.value.toUpperCase() === check.shouldHaveNext.toUpperCase())) {
console.log(` β
${check.desc}: Next token is ${nextToken.value}`);
passedTests++;
} else {
console.log(` β ${check.desc}: Next token should be ${check.shouldHaveNext}, got ${nextToken?.value || 'none'}`);
failedTests++;
}
} else {
// Just check it exists
console.log(` β
${check.desc}: Found`);
passedTests++;
}
});
}
});
console.log('\n' + '='.repeat(70));
console.log(`\nπ Results: ${passedTests}/${totalTests} passed, ${failedTests} failed`);
console.log(` Pass rate: ${((passedTests/totalTests)*100).toFixed(1)}%\n`);
if (failedTests > 0) {
process.exit(1);
}