+# Like test_expect_equal, but arguments are JSON expressions to be
+# canonicalized before diff'ing. If an argument cannot be parsed, it
+# is used unchanged so that there's something to diff against.
+test_expect_equal_json () {
+ # The test suite forces LC_ALL=C, but this causes Python 3 to
+ # decode stdin as ASCII. We need to read JSON in UTF-8, so
+ # override Python's stdio encoding defaults.
+ output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
+ || echo "$1")
+ expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
+ || echo "$2")
+ shift 2
+ test_expect_equal "$output" "$expected" "$@"
+}
+
+# Sort the top-level list of JSON data from stdin.
+test_sort_json () {
+ PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c \
+ "import sys, json; json.dump(sorted(json.load(sys.stdin)),sys.stdout)"
+}
+
+test_emacs_expect_t () {
+ test "$#" = 1 ||
+ error "bug in the test script: not 1 parameter to test_emacs_expect_t"
+ if [ -z "$inside_subtest" ]; then
+ error "bug in the test script: test_emacs_expect_t without test_begin_subtest"
+ fi
+
+ # Run the test.
+ if ! test_skip "$test_subtest_name"
+ then
+ test_emacs "(notmuch-test-run $1)" >/dev/null
+
+ # Restore state after the test.
+ exec 1>&6 2>&7 # Restore stdout and stderr
+ inside_subtest=
+
+ # Report success/failure.
+ result=$(cat OUTPUT)
+ if [ "$result" = t ]
+ then
+ test_ok_
+ else
+ test_failure_ "${result}"
+ fi
+ else
+ # Restore state after the (non) test.
+ exec 1>&6 2>&7 # Restore stdout and stderr
+ inside_subtest=
+ fi
+}
+