Add *proper* tests

master
Yury Kurlykov 2020-06-28 17:24:38 +10:00
parent 3641570e4b
commit 26dbd6ee10
Signed by: t1meshift
GPG Key ID: B133F3167ABF94D8
32 changed files with 593 additions and 18 deletions

View File

@ -12,7 +12,6 @@ Another JavaScript interpreter written on Python 3.
To run tests:
- pytest
- Tox
You can get ANTLR [here](https://www.antlr.org/), other dependencies could be installed with pip:
```bash
@ -26,6 +25,12 @@ antlr4 -Xexact-output-dir -o jasminesnake/lex -package lex -Dlanguage=Python3 -l
python -m jasminesnake
```
# Testing
```bash
# Running with -s is optional
python -m pytest -s
```
## Credits
ESTree specification:

View File

@ -1,2 +0,0 @@
// Guess what? It should be parsable, but it's not (git rev 44d7e241)
{let a = 15, b; {1_337.2_28, false, "String"}}

View File

@ -0,0 +1,8 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: " while executing client event 1"
| | | | | +-- value: while executing client event 1

View File

@ -0,0 +1,2 @@
// The code below won't work since the grammar treat Unicode
"\x20\x77hil\x65\x20exe\x63uting c\x6cient e\x76\145\x6et 1";

View File

@ -0,0 +1,120 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 1:0
| | | | | +-- operator: =
| | | | | +-- left: Identifier at 1:0
| | | | | | +-- name: a
| | | | | +-- right: Literal at 1:4
| | | | | | +-- value: 1.0
| +-- 1: ExpressionStatement at 2:0
| | +-- expression: SequenceExpression at 2:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 2:0
| | | | | +-- operator: *=
| | | | | +-- left: Identifier at 2:0
| | | | | | +-- name: q
| | | | | +-- right: Literal at 2:5
| | | | | | +-- value: 2.0
| +-- 2: ExpressionStatement at 3:0
| | +-- expression: SequenceExpression at 3:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 3:0
| | | | | +-- operator: /=
| | | | | +-- left: Identifier at 3:0
| | | | | | +-- name: d
| | | | | +-- right: Literal at 3:5
| | | | | | +-- value: 2.0
| +-- 3: ExpressionStatement at 4:0
| | +-- expression: SequenceExpression at 4:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 4:0
| | | | | +-- operator: %=
| | | | | +-- left: Identifier at 4:0
| | | | | | +-- name: w
| | | | | +-- right: Literal at 4:5
| | | | | | +-- value: 15.0
| +-- 4: ExpressionStatement at 5:0
| | +-- expression: SequenceExpression at 5:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 5:0
| | | | | +-- operator: +=
| | | | | +-- left: Identifier at 5:0
| | | | | | +-- name: b
| | | | | +-- right: Literal at 5:5
| | | | | | +-- value: 12.0
| +-- 5: ExpressionStatement at 6:0
| | +-- expression: SequenceExpression at 6:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 6:0
| | | | | +-- operator: -=
| | | | | +-- left: Identifier at 6:0
| | | | | | +-- name: c
| | | | | +-- right: Literal at 6:5
| | | | | | +-- value: 14.0
| +-- 6: ExpressionStatement at 7:0
| | +-- expression: SequenceExpression at 7:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 7:0
| | | | | +-- operator: <<=
| | | | | +-- left: Identifier at 7:0
| | | | | | +-- name: a
| | | | | +-- right: Literal at 7:6
| | | | | | +-- value: 1.0
| +-- 7: ExpressionStatement at 8:0
| | +-- expression: SequenceExpression at 8:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 8:0
| | | | | +-- operator: >>=
| | | | | +-- left: Identifier at 8:0
| | | | | | +-- name: b
| | | | | +-- right: Literal at 8:6
| | | | | | +-- value: 2.0
| +-- 8: ExpressionStatement at 9:0
| | +-- expression: SequenceExpression at 9:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 9:0
| | | | | +-- operator: >>>=
| | | | | +-- left: Identifier at 9:0
| | | | | | +-- name: z
| | | | | +-- right: Literal at 9:7
| | | | | | +-- value: 2.0
| +-- 9: ExpressionStatement at 10:0
| | +-- expression: SequenceExpression at 10:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 10:0
| | | | | +-- operator: &=
| | | | | +-- left: Identifier at 10:0
| | | | | | +-- name: v
| | | | | +-- right: Literal at 10:5
| | | | | | +-- value: 1.0
| +-- 10: ExpressionStatement at 11:0
| | +-- expression: SequenceExpression at 11:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 11:0
| | | | | +-- operator: ^=
| | | | | +-- left: Identifier at 11:0
| | | | | | +-- name: cc
| | | | | +-- right: Identifier at 11:6
| | | | | | +-- name: cc
| +-- 11: ExpressionStatement at 12:0
| | +-- expression: SequenceExpression at 12:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 12:0
| | | | | +-- operator: |=
| | | | | +-- left: Identifier at 12:0
| | | | | | +-- name: vv
| | | | | +-- right: Identifier at 12:5
| | | | | | +-- name: a
| +-- 12: ExpressionStatement at 13:0
| | +-- expression: SequenceExpression at 13:0
| | | +-- expressions:
| | | | +-- 0: AssignmentExpression at 13:0
| | | | | +-- operator: **=
| | | | | +-- left: Identifier at 13:0
| | | | | | +-- name: x
| | | | | +-- right: Literal at 13:6
| | | | | | +-- value: 2.0

View File

@ -0,0 +1,42 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: BinaryExpression at 1:0
| | | | | +-- operator: +
| | | | | +-- left: Literal at 1:0
| | | | | | +-- value: 2.0
| | | | | +-- right: BinaryExpression at 1:2
| | | | | | +-- operator: *
| | | | | | +-- left: Literal at 1:2
| | | | | | | +-- value: 2.0
| | | | | | +-- right: Literal at 1:4
| | | | | | | +-- value: 2.0
| +-- 1: ExpressionStatement at 2:0
| | +-- expression: SequenceExpression at 2:0
| | | +-- expressions:
| | | | +-- 0: BinaryExpression at 2:0
| | | | | +-- operator: ==
| | | | | +-- left: BinaryExpression at 2:0
| | | | | | +-- operator: +
| | | | | | +-- left: Literal at 2:0
| | | | | | | +-- value: 2.0
| | | | | | +-- right: Literal at 2:6
| | | | | | | +-- value: 2.0
| | | | | +-- right: Literal at 2:11
| | | | | | +-- value: 5.0
| +-- 2: ExpressionStatement at 3:0
| | +-- expression: SequenceExpression at 3:0
| | | +-- expressions:
| | | | +-- 0: BinaryExpression at 3:0
| | | | | +-- operator: !==
| | | | | +-- left: BinaryExpression at 3:0
| | | | | | +-- operator: /
| | | | | | +-- left: Literal at 3:0
| | | | | | | +-- value: 1.0
| | | | | | +-- right: Literal at 3:2
| | | | | | | +-- value: 0.0
| | | | | +-- right: Identifier at 3:8
| | | | | | +-- name: infty

View File

@ -0,0 +1,7 @@
Program at 1:1
+-- sourceType: script
+-- body:
| +-- 0: EmptyStatement at 1:1
| +-- 1: EmptyStatement at 2:0
| +-- 2: EmptyStatement at 2:2
| +-- 3: EmptyStatement at 3:1

View File

@ -0,0 +1,32 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: UpdateExpression at 1:0
| | | | | +-- operator: ++
| | | | | +-- argument: Identifier at 1:2
| | | | | | +-- name: a
| | | | | +-- prefix: True
| +-- 1: ExpressionStatement at 1:5
| | +-- expression: SequenceExpression at 1:5
| | | +-- expressions:
| | | | +-- 0: UpdateExpression at 1:5
| | | | | +-- operator: ++
| | | | | +-- argument: Identifier at 1:5
| | | | | | +-- name: b
| | | | | +-- prefix: False
| +-- 2: ExpressionStatement at 2:0
| | +-- expression: SequenceExpression at 2:0
| | | +-- expressions:
| | | | +-- 0: UpdateExpression at 2:0
| | | | | +-- operator: --
| | | | | +-- argument: Identifier at 2:0
| | | | | | +-- name: a
| | | | | +-- prefix: False
| | | | +-- 1: UpdateExpression at 2:5
| | | | | +-- operator: --
| | | | | +-- argument: Identifier at 2:7
| | | | | | +-- name: b
| | | | | +-- prefix: True

View File

@ -0,0 +1,13 @@
a = 1;
q *= 2;
d /= 2;
w %= 15;
b += 12;
c -= 14;
a <<= 1;
b >>= 2;
z >>>= 2;
v &= 1;
cc ^= cc;
vv|= a;
x **= 2;

View File

@ -0,0 +1,3 @@
2+2*2;
2 + 2 == 5;
1/0 !== infty;

View File

@ -0,0 +1,3 @@
;
; ;
;

View File

@ -0,0 +1,2 @@
++a; b++;
a--, --b;

103
tests/js_test_suite.py Normal file
View File

@ -0,0 +1,103 @@
from typing import List, Optional
import os
from pathlib import Path
import difflib
from jasminesnake.js_stream import JSStringStream
import jasminesnake.ast as js_ast
class JSTest:
_test_name: str
_test_file: str
_result_file: Optional[str]
@property
def name(self):
return self._test_name
def __init__(self, name: str, test_file: str, result_file: Optional[str]):
self._test_name = name
self._test_file = test_file
self._result_file = result_file
def run(self, must_fail: bool = False):
payload = Path(self._test_file).read_text()
jst = JSStringStream(payload)
tree = jst.parse()
ast_tree = None
try:
ast_tree = js_ast.from_parse_tree(tree)
except NotImplementedError as e:
print("Seems like some nodes are not implemented :^)")
print("Error message: ")
if hasattr(e, "message"):
print(e.message)
else:
print(e)
return must_fail
expected = None
if self._result_file is not None:
expected = Path(self._result_file).read_text()
got = js_ast.to_ascii_tree(ast_tree, ast_format="short")
del jst
del tree
del ast_tree
if expected is not None:
if expected == got:
print("Test {} OK...".format(self._test_name))
else:
print(
"Error in test `{}':\nExpected:\n```{}```\nGot:\n```{}```".format(
self._test_name, expected, got
)
)
# for i, s in enumerate(difflib.ndiff(expected, got)):
# if s[0] != " ":
# print(s[0], ord(s[-1]), "at", i)
return False
else:
print("Test {} has no result file!\nGot:\n{}".format(self._test_name, got))
return False
return True
class JSTestCollection:
tests: List[JSTest]
def __init__(self, module_path: str):
self.tests = []
basedir = os.path.abspath(module_path)
tests_dir = os.path.join(basedir, "t")
results_dir = os.path.join(basedir, "r")
# Collect tests
test_files = [
os.path.join(tests_dir, f)
for f in os.listdir(tests_dir)
if os.path.isfile(os.path.join(tests_dir, f))
]
test_names = [
os.path.splitext(os.path.basename(path))[0] for path in test_files
]
for (test_name, test_file) in zip(test_names, test_files):
result_file = os.path.join(results_dir, test_name) + ".ast"
print(test_name, test_file, result_file)
if not os.path.isfile(result_file):
result_file = None
test_case = JSTest(test_name, test_file, result_file)
self.tests.append(test_case)
def run_all(self, must_fail: bool = False):
for test in self.tests:
if not test.run(must_fail):
return False
return True

18
tests/literals/r/0001.ast Normal file
View File

@ -0,0 +1,18 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: ArrayExpression at 1:0
| | | | | +-- elements:
| | | | | | +-- 0: Literal at 1:1
| | | | | | | +-- value: 4.0
| | | | | | +-- 1: Literal at 1:3
| | | | | | | +-- value: 4.0
| | | | | | +-- 2: Literal at 1:5
| | | | | | | +-- value: 4.0
| | | | | | +-- 3: Literal at 1:7
| | | | | | | +-- value: 4.0
| | | | | | +-- 4: Literal at 1:9
| | | | | | | +-- value: 4.0

13
tests/literals/r/0002.ast Normal file
View File

@ -0,0 +1,13 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: ArrayExpression at 1:0
| | | | | +-- elements:
| | | | | | +-- 0: SpreadElement at 1:1
| | | | | | | +-- argument: Identifier at 1:4
| | | | | | | | +-- name: a
| | | | | | +-- 1: Identifier at 1:7
| | | | | | | +-- name: b

19
tests/literals/r/0003.ast Normal file
View File

@ -0,0 +1,19 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: VariableDeclaration at 1:0
| | +-- kind: let
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 1:4
| | | | +-- id: ArrayPattern at 1:4
| | | | | +-- elements:
| | | | | | +-- 0: Identifier at 1:5
| | | | | | | +-- name: a
| | | | | | +-- 1: Identifier at 1:8
| | | | | | | +-- name: b
| | | | +-- init: ArrayExpression at 1:13
| | | | | +-- elements:
| | | | | | +-- 0: Literal at 1:14
| | | | | | | +-- value: 4.0
| | | | | | +-- 1: Literal at 1:17
| | | | | | | +-- value: 4.0

33
tests/literals/r/0004.ast Normal file
View File

@ -0,0 +1,33 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: Literal at 1:0
| | | | | +-- value: 4444.0
| +-- 1: ExpressionStatement at 2:0
| | +-- expression: SequenceExpression at 2:0
| | | +-- expressions:
| | | | +-- 0: Literal at 2:0
| | | | | +-- value: 444444444.4444444
| +-- 2: ExpressionStatement at 3:0
| | +-- expression: SequenceExpression at 3:0
| | | +-- expressions:
| | | | +-- 0: Literal at 3:0
| | | | | +-- value: 4.0
| +-- 3: ExpressionStatement at 4:0
| | +-- expression: SequenceExpression at 4:0
| | | +-- expressions:
| | | | +-- 0: true
| | | | | +-- value: True
| +-- 4: ExpressionStatement at 5:0
| | +-- expression: SequenceExpression at 5:0
| | | +-- expressions:
| | | | +-- 0: false
| | | | | +-- value: False
| +-- 5: ExpressionStatement at 6:0
| | +-- expression: SequenceExpression at 6:0
| | | +-- expressions:
| | | | +-- 0: "String literal"
| | | | | +-- value: String literal

View File

@ -0,0 +1,8 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: ExpressionStatement at 1:0
| | +-- expression: SequenceExpression at 1:0
| | | +-- expressions:
| | | | +-- 0: "String literal, but single quoted"
| | | | | +-- value: String literal, but single quoted

1
tests/literals/t/0001.js Normal file
View File

@ -0,0 +1 @@
[4,4,4,4,4,];

1
tests/literals/t/0002.js Normal file
View File

@ -0,0 +1 @@
[...a, b];

1
tests/literals/t/0003.js Normal file
View File

@ -0,0 +1 @@
let [a, b] = [4, 4];

6
tests/literals/t/0004.js Normal file
View File

@ -0,0 +1,6 @@
4444;
444_444_444.444_444_444;
4;
true;
false;
"String literal";

1
tests/literals/t/0005.js Normal file
View File

@ -0,0 +1 @@
'String literal, but single quoted'

View File

@ -0,0 +1,38 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: BlockStatement at 1:0
| | +-- body:
| | | +-- 0: VariableDeclaration at 1:1
| | | | +-- kind: let
| | | | +-- declarations:
| | | | | +-- 0: VariableDeclarator at 1:5
| | | | | | +-- id: Identifier at 1:5
| | | | | | | +-- name: a
| | | | | | +-- init: Literal at 1:9
| | | | | | | +-- value: 15.0
| | | | | +-- 1: VariableDeclarator at 1:13
| | | | | | +-- id: Identifier at 1:13
| | | | | | | +-- name: b
| | | | | | +-- init: None
| | | +-- 1: BlockStatement at 1:16
| | | | +-- body:
| | | | | +-- 0: ExpressionStatement at 1:17
| | | | | | +-- expression: SequenceExpression at 1:17
| | | | | | | +-- expressions:
| | | | | | | | +-- 0: Literal at 1:17
| | | | | | | | | +-- value: 1337.228
| | | | | | | | +-- 1: false
| | | | | | | | | +-- value: False
| | | | | | | | +-- 2: "String"
| | | | | | | | | +-- value: String
| | | +-- 2: EmptyStatement at 1:45
| | | +-- 3: BlockStatement at 1:47
| | | | +-- body:
| | | | | +-- 0: VariableDeclaration at 1:48
| | | | | | +-- kind: var
| | | | | | +-- declarations:
| | | | | | | +-- 0: VariableDeclarator at 1:52
| | | | | | | | +-- id: Identifier at 1:52
| | | | | | | | | +-- name: aaaaaaaaaaaaaa
| | | | | | | | +-- init: None

View File

@ -0,0 +1,65 @@
Program at 1:0
+-- sourceType: script
+-- body:
| +-- 0: VariableDeclaration at 1:0
| | +-- kind: let
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 1:4
| | | | +-- id: Identifier at 1:4
| | | | | +-- name: a
| | | | +-- init: None
| +-- 1: VariableDeclaration at 2:0
| | +-- kind: var
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 2:4
| | | | +-- id: Identifier at 2:4
| | | | | +-- name: b
| | | | +-- init: None
| +-- 2: VariableDeclaration at 4:0
| | +-- kind: let
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 4:4
| | | | +-- id: Identifier at 4:4
| | | | | +-- name: zzz
| | | | +-- init: Literal at 4:10
| | | | | +-- value: 0.0
| | | +-- 1: VariableDeclarator at 4:13
| | | | +-- id: Identifier at 4:13
| | | | | +-- name: k
| | | | +-- init: None
| +-- 3: VariableDeclaration at 5:0
| | +-- kind: let
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 5:4
| | | | +-- id: Identifier at 5:4
| | | | | +-- name: cc
| | | | +-- init: None
| | | +-- 1: VariableDeclarator at 5:8
| | | | +-- id: Identifier at 5:8
| | | | | +-- name: asa
| | | | +-- init: None
| +-- 4: VariableDeclaration at 6:0
| | +-- kind: let
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 6:4
| | | | +-- id: Identifier at 6:4
| | | | | +-- name: cccccc
| | | | +-- init: None
| | | +-- 1: VariableDeclarator at 6:12
| | | | +-- id: Identifier at 6:12
| | | | | +-- name: asasa
| | | | +-- init: Literal at 6:20
| | | | | +-- value: 0.0
| +-- 5: VariableDeclaration at 8:0
| | +-- kind: var
| | +-- declarations:
| | | +-- 0: VariableDeclarator at 8:4
| | | | +-- id: Identifier at 8:4
| | | | | +-- name: x
| | | | +-- init: Literal at 8:8
| | | | | +-- value: 0.0
| | | +-- 1: VariableDeclarator at 8:11
| | | | +-- id: Identifier at 8:11
| | | | | +-- name: y
| | | | +-- init: Literal at 8:15
| | | | | +-- value: 0.0

View File

@ -0,0 +1 @@
{let a = 15, b; {1_337.2_28, false, "String"}; {var aaaaaaaaaaaaaa}}

View File

@ -0,0 +1,8 @@
let a;
var b;
let zzz = 0, k;
let cc, asa;
let cccccc, asasa = 0;
var x = 0, y = 0

View File

@ -1,15 +0,0 @@
// The code below won't work since the grammar treat Unicode
//"\x20\x77hil\x65\x20exe\x63uting c\x6cient e\x76\145\x6et ";
//{let a = 15, b}
{{{let t = false, n = true}}}
//++a15;
var a, b=14;
let c;
;
4444444444444444444444;
//228,1444444444444;t,fnm
//if (1) 0;
//function f4444444(){}

30
tests/test_ast.py Normal file
View File

@ -0,0 +1,30 @@
import os
import pytest
from js_test_suite import *
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
class TestAST:
def test_literals(self):
tcl = JSTestCollection(os.path.join(BASE_PATH, "literals"))
assert tcl.run_all()
def test_statements(self):
tcs = JSTestCollection(os.path.join(BASE_PATH, "statements"))
assert tcs.run_all()
# @pytest.mark.skip(reason="Not yet implemented")
def test_expressions(self):
tce = JSTestCollection(os.path.join(BASE_PATH, "expressions"))
assert tce.run_all()
# @pytest.mark.xfail(reason="Not yet implemented features.")
def test_todos(self):
tcb = JSTestCollection(os.path.join(BASE_PATH, "todos"))
assert tcb.run_all(must_fail=True)
@pytest.mark.xfail(reason="Bugs.")
def test_bugs(self):
tcb = JSTestCollection(os.path.join(BASE_PATH, "bugs"))
assert tcb.run_all(must_fail=True)

View File

@ -0,0 +1 @@
if (1) 0;

View File

@ -0,0 +1,4 @@
for (let a = 0; a < b; ++a) c++;
for (;b > c; --b) {a = b/c}
for (;;) {}

View File

@ -0,0 +1,4 @@
// Object literals?
let a = {a: 4, b: 4};
let k = {"a444": 444, 'b555': 555};