Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra tests for locals #7

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,038 changes: 2,703 additions & 3,335 deletions src/qsplib/public/qsp-engine-debug.js

Large diffs are not rendered by default.

Binary file modified src/qsplib/public/qsp-engine-debug.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/qsplib/public/qsp-engine-debug.wasm.map

Large diffs are not rendered by default.

928 changes: 9 additions & 919 deletions src/qsplib/public/qsp-engine.js

Large diffs are not rendered by default.

Binary file modified src/qsplib/public/qsp-engine.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/acts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Sign [:] not found!',
errorCode: 106,
errorCode: 16,
line: 2,
lineSrc: 'ACT \'test\'',
localLine: 2,
Expand Down
2 changes: 1 addition & 1 deletion tests/arrays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ $res = $arr[1,3]
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Type mismatch!',
errorCode: 101,
errorCode: 11,
line: 7,
lineSrc: "SORTARR('$a')",
localLine: 7,
Expand Down
12 changes: 6 additions & 6 deletions tests/conditionals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ else
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: '[end] not found!',
errorCode: 111,
errorCode: 21,
line: 2,
lineSrc: 'IF ABCD = 3:',
localLine: 2,
Expand All @@ -868,7 +868,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: '[end] not found!',
errorCode: 111,
errorCode: 21,
line: 3,
lineSrc: 'IF ABC <> 3:',
localLine: 3,
Expand All @@ -887,7 +887,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Unknown action!',
errorCode: 118,
errorCode: 28,
line: 2,
lineSrc: 'IF ABCD=3: K1=34 ELSE K1=25 END',
localLine: 2,
Expand All @@ -907,7 +907,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Sign [:] not found!',
errorCode: 106,
errorCode: 16,
line: 2,
lineSrc: 'IF 0',
localLine: 2,
Expand All @@ -928,7 +928,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Sign [:] not found!',
errorCode: 106,
errorCode: 16,
line: 3,
lineSrc: 'ELSEIF 3',
localLine: 3,
Expand All @@ -949,7 +949,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Sign [:] not found!',
errorCode: 106,
errorCode: 16,
line: 3,
lineSrc: 'ELSE IF 3',
localLine: 3,
Expand Down
8 changes: 4 additions & 4 deletions tests/expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('operations', () => {
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Type mismatch!',
errorCode: 101,
errorCode: 11,
line: 1,
lineSrc: "RES = '2a' - '1'",
localLine: 1,
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('operations', () => {
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Type mismatch!',
errorCode: 101,
errorCode: 11,
line: 1,
lineSrc: "RES = -'2a'",
localLine: 1,
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('operations', () => {
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Type mismatch!',
errorCode: 101,
errorCode: 11,
line: 1,
lineSrc: "RES = '2a' / '1'",
localLine: 1,
Expand All @@ -242,7 +242,7 @@ describe('operations', () => {
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Division by zero!',
errorCode: 100,
errorCode: 10,
line: 1,
lineSrc: 'RES = 2 / 0',
localLine: 1,
Expand Down
6 changes: 3 additions & 3 deletions tests/labels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ act '1': x = 1 & jump 'end'
expect(api.readVariable('x')).toBe(1);
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
errorCode: 112,
errorCode: 22,
description: 'Label not found!',
line: 2,
localLine: 2,
Expand All @@ -188,7 +188,7 @@ dynamic { x = 1 & jump 'end' }
expect(api.readVariable('x')).toBe(1);
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
errorCode: 112,
errorCode: 22,
description: 'Label not found!',
line: 2,
localLine: 1,
Expand All @@ -209,7 +209,7 @@ DYNEVAL(" x = 1 & jump 'end' ")
expect(api.readVariable('x')).toBe(1);
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
errorCode: 112,
errorCode: 22,
description: 'Label not found!',
line: 2,
localLine: 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ end
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: 'Sign [:] not found!',
errorCode: 106,
errorCode: 16,
line: 2,
lineSrc: 'LOOP WHILE 0',
localLine: 2,
Expand Down
2 changes: 1 addition & 1 deletion tests/tuples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ $a, $b = func('other')
expect(error).toHaveBeenCalledWith({
actionIndex: -1,
description: "Incorrect arguments' count!",
errorCode: 119,
errorCode: 29,
line: 2,
lineSrc:
'%A = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21"]',
Expand Down
108 changes: 85 additions & 23 deletions tests/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,42 +111,62 @@ describe('variables', () => {
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
a, $b, c = 1, 'old', 2
if 1:
local a, $b, c
a_new, $b_new, c_new = a, $b, c
end
a_old, $b_old, c_old = a, $b, c
`);

expect(api.readVariable('a_new')).toBe(0);
expect(api.readVariable('$b_new')).toBe('');
expect(api.readVariable('c_new')).toBe(0);
expect(api.readVariable('a_old')).toBe(1);
expect(api.readVariable('$b_old')).toBe('old');
expect(api.readVariable('c_old')).toBe(2);
});

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
a, $b, $c, d = 1, 'old', 'oldest', 2
if 1:
local a, $b, $c, d = 3, 'test', 'other', 4
a_new, $b_new, $c_new, d_new = a, $b, $c, d
end
a_old, $b_old, $c_old, d_old = 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);
expect(api.readVariable('a_old')).toBe(1);
expect(api.readVariable('$b_old')).toBe('old');
expect(api.readVariable('$c_old')).toBe('oldest');
expect(api.readVariable('d_old')).toBe(2);
});

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
a, $b, $c, d = 1, 'old', 'oldest', 2
if 1:
local a, $b, $c, d = 3, 'test'
a_new, $b_new, $c_new, d_new = a, $b, $c, d
end
a_old, $b_old, $c_old, d_old = 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);
expect(api.readVariable('a_old')).toBe(1);
expect(api.readVariable('$b_old')).toBe('old');
expect(api.readVariable('$c_old')).toBe('oldest');
expect(api.readVariable('d_old')).toBe(2);
});

test('multi assignment with the same variable and last num value', () => {
Expand Down Expand Up @@ -225,31 +245,73 @@ $y, $x = $x, $y
runTestFile(
api,
`
$a = "aa" & $b = "bb" & $c = "cc" & $d = "dd" & $e = "ee" & $f = "ff"
$a = "aa" & $b = "bb" & c = 11 & $d = "dd"
gs 'other'
---
# other
local $a,$b,$c,$d,$e,$f = "la","lb","lc","ld","le","lf"
local $a,$b,c,$d = "la","lb",22,"ld"
$la = $a
$lb = $b
$lc = $c
lc = c
$ld = $d
$le = $e
$lf = $f
`);

expect(api.readVariable('$a')).toBe("aa");
expect(api.readVariable('$la')).toBe("la");
expect(api.readVariable('$b')).toBe("bb");
expect(api.readVariable('$lb')).toBe("lb");
expect(api.readVariable('$c')).toBe("cc");
expect(api.readVariable('$lc')).toBe("lc");
expect(api.readVariable('c')).toBe(11);
expect(api.readVariable('lc')).toBe(22);
expect(api.readVariable('$d')).toBe("dd");
expect(api.readVariable('$ld')).toBe("ld");
expect(api.readVariable('$e')).toBe("ee");
expect(api.readVariable('$le')).toBe("le");
expect(api.readVariable('$f')).toBe("ff");
expect(api.readVariable('$lf')).toBe("lf");
});

test.each(['gt', 'goto', 'xgt', 'xgoto'])
('global variables get restored on %s', (s) => {
runTestFile(api,
`
$test='value'
local $test='value 1'
if 1:
local $test='value 2'
$last_loc_test = $test
${s} 'other'
end
---
# other
$glob_test = $test
`);

expect(api.readVariable('$last_loc_test')).toBe("value 2");
expect(api.readVariable('$glob_test')).toBe("value");
});

test('global variables get temporary restored on savegame', () => {
const onSaveGame = vi.fn((_, callback) => { api.saveGame(); callback(); });
api.on('save_game', onSaveGame);

runTestFile(api,
`
$ongsave = 'other'
$test='value'
local $test='value 1'
if 1:
local $test='value 2'
$last_loc_test1 = $test
savegame 'test.sav'
$last_loc_test2 = $test
end
$last_loc_test3 = $test
---
# other
$glob_test = $test
`);

expect(api.readVariable('$last_loc_test1')).toBe("value 2");
expect(onSaveGame).toHaveBeenCalledWith('test.sav', expect.any(Function));
expect(api.readVariable('$last_loc_test2')).toBe("value 2");
expect(api.readVariable('$last_loc_test3')).toBe("value 1");
expect(api.readVariable('$glob_test')).toBe("value");
});

test('local variables in nested calls are preserved (shadowing global)', () => {
Expand All @@ -258,14 +320,14 @@ $lf = $f
`
$a = "aa"
gs 'other'
---
---
# other
local $a = "la"
$la = $a
gs 'nested'
---
# nested
$na = $a
$na = $a
`);

expect(api.readVariable('$a')).toBe("aa");
Expand Down Expand Up @@ -351,5 +413,5 @@ end
expect(api.readVariable('$i')).toBe("ii");
onMsg.mock.calls[0][1]();
expect(api.readVariable('$i')).toBe("");
})
});
});