Skip to content

Commit 0626280

Browse files
authored
Merge pull request #19 from lark-parser/python_tests
Added python tests
2 parents 46f5aa8 + 61ec095 commit 0626280

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

.github/workflows/node.js.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@ jobs:
2828
cache: 'npm'
2929
- run: npm ci
3030
- run: npm run build --if-present
31-
- run: npm test
31+
- name: Test JS library code
32+
run: npm test
33+
- name: Set up Python 3.8
34+
uses: actions/setup-python@v2
35+
with:
36+
python-version: 3.8
37+
- name: Install dependencies
38+
run: python -m pip install lark
39+
- name: Test parser generator (Python)
40+
run: python -m test.test

test/__init__.py

Whitespace-only changes.

test/json.lark

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
?start: value
2+
3+
?value: object
4+
| array
5+
| string
6+
| SIGNED_NUMBER -> number
7+
| "true" -> true
8+
| "false" -> false
9+
| "null" -> null
10+
11+
array : "[" [value ("," value)*] "]"
12+
object : "{" [pair ("," pair)*] "}"
13+
pair : string ":" value
14+
15+
string : ESCAPED_STRING
16+
17+
%import common.ESCAPED_STRING
18+
%import common.SIGNED_NUMBER
19+
%import common.WS
20+
21+
%ignore WS

test/test.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from subprocess import Popen, PIPE
2+
3+
from larkjs.__main__ import generate_js_standalone
4+
from lark import Lark
5+
6+
TEST_CODE = r"""
7+
8+
const parser = get_parser({transformer})
9+
10+
console.log(JSON.stringify(parser.parse(input_text)))
11+
"""
12+
13+
14+
class JsParser:
15+
def __init__(self, lark_instance):
16+
self.code = generate_js_standalone(lark_instance)
17+
18+
def parse(self, text, transformer=None):
19+
js_code = self.code
20+
js_code += 'const input_text = `' + text + '`;'
21+
if transformer:
22+
js_code += transformer
23+
js_code += TEST_CODE
24+
25+
p = Popen(["node", "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
26+
stdout, stderr = p.communicate(js_code.encode())
27+
28+
if stderr:
29+
raise ValueError(stderr.decode())
30+
return stdout.decode()
31+
32+
33+
34+
35+
def test_json_parser():
36+
parser = Lark.open('json.lark', rel_to=__file__, parser="lalr")
37+
js_parser = JsParser(parser)
38+
39+
transformer = """
40+
let transformer = {
41+
number: ([n]) => parseFloat(n.value),
42+
string: ([s]) => s.value.slice(1, -1),
43+
array: Array.from,
44+
pair: Array.from,
45+
object: Object.fromEntries,
46+
47+
null: () => null,
48+
true: () => true,
49+
false: () => false,
50+
}
51+
"""
52+
53+
text = r"""
54+
{
55+
"empty_object" : {},
56+
"empty_array" : [],
57+
"booleans" : { "YES" : true, "NO" : false },
58+
"numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
59+
"strings" : [ "This", [ "And" , "That", "And a \\"b" ] ],
60+
"nothing" : null
61+
}
62+
"""
63+
64+
res = js_parser.parse(text, transformer)
65+
expected = r"""{"empty_object":{},"empty_array":[],"booleans":{"YES":true,"NO":false},"numbers":[0,1,-2,3.3,440000,6.6e-7],"strings":["This",["And","That","And a \\\"b"]],"nothing":null}"""
66+
assert res.strip() == expected
67+
68+
def test():
69+
test_json_parser()
70+
71+
if __name__ == '__main__':
72+
test()

0 commit comments

Comments
 (0)