diff --git a/tests/fixtures/multi_line_comment.hcl b/tests/fixtures/multi_line_comment.hcl new file mode 100644 index 0000000..f90cb11 --- /dev/null +++ b/tests/fixtures/multi_line_comment.hcl @@ -0,0 +1,4 @@ +/* + test multi line comment +*/ +foo = "bar" \ No newline at end of file diff --git a/tests/fixtures/multi_line_comment.json b/tests/fixtures/multi_line_comment.json new file mode 100644 index 0000000..8a79687 --- /dev/null +++ b/tests/fixtures/multi_line_comment.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} \ No newline at end of file diff --git a/tests/fixtures/multi_line_comment_M.json b/tests/fixtures/multi_line_comment_M.json new file mode 100644 index 0000000..9fe6147 --- /dev/null +++ b/tests/fixtures/multi_line_comment_M.json @@ -0,0 +1,4 @@ +{ + "comment-L001": "/*\n test multi line comment\n*/", + "foo": "bar" +} \ No newline at end of file diff --git a/tests/fixtures/single_line_comment.hcl b/tests/fixtures/single_line_comment.hcl new file mode 100644 index 0000000..812c60a --- /dev/null +++ b/tests/fixtures/single_line_comment.hcl @@ -0,0 +1,3 @@ +// test single line comment with slash +# test single line comment with hashtag +foo = "bar" diff --git a/tests/fixtures/single_line_comment.json b/tests/fixtures/single_line_comment.json new file mode 100644 index 0000000..8a79687 --- /dev/null +++ b/tests/fixtures/single_line_comment.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} \ No newline at end of file diff --git a/tests/fixtures/single_line_comment_L.json b/tests/fixtures/single_line_comment_L.json new file mode 100644 index 0000000..978390c --- /dev/null +++ b/tests/fixtures/single_line_comment_L.json @@ -0,0 +1,5 @@ +{ + "comment-L001": "test single line comment with slash", + "comment-L002": "test single line comment with hashtag", + "foo": "bar" +} \ No newline at end of file diff --git a/tests/fixtures/structure_comment.hcl b/tests/fixtures/structure_comment.hcl new file mode 100644 index 0000000..72596bd --- /dev/null +++ b/tests/fixtures/structure_comment.hcl @@ -0,0 +1,7 @@ +foo { + // single line comment + /* + multi line comment + */ + foo = "bar" +} \ No newline at end of file diff --git a/tests/fixtures/structure_comment_A.json b/tests/fixtures/structure_comment_A.json new file mode 100644 index 0000000..c53c66c --- /dev/null +++ b/tests/fixtures/structure_comment_A.json @@ -0,0 +1,7 @@ +{ + "foo": { + "comment-L002": "single line comment", + "comment-L003": "/*\n multi line comment\n */", + "foo": "bar" + } +} \ No newline at end of file diff --git a/tests/fixtures/structure_comment_L.json b/tests/fixtures/structure_comment_L.json new file mode 100644 index 0000000..19a508f --- /dev/null +++ b/tests/fixtures/structure_comment_L.json @@ -0,0 +1,6 @@ +{ + "foo": { + "comment-L002": "single line comment", + "foo": "bar" + } +} \ No newline at end of file diff --git a/tests/fixtures/structure_comment_M.json b/tests/fixtures/structure_comment_M.json new file mode 100644 index 0000000..1e9b3d7 --- /dev/null +++ b/tests/fixtures/structure_comment_M.json @@ -0,0 +1,6 @@ +{ + "foo": { + "comment-L003": "/*\n multi line comment\n */", + "foo": "bar" + } +} \ No newline at end of file diff --git a/tests/test_decoder.py b/tests/test_decoder.py index d4ba6ce..c87d990 100644 --- a/tests/test_decoder.py +++ b/tests/test_decoder.py @@ -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 \ No newline at end of file diff --git a/tests/test_lexer.py b/tests/test_lexer.py index 1a76c10..7f2f16c 100644 --- a/tests/test_lexer.py +++ b/tests/test_lexer.py @@ -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", "//"), diff --git a/tests/test_load_dump.py b/tests/test_load_dump.py index 5984541..7c6f9b2 100644 --- a/tests/test_load_dump.py +++ b/tests/test_load_dump.py @@ -70,22 +70,23 @@ ), ] +@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: @@ -93,7 +94,7 @@ def test_parser_str(hcl_fname, invalid): 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) diff --git a/tests/test_parser.py b/tests/test_parser.py index 5984541..8cec4a6 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -70,8 +70,9 @@ ), ] +@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: @@ -79,13 +80,14 @@ def test_parser_bytes(hcl_fname, invalid): 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: @@ -93,7 +95,7 @@ def test_parser_str(hcl_fname, invalid): 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)