Skip to content

Commit e271723

Browse files
committed
Refine cache in interpreter, and add more test cases
1 parent 493b4a5 commit e271723

File tree

6 files changed

+119
-2
lines changed

6 files changed

+119
-2
lines changed

lib/Builder.js

+15
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,21 @@ class Builder {
621621
return this
622622
}
623623

624+
/**
625+
* @return {Builder}
626+
*/
627+
clone() {
628+
const clone = new Builder()
629+
630+
// Copy deeply
631+
clone._regEx = Array.from(this._regEx)
632+
clone._modifiers = this._modifiers
633+
clone._lastMethodType = this._lastMethodType
634+
clone._group = this._group
635+
636+
return clone
637+
}
638+
624639
/**********************************************************/
625640
/* REGEX METHODS */
626641
/**********************************************************/

lib/Language/Helpers/buildQuery.js

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ function buildQuery(query, builder = new Builder()) {
3838
query.splice(i + 1, 1)
3939
}
4040

41-
// console.log(parameters)
4241
try {
4342
// Now, append that method to the builder object.
4443
method.setParameters(parameters).callMethodOn(builder)

lib/Language/Interpreter.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const Builder = require('../Builder')
34
const Cache = require('./Helpers/Cache')
45
const Literally = require('./Helpers/Literally')
56
const parseParentheses = require('./Helpers/parseParentheses')
@@ -15,7 +16,7 @@ class Interpreter {
1516
const rawQuery = this.rawQuery = query.trim().replace(/\s*;$/, '')
1617

1718
if (Cache.has(rawQuery)) {
18-
this.builder = Cache.get(rawQuery)
19+
this.builder = Cache.get(rawQuery).clone()
1920
} else {
2021
this.build()
2122
}

test/builder-test.js

+42
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ describe('Builder Test', () => {
7676
assert.equal(matches[1], 'green')
7777
})
7878

79+
it('More Methods', () => {
80+
const regex = new SRL()
81+
.noWhitespace()
82+
.literally('a')
83+
.ifFollowedBy((builder) => {
84+
return builder.noCharacter()
85+
})
86+
.tab()
87+
.mustEnd()
88+
.multiLine()
89+
.get()
90+
const target = `
91+
ba\t
92+
aaabbb
93+
`
94+
assert.ok(regex.test(target))
95+
96+
const regex2 = new SRL()
97+
.startsWith()
98+
.literally('a')
99+
.newLine()
100+
.whitespace()
101+
.onceOrMore()
102+
.literally('b')
103+
.mustEnd()
104+
.get()
105+
const target2 = `a
106+
b`
107+
assert.ok(regex2.test(target2))
108+
})
109+
79110
it('Replace', () => {
80111
const regex = new SRL()
81112
.capture((query) => {
@@ -107,6 +138,17 @@ describe('Builder Test', () => {
107138
const matches = ',, '.match(regex)
108139
assert.equal(matches[1], ',,')
109140
assert.notEqual(matches[1], ',, ')
141+
142+
const regex2 = new SRL()
143+
.literally(',')
144+
.atLeast(1)
145+
.lazy()
146+
.get()
147+
148+
const matches2 = regex2.exec(',,,,,')
149+
assert.equal(matches2[0], ',')
150+
assert.notEqual(matches2[0], ',,,,,')
151+
110152
})
111153

112154
it('Global', () => {

test/cache-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const Cache = require('../lib/Language/Helpers/Cache')
5+
const Interpreter = require('../lib/Language/Interpreter')
6+
7+
describe('Cache', () => {
8+
it('Basic', () => {
9+
const re = {}
10+
Cache.set('test', re)
11+
assert.deepEqual(Cache.get('test'), re)
12+
})
13+
14+
it('In interpreter', () => {
15+
const RE = /(?:a)/
16+
const query = new Interpreter('Literally "a"')
17+
assert.deepEqual(query.get(), RE)
18+
19+
const query2 = new Interpreter('Literally "a"')
20+
assert.notEqual(query2, RE)
21+
22+
assert(query !== query2)
23+
})
24+
})

test/exceptions-test.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const SRL = require('../')
5+
6+
const BuilderException = require('../lib/Exceptions/Builder')
7+
const ImplementationException = require('../lib/Exceptions/Implementation')
8+
9+
describe('Builder Exceptions', () => {
10+
it('Raw method', () => {
11+
const regex = new SRL('Literally "a"')
12+
13+
assert.throws(() => {
14+
regex.raw(')')
15+
}, (error) => {
16+
return error instanceof BuilderException &&
17+
error.message === 'Adding raw would invalidate this regular expression. Reverted.'
18+
&& regex.test('a')
19+
})
20+
})
21+
22+
23+
})
24+
25+
describe('Implementation Exception', () => {
26+
it('Lazy Method', () => {
27+
const regex = new SRL('Literally "a"')
28+
29+
assert.throws(() => {
30+
regex.lazy()
31+
}, (error) => {
32+
return error instanceof ImplementationException &&
33+
error.message === 'Cannot apply laziness at this point. Only applicable after quantifier.'
34+
})
35+
})
36+
})

0 commit comments

Comments
 (0)