-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_basic.py
63 lines (50 loc) · 1.5 KB
/
test_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pytest
@pytest.fixture(params=[
('40', ['40']),
('[40]', '[ 40 ]'.split()),
('[true, false, null]', '[ true , false , null ]'.split()),
('{"foo": "bar"}', '{ "foo" : "bar" }'.split()),
])
def good(request):
return request.param
@pytest.fixture(params=['{True, False}', "'hello!'"])
def bad(request):
return request.param
@pytest.fixture
def lexer():
import lexer
try:
return lexer.lexer
except AttributeError:
msg = 'você não pode modificar o nome da função lexer() no módulo lexer!'
raise RuntimeError(msg)
class TestJSONLexer:
@pytest.fixture(params=[
('40', 'number'),
('"hello"', 'string'),
('true', 'constant'),
('false', 'constant'),
('null', 'constant'),
('[', 'lbrack'),
(']', 'rbrack'),
('{', 'lbrace'),
('}', 'rbrace'),
(',', 'comma'),
(':', 'colon'),
])
def tok_type(self, request):
return request.param
def test_lexer_do_not_raise_errors(self, lexer):
lexer('42')
def test_basic_tokens(self, tok_type, lexer):
src, tk_type = tok_type
tok = lexer(src)[0]
assert tok.type == tk_type
assert tok.value == src
def test_good_example(self, good, lexer):
src, expected = good
tokens = [tk.value for tk in lexer(src)]
assert tokens == expected
def test_bad_example(self, bad, lexer):
with pytest.raises(SyntaxError):
lexer(bad)