fix up tests and related issues

This commit is contained in:
Jonathan Wren 2020-02-29 14:44:42 -08:00
parent 47e0305aa0
commit 4d768ae433
No known key found for this signature in database
GPG key ID: 43D5FF8722E7F68A
4 changed files with 9 additions and 63 deletions

View file

@ -14,7 +14,7 @@ Feature: Contains
Given we use the config "tags.yaml" Given we use the config "tags.yaml"
When we run "jrnl @idea -contains software" When we run "jrnl @idea -contains software"
Then we should get no error Then we should get no error
and the output should contain "software" And the output should contain "software"
Scenario: Searching for a string within AND tag results Scenario: Searching for a string within AND tag results
Given we use the config "tags.yaml" Given we use the config "tags.yaml"

View file

@ -36,12 +36,12 @@ Feature: Basic reading and writing to a journal
@skip_win @skip_win
Scenario: Writing an empty entry from the editor Scenario: Writing an empty entry from the editor
Given we use the config "editor.yaml" Given we use the config "editor.yaml"
When we open the editor and enter "" When we open the editor and enter nothing
Then we should see the message "[Nothing saved to file]" Then we should see the message "[Nothing saved to file]"
Scenario: Writing an empty entry from the command line Scenario: Writing an empty entry from the command line
Given we use the config "basic.yaml" Given we use the config "basic.yaml"
When we run "jrnl" and enter "" When we run "jrnl" and enter nothing
Then the output should be Then the output should be
""" """

View file

@ -82,18 +82,15 @@ def set_config(context, config_file):
cf.write("version: {}".format(__version__)) cf.write("version: {}".format(__version__))
@when('we open the editor and enter ""')
@when('we open the editor and enter "{text}"') @when('we open the editor and enter "{text}"')
@when("we open the editor and enter nothing")
def open_editor_and_enter(context, text=""): def open_editor_and_enter(context, text=""):
text = text or context.text text = text or context.text or ""
def _mock_editor_function(command): def _mock_editor_function(command):
tmpfile = command[-1] tmpfile = command[-1]
with open(tmpfile, "w+") as f: with open(tmpfile, "w+") as f:
if text is not None: f.write(text)
f.write(text)
else:
f.write("")
return tmpfile return tmpfile
@ -119,7 +116,7 @@ def _mock_input(inputs):
@when('we run "{command}" and enter') @when('we run "{command}" and enter')
@when('we run "{command}" and enter ""') @when('we run "{command}" and enter nothing')
@when('we run "{command}" and enter "{inputs}"') @when('we run "{command}" and enter "{inputs}"')
def run_with_input(context, command, inputs=""): def run_with_input(context, command, inputs=""):
# create an iterator through all inputs. These inputs will be fed one by one # create an iterator through all inputs. These inputs will be fed one by one
@ -186,53 +183,6 @@ def no_error(context):
assert context.exit_status == 0, context.exit_status assert context.exit_status == 0, context.exit_status
@then("the output should be parsable as json")
def check_output_json(context):
out = context.stdout_capture.getvalue()
assert json.loads(out), out
@then('"{field}" in the json output should have {number:d} elements')
@then('"{field}" in the json output should have 1 element')
def check_output_field(context, field, number=1):
out = context.stdout_capture.getvalue()
out_json = json.loads(out)
assert field in out_json, [field, out_json]
assert len(out_json[field]) == number, len(out_json[field])
@then('"{field}" in the json output should not contain "{key}"')
def check_output_field_not_key(context, field, key):
out = context.stdout_capture.getvalue()
out_json = json.loads(out)
assert field in out_json
assert key not in out_json[field]
@then('"{field}" in the json output should contain "{key}"')
def check_output_field_key(context, field, key):
out = context.stdout_capture.getvalue()
out_json = json.loads(out)
assert field in out_json
assert key in out_json[field]
@then('the json output should contain {path} = "{value}"')
def check_json_output_path(context, path, value):
""" E.g.
the json output should contain entries.0.title = "hello"
"""
out = context.stdout_capture.getvalue()
struct = json.loads(out)
for node in path.split("."):
try:
struct = struct[int(node)]
except ValueError:
struct = struct[node]
assert struct == value, struct
def process_ANSI_escapes(text): def process_ANSI_escapes(text):
"""Escapes and 'unescapes' a string with ANSI escapes so that behave stdout """Escapes and 'unescapes' a string with ANSI escapes so that behave stdout
comparisons work properly. This will render colors, and works with unicode comparisons work properly. This will render colors, and works with unicode

View file

@ -226,13 +226,9 @@ def highlight_tags_with_background_color(entry, text, color, is_title=False):
if config["highlight"]: # highlight tags if config["highlight"]: # highlight tags
if entry.journal.search_tags: if entry.journal.search_tags:
text_fragments = [] text_fragments = []
for tag in entry.search_tags: for tag in entry.journal.search_tags:
text_fragments.extend( text_fragments.extend(
re.split( re.split(re.compile(re.escape(tag), re.IGNORECASE), text)
re.compile(re.escape(tag), re.IGNORECASE),
text,
flags=re.UNICODE,
)
) )
else: else:
text_fragments = re.split(entry.tag_regex(config["tagsymbols"]), text) text_fragments = re.split(entry.tag_regex(config["tagsymbols"]), text)