Skip to content

Commit 6c76604

Browse files
committed
Added tests
1 parent 49eff9e commit 6c76604

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Cython plugin for Lark, reimplementing the LALR parser & lexer for better pe
77
Usage:
88

99
```python
10-
from lark_cython import lark_cython
10+
import lark_cython
1111

12-
parser = Lark(grammar, _plugins=lark_cython.plugins)
12+
parser = Lark(grammar, parser="lalr", _plugins=lark_cython.plugins)
1313

1414
# Use Lark as you usually would, with a huge performance boost
1515
```

lark_cython/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
__version__ = "0.0.7"
1+
from .lark_cython import plugins, Token
2+
3+
__version__ = "0.0.8"

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
minversion = 6.0
3+
addopts = -ra -q
4+
testpaths =
5+
tests

tests/test_basic.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from lark import Lark, Tree
2+
import lark_cython
3+
4+
5+
def test_minimal():
6+
parser = Lark('!start: "a" "b"', parser='lalr', _plugins=lark_cython.plugins)
7+
res = parser.parse("ab")
8+
assert isinstance(res, Tree)
9+
assert all(isinstance(t, lark_cython.Token) for t in res.children)
10+
assert [t.value for t in res.children] == ['a', 'b']
11+
12+
def test_no_placeholders():
13+
parser = Lark('!start: "a" ["b"]', parser='lalr', _plugins=lark_cython.plugins, maybe_placeholders=True)
14+
15+
assert len(parser.parse("a").children) == 2
16+
assert len(parser.parse("ab").children) == 2
17+
18+
parser = Lark('!start: "a" ["b"]', parser='lalr', _plugins=lark_cython.plugins, maybe_placeholders=False)
19+
20+
assert len(parser.parse("a").children) == 1
21+
assert len(parser.parse("ab").children) == 2
22+
23+
def test_start():
24+
parser = Lark('!x: "a" "b"', parser='lalr', _plugins=lark_cython.plugins, start='x')
25+
res = parser.parse("ab")
26+
assert [t.value for t in res.children] == ['a', 'b']
27+
28+
def test_lexer_callbacks():
29+
comments = []
30+
31+
parser = Lark("""
32+
start: INT*
33+
34+
COMMENT: /#.*/
35+
36+
%import common (INT, WS)
37+
%ignore COMMENT
38+
%ignore WS
39+
""", parser="lalr", _plugins=lark_cython.plugins, lexer_callbacks={'COMMENT': comments.append})
40+
41+
42+
res = parser.parse("""
43+
1 2 3 # hello
44+
# world
45+
4 5 6
46+
""")
47+
48+
assert isinstance(res.children[0], lark_cython.Token)
49+
assert isinstance(comments[0], lark_cython.Token)
50+
assert len(comments) == 2

0 commit comments

Comments
 (0)