-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-regex-support.ts
More file actions
98 lines (81 loc) · 3.03 KB
/
test-regex-support.ts
File metadata and controls
98 lines (81 loc) · 3.03 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
#!/usr/bin/env bun
/**
* Test for regex pattern matching in EQL-S
*
* This test validates that regex patterns work correctly for the MATCHES operator
*/
import { EAVStore, jsonEntityFacts } from '../src/eav-engine.js';
import { DatalogEvaluator } from '../src/query/datalog-evaluator.js';
import { EQLSProcessor } from '../src/query/eqls-parser.js';
console.log('🔍 Testing regex pattern matching in EQL-S\n');
// Create test data
const testData = [
{ id: 1, family: 'Abhaya Libre', category: 'serif' },
{ id: 2, family: 'ABeeZee', category: 'sans-serif' },
{ id: 3, family: 'Montserrat', category: 'sans-serif' },
{ id: 4, family: 'Roboto', category: 'sans-serif' },
{ id: 5, family: 'Arial', category: 'sans-serif' },
{ id: 6, family: 'Times New Roman', category: 'serif' },
];
// Create EAV store
const store = new EAVStore();
for (let i = 0; i < testData.length; i++) {
const item = testData[i]!;
const facts = jsonEntityFacts(`font:${item.id}`, item, 'font');
store.addFacts(facts);
}
// Setup processor and evaluator
const evaluator = new DatalogEvaluator(store);
const processor = new EQLSProcessor();
processor.setSchema(store.getCatalog());
function runTest(description: string, query: string) {
console.log(`\n📋 Test: ${description}`);
console.log(`🔍 Query: ${query}`);
try {
const result = processor.process(query);
if (result.errors.length > 0) {
console.log('❌ Parsing failed:');
for (const error of result.errors) {
console.log(` Line ${error.line}, Column ${error.column}: ${error.message}`);
}
return;
}
const queryResult = evaluator.evaluate(result.query!);
console.log(`✅ Results (${queryResult.bindings.length} items):`);
if (queryResult.bindings.length === 0) {
console.log(' No results found.');
return;
}
for (const binding of queryResult.bindings) {
const values = Object.entries(binding)
.map(([key, value]) => `${key}: ${value}`)
.join(', ');
console.log(` ${values}`);
}
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
console.log(`❌ Error: ${message}`);
}
}
// Run tests
runTest(
'Simple font name matching with regex',
'FIND font AS ?f WHERE ?f.family MATCHES /^A/ RETURN ?f.family'
);
runTest(
'Font name matching with case-insensitive regex',
'FIND font AS ?f WHERE ?f.family MATCHES /^a/i RETURN ?f.family'
);
runTest(
'Font name matching with alternation',
'FIND font AS ?f WHERE ?f.family MATCHES /(Arial|Roboto)/ RETURN ?f.family'
);
runTest(
'Category matching with regex',
'FIND font AS ?f WHERE ?f.category MATCHES /serif/ RETURN ?f.family, ?f.category'
);
runTest(
'Combined criteria with AND',
'FIND font AS ?f WHERE ?f.family MATCHES /^A/ AND ?f.category = "serif" RETURN ?f.family, ?f.category'
);
console.log('\n✨ Regex testing complete!');