This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrules-test.js
More file actions
152 lines (126 loc) · 4.38 KB
/
rules-test.js
File metadata and controls
152 lines (126 loc) · 4.38 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
'use strict'
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const SRL = require('../')
function testRules() {
const rulesDir = path.resolve(__dirname, './rules')
const files = fs.readdirSync(rulesDir)
files.forEach((file) => {
// Ignore
if (path.extname(file) !== '.rule') {
return
}
it(file.slice(0, -5).split('_').join(' '), () => {
const lines = fs.readFileSync(path.join(rulesDir, file), { encoding: 'utf-8' }).split('\n')
runAssertions(buildData(lines))
})
})
}
function applySpecialChars(target) {
return target.replace('\\n', '\n').replace('\\t', '\t')
}
function getExpression(srl, query) {
return `\n\nSupplied SRL Query: ${srl}\nGenerated Expression: ${query.getRawRegex()}\n\n`
}
function buildData(lines) {
const data = {
srl: null,
matches: [],
no_matches: [],
captures: {}
}
let inCapture = false
lines.forEach((line) => {
if (line === '' || line.startsWith('#')) {
return
}
if (inCapture && !line.startsWith('-')) {
inCapture = false
}
if (line.startsWith('srl: ')) {
data.srl = line.substr(5)
} else if (line.startsWith('match: "')) {
data.matches.push(applySpecialChars(line.slice(8, -1)))
} else if (line.startsWith('no match: "')) {
data.no_matches.push(applySpecialChars(line.slice(11, -1)))
} else if (
line.startsWith('capture for "') &&
line.substr(-2, 2) === '":'
) {
inCapture = line.slice(13, -2)
data.captures[inCapture] = []
} else if (inCapture && line.startsWith('-')) {
const split = line.substr(1).split(': ')
let target = data.captures[inCapture][Number(split[0])]
if (!target) {
target = data.captures[inCapture][Number(split[0])] = []
}
target[split[1]] = applySpecialChars(split[2].slice(1, -1))
}
})
return data
}
function runAssertions(data) {
assert(data.srl, 'SRL for rule is empty. Invalid rule.')
let query, assertionMade = false
try {
query = new SRL(data.srl)
} catch (e) {
assert(false, `Parser error: ${e.message}\n\nSupplied SRL Query: ${data.srl}\n\n`)
}
data.matches.forEach((match) => {
assert(
query.isMatching(match),
`Failed asserting that this query matches '${match}'.${getExpression(data.srl, query)}`
)
assertionMade = true
})
data.no_matches.forEach((noMatch) => {
assert(
!query.isMatching(noMatch),
`Failed asserting that this query does not match '${noMatch}'.${getExpression(data.srl, query)}`
)
assertionMade = true
})
Object.keys(data.captures).forEach((test) => {
const expected = data.captures[test]
let matches = null
try {
matches = query.getMatches(test)
} catch (e) {
assert(false, `Parser error: ${e.message}${getExpression(data.srl, query)}`)
}
assert.equal(
matches.length,
expected.length,
`Invalid match count for test ${test}.${getExpression(data.srl, query)}`
)
matches.forEach((capture, index) => {
// const result = Array.from(capture).slice(1).map((item) => {
// return item === undefined ? '' : item
// })
const item = expected[index]
for (const key in item) {
if (typeof key === 'number') {
assert.equal(
capture[key + 1],
item[key],
`The capture group did not return the expected results for test ${test}.${getExpression(data.srl, query)}`
)
} else {
assert.equal(
capture[key],
item[key],
`The capture group did not return the expected results for test ${test}.${getExpression(data.srl, query)}`
)
}
}
})
assertionMade = true
})
assert(assertionMade, `No assertion. Invalid rule. ${getExpression(data.srl, query)}`)
}
describe('Rules', () => {
testRules()
})