Skip to content

Commit

Permalink
Add more tests for assignment and locals
Browse files Browse the repository at this point in the history
  • Loading branch information
Varg committed Sep 20, 2024
1 parent 725aa83 commit 4599dd9
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tests/tuples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ $a, $b = func('other')
});
});

test('it errors out on more than 20 eleemnts', () => {
test('it errors out on more than 20 elements', () => {
runTestFile(
api,
`
Expand Down
180 changes: 164 additions & 16 deletions tests/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ describe('variables', () => {
expect(api.readVariable('money')).toBe(100);
});

test('assignment to array item by numeric index', () => {
runTestFile(api, `money[2] = 100`);

expect(api.readVariableByIndex('money', 0)).toBe(0);
expect(api.readVariableByIndex('money', 1)).toBe(0);
expect(api.readVariableByIndex('money', 2)).toBe(100);
});

test('assignment to array item by string index', () => {
runTestFile(api, `money['key'] = 100`);

expect(api.readVariableByKey('money', 'key')).toBe(100);
});

test('multi assignment', () => {
runTestFile(api, `a, $b, $c, d = 1, 'test', 'other', 2`);

Expand All @@ -57,14 +71,151 @@ describe('variables', () => {
expect(api.readVariable('d')).toBe(2);
});

test('multi assignment with explicit tuple', () => {
runTestFile(api, `a, $b, $c, d = [1, 'test', 'other', 2]`);

expect(api.readVariable('a')).toBe(1);
expect(api.readVariable('$b')).toBe('test');
expect(api.readVariable('$c')).toBe('other');
expect(api.readVariable('d')).toBe(2);
});

test('multi assignment with array and numeric indices', () => {
runTestFile(api, `a[3], $b[2], $c[1], d = 1, 'test', 'other', 2`);

expect(api.readVariableByIndex('a', 3)).toBe(1);
expect(api.readVariableByIndex('$b', 2)).toBe('test');
expect(api.readVariableByIndex('$c', 1)).toBe('other');
expect(api.readVariable('d')).toBe(2);
});

test('multi assignment with array and string indices', () => {
runTestFile(api, `a['t1'], $b['t2'], $c['t3'], d = 1, 'test', 'other', 2`);

expect(api.readVariableByKey('a', 't1')).toBe(1);
expect(api.readVariableByKey('$b', 't2')).toBe('test');
expect(api.readVariableByKey('$c', 't3')).toBe('other');
expect(api.readVariable('d')).toBe(2);
});

test('partial assignment', () => {
runTestFile(api, `a, $b, $c, d, %e = 3, 'test'`);

expect(api.readVariable('a')).toBe(3);
expect(api.readVariable('$b')).toBe('test');
expect(api.readVariable('$c')).toBe('');
expect(api.readVariable('d')).toBe(0);
expect(api.readVariable('%e')).toEqual([]);
});

test('local without assignment', () => {
runTestFile(api,
`
a_new, $b_new, c_new = 1, 'old', 2
local a, $b, c
a_new, $b_new, c_new = a, $b, c
`);

expect(api.readVariable('a_new')).toBe(0);
expect(api.readVariable('$b_new')).toBe('');
expect(api.readVariable('c_new')).toBe(0);
});

test('multi assignment local', () => {
runTestFile(api,
`
a_new, $b_new, $c_new, d_new = 1, 'old', 'oldest', 2
local a, $b, $c, d = 3, 'test', 'other', 4
a_new, $b_new, $c_new, d_new = a, $b, $c, d
`);

expect(api.readVariable('a_new')).toBe(3);
expect(api.readVariable('$b_new')).toBe('test');
expect(api.readVariable('$c_new')).toBe('other');
expect(api.readVariable('d_new')).toBe(4);
});

test('local with partial assignment', () => {
runTestFile(api,
`
a_new, $b_new, $c_new, d_new = 1, 'old', 'oldest', 2
local a, $b, $c, d = 3, 'test'
a_new, $b_new, $c_new, d_new = a, $b, $c, d
`);

expect(api.readVariable('a_new')).toBe(3);
expect(api.readVariable('$b_new')).toBe('test');
expect(api.readVariable('$c_new')).toBe('');
expect(api.readVariable('d_new')).toBe(0);
});

test('multi assignment with the same variable and last num value', () => {
runTestFile(api, `a, $a, A = 2, 'test', 4`);

expect(api.readVariable('a')).toBe(4);
expect(api.readVariable('$a')).toBe('');
});

test('multi assignment with the same variable and last string value', () => {
runTestFile(api, `$a, a, $A = 'test', 3, 'new'`);

expect(api.readVariable('a')).toBe(0);
expect(api.readVariable('$a')).toBe('new');
});

test('multi assignment local with the same variable and last num value', () => {
runTestFile(api,
`
local a, $a, A = 2, 'test', 4
new_num, $new_str = a, $a
`);

expect(api.readVariable('new_num')).toBe(4);
expect(api.readVariable('$new_str')).toBe('');
});

test('multi assignment local with the same variable and last string value', () => {
runTestFile(api,
`
local $a, a, $A = 'test', 3, 'new'
new_num, $new_str = a, $a
`);

expect(api.readVariable('new_num')).toBe(0);
expect(api.readVariable('$new_str')).toBe('new');
});

test('multi assignment with the same array', () => {
runTestFile(api, `a[1], $a[2], A[3] = 2, 'test', 4`);

expect(api.readVariableByIndex('a', 1)).toBe(2);
expect(api.readVariableByIndex('$a', 2)).toBe('test');
expect(api.readVariableByIndex('a', 3)).toBe(4);
});

test('global variables get restored after killvar with local', () => {
runTestFile(api,
`
a = 1 & $b = 'test' & c = 3
if 1:
local a, $b
killvar
end
`);

expect(api.readVariable('a')).toBe(1);
expect(api.readVariable('$b')).toBe('test');
expect(api.readVariable('c')).toBe(0);
});

test('variable swap', () => {
runTestFile(
api,
`
$x = "aa"
$y = "bb"
$y, $x = $x, $y`,
);
$y, $x = $x, $y
`);

expect(api.readVariable('$x')).toBe("bb");
expect(api.readVariable('$y')).toBe("aa");
Expand All @@ -85,8 +236,7 @@ $lc = $c
$ld = $d
$le = $e
$lf = $f
`,
);
`);

expect(api.readVariable('$a')).toBe("aa");
expect(api.readVariable('$la')).toBe("la");
Expand Down Expand Up @@ -116,8 +266,7 @@ gs 'nested'
---
# nested
$na = $a
`,
);
`);

expect(api.readVariable('$a')).toBe("aa");
expect(api.readVariable('$la')).toBe("la");
Expand All @@ -136,8 +285,7 @@ local $a
$a[] = $args[1]
size[$args[0]] = arrsize('a')
$res[$args[0]] = $a[0]
`,
);
`);

expect(api.readVariableByKey('$res', 'first')).toBe("ff");
expect(api.readVariableByKey('$res', 'second')).toBe("ss");
Expand All @@ -152,11 +300,10 @@ $res[$args[0]] = $a[0]
runTestFileWithGoto(
api,
`
local $a = "aa"
$args[0] = "bb"
act '1': $a & $args[0]
`,
);
local $a = "aa"
$args[0] = "bb"
act '1': $a & $args[0]
`);

api.selectAction(0);
api.execSelectedAction();
Expand All @@ -170,15 +317,16 @@ $res[$args[0]] = $a[0]

runTestFile(
api,
`$i="ii"
`
$i="ii"
act "local i":
local $i = "lii"
*pl $i
end
act "global i":
*pl $i
end`,
);
end
`);

api.selectAction(0);
api.execSelectedAction();
Expand Down

0 comments on commit 4599dd9

Please sign in to comment.