diff --git a/tests/lib/helpers.py b/tests/lib/helpers.py index c91633d1..a6ec5cb9 100644 --- a/tests/lib/helpers.py +++ b/tests/lib/helpers.py @@ -4,8 +4,6 @@ import functools import os -from parse_type import TypeBuilder - def does_directory_contain_files(file_list, directory_path): if not os.path.isdir(directory_path): @@ -34,11 +32,6 @@ def does_directory_contain_n_files(directory_path, number): return int(number) == count -should_or_should_not_choice = TypeBuilder.make_enum( - {"should": True, "should not": False} -) - - def assert_equal_tags_ignoring_order( actual_line, expected_line, actual_content, expected_content ): diff --git a/tests/lib/then_steps.py b/tests/lib/then_steps.py index 3e55929b..fd697dfb 100644 --- a/tests/lib/then_steps.py +++ b/tests/lib/then_steps.py @@ -15,7 +15,9 @@ from tests.lib.helpers import assert_equal_tags_ignoring_order from tests.lib.helpers import does_directory_contain_files from tests.lib.helpers import does_directory_contain_n_files from tests.lib.helpers import get_nested_val -from tests.lib.helpers import should_or_should_not_choice +from tests.lib.type_builders import should_choice + +SHOULD_DICT = {"Should": should_choice} @then("we should get no error") @@ -31,54 +33,38 @@ def output_should_match(regex, cli_run): assert matches, f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}" +@then(parse("the output {it_should:Should} contain\n{expected_output}", SHOULD_DICT)) +@then(parse('the output {it_should:Should} contain "{expected_output}"', SHOULD_DICT)) @then( parse( - "the output {should_or_should_not:ShouldOrShouldNot} contain\n{expected_output}", - dict(ShouldOrShouldNot=should_or_should_not_choice), + "the {which_output_stream} output {it_should:Should} contain\n{expected_output}", + SHOULD_DICT, ) ) @then( parse( - 'the output {should_or_should_not:ShouldOrShouldNot} contain "{expected_output}"', - dict(ShouldOrShouldNot=should_or_should_not_choice), + 'the {which_output_stream} output {it_should:Should} contain "{expected_output}"', + SHOULD_DICT, ) ) -@then( - parse( - "the {which_output_stream} output {should_or_should_not:ShouldOrShouldNot} contain\n{expected_output}", - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -@then( - parse( - 'the {which_output_stream} output {should_or_should_not:ShouldOrShouldNot} contain "{expected_output}"', - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -def output_should_contain( - expected_output, which_output_stream, cli_run, should_or_should_not -): +def output_should_contain(expected_output, which_output_stream, cli_run, it_should): output_str = f"\nEXPECTED:\n{expected_output}\n\nACTUAL STDOUT:\n{cli_run['stdout']}\n\nACTUAL STDERR:\n{cli_run['stderr']}" assert expected_output if which_output_stream is None: - assert ((expected_output in cli_run["stdout"]) == should_or_should_not) or ( - (expected_output in cli_run["stderr"]) == should_or_should_not + assert ((expected_output in cli_run["stdout"]) == it_should) or ( + (expected_output in cli_run["stderr"]) == it_should ), output_str elif which_output_stream == "standard": - assert ( - expected_output in cli_run["stdout"] - ) == should_or_should_not, output_str + assert (expected_output in cli_run["stdout"]) == it_should, output_str elif which_output_stream == "error": - assert ( - expected_output in cli_run["stderr"] - ) == should_or_should_not, output_str + assert (expected_output in cli_run["stderr"]) == it_should, output_str else: assert ( expected_output in cli_run[which_output_stream] - ) == should_or_should_not, output_str + ) == it_should, output_str @then(parse("the output should not contain\n{expected_output}")) @@ -144,29 +130,19 @@ def default_journal_location(journal_file, journal_dir, config_on_disk, temp_dir @then( parse( - 'the config for journal "{journal_name}" {should_or_should_not:ShouldOrShouldNot} contain "{some_yaml}"', - dict(ShouldOrShouldNot=should_or_should_not_choice), + 'the config for journal "{journal_name}" {it_should:Should} contain "{some_yaml}"', + SHOULD_DICT, ) ) @then( parse( - 'the config for journal "{journal_name}" {should_or_should_not:ShouldOrShouldNot} contain\n{some_yaml}', - dict(ShouldOrShouldNot=should_or_should_not_choice), + 'the config for journal "{journal_name}" {it_should:Should} contain\n{some_yaml}', + SHOULD_DICT, ) ) -@then( - parse( - 'the config {should_or_should_not:ShouldOrShouldNot} contain "{some_yaml}"', - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -@then( - parse( - "the config {should_or_should_not:ShouldOrShouldNot} contain\n{some_yaml}", - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -def config_var_on_disk(config_on_disk, journal_name, should_or_should_not, some_yaml): +@then(parse('the config {it_should:Should} contain "{some_yaml}"', SHOULD_DICT)) +@then(parse("the config {it_should:Should} contain\n{some_yaml}", SHOULD_DICT)) +def config_var_on_disk(config_on_disk, journal_name, it_should, some_yaml): actual = config_on_disk if journal_name: actual = actual["journals"][journal_name] @@ -178,36 +154,28 @@ def config_var_on_disk(config_on_disk, journal_name, should_or_should_not, some_ # `expected` objects formatted in yaml only compare one level deep actual_slice = {key: actual.get(key, None) for key in expected.keys()} - assert (expected == actual_slice) == should_or_should_not + assert (expected == actual_slice) == it_should @then( parse( - 'the config in memory for journal "{journal_name}" {should_or_should_not:ShouldOrShouldNot} contain "{some_yaml}"', - dict(ShouldOrShouldNot=should_or_should_not_choice), + 'the config in memory for journal "{journal_name}" {it_should:Should} contain "{some_yaml}"', + SHOULD_DICT, ) ) @then( parse( - 'the config in memory for journal "{journal_name}" {should_or_should_not:ShouldOrShouldNot} contain\n{some_yaml}', - dict(ShouldOrShouldNot=should_or_should_not_choice), + 'the config in memory for journal "{journal_name}" {it_should:Should} contain\n{some_yaml}', + SHOULD_DICT, ) ) @then( - parse( - 'the config in memory {should_or_should_not:ShouldOrShouldNot} contain "{some_yaml}"', - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) + parse('the config in memory {it_should:Should} contain "{some_yaml}"', SHOULD_DICT) ) @then( - parse( - "the config in memory {should_or_should_not:ShouldOrShouldNot} contain\n{some_yaml}", - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) + parse("the config in memory {it_should:Should} contain\n{some_yaml}", SHOULD_DICT) ) -def config_var_in_memory( - config_in_memory, journal_name, should_or_should_not, some_yaml -): +def config_var_in_memory(config_in_memory, journal_name, it_should, some_yaml): actual = config_in_memory["overrides"] if journal_name: actual = actual["journals"][journal_name] @@ -219,7 +187,7 @@ def config_var_in_memory( # `expected` objects formatted in yaml only compare one level deep actual_slice = {key: get_nested_val(actual, key) for key in expected.keys()} - assert (expected == actual_slice) == should_or_should_not + assert (expected == actual_slice) == it_should @then("we should be prompted for a password") @@ -258,33 +226,27 @@ def journal_directory_should_not_exist(config_on_disk, journal_name): ), f'Journal "{journal_name}" does exist' -@then( - parse( - "the journal {should_or_should_not:ShouldOrShouldNot} exist", - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -def journal_should_not_exist(config_on_disk, should_or_should_not): +@then(parse("the journal {it_should:Should} exist", SHOULD_DICT)) +def journal_should_not_exist(config_on_disk, it_should): scoped_config = scope_config(config_on_disk, "default") expected_path = scoped_config["journal"] contains_files = does_directory_contain_files(expected_path, ".") - assert contains_files == should_or_should_not + assert contains_files == it_should @then( parse( - 'the journal "{journal_name}" directory {should_or_should_not:ShouldOrShouldNot} exist', - dict(ShouldOrShouldNot=should_or_should_not_choice), + 'the journal "{journal_name}" directory {it_should:Should} exist', SHOULD_DICT ) ) -def directory_should_not_exist(config_on_disk, should_or_should_not, journal_name): +def directory_should_not_exist(config_on_disk, it_should, journal_name): scoped_config = scope_config(config_on_disk, journal_name) expected_path = scoped_config["journal"] dir_exists = os.path.isdir(expected_path) - assert dir_exists == should_or_should_not + assert dir_exists == it_should @then(parse('the content of file "{file_path}" in the cache should be\n{file_content}')) @@ -419,33 +381,23 @@ def count_elements(number, item, cli_run): assert len(xml_tree.findall(".//" + item)) == number +@then(parse("the editor {it_should:Should} have been called", SHOULD_DICT)) @then( parse( - "the editor {should_or_should_not:ShouldOrShouldNot} have been called", - dict(ShouldOrShouldNot=should_or_should_not_choice), + "the editor {it_should:Should} have been called with {num_args} arguments", + SHOULD_DICT, ) ) -@then( - parse( - "the editor {should_or_should_not:ShouldOrShouldNot} have been called with {num_args} arguments", - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -def count_editor_args(num_args, cli_run, editor_state, should_or_should_not): - assert cli_run["mocks"]["editor"].called == should_or_should_not +def count_editor_args(num_args, cli_run, editor_state, it_should): + assert cli_run["mocks"]["editor"].called == it_should if isinstance(num_args, int): assert len(editor_state["command"]) == int(num_args) -@then( - parse( - "the stdin prompt {should_or_should_not:ShouldOrShouldNot} have been called", - dict(ShouldOrShouldNot=should_or_should_not_choice), - ) -) -def stdin_prompt_called(cli_run, should_or_should_not): - assert cli_run["mocks"]["stdin_input"].called == should_or_should_not +@then(parse("the stdin prompt {it_should:Should} have been called", SHOULD_DICT)) +def stdin_prompt_called(cli_run, it_should): + assert cli_run["mocks"]["stdin_input"].called == it_should @then(parse('the editor filename should end with "{suffix}"')) diff --git a/tests/lib/type_builders.py b/tests/lib/type_builders.py new file mode 100644 index 00000000..16171ef0 --- /dev/null +++ b/tests/lib/type_builders.py @@ -0,0 +1,11 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +from parse_type import TypeBuilder + +should_choice = TypeBuilder.make_enum( + { + "should": True, + "should not": False, + } +)