From ef6ed93ecdc8cbbaedf8937f0a2b0a34c755fe86 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 10 Apr 2021 12:43:51 -0700 Subject: [PATCH] Add JSON parsing test and combine it with XML parsing test Co-authored-by: Jonathan Wren --- tests/features/format.feature | 8 ++++---- tests/step_defs/conftest.py | 18 +++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/features/format.feature b/tests/features/format.feature index 2ac5cdf2..8d3b0765 100644 --- a/tests/features/format.feature +++ b/tests/features/format.feature @@ -5,7 +5,7 @@ Feature: Custom formats And we use the password "test" if prompted When we run "jrnl --format json" Then we should get no error - And the output should be parsable as json + And the output should be valid JSON And "entries" in the json output should have 3 elements And "tags" in the json output should contain "@ipsum" And "tags" in the json output should contain "@tagone" @@ -26,7 +26,7 @@ Feature: Custom formats Given we use the config "dayone.yaml" When we run "jrnl --export json" Then we should get no error - And the output should be parsable as json + And the output should be valid JSON And the json output should contain entries.0.uuid = "4BB1F46946AD439996C9B59DE7C4DDC1" Scenario Outline: Printing a journal that has multiline entries with tags @@ -67,9 +67,9 @@ Feature: Custom formats Given we use the config "" And we use the password "test" if prompted When we run "jrnl -until 'August 2020' --format json" - Then the output should be parsable as json + Then the output should be valid JSON Then we should get no error - And the output should be parsable as json + And the output should be valid JSON And "entries" in the json output should have 2 elements And "tags" in the json output should contain "@ipsum" And "tags" in the json output should contain "@tagone" diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index e0c60a5e..b9eeec20 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -2,6 +2,7 @@ # License: https://www.gnu.org/licenses/gpl-3.0.html import ast +import json import os from collections import defaultdict from keyring import backend @@ -446,13 +447,16 @@ def cache_dir_contains_files(file_list, cache_dir): assert actual_files == expected_files, [actual_files, expected_files] - -@then("the output should be a valid XML string") -def assert_valid_xml_string(cli_run): - output = cli_run["stdout"] - xml_tree = ElementTree.fromstring(output) - assert xml_tree, output - +@then(parse("the output should be valid {language_name}")) +def assert_output_is_valid_language(cli_run, language_name): + language_name = language_name.upper() + if language_name == "XML": + xml_tree = ElementTree.fromstring(cli_run["stdout"]) + assert xml_tree, "Invalid XML" + elif language_name == "JSON": + assert json.loads(cli_run["stdout"]), "Invalid JSON" + else: + assert False, f"Language name {language_name} not recognized" @then(parse('"{item}" node in the xml output should have {number:d} elements')) def assert_xml_output_entries_count(item, number, cli_run):