#!/bin/sh # # Unit tests, mostly for substitution command because regular # expressions introduce a lot of edge cases for empty matches. fail=0 t() # run test for ned program with $1 stdin expecting $2 stdout { in=$1 out=$(printf %b "$2") res=$(printf %b "$in" | ./ned) [ "$res" = "$out" ] && return # pass fail=$((fail + 1)) printf 'FAIL\t%s\n\texpected [%s]\n\tgot [%s]\n' "$in" "$out" "res" } # Capture groups still don't work, I have to create new # substitution string after each search instead of what # I'm doing capturing it once at the beginning. # # t 'i"abc\\n" s/.(.)/\\0\\1/ pQ' abbc # capture groups t 'i"foo\\n" s/o/0/ pQ' f00 # simple replace t 'i"aaa\\n" s@a@b@ pQ' bbb # custom divider t 'i"abc\\n" s/b/B/ pQ' aBc # single char replace t 'i"fooo\\n" s/o/0/ pQ' f000 # replace all in region t 'i"a-b-c\\n" s/-// pQ' abc # delete by empty replace t 'i"ab\\n" s/z/Z\n pQ' ?\\nab # no match leaves text t 'i"ab\\na" s/^a/X/ pQ' Xb\\nX # caret per line t 'i"ab\\nab" s/b$/X/ pQ' aX\\naX # dollar per line t 'i"AaA\\n" Is/a/X/ pQ' XXX # case insensitive t 'i"bb" s/a*/X/ pQ' XbXb # empty match, nonempty sub t 'i"bb" s/a*// pQ' bb # empty match, empty sub t 'i"abc" s/x*/-/ pQ' -a-b-c # split every char t 'i"abb" s/b*/X/ pQ' XaX # no empty match after match t 'i"aab" s/a// pQ' b # delete adjacent matches t 'i"foo" s/o// pQ' f # delete keeps next char t 'i"abc" 0/b/ i"X" %pQ' aXbc # insert before region t 'i"abc" 0/b/ a"X" %pQ' abXc # append after region t 'i"abc" 0/b/ c"X" %pQ' aXc # change region t 'i"abc" 0/b/ d %pQ' ac # delete region t 'i"abc" i"X" %pQ' Xabc # insert at start t 'i"abc" c"X" %pQ' X # change whole buffer t 'i"abc" d %pQ' '' # delete whole buffer t 'i"abc" 0/b/yx %pQ' abbc # yank then paste t 'i"1\\n2\\n3\\n" 2d %pQ' 1\\n3 # delete second line t 'i"1\\n2\\n3\\n" 2 pQ' 2 # select and print a line exit $fail