Skip to content

Commit 1dbdd6d

Browse files
committed
Implement =~
And `assert_match`/`assert_notmatch` enabled by it
1 parent 54e1957 commit 1dbdd6d

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Value,
2020
VariableExpression,
2121
} from './types';
22+
import { Pattern, SearchDirection } from '../pattern';
2223

2324
// ID of next lambda; incremented each time one is created
2425
let lambdaNumber = 1;
@@ -620,7 +621,11 @@ export class EvaluationContext {
620621
case '>':
621622
return lhs.value > rhs.value;
622623
case '=~':
623-
return false; // TODO
624+
const pattern = Pattern.parser({
625+
direction: SearchDirection.Forward,
626+
delimiter: '/', // TODO: Are these params right?
627+
}).tryParse(toString(lhs));
628+
return pattern.regex.test(toString(rhs));
624629
}
625630
}
626631
}
@@ -706,25 +711,43 @@ export class EvaluationContext {
706711
);
707712
}
708713
// TODO: assert_inrange()
709-
// TODO: assert_match()
714+
case 'assert_match': {
715+
const [pattern, actual, msg] = getArgs(2, 3);
716+
if (this.evaluateComparison('=~', true, actual!, pattern!)) {
717+
return assertPassed();
718+
}
719+
return assertFailed(
720+
msg
721+
? toString(msg)
722+
: `Pattern '${toString(pattern!)}' does not match '${toString(actual!)}'`,
723+
);
724+
}
710725
case 'assert_nobeep': {
711726
return assertPassed();
712727
}
713728
case 'assert_notequal': {
714729
const [expected, actual, msg] = getArgs(2, 3);
715-
if (this.evaluateComparison('!=', true, expected!, actual!)) {
730+
if (this.evaluateComparison('=~', true, expected!, actual!)) {
716731
return assertPassed();
717732
}
718733
return assertFailed(
719734
msg ? toString(msg) : `Expected not equal to ${displayValue(expected!)}`,
720735
);
721736
}
722-
// TODO: assert_notmatch()
737+
case 'assert_notmatch': {
738+
const [pattern, actual, msg] = getArgs(2, 3);
739+
if (this.evaluateComparison('=~', true, actual!, pattern!)) {
740+
return assertPassed();
741+
}
742+
return assertFailed(
743+
msg ? toString(msg) : `Pattern '${toString(pattern!)}' does match '${toString(actual!)}'`,
744+
);
745+
}
723746
case 'assert_report': {
724747
return assertFailed(toString(getArgs(1)[0]!));
725748
}
726749
case 'assert_true': {
727-
const [actual, msg] = getArgs(2, 3);
750+
const [actual, msg] = getArgs(1, 2);
728751
if (this.evaluateComparison('==', true, bool(true), actual!)) {
729752
return assertPassed();
730753
}

0 commit comments

Comments
 (0)