Skip to content

Commit 4a9984a

Browse files
Merge pull request #296 from splitio/tests_refactor
Tests refactor: reusing a CSV reader utility
2 parents 4d46ac0 + 07ed46e commit 4a9984a

File tree

21 files changed

+75
-251
lines changed

21 files changed

+75
-251
lines changed

package-lock.json

Lines changed: 0 additions & 156 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"@typescript-eslint/eslint-plugin": "^6.6.0",
6464
"@typescript-eslint/parser": "^6.6.0",
6565
"cross-env": "^7.0.2",
66-
"csv-streamify": "^4.0.0",
6766
"eslint": "^8.48.0",
6867
"eslint-plugin-compat": "^4.2.0",
6968
"eslint-plugin-import": "^2.25.3",

src/__tests__/testUtils/csv.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import fs from 'fs';
2+
import rl from 'readline';
3+
4+
export function readCSV(filePath: string, delimiter = ','): Promise<string[][]> {
5+
return new Promise((resolve) => {
6+
const parser = rl.createInterface({
7+
input: fs.createReadStream(filePath)
8+
});
9+
10+
const data: string[][] = [];
11+
12+
parser
13+
.on('line', line => {
14+
data.push(line.split(delimiter));
15+
})
16+
.on('close', () => resolve(data));
17+
});
18+
}
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { matcherTypes } from '../matcherTypes';
22
import { matcherFactory } from '..';
3-
import fs from 'fs';
4-
import rl from 'readline';
53
import { IMatcher, IMatcherDto } from '../../types';
64
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
5+
import { readCSV } from '../../../__tests__/testUtils/csv';
76

8-
test('MATCHER REGEX (STRING) / should match the attribute value only with the string starts with hello', function () {
7+
test('MATCHER REGEX (STRING) / should match the attribute value only with the string starts with hello', () => {
98
// @ts-ignore
109
const matcher = matcherFactory(loggerMock, {
1110
type: matcherTypes.MATCHES_STRING,
@@ -16,7 +15,7 @@ test('MATCHER REGEX (STRING) / should match the attribute value only with the st
1615
expect(matcher('hello dude!')).toBe(true);
1716
});
1817

19-
test('MATCHER REGEX (STRING) / incorrectly matches unicode characters', function () {
18+
test('MATCHER REGEX (STRING) / incorrectly matches unicode characters', () => {
2019
// @ts-ignore
2120
const matcher = matcherFactory(loggerMock, {
2221
type: matcherTypes.MATCHES_STRING,
@@ -31,31 +30,20 @@ test('MATCHER REGEX (STRING) / incorrectly matches unicode characters', function
3130
'regex.txt'
3231
].forEach(filename => {
3332

34-
test('MATCHER REGEX (STRING) / validate regex behavior using sample data', (done) => {
35-
const parser = rl.createInterface({
36-
terminal: false,
37-
input: fs.createReadStream(require.resolve(`./mocks/${filename}`))
38-
});
33+
test('MATCHER REGEX (STRING) / validate regex behavior using sample data', async () => {
34+
const lines = await readCSV(require.resolve(`./mocks/${filename}`), '#');
3935

40-
parser
41-
.on('line', line => {
42-
const parts = line.toString().split('#');
36+
for (const [regex, input, test] of lines) {
37+
const isTestTrue = test === 'true';
4338

44-
if (parts.length === 3) {
45-
let [regex, input, test] = parts;
39+
// @ts-ignore
40+
const matcher = matcherFactory(loggerMock, {
41+
type: matcherTypes.MATCHES_STRING,
42+
value: regex
43+
} as IMatcherDto) as IMatcher;
4644

47-
const isTestTrue = test === 'true';
48-
49-
// @ts-ignore
50-
const matcher = matcherFactory(loggerMock, {
51-
type: matcherTypes.MATCHES_STRING,
52-
value: regex
53-
} as IMatcherDto) as IMatcher;
54-
55-
expect(matcher(input) === isTestTrue).toBe(true);
56-
}
57-
})
58-
.on('close', done);
45+
expect(matcher(input) === isTestTrue).toBe(true);
46+
}
5947
});
6048

6149
});

src/evaluator/matchers/between.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IBetweenMatcherData } from '../../dtos/types';
22
import { ENGINE_MATCHER_BETWEEN } from '../../logger/constants';
33
import { ILogger } from '../../logger/types';
44

5-
export function betweenMatcherContext(log: ILogger, ruleVO: IBetweenMatcherData) /*: Function */ {
5+
export function betweenMatcherContext(log: ILogger, ruleVO: IBetweenMatcherData) {
66
return function betweenMatcher(runtimeAttr: number): boolean {
77

88
let isBetween = runtimeAttr >= ruleVO.start && runtimeAttr <= ruleVO.end;

src/evaluator/matchers/boolean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ENGINE_MATCHER_BOOLEAN } from '../../logger/constants';
22
import { ILogger } from '../../logger/types';
33

4-
export function booleanMatcherContext(log: ILogger, ruleAttr: boolean) /*: Function */ {
4+
export function booleanMatcherContext(log: ILogger, ruleAttr: boolean) {
55
return function booleanMatcher(runtimeAttr: boolean): boolean {
66
let booleanMatches = ruleAttr === runtimeAttr;
77

0 commit comments

Comments
 (0)