From 1f563060887b979e0138567fb13bef97dc8fd915 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 18 Jul 2025 00:07:30 +0200 Subject: fix tests --- mp | 29 ++++++++++++++--------------- readme.md | 10 +++++----- tests | 4 +++- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/mp b/mp index c263004..c8d4bd7 100755 --- a/mp +++ b/mp @@ -1,4 +1,4 @@ -#!/usr/bin/awk -f +#!/usr/bin/env awk -f BEGIN { version = "0.1" @@ -10,11 +10,15 @@ BEGIN { " Prints cmd help or general help text.\n" \ " Example: help help\n" - filename = ARGV[1] - ARGV[1] = "/dev/stdin" - tty = 1 - system("test -t 0") + if (str) { + str = ARGV[1] + parse("", v, "", str) + print format(v) + exit ERROR != 0 + } else filename = ARGV[1] + delete ARGV[1] if (filename) parse(filename, v) - prompt("mp-" version " meta parser. Try \"help\".\n") + if (!notty) printf "%s\nmp> ", "mp version " version ". Type \"help\" for help." } { ERROR = "" @@ -41,22 +45,17 @@ BEGIN { } else if ($1 == "parse") { delete v parse($2, v) - } else if (filename && NF) { + } else if (NF) { error("invalid command: " $1) - } else { - parse("", v, "", $0) - print format(v) } - prompt() + if (ERROR) print ERROR > "/dev/stderr" + if (!notty) printf "%smp> ", s } -END { if (ERROR) exit 1 } +END { exit ERROR != "" } function error(s, n) { ERROR = s; return n } -function prompt(s) { - if (ERROR) print ERROR > "/dev/stderr" - if (tty) printf "%s> ", s > "/dev/stderr" -} +function debug(s) { print s > "/dev/stderr" } function scan(infile, line, r) { TOKEN = "" diff --git a/readme.md b/readme.md index 57b6462..66e468b 100644 --- a/readme.md +++ b/readme.md @@ -10,16 +10,16 @@ an array of strings indexed by strings. The JSON types are: true, false, Key elements: -- T: true type -- F: false type -- N: null type +- t: true type +- f: false type +- n: null type - s: string type -- n: number type +- d: number type - a: array type - o: object type - k: key (object only) - v: value (array and object) -- t: type (one of T, F, N, s, n, a, o) +- t: type (one of t, f, n, s, d, a, o) - [0-9]+: index of key or value ## I/O diff --git a/tests b/tests index 2fcbc3b..690da7a 100755 --- a/tests +++ b/tests @@ -1,9 +1,11 @@ #!/bin/sh # Test suite for mp. +# Usage: [FAILFAST=1] tests [pattern] +# Example: tests "obj*" run() { [ "$filter" ] && case $1 in ($filter) ;; (*) return; esac - out=$(printf %s "$2" | ./mp 2>&1) + out=$(./mp -v str=1 "$2" 2>&1) [ "$out" = "$3" ] && pass=$((pass + 1)) && return printf "%s FAIL\nWant: '%s'\n Got: '%s'\n" "$1" "$3" "$out" fail=$((fail + 1)) -- cgit v1.2.3