edward

An extensible POSIX-compatible implementation of the ed(1) text editor

git clone https://git.8pit.net/edward.git

 1#!/bin/sh
 2
 3cd "$(dirname "$0")"
 4
 5abort() {
 6	printf "${1}\n" 1>&2
 7	exit 1
 8}
 9
10run_editor() {
11	[ $# -eq 4 ] || exit 1
12	cmd="${1}"
13	cwd="${2}"
14	dat="${3}"
15	opt="${4}"
16
17	mkdir "${cwd}"
18	[ -d "${dat}" ] && cp -r "${dat}" "${cwd}"
19	cd "${cwd}"
20
21	if [ -s "${opt}" ]; then
22		set -- $(cat "${opt}")
23		"${cmd}" $@ 2>/dev/null
24	else
25		"${cmd}" 2>/dev/null
26	fi
27
28	cd - >/dev/null
29	unset cmd cwd dat opt
30}
31
32EDWARD="${EDWARD:-$(pwd)/../../bin/edward}"
33[ -x "${EDWARD}" ] || \
34	abort "Couldn't find edward executable '${EDWARD}'."
35
36REF_IMPL="$(command -v ed)"
37[ -x "${REF_IMPL}" ] || \
38	abort "Reference implementation not found."
39
40TESTDIR="/tmp/edward-test"
41TESTCWD="${TESTDIR}/cwd"
42EXPECTED="${TESTDIR}/expected"
43ACTUAL="${TESTDIR}/actual"
44
45mkdir -p "${TESTDIR}"
46trap "rm -rf '${TESTDIR}'" INT EXIT
47
48for test in *; do
49	[ -d "${test}" ] || continue
50
51	name="${test##*/}"
52	printf "Running test case '%s': " "${name}"
53
54	cmds="$(pwd)/${test}/cmds"
55	opts="$(pwd)/${test}/opts"
56
57	run_editor "${REF_IMPL}" "${TESTCWD}.expected" "${test}/testdata" "${opts}" \
58		> "${EXPECTED}" < "${cmds}"
59
60	run_editor "${EDWARD}" "${TESTCWD}.actual" "${test}/testdata" "${opts}" \
61		> "${ACTUAL}" < "${cmds}"
62
63	diff=$(diff -ur "${TESTCWD}.expected" "${TESTCWD}.actual")
64	if [ $? -ne 0 ]; then
65		printf "FAIL: Modified files differ.\n\n"
66		printf "%s\n" "${diff}"
67		exit 1
68	fi
69
70	diff=$(diff -u "${EXPECTED}" "${ACTUAL}")
71	if [ $? -ne 0 ]; then
72		printf "FAIL: Standard output didn't match.\n\n"
73		printf "%s\n" "${diff}"
74		exit 1
75	fi
76
77	printf "OK.\n"
78	rm -rf "${TESTCWD}.actual" "${TESTCWD}.expected"
79done