Skip to content

Commit

Permalink
refactor: comparisons tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed May 29, 2024
1 parent aeb5912 commit e7b37fa
Show file tree
Hide file tree
Showing 29 changed files with 292 additions and 87 deletions.
23 changes: 16 additions & 7 deletions src/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ function containsStrict(val1, val2): string {

function contains(val1, val2): string {
const code: string[] = [];
code.push(`(typeof ${val1} === 'string' && `);
code.push(`typeof ${val2} === 'string' && `);
code.push(`${val1}.toLowerCase().includes(${val2}.toLowerCase()))`);
code.push(`(typeof ${val1} === 'string' && typeof ${val2} === 'string') ?`);
code.push(`(${val1}.toLowerCase().includes(${val2}.toLowerCase()))`);
code.push(':');
code.push(`(Array.isArray(${val1}) && (${val1}.includes(${val2})`);
code.push(`|| (typeof ${val2} === 'string' && ${val1}.includes(${val2}.toLowerCase()))))`);
return code.join('');
}

Expand All @@ -56,7 +58,14 @@ export const binaryOperators = {

'!==': (val1, val2): string => `${val1}!==${val2}`,

'!=': (val1, val2): string => `${val1}!=${val2}`,
'!=': (val1, val2): string => {
const code: string[] = [];
code.push(`(typeof ${val1} == 'string' && typeof ${val2} == 'string') ?`);
code.push(`(${val1}.toLowerCase() != ${val2}.toLowerCase())`);
code.push(':');
code.push(`(${val1} != ${val2})`);
return code.join('');
},

'^==': startsWithStrict,

Expand All @@ -77,11 +86,11 @@ export const binaryOperators = {
'=~': (val1, val2): string =>
`(${val2} instanceof RegExp) ? (${val2}.test(${val1})) : (${val1}==${val2})`,

contains: containsStrict,
contains,

'==*': (val1, val2): string => containsStrict(val2, val1),
'==*': (val1, val2): string => containsStrict(val1, val2),

'=*': (val1, val2): string => contains(val2, val1),
'=*': (val1, val2): string => contains(val1, val2),

size: (val1, val2): string => `${val1}.length === ${val2}`,

Expand Down
9 changes: 6 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,9 @@ export class JsonTemplateParser {
this.lexer.ignoreTokens(1);
key = this.parseBaseExpr();
this.lexer.expect(']');
} else if (this.lexer.matchID()) {
} else if (this.lexer.matchID() || this.lexer.matchKeyword()) {
key = this.lexer.value();
} else if (this.lexer.matchTokenType(TokenType.STR)) {
} else if (this.lexer.matchLiteral() && !this.lexer.matchTokenType(TokenType.REGEXP)) {
key = this.parseLiteralExpr();
} else {
this.lexer.throwUnexpectedToken();
Expand All @@ -1070,7 +1070,10 @@ export class JsonTemplateParser {
}

private parseShortKeyValueObjectPropExpr(): ObjectPropExpression | undefined {
if (this.lexer.matchID() && (this.lexer.match(',', 1) || this.lexer.match('}', 1))) {
if (
(this.lexer.matchID() || this.lexer.matchKeyword()) &&
(this.lexer.match(',', 1) || this.lexer.match('}', 1))
) {
const key = this.lexer.lookahead().value;
const value = this.parseBaseExpr();
return {
Expand Down
2 changes: 1 addition & 1 deletion test/scenarios/bad_templates/object_with_invalid_key.jt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{1: 2}
{/1/: 2}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/anyof.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: [1, 2] anyof [2, 3],
false: [1, 2] anyof [3, 4]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/contains.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: ["aBc" ==* "aB", "abc" contains "c", ["a", "b", "c"] contains "c"],
false: ["aBc" ==* "ab", "abc" contains "d", ["a", "b", "c"] contains "d"]
}
211 changes: 173 additions & 38 deletions test/scenarios/comparisons/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,178 @@ import { Scenario } from '../../types';

export const data: Scenario[] = [
{
output: [
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
],
templatePath: 'anyof.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'contains.jt',
output: {
true: [true, true, true],
false: [false, false, false],
},
},
{
templatePath: 'empty.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'string_contains_ignore_case.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'ends_with.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'ends_with_ignore_case.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'eq.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'ge.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'gte.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'in.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'le.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'lte.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'ne.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'noneof.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'not_in.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'regex.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'size.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'starts_with.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'starts_with_ignore_case.jt',
output: {
true: [true, true],
false: [false, false],
},
},
{
templatePath: 'string_eq.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'string_ne.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'string_eq_ingore_case.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'string_ne.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'string_ne_ingore_case.jt',
output: {
true: true,
false: false,
},
},
{
templatePath: 'subsetof.jt',
output: {
true: [true, true],
false: [false, false],
},
},
];
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/empty.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: ["" empty true, [] empty true],
false: ["a" empty true, ["a"] empty true]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/ends_with.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true:["EndsWith" $== "With", "With" ==$ "EndsWith"],
false: ["EndsWith" $== "NotWith", "NotWith" ==$ "EndsWith"]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/ends_with_ignore_case.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true:["EndsWith" $= "with", "with" =$ "EndsWith"],
false: ["EndsWith" $= "NotWith", "NotWith" =$ "EndsWith"]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/eq.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: 10 == 10,
false: 10 == 2
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/ge.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: 10 > 2,
false: 2 > 10
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/gte.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: 10 >= 10,
false: 2 >= 10
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/in.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: ["a" in ["a", "b"], "a" in {"a": 1, "b": 2}],
false: ["c" in ["a", "b"], "c" in {"a": 1, "b": 2}]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/le.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: 2 < 10,
false: 10 < 2
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/lte.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: 10 <= 10,
false: 10 <= 2
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/ne.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: 10 != 2,
false: 10 != 10
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/noneof.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: [1, 2] noneof [3, 4],
false: [1, 2] noneof [2, 3],
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/not_in.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: ["c" nin ["a", "b"], "c" nin {"a": 1, "b": 2}],
false: ["a" nin ["a", "b"], "a" nin {"a": 1, "b": 2}]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/regex.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: ['abc' =~ /a.*c/, 'aBC' =~ /a.*c/i],
false: ['abC' =~ /a.*c/, 'aBd' =~ /a.*c/i]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/size.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true: [["a", "b"] size 2, "ab" size 2],
false: [[] size 1, "" size 1]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/starts_with.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true:["StartsWith" ^== "Starts", "Starts" ==^ "StartsWith"],
false: ["StartsWith" ^= "NotStarts", "NotStarts" =^ "StartsWith"]
}
4 changes: 4 additions & 0 deletions test/scenarios/comparisons/starts_with_ignore_case.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
true:["StartsWith" ^= "starts", "starts" =^ "StartsWith"],
false: ["StartsWith" ^= "NotStarts", "NotStarts" =^ "StartsWith"]
}
Loading

0 comments on commit e7b37fa

Please sign in to comment.