Skip to content

Commit 3620208

Browse files
committed
Let claude write some expression tests
1 parent 6fe0442 commit 3620208

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

test/vimscript/expression.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ suite('Vimscript expressions', () => {
122122
exprTest('""', { expr: str('') });
123123
exprTest('"\\""', { expr: str('"') });
124124
exprTest('"one\\ntwo\\tthree"', { expr: str('one\ntwo\tthree') });
125+
exprTest('"\\\\"', { expr: str('\\') }); // "\\" -> single backslash
126+
exprTest('"\\n"', { expr: str('\n') });
127+
exprTest('"\\t"', { expr: str('\t') });
128+
exprTest('"hello world"', { expr: str('hello world') });
125129
});
126130

127131
suite('Literal strings', () => {
@@ -410,6 +414,12 @@ suite('Vimscript expressions', () => {
410414
exprTest('4/5', { value: int(0) });
411415
exprTest('4/5.0', { display: '0.8' });
412416
exprTest('4.0/5', { display: '0.8' });
417+
418+
exprTest('10 - 3', { value: int(7) });
419+
exprTest('3 - 10', { value: int(-7) });
420+
exprTest('10 - 3.0', { display: '7.0' });
421+
exprTest('1 + 2 + 3', { value: int(6) });
422+
exprTest('1 + 2 - 3', { value: int(0) });
413423
});
414424

415425
suite('Precedence', () => {
@@ -458,9 +468,19 @@ suite('Vimscript expressions', () => {
458468

459469
suite('Pattern matching', () => {
460470
exprTest("'apple' =~ '^a.*e$'", { value: bool(true) });
471+
exprTest("'apple' =~# '^a.*e$'", { value: bool(true) });
472+
exprTest("'apple' =~# '^A.*E$'", { value: bool(false) });
473+
exprTest("'apple' =~? '^A.*E$'", { value: bool(true) });
461474
// TODO
462475
});
463476

477+
suite('Pattern not-matching', () => {
478+
exprTest("'apple' !~ '^a.*e$'", { value: bool(false) });
479+
exprTest("'apple' !~ '^b'", { value: bool(true) });
480+
exprTest("'foo' !~# '^F'", { value: bool(true) });
481+
exprTest("'foo' !~? '^F'", { value: bool(false) });
482+
});
483+
464484
suite('Numeric comparisons', () => {
465485
exprTest('1 < 2', { value: bool(true) });
466486
exprTest('2 < 1', { value: bool(false) });
@@ -608,6 +628,14 @@ suite('Vimscript expressions', () => {
608628
exprTest("assert_match('^f.*o$', 'foobar')", FAIL);
609629

610630
exprTest('assert_report("whatever")', FAIL);
631+
632+
exprTest('assert_notequal(1, 2)', PASS);
633+
exprTest('assert_notequal(1, 1)', FAIL);
634+
exprTest('assert_notequal("abc", "def")', PASS);
635+
exprTest('assert_notequal("abc", "abc")', FAIL);
636+
637+
exprTest("assert_notmatch('^f.*o$', 'bar')", PASS);
638+
exprTest("assert_notmatch('^f.*o$', 'foo')", FAIL);
611639
});
612640

613641
suite('add', () => {
@@ -771,7 +799,10 @@ suite('Vimscript expressions', () => {
771799
});
772800

773801
suite('invert', () => {
802+
exprTest('invert(0)', { value: int(-1) });
803+
exprTest('invert(-1)', { value: int(0) });
774804
exprTest('invert(123)', { value: int(-124) });
805+
exprTest('invert(-124)', { value: int(123) });
775806
});
776807

777808
suite('isnan/isinf', () => {
@@ -936,6 +967,10 @@ suite('Vimscript expressions', () => {
936967
exprTest('stridx("0123456789", "6")', { value: int(6) });
937968
exprTest('stridx("0123456789", "456")', { value: int(4) });
938969
exprTest('stridx("0123456789", "X")', { value: int(-1) });
970+
exprTest('stridx("0123456789", "6", 5)', { value: int(6) });
971+
exprTest('stridx("0123456789", "6", 7)', { value: int(-1) });
972+
exprTest('stridx("abcabc", "b")', { value: int(1) });
973+
exprTest('stridx("abcabc", "b", 2)', { value: int(4) });
939974
});
940975

941976
suite('string', () => {
@@ -1044,5 +1079,90 @@ suite('Vimscript expressions', () => {
10441079
exprTest('sort([4,2,1,3,5], {x,y->y-x})', { display: '[5, 4, 3, 2, 1]' });
10451080
// TODO
10461081
});
1082+
1083+
suite('abs', () => {
1084+
exprTest('abs(-5)', { value: float(5) });
1085+
exprTest('abs(5)', { value: float(5) });
1086+
exprTest('abs(-3.5)', { value: float(3.5) });
1087+
exprTest('abs(0)', { value: float(0) });
1088+
});
1089+
1090+
suite('and/or/xor', () => {
1091+
exprTest('and(0xff, 0x0f)', { value: int(15) });
1092+
exprTest('and(0b1010, 0b1100)', { value: int(8) });
1093+
exprTest('and(0, 0xff)', { value: int(0) });
1094+
exprTest('or(0xf0, 0x0f)', { value: int(255) });
1095+
exprTest('or(0b1010, 0b0101)', { value: int(15) });
1096+
exprTest('or(0, 0)', { value: int(0) });
1097+
exprTest('xor(0xff, 0x0f)', { value: int(240) });
1098+
exprTest('xor(0b1010, 0b1010)', { value: int(0) });
1099+
exprTest('xor(0b1010, 0b0101)', { value: int(15) });
1100+
});
1101+
1102+
suite('copy/deepcopy', () => {
1103+
exprTest('copy([1, 2, 3])', { value: list([int(1), int(2), int(3)]) });
1104+
exprTest('deepcopy([1, 2, 3])', { value: list([int(1), int(2), int(3)]) });
1105+
exprTest("copy('hello')", { value: str('hello') });
1106+
exprTest("deepcopy('hello')", { value: str('hello') });
1107+
exprTest('copy(42)', { value: int(42) });
1108+
});
1109+
1110+
suite('eval', () => {
1111+
exprTest("eval('1 + 2')", { value: int(3) });
1112+
exprTest('eval(\'"hello"\')', { value: str('hello') });
1113+
exprTest("eval('[1, 2, 3]')", { value: list([int(1), int(2), int(3)]) });
1114+
exprTest("eval('#{a: 1}')", { display: "{'a': 1}" });
1115+
});
1116+
1117+
suite('exp/log/log10/pow', () => {
1118+
exprTest('exp(0)', { value: float(1) });
1119+
exprTest('log(1)', { value: float(0) });
1120+
exprTest('log10(1)', { value: float(0) });
1121+
exprTest('log10(100)', { value: float(2) });
1122+
exprTest('pow(2, 10)', { value: float(1024) });
1123+
exprTest('pow(9, 0.5)', { value: float(3) });
1124+
exprTest('pow(2, 0)', { value: float(1) });
1125+
});
1126+
1127+
suite('Trigonometric', () => {
1128+
exprTest('sin(0)', { value: float(0) });
1129+
exprTest('cos(0)', { value: float(1) });
1130+
exprTest('tan(0)', { value: float(0) });
1131+
exprTest('asin(0)', { value: float(0) });
1132+
exprTest('acos(1)', { value: float(0) });
1133+
exprTest('atan2(0, 1)', { value: float(0) });
1134+
exprTest('sinh(0)', { value: float(0) });
1135+
exprTest('cosh(0)', { value: float(1) });
1136+
exprTest('tanh(0)', { value: float(0) });
1137+
});
1138+
1139+
suite('has', () => {
1140+
exprTest("has('vscode')", { value: bool(true) });
1141+
exprTest("has('nvim')", { value: bool(false) });
1142+
exprTest("has('gui')", { value: bool(false) });
1143+
});
1144+
1145+
suite('type', () => {
1146+
exprTest('type(0)', { value: int(0) });
1147+
exprTest("type('str')", { value: int(1) });
1148+
exprTest("type(function('abs'))", { value: int(2) });
1149+
exprTest('type([])', { value: int(3) });
1150+
exprTest('type({})', { value: int(4) });
1151+
exprTest('type(0.0)', { value: int(5) });
1152+
exprTest('type(0z)', { value: int(8) });
1153+
});
1154+
1155+
suite('flattennew', () => {
1156+
exprTest('flattennew([1, [2, [3, 4]], 5])', { display: '[1, 2, 3, 4, 5]' });
1157+
exprTest('flattennew([1, [2, [3, 4]], 5], 1)', { display: '[1, 2, [3, 4], 5]' });
1158+
exprTest('flattennew([1, [2, [3, 4]], 5], 0)', { display: '[1, [2, [3, 4]], 5]' });
1159+
exprTest('flattennew({})', { error: VimError.ArgumentMustBeAList('flattennew') });
1160+
});
1161+
1162+
suite('mapnew', () => {
1163+
exprTest('mapnew([10, 20, 30], {k, v -> v * 2})', { display: '[20, 40, 60]' });
1164+
exprTest("mapnew([10, 20, 30], 'v:val + 1')", { display: '[11, 21, 31]' });
1165+
exprTest("mapnew([10, 20, 30], 'v:key')", { value: list([int(0), int(1), int(2)]) });
1166+
});
10471167
});
10481168
});

0 commit comments

Comments
 (0)