[Libguestfs] [PATCH 2/3] fish: basic tests for readline escaping
Pino Toscano
ptoscano at redhat.com
Wed Nov 5 18:11:54 UTC 2014
On Friday 31 October 2014 18:18:15 mzatko at redhat.com wrote:
> +#testquoting_LDADD = $(top_builddir)/fish/rl.o
Make sure to not leave commented stuff around.
> +# testquoting_CFLAGS = -I$(top_srcdir)
Ditto.
> diff --git a/fish/test/testquoting.c b/fish/test/testquoting.c
> new file mode 100644
> index 0000000..b0b525b
> --- /dev/null
> +++ b/fish/test/testquoting.c
> @@ -0,0 +1,120 @@
> +#include <config.h>
Missing copyright/license block here.
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <memory.h>
memory.h is not needed here.
> +#include <guestfs.h>
> +#include <guestfs-internal-frontend.h>
> +#include <rl.h>
Prefer the "..." form for these.
> +int
> +eq_bracket (char *(*fn)(char*), char * in, char * out)
> +{
> + char * q = fn(in);
> + return (q != NULL) && STREQ(q, out);
While it's just a test, make sure to free 'q' here; this eases
debugging with valgrind for memory leaks in the non-test code.
Also 'out' can be const, since it is passed as const buffer.
> +int
> +test_empty_escape (void)
> +{
> + return eq_bracket(bs_escape_filename, "", "");
> +}
> +
> +int
> +test_empty_unescape (void)
> +{
> + return eq_bracket(bs_unescape_filename, "", "");
> +}
> +
> +int
> +test_singlespace_escape (void)
> +{
> + return eq_bracket(bs_escape_filename, " ", "\\ ");
> +}
> +
> +int
> +test_singlespace_unescape (void)
> +{
> + return eq_bracket(bs_unescape_filename, "\\ ", " ");
> +}
> +
> +int
> +test_singleword_escape (void)
> +{
> + return eq_bracket(bs_escape_filename, "singleword", "singleword");
> +}
> +
> +int
> +test_singleword_unescape (void)
> +{
> + return eq_bracket(bs_unescape_filename, "singleword",
> "singleword"); +}
> +
> +int
> +test_multiword_escape (void)
> +{
> + return eq_bracket(bs_escape_filename, "more than one word\n",
> "more\\ than\\ one\\ word\\n"); +}
> +
> +int
> +test_nonprinting_escape (void)
> +{
> + return eq_bracket(bs_escape_filename, "\xac\xec\x8",
> "\\xac\\xec\\b"); +}
> +
> +int
> +test_multiword_unescape (void)
> +{
> + return eq_bracket(bs_unescape_filename, "more\\ than\\ one\\ word",
> "more than one word"); +}
> +
> +int
> +test_nonprinting_unescape (void)
> +{
> + return eq_bracket(bs_unescape_filename, "\\xac\\xec\\b",
> "\xac\xec\b"); +}
> +
> +
> +
> +struct test_t {
> + char *name;
> + int (*fn)(void);
> + int expect;
> +};
Hmm there's lot of repetition in the code above; what about providing
the tests in a structure? Something like:
struct string_test_data {
const char *in;
const char *out;
int pass;
}
struct string_test_data escape_tests[] = {
{ "", "", 1 },
{ " ", "\\ ", 1 },
...
}
size_t nr_escape_tests = sizeof(escape_tests) / sizeof(*escape_tests);
(ditto for unescape tests)
then do two iterations, for escape_tests and unescape_tests.
> +int
> +main (int argc, char *argv[])
> +{
> + int nr_failed = 0; // failed test count
> + setbuf(stdout, NULL);
No need to explicitly unbuffer stdout.
> + for (int i = 0; i < nr_tests; i++) {
> + fprintf(stdout, "%s: ", tests[i].name);
> + int r = tests[i].fn() == tests[i].expect;
> + fprintf(stdout, "%s\n", r ? "PASS" : "FAIL");
> + nr_failed += !r;
Just add nr_failed by 1, so the return value of the comparison can
change without affecting the count of failed tests.
--
Pino Toscano
More information about the Libguestfs
mailing list