From 2b83272dcbcf646b69170dc6670b9be33c8cb0e3 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 23 Jan 2022 10:40:29 -0500 Subject: [PATCH 1/3] improve handling of tokens ending with braces --- tldr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 8acfbf4..57f48eb 100755 --- a/tldr.py +++ b/tldr.py @@ -296,7 +296,7 @@ def get_page( LEADING_SPACES_NUM = 2 -COMMAND_SPLIT_REGEX = re.compile(r'(?P{{.+?}})') +COMMAND_SPLIT_REGEX = re.compile(r'(?P{{.+?}*}})') PARAM_REGEX = re.compile(r'(?:{{)(?P.+?)(?:}})') From 9b72903efc5fad2a09a114df7a0e9329a22e7846 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 23 Jan 2022 10:52:14 -0500 Subject: [PATCH 2/3] add a testcase for jq --- tests/data/jq.md | 37 +++++++++++++++++++++++++++++++++++++ tests/data/jq_rendered | 30 ++++++++++++++++++++++++++++++ tests/test_tldr.py | 13 ++++++++----- 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 tests/data/jq.md create mode 100644 tests/data/jq_rendered diff --git a/tests/data/jq.md b/tests/data/jq.md new file mode 100644 index 0000000..0da5616 --- /dev/null +++ b/tests/data/jq.md @@ -0,0 +1,37 @@ +# jq + +> A command-line JSON processor that uses a domain-specific language. +> More information: . + +- Output a JSON file, in pretty-print format: + +`jq . {{file.json}}` + +- Output all elements from arrays (or all the values from objects) in a JSON file: + +`jq '.[]' {{file.json}}` + +- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`): + +`jq --slurp . {{file.json}}` + +- Output the first element in a JSON file: + +`jq '.[0]' {{file.json}}` + +- Output the value of a given key of each element in a JSON text from stdin: + +`cat {{file.json}} | jq 'map(.{{key_name}})'` + +- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`): + +`cat {{file.json}} | jq '{{{my_new_key}}: .{{key_name}}, {{my_other_key}}: .{{other_key_name}}}'` + +- Combine multiple filters: + +`cat {{file.json}} | jq 'unique | sort | reverse'` + +- Output the value of a given key to a string (and disable JSON output): + +`cat {{file.json}} | jq --raw-output '"some text: \(.{{key_name}})"'` + diff --git a/tests/data/jq_rendered b/tests/data/jq_rendered new file mode 100644 index 0000000..f850dbf --- /dev/null +++ b/tests/data/jq_rendered @@ -0,0 +1,30 @@ + + jq + + A command-line JSON processor that uses a domain-specific language. + More information: https://stedolan.github.io/jq. + + - Output a JSON file, in pretty-print format: + jq . file.json + + - Output all elements from arrays (or all the values from objects) in a JSON file: + jq '.[]' file.json + + - Read JSON objects from a file into an array, and output it (inverse of `jq .[]`): + jq --slurp . file.json + + - Output the first element in a JSON file: + jq '.[0]' file.json + + - Output the value of a given key of each element in a JSON text from stdin: + cat file.json | jq 'map(.key_name)' + + - Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`): + cat file.json | jq '{my_new_key: .key_name, my_other_key: .other_key_name}' + + - Combine multiple filters: + cat file.json | jq 'unique | sort | reverse' + + - Output the value of a given key to a string (and disable JSON output): + cat file.json | jq --raw-output '"some text: \(.key_name)"' + diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 1d3c02f..066e5e6 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -6,10 +6,12 @@ import types from unittest import mock +page_names = ('gem', 'jq') -def test_whole_page(): - with open("tests/data/gem.md", "rb") as f_original: - with open("tests/data/gem_rendered", "rb") as f_rendered: +@pytest.mark.parametrize("page_name", page_names) +def test_whole_page(page_name): + with open(f"tests/data/{page_name}.md", "rb") as f_original: + with open(f"tests/data/{page_name}_rendered", "rb") as f_rendered: old_stdout = sys.stdout sys.stdout = io.StringIO() sys.stdout.buffer = types.SimpleNamespace() @@ -24,8 +26,9 @@ def test_whole_page(): assert tldr_output == correct_output -def test_markdown_mode(): - with open("tests/data/gem.md", "rb") as f_original: +@pytest.mark.parametrize("page_name", page_names) +def test_markdown_mode(page_name): + with open(f"tests/data/{page_name}.md", "rb") as f_original: d_original = f_original.read() old_stdout = sys.stdout sys.stdout = io.StringIO() From b51588bd34b7b52776b441f5101fd3dd72568555 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 23 Jan 2022 10:52:58 -0500 Subject: [PATCH 3/3] add comments and fix linting --- tests/test_tldr.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 066e5e6..0ed0dd1 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -6,8 +6,11 @@ import types from unittest import mock +# gem is a basic test of page rendering +# jq is a more complicated test for token parsing page_names = ('gem', 'jq') + @pytest.mark.parametrize("page_name", page_names) def test_whole_page(page_name): with open(f"tests/data/{page_name}.md", "rb") as f_original: