about summary refs log tree commit diff
path: root/test.sh
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2023-07-27 16:08:28 -0400
committerequa <equaa@protonmail.com>2023-07-27 16:16:37 -0400
commit77ec331c7dacad6b5028783288eb705bdb9ad22e (patch)
treeda634e1e17752a34e0898592f7735fb1679066e7 /test.sh
initial commit
Diffstat (limited to 'test.sh')
-rwxr-xr-xtest.sh41
1 files changed, 41 insertions, 0 deletions
diff --git a/test.sh b/test.sh
new file mode 100755
index 0000000..a9b4e44
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+# basic testing framework!
+# we use lua, sqlite3 and sqldiff to generate and compare csv files.
+#
+# TODO: test the behavior of malformed csv files properly
+
+TARGETS="./csv.sh ./csv-min.sh"
+FUZZ=./fuzz.lua
+
+assert () {
+	test_name=$1
+	shift
+	# TODO make this nicer somehow
+	"$@" || printf "  test '%s' failed:\n    %.100s\n" "$test_name" "$*"
+}
+
+for target in $TARGETS; do
+	printf "testing %s\n" "$target"
+	. $target
+
+	rm -rf tmp/
+	mkdir -p tmp/
+
+	assert 'fuzz: correct number of entries' \
+		[ "$(lua "$FUZZ" 1000 | sqlite3 | from_csv | wc -l)" -eq 1000 ]
+
+	lua "$FUZZ" 1000 | sqlite3 > "tmp/a.csv"
+	from_csv < "tmp/a.csv" | to_csv > "tmp/b.csv"
+	for x in a b; do
+		echo "create table stuff(a, b, c);\n.import tmp/$x.csv stuff --csv" | sqlite3 "tmp/$x.sqlite3"
+	done
+
+	assert "processed CSV is the same" [ -z "$(sqldiff tmp/a.sqlite3 tmp/b.sqlite3)" ]
+
+	sed -n '/^< /s///p' tests.txt > tmp/a.txt
+	sed -n '/^> /s///p' tests.txt | from_csv > tmp/b.txt
+	assert "tests.txt" diff tmp/a.txt tmp/b.txt
+
+	rm -r tmp/
+done