Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeDR committed Mar 16, 2020
1 parent 63b9e3c commit daa695a
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 28 deletions.
4 changes: 4 additions & 0 deletions tests/fixtures/multi_line_comment.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
test multi line comment
*/
foo = "bar"
3 changes: 3 additions & 0 deletions tests/fixtures/multi_line_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
4 changes: 4 additions & 0 deletions tests/fixtures/multi_line_comment_M.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"comment-L001": "/*\n test multi line comment\n*/",
"foo": "bar"
}
3 changes: 3 additions & 0 deletions tests/fixtures/single_line_comment.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// test single line comment with slash
# test single line comment with hashtag
foo = "bar"
3 changes: 3 additions & 0 deletions tests/fixtures/single_line_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
5 changes: 5 additions & 0 deletions tests/fixtures/single_line_comment_L.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"comment-L001": "test single line comment with slash",
"comment-L002": "test single line comment with hashtag",
"foo": "bar"
}
7 changes: 7 additions & 0 deletions tests/fixtures/structure_comment.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
foo {
// single line comment
/*
multi line comment
*/
foo = "bar"
}
7 changes: 7 additions & 0 deletions tests/fixtures/structure_comment_A.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"foo": {
"comment-L002": "single line comment",
"comment-L003": "/*\n multi line comment\n */",
"foo": "bar"
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/structure_comment_L.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"foo": {
"comment-L002": "single line comment",
"foo": "bar"
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/structure_comment_M.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"foo": {
"comment-L003": "/*\n multi line comment\n */",
"foo": "bar"
}
}
25 changes: 25 additions & 0 deletions tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,28 @@ def test_decoder(hcl_fname, json_fname, struct):

if struct is not None:
assert hcl_json == struct


COMMENTED_FIXTURES = [
('single_line_comment.hcl', 'single_line_comment_L.json', "single_line_comment.json", 'single_line_comment_L.json'),
('multi_line_comment.hcl', 'multi_line_comment.json', 'multi_line_comment_M.json', 'multi_line_comment_M.json'),
('structure_comment.hcl', 'structure_comment_L.json', 'structure_comment_M.json', 'structure_comment_A.json'),
('array_comment.hcl', 'array_comment.json', 'array_comment.json', 'array_comment.json')
]

@pytest.mark.parametrize("export_comments", ['LINE', 'MULTILINE', 'ALL'])
@pytest.mark.parametrize("hcl_fname,sline_fname,mline_fname,aline_fname", COMMENTED_FIXTURES)
def test_decoder_export_comments(hcl_fname, sline_fname, mline_fname, aline_fname, export_comments):
with open(join(FIXTURE_DIR, hcl_fname), 'r') as fp:
hcl_json = hcl.load(fp, export_comments)

json_fname = {
"LINE": sline_fname,
"MULTILINE": mline_fname,
"ALL": aline_fname
}

with open(join(FIXTURE_DIR, json_fname[export_comments]), 'r') as fp:
good_json = json.load(fp)

assert hcl_json == good_json
18 changes: 3 additions & 15 deletions tests/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,21 +391,9 @@ def test_tokens(token, input_string):
assert token == lex_tok.type
assert lexer.token() is None

@pytest.mark.parametrize("token,input_string", TOKEN_FIXTURES)
def test_tokens_with_export_comments_wrong_parameter(token, input_string):

print(input_string)

lexer = hcl.lexer.Lexer(export_comments="WRONG")
lexer.input(input_string)

lex_tok = lexer.token()

if lex_tok is None:
assert token is None
else:
assert token == lex_tok.type
assert lexer.token() is None
def test_export_comments_wrong_parameter():
with pytest.raises(ValueError):
lexer = hcl.lexer.Lexer(export_comments="WRONG")

ONE_LINE_COMMENT_FIXTURES = [
("COMMENT", "//"),
Expand Down
15 changes: 8 additions & 7 deletions tests/test_load_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,31 @@
),
]

@pytest.mark.parametrize("export_comments", [None, 'LINE', 'MULTILINE', 'ALL'])
@pytest.mark.parametrize("hcl_fname,invalid", PARSE_FIXTURES)
def test_parser_bytes(hcl_fname, invalid):

def test_parser_bytes(hcl_fname, invalid, export_comments):
with open(join(PARSE_FIXTURE_DIR, hcl_fname), 'rb') as fp:

input = fp.read()
print(input)

if not invalid:
hcl.loads(input)
hcl.loads(input, export_comments)
else:
with pytest.raises(ValueError):
hcl.loads(input)
hcl.loads(input, export_comments)

@pytest.mark.parametrize("export_comments", [None, 'LINE', 'MULTILINE', 'ALL'])
@pytest.mark.parametrize("hcl_fname,invalid", PARSE_FIXTURES)
def test_parser_str(hcl_fname, invalid):
def test_parser_str(hcl_fname, invalid, export_comments):

with open(join(PARSE_FIXTURE_DIR, hcl_fname), 'r') as fp:

input = fp.read()
print(input)

if not invalid:
hcl.loads(input)
hcl.loads(input, export_comments)
else:
with pytest.raises(ValueError):
hcl.loads(input)
hcl.loads(input, export_comments)
14 changes: 8 additions & 6 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,32 @@
),
]

@pytest.mark.parametrize("export_comments", [None, 'LINE', 'MULTILINE', 'ALL'])
@pytest.mark.parametrize("hcl_fname,invalid", PARSE_FIXTURES)
def test_parser_bytes(hcl_fname, invalid):
def test_parser_bytes(hcl_fname, invalid, export_comments):

with open(join(PARSE_FIXTURE_DIR, hcl_fname), 'rb') as fp:

input = fp.read()
print(input)

if not invalid:
hcl.loads(input)
hcl.loads(input, export_comments)
else:
with pytest.raises(ValueError):
hcl.loads(input)
hcl.loads(input, export_comments)

@pytest.mark.parametrize("export_comments", [None, 'LINE', 'MULTILINE', 'ALL'])
@pytest.mark.parametrize("hcl_fname,invalid", PARSE_FIXTURES)
def test_parser_str(hcl_fname, invalid):
def test_parser_str(hcl_fname, invalid, export_comments):

with open(join(PARSE_FIXTURE_DIR, hcl_fname), 'r') as fp:

input = fp.read()
print(input)

if not invalid:
hcl.loads(input)
hcl.loads(input, export_comments)
else:
with pytest.raises(ValueError):
hcl.loads(input)
hcl.loads(input, export_comments)

0 comments on commit daa695a

Please sign in to comment.