From 519be192501bae330db78af728c4d6e26b9053ee Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Tue, 30 Oct 2012 22:32:34 +0200 Subject: [PATCH 1/1] test: add new test tool parse-time for date/time parser Add a smoke testing tool to support testing the date/time parser module directly and independent of the rest of notmuch. Credits to Michal Sojka for the stdin parsing idea and consequent massive improvement in testability. --- test/Makefile.local | 7 +- test/basic | 2 +- test/parse-time.c | 314 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 321 insertions(+), 2 deletions(-) create mode 100644 test/parse-time.c diff --git a/test/Makefile.local b/test/Makefile.local index 45df4c71..9ae130a2 100644 --- a/test/Makefile.local +++ b/test/Makefile.local @@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules) $(dir)/symbol-test: $(dir)/symbol-test.o $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch $(XAPIAN_LDFLAGS) +$(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o + $(call quiet,CC) $^ -o $@ + .PHONY: test check -test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test +test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \ + $(dir)/parse-time test: all test-binaries @${dir}/notmuch-test $(OPTIONS) @@ -32,4 +36,5 @@ SRCS := $(SRCS) $(smtp_dummy_srcs) CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \ $(dir)/symbol-test $(dir)/symbol-test.o \ $(dir)/arg-test $(dir)/arg-test.o \ + $(dir)/parse-time $(dir)/parse-time.o \ $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.* diff --git a/test/basic b/test/basic index 3b635c83..c47197cb 100755 --- a/test/basic +++ b/test/basic @@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test' eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test) tests_in_suite=$(for i in $TESTS; do echo $i; done | sort) available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 | \ - sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \ + sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \ sort) test_expect_equal "$tests_in_suite" "$available" diff --git a/test/parse-time.c b/test/parse-time.c new file mode 100644 index 00000000..901a4dde --- /dev/null +++ b/test/parse-time.c @@ -0,0 +1,314 @@ +/* + * parse time string - user friendly date and time parser + * Copyright © 2012 Jani Nikula + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Author: Jani Nikula + */ + +#include +#include +#include +#include +#include +#include + +#include "parse-time-string.h" + +#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0])) + +static const char *parse_time_error_strings[] = { + [PARSE_TIME_OK] = "OK", + [PARSE_TIME_ERR] = "ERR", + [PARSE_TIME_ERR_LIB] = "LIB", + [PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET", + [PARSE_TIME_ERR_FORMAT] = "FORMAT", + [PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT", + [PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT", + [PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE", + [PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME", + [PARSE_TIME_ERR_KEYWORD] = "KEYWORD", +}; + +static const char * +parse_time_strerror (unsigned int errnum) +{ + if (errnum < ARRAY_SIZE (parse_time_error_strings)) + return parse_time_error_strings[errnum]; + else + return NULL; +} + +/* + * concat argv[start]...argv[end - 1], separating them by a single + * space, to a malloced string + */ +static char * +concat_args (int start, int end, char *argv[]) +{ + int i; + size_t len = 1; + char *p; + + for (i = start; i < end; i++) + len += strlen (argv[i]) + 1; + + p = malloc (len); + if (!p) + return NULL; + + *p = 0; + + for (i = start; i < end; i++) { + if (i != start) + strcat (p, " "); + strcat (p, argv[i]); + } + + return p; +} + +#define DEFAULT_FORMAT "%a %b %d %T %z %Y" + +static void +usage (const char *name) +{ + printf ("Usage: %s [options ...] []\n\n", name); + printf ( + "Parse and display it in given format. If is\n" + "not given, parse each line in stdin according to:\n\n" + " [(==>|==_>|==^>|==^^>)] [#]\n\n" + "and produce output:\n\n" + " (==>|==_>|==^>|==^^>)