changeset 689:4076dd063101

qstring: check test output against expected values Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 28 Feb 2019 22:10:03 -0500
parents e338babd5670
children 8823796c3498
files tests/qstring-basic/comment-body.lisp tests/qstring-basic/empty.lisp tests/qstring-basic/multi-char.lisp tests/qstring-basic/no-value-long.lisp tests/qstring-basic/no-value.lisp tests/qstring-basic/one.lisp tests/qstring-basic/two.lisp tests/test_qstring.c
diffstat 8 files changed, 70 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/comment-body.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,9 @@
+(("a" . "seo")
+ ("c" . "Hello Web Admin, I noticed that\n")
+ ("d" . "1448913859968688486")
+ ("e" . "dsqbketcxa@example.com")
+ ("i" . "535")
+ ("s" . "Submit Comment")
+ ("u" . "http://www.example.com/")
+ ("v" . "seo")
+ ("x" . "seo"))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/empty.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,1 @@
+()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/multi-char.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,2 @@
+(("p" . "5")
+ ("preview" . "1234"))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/no-value-long.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,1 @@
+(("just-a-very-long-string---longer-than-most-variable-names-would-be-because-we-want-to-make-sure-that-nothing-blows-up-when-there-is-no-value-and-just-a-long-name" . #n))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/no-value.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,1 @@
+(("abc" . #n))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/one.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,1 @@
+(("a" . "b"))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/qstring-basic/two.lisp	Thu Feb 28 22:10:03 2019 -0500
@@ -0,0 +1,2 @@
+(("a" . "b")
+ ("c" . "d"))
--- a/tests/test_qstring.c	Thu Feb 28 09:12:55 2019 -0500
+++ b/tests/test_qstring.c	Thu Feb 28 22:10:03 2019 -0500
@@ -22,12 +22,13 @@
 
 #include <stdlib.h>
 
+#include <jeffpc/sexpr.h>
 #include <jeffpc/qstring.h>
 #include <jeffpc/io.h>
 
 #include "test.c"
 
-static int onefile(char *ibuf, size_t len)
+static int onefile(char *ibuf, size_t len, struct nvlist *out)
 {
 	struct nvlist *vars;
 	int ret;
@@ -37,23 +38,72 @@
 		return -ENOMEM;
 
 	ret = qstring_parse_len(vars, ibuf, len);
-	if (!ret)
+	if (!ret) {
+		fprintf(stderr, "Got:\n");
 		nvl_dump_file(stderr, vars);
 
+		if (sexpr_equal(nvl_getref_val(out),
+				 nvl_getref_val(vars)))
+			fprintf(stderr, "ok.\n");
+		else
+			fail("mismatch!");
+	}
+
 	nvl_putref(vars);
 
 	return ret;
 }
 
+static struct nvlist *get_expected(const char *infname)
+{
+	const size_t len = strlen(infname);
+	char outfname[len + 3];
+	struct val *outval;
+	char *out;
+
+	strcpy(outfname, infname); /* duplicate */
+	outfname[len - 2] = '\0'; /* strip off 'qs' */
+	strcat(outfname, "lisp"); /* append 'lisp' */
+
+	out = read_file(outfname);
+	ASSERT(!IS_ERR(out));
+
+	outval = sexpr_parse(out, strlen(out));
+	ASSERT(!IS_ERR(outval));
+
+	outval = sexpr_compact(outval);
+	ASSERT(!IS_ERR(outval));
+
+	if (sexpr_is_null(outval)) {
+		val_putref(outval);
+
+		outval = val_alloc_nvl();
+		ASSERT(!IS_ERR(outval));
+	}
+
+	return val_cast_to_nvl(outval);
+
+}
+
 static void test(const char *fname)
 {
+	struct nvlist *out;
 	char *in;
 
+	/* input */
 	in = read_file(fname);
 	ASSERT(!IS_ERR(in));
 
-	if (onefile(in, strlen(in)))
+	/* output */
+	out = get_expected(fname);
+
+	fprintf(stderr, "Expected:\n");
+	nvl_dump_file(stderr, out);
+
+	if (onefile(in, strlen(in), out))
 		fail("failed");
 
+	nvl_putref(out);
+
 	free(in);
 }