From 4599dd9c8a20da552abfa8970666579e7e7d210c Mon Sep 17 00:00:00 2001 From: Varg Date: Fri, 20 Sep 2024 23:36:58 +0200 Subject: [PATCH 1/3] Add more tests for assignment and locals --- tests/tuples.test.ts | 2 +- tests/variables.test.ts | 180 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 165 insertions(+), 17 deletions(-) diff --git a/tests/tuples.test.ts b/tests/tuples.test.ts index 96babcc..0dbc3c7 100644 --- a/tests/tuples.test.ts +++ b/tests/tuples.test.ts @@ -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, ` diff --git a/tests/variables.test.ts b/tests/variables.test.ts index 6bc61f3..9819211 100644 --- a/tests/variables.test.ts +++ b/tests/variables.test.ts @@ -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`); @@ -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"); @@ -85,8 +236,7 @@ $lc = $c $ld = $d $le = $e $lf = $f - `, - ); + `); expect(api.readVariable('$a')).toBe("aa"); expect(api.readVariable('$la')).toBe("la"); @@ -116,8 +266,7 @@ gs 'nested' --- # nested $na = $a -`, - ); + `); expect(api.readVariable('$a')).toBe("aa"); expect(api.readVariable('$la')).toBe("la"); @@ -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"); @@ -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(); @@ -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(); From 550010c83a39a01e55e38c67d48ea2c4bd1e6d92 Mon Sep 17 00:00:00 2001 From: Varg Date: Sat, 21 Sep 2024 00:16:13 +0200 Subject: [PATCH 2/3] Extra tests for empty tuples --- tests/tuples.test.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/tuples.test.ts b/tests/tuples.test.ts index 0dbc3c7..e2f7b27 100644 --- a/tests/tuples.test.ts +++ b/tests/tuples.test.ts @@ -68,6 +68,16 @@ describe('tuples', () => { expect(api.readVariable('%a')).toEqual([1]); }); + test('empty tuple with parenthesis', () => { + runTestFile(api, `a = 2 & %a = ()`); + expect(api.readVariable('%a')).toEqual([]); + }); + + test('empty tuple with brackets', () => { + runTestFile(api, `a = 2 & %a = []`); + expect(api.readVariable('%a')).toEqual([]); + }); + test('nested tuple with single item', () => { runTestFile(api, `%a = ("a", ["b"]) & %b = ("a", ("b"))`); expect(api.readVariable('%a')).toEqual(['a', ['b']]); @@ -147,7 +157,7 @@ describe('tuples', () => { expect(api.readVariable('%rest')).toEqual([]); }); - test('swaping value without tmp variable', () => { + test('swapping value without tmp variable', () => { runTestFile( api, `a = 1 & b = 2 From 3e662e183a52ea821c72b3e8cd34232907c75036 Mon Sep 17 00:00:00 2001 From: Varg Date: Sat, 21 Sep 2024 00:22:43 +0200 Subject: [PATCH 3/3] Extra tests for empty tuples --- tests/tuples.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/tuples.test.ts b/tests/tuples.test.ts index e2f7b27..b010a91 100644 --- a/tests/tuples.test.ts +++ b/tests/tuples.test.ts @@ -71,11 +71,13 @@ describe('tuples', () => { test('empty tuple with parenthesis', () => { runTestFile(api, `a = 2 & %a = ()`); expect(api.readVariable('%a')).toEqual([]); + expect(api.readVariable('a')).toEqual(0); }); test('empty tuple with brackets', () => { runTestFile(api, `a = 2 & %a = []`); expect(api.readVariable('%a')).toEqual([]); + expect(api.readVariable('a')).toEqual(0); }); test('nested tuple with single item', () => {