Skip to content

Commit 4bfa455

Browse files
authored
Merge pull request #3 from sudo-jarvis/create-lexer
Add some basic tests for the lexer
2 parents 803fa6e + 25e2897 commit 4bfa455

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed
+147
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,149 @@
1+
"""
2+
JSON Schema Lexer tests
3+
"""
4+
5+
from pygments.token import Token
6+
import pytest
7+
8+
from jsonschema_lexer.lexer import JSONSchemaLexer
9+
10+
# Test helpers.
11+
12+
13+
@pytest.fixture()
14+
def lexer():
15+
return JSONSchemaLexer()
16+
17+
18+
@pytest.fixture()
19+
def data_types():
20+
return JSONSchemaLexer().parsed_data_types
21+
22+
23+
@pytest.fixture()
24+
def keywords():
25+
return JSONSchemaLexer().parsed_keywords
26+
27+
28+
def assert_single_token(lexer, s, token):
29+
"""Show that a given string generates only one token."""
30+
tokens = list(lexer.get_tokens_unprocessed(s))
31+
assert len(tokens) == 1
32+
assert s == tokens[0][2]
33+
assert token == tokens[0][1]
34+
35+
36+
def assert_tokens(lexer, string, expected_tokens):
37+
"""Show that a given string generates the expected tokens."""
38+
tokens = list(lexer.get_tokens_unprocessed(string))
39+
parsed_tokens = [t[1] for t in tokens]
40+
assert parsed_tokens == expected_tokens
41+
42+
43+
# Tests
44+
45+
146
def test_it_imports():
247
import jsonschema_lexer # noqa: F401
48+
49+
50+
def test_data_type_tokens(lexer, data_types):
51+
for data_type in data_types:
52+
assert_single_token(lexer, data_type, Token.Name.Decorator)
53+
54+
55+
def test_keyword_tokens(lexer, keywords):
56+
for keyword in keywords:
57+
sample_json_schema = f"""
58+
{{
59+
{keyword}:"test"
60+
}}
61+
""".strip()
62+
assert_tokens(
63+
lexer,
64+
sample_json_schema,
65+
[
66+
Token.Punctuation,
67+
Token.Text.Whitespace,
68+
Token.Keyword,
69+
Token.Punctuation,
70+
Token.Literal.String.Double,
71+
Token.Text.Whitespace,
72+
Token.Punctuation,
73+
],
74+
)
75+
76+
77+
def test_nested_json_schema(lexer):
78+
test_json_schema = """
79+
{
80+
"$schema": "https://json-schema.org/draft/2020-12/schema",
81+
"title": "Product",
82+
"description": "A product from Acme's catalog",
83+
"type": "object",
84+
"properties": {
85+
"productId": {
86+
"description": "The unique identifier for a product",
87+
"type": "integer"
88+
}
89+
}
90+
}
91+
""".strip()
92+
assert_tokens(
93+
lexer,
94+
test_json_schema,
95+
[
96+
Token.Punctuation,
97+
Token.Text.Whitespace,
98+
Token.Keyword,
99+
Token.Punctuation,
100+
Token.Text.Whitespace,
101+
Token.Literal.String.Double,
102+
Token.Punctuation,
103+
Token.Text.Whitespace,
104+
Token.Keyword,
105+
Token.Punctuation,
106+
Token.Text.Whitespace,
107+
Token.Literal.String.Double,
108+
Token.Punctuation,
109+
Token.Text.Whitespace,
110+
Token.Keyword,
111+
Token.Punctuation,
112+
Token.Text.Whitespace,
113+
Token.Literal.String.Double,
114+
Token.Punctuation,
115+
Token.Text.Whitespace,
116+
Token.Keyword,
117+
Token.Punctuation,
118+
Token.Text.Whitespace,
119+
Token.Name.Decorator,
120+
Token.Punctuation,
121+
Token.Text.Whitespace,
122+
Token.Keyword,
123+
Token.Punctuation,
124+
Token.Text.Whitespace,
125+
Token.Punctuation,
126+
Token.Text.Whitespace,
127+
Token.Name.Tag,
128+
Token.Punctuation,
129+
Token.Text.Whitespace,
130+
Token.Punctuation,
131+
Token.Text.Whitespace,
132+
Token.Keyword,
133+
Token.Punctuation,
134+
Token.Text.Whitespace,
135+
Token.Literal.String.Double,
136+
Token.Punctuation,
137+
Token.Text.Whitespace,
138+
Token.Keyword,
139+
Token.Punctuation,
140+
Token.Text.Whitespace,
141+
Token.Name.Decorator,
142+
Token.Text.Whitespace,
143+
Token.Punctuation,
144+
Token.Text.Whitespace,
145+
Token.Punctuation,
146+
Token.Text.Whitespace,
147+
Token.Punctuation,
148+
],
149+
)

noxfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def tests(session):
3737
"""
3838
Run the test suite.
3939
"""
40-
session.install("pytest")
40+
session.install("-r", REQUIREMENTS["tests"])
4141

4242
if session.posargs and session.posargs[0] == "coverage":
4343
if len(session.posargs) > 1 and session.posargs[1] == "github":

test-requirements.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
file:.#egg=jsonschema_lexer
22
pytest
3+
pygments==2.17.2

test-requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
file:.#egg=jsonschema_lexer
2+
pytest
3+
pygments==2.17.2

0 commit comments

Comments
 (0)