summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2025-07-11 23:24:46 +0200
committerMarc Vertes <mvertes@free.fr>2025-07-11 23:24:46 +0200
commitf89f3d28a14d0d8ea8c02acf115c9c1c972b70cf (patch)
treedcc025a073007ce15221db73406041e9300f5957
parent43e3a8a08668b068eb07b5c1ee3aaa767021d3c0 (diff)
add read command. Improve tests
-rwxr-xr-xmp16
-rw-r--r--test.json21
-rwxr-xr-xtests19
3 files changed, 42 insertions, 14 deletions
diff --git a/mp b/mp
index 9f5209a..6bd2fdf 100755
--- a/mp
+++ b/mp
@@ -26,18 +26,24 @@ $1 == "src" {
prompt()
next
}
+$1 == "read" {
+ sub(/^[[:space:]]+/, "")
+ $0 = substr($0, 6)
+ src = getfile($0)
+ prompt()
+ next
+}
$1 == "format" {
- print format_json(v, "", 1)
+ print format_json(v, "", $2)
prompt()
next
}
$1 == "parse" {
delete v
sub(/^[[:space:]]+/, "")
- $0 = substr($0, 6)
+ $0 = substr($0, 7)
if ($0) src = $0
parse_json(src, v)
- print format_json(v)
prompt()
next
}
@@ -56,7 +62,7 @@ function prompt(s) {
function getfile(name, l, r, o) {
while (r = getline l < name) {
- if (r == -1) return error("getfile " name ": getline error")
+ if (r == -1) return error("getfile " name ": getline error", o)
o = o l "\n"
}
return o
@@ -120,7 +126,7 @@ function parse_json(str, arr, pk, i, n) {
return n
}
-function format_json(arr, pk, il, i, l, t, s, bl, al, ps) {
+function format_json(arr, pk, il, i, l, t, s, bl, al, ps) {
if (il) {
bl = space(il * 2)
al = "\n"
diff --git a/test.json b/test.json
new file mode 100644
index 0000000..cd0eda1
--- /dev/null
+++ b/test.json
@@ -0,0 +1,21 @@
+{
+ "slideshow": {
+ "author": "Yours Truly",
+ "date": "date of publication",
+ "slides": [
+ {
+ "title": "Wake up to WonderWidgets!",
+ "type": "all"
+ },
+ {
+ "items": [
+ "Why <em>WonderWidgets</em> are great",
+ "Who <em>buys</em> WonderWidgets"
+ ],
+ "title": "Overview",
+ "type": "all"
+ }
+ ],
+ "title": "Sample Slide Show"
+ }
+}
diff --git a/tests b/tests
index 50e1c75..a13cfdb 100755
--- a/tests
+++ b/tests
@@ -2,7 +2,6 @@
# Test suite for mp.
run() {
- # shellcheck disable=SC2254
[ "$filter" ] && case $1 in ($filter) ;; (*) return; esac
out=$(echo "$2" | ./mp 2>&1)
[ "$out" = "$3" ] && pass=$((pass + 1)) && return
@@ -15,12 +14,14 @@ run() {
pass=0 fail=0 filter="$1"
trap 'echo "$pass passed, $fail failed"; exit $((fail))' EXIT
-run basic1 'parse' ''
-run basic2 'parse null' 'null'
-run basic3 'parse true' 'true'
-run basic4 'parse false' 'false'
-run num1 'parse 12' '12'
-run str1 'parse "hello"' '"hello"'
-run obj1 'parse {}' '{}'
-run arr1 'parse []' '[]'
+run basic1 "parse\nformat" ''
+run basic2 'parse null\nformat' 'null'
+run basic3 'parse true\nformat' 'true'
+run basic4 'parse false\nformat' 'false'
+run num1 'parse 12\nformat' '12'
+run str1 'parse "hello"\nformat' '"hello"'
+run arr1 'parse []\nformat' '[]'
+run arr2 'parse [null]\nformat' '[null]'
+run arr3 'parse [true,false]\nformat' '[true,false]'
+run obj1 'parse {}\nformat' '{}'