From f039dc7fcb3e9dd20166ecfe64d5428a812d5990 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 24 May 2020 10:49:37 +0300 Subject: test(sp-meta): amend scenario to also test JSON output --- subplot.md | 34 ++++++++++++++++++++++++++++++++++ subplot.py | 13 +++++++++++++ subplot.yaml | 6 ++++++ templates/python/template.py | 14 ++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/subplot.md b/subplot.md index ff1d702..74e23cf 100644 --- a/subplot.md +++ b/subplot.md @@ -1461,6 +1461,7 @@ and file f.py and file other.py and file foo.bib and file bar.bib +and file expected.json when I run sp-meta images.md then output matches /source: images.md/ and output matches /source: b.yaml/ @@ -1473,6 +1474,8 @@ and output matches /source: image.gif/ and output matches /bindings: b.yaml/ and output matches /bindings: other.yaml/ and output matches /functions: f.py/ +when I run sp-meta images.md -o json +then JSON output matches expected.json ~~~ @@ -1518,6 +1521,35 @@ bibliography: [foo.bib, bar.bib] } ~~~ +~~~{#expected.json .file .json} +{ + "title": "Document refers to external images", + "sources": [ + "images.md", + "b.yaml", + "other.yaml", + "f.py", + "other.py", + "foo.bib", + "bar.bib", + "image.gif" + ], + "binding_files": [ + "b.yaml", + "other.yaml" + ], + "function_files": [ + "f.py", + "other.py" + ], + "bibliographies": [ + "foo.bib", + "bar.bib" + ], + "scenarios": [] +} +~~~ + ## Embedded files @@ -1851,4 +1883,6 @@ functions: - subplot.py - runcmd.py - files.py +classes: +- json ... diff --git a/subplot.py b/subplot.py index 9e16168..bf6415f 100644 --- a/subplot.py +++ b/subplot.py @@ -1,3 +1,4 @@ +import json import os import re import subprocess @@ -54,6 +55,12 @@ def run_meta(ctx, filename=None): exit_code_is(ctx, 0) +def run_meta_json(ctx, filename=None): + meta = binary("sp-meta") + runcmd(ctx, [meta, filename, "-o", "json"]) + exit_code_is(ctx, 0) + + def run_pandoc_with_filter(ctx, filename=None, output=None): sp_filter = binary("sp-filter") runcmd(ctx, ["pandoc", "--filter", sp_filter, filename, "-o", output]) @@ -94,5 +101,11 @@ def cleanup_was_not_run(ctx, keyword=None, name=None): stdout_does_not_contain(ctx, pattern="\n cleanup: {} {}\n".format(keyword, name)) +def json_output_matches_file(ctx, filename=None): + actual = json.loads(ctx["stdout"]) + expected = json.load(open(filename)) + assert_dict_eq(actual, expected) + + def binary(basename): return os.path.join(srcdir, "target", "debug", basename) diff --git a/subplot.yaml b/subplot.yaml index f35cdef..da84a7d 100644 --- a/subplot.yaml +++ b/subplot.yaml @@ -25,6 +25,9 @@ - when: I run sp-meta {filename} function: run_meta +- when: I run sp-meta {filename} -o json + function: run_meta_json + - when: I run pandoc --filter sp-filter {filename} -o {output} function: run_pandoc_with_filter @@ -51,3 +54,6 @@ - then: cleanup for "(?Pgiven|when|then) (?P.+)" was not run function: cleanup_was_not_run regex: true + +- then: JSON output matches {filename} + function: json_output_matches_file diff --git a/templates/python/template.py b/templates/python/template.py index e430b1b..a1fa3ee 100644 --- a/templates/python/template.py +++ b/templates/python/template.py @@ -67,6 +67,20 @@ def assert_eq(a, b): def assert_ne(a, b): assert a != b, 'expected %r != %r' % (a, b) +# Check that two dict values are equal. +def assert_dict_eq(a, b): + assert isinstance(a, dict) + assert isinstance(b, dict) + for key in a: + assert key in b, f"exected {key} in both dicts" + av = a[key] + bv = b[key] + assert_eq(type(av), type(bv)) + if isinstance(av, list): + assert_eq(list(sorted(av)), list(sorted(bv))) + for key in b: + assert key in a, f"exected {key} in both dicts" + # Remember where we started from. The step functions may need to refer # to files there. srcdir = os.getcwd() -- cgit v1.2.1