Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 32F7342D28D for ; Sun, 23 Jan 2011 03:48:08 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qzh4m-zf4cdL for ; Sun, 23 Jan 2011 03:48:06 -0800 (PST) Received: from mail.loccal.net (gw.loccal.net [94.142.235.206]) by olra.theworths.org (Postfix) with ESMTP id C6B1642D280 for ; Sun, 23 Jan 2011 03:48:02 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.loccal.net (Postfix) with ESMTP id E43C11043A; Sun, 23 Jan 2011 13:01:17 +0100 (CET) X-Virus-Scanned: amavisd-new at loccal.net Received: from mail.loccal.net ([127.0.0.1]) by localhost (mail.loccal.net [127.0.0.1]) (amavisd-new, port 10024) with LMTP id N48jDBcPsm-s; Sun, 23 Jan 2011 13:01:10 +0100 (CET) Received: from steelpick.2x.cz (unknown [10.21.129.4]) by mail.loccal.net (Postfix) with ESMTPS id 9DB5B1BDAF; Sun, 23 Jan 2011 13:00:54 +0100 (CET) Received: from wsh by steelpick.2x.cz with local (Exim 4.72) (envelope-from ) id 1PgyPu-0005i8-RD; Sun, 23 Jan 2011 12:47:34 +0100 From: Michal Sojka To: notmuch@notmuchmail.org Subject: [PATCH 4/4] Add first date parser tests Date: Sun, 23 Jan 2011 12:47:27 +0100 Message-Id: <1295783247-21900-5-git-send-email-sojkam1@fel.cvut.cz> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1295783247-21900-1-git-send-email-sojkam1@fel.cvut.cz> References: <1295783247-21900-1-git-send-email-sojkam1@fel.cvut.cz> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Jan 2011 11:48:08 -0000 Some tests are currently broken for several reasons: 1) in the context of e-mails, we implicitly mean past dates e.g. Monday means the last Monday and not the next one, as it is currently implemented by the parser. 2) yesterday means "now -24 hours" and I think that should be "midnight -24 hours". --- lib/getdate.c | 52 +++++++++++++++++++++++++++++++++++++++----------- lib/getdate.y | 52 +++++++++++++++++++++++++++++++++++++++----------- test/Makefile.local | 9 ++++++- test/basic | 2 +- test/date-parser | 37 ++++++++++++++++++++++++++++++++++++ test/notmuch-test | 2 +- 6 files changed, 126 insertions(+), 28 deletions(-) create mode 100755 test/date-parser diff --git a/lib/getdate.c b/lib/getdate.c index 57f33e9..685c51f 100644 --- a/lib/getdate.c +++ b/lib/getdate.c @@ -3471,19 +3471,50 @@ get_date (struct timespec *result, char const *p, struct timespec const *now) #if TEST int +_internal_error (const char *format, ...) +{ + va_list va_args; + + va_start (va_args, format); + + fprintf (stderr, "Internal error: "); + vfprintf (stderr, format, va_args); + + exit (1); + + return 1; +} + +int main (int ac, char **av) { char buff[BUFSIZ]; - printf ("Enter date, or blank line to exit.\n\t> "); - fflush (stdout); - - buff[BUFSIZ - 1] = '\0'; - while (fgets (buff, BUFSIZ - 1, stdin) && buff[0]) + buff[BUFSIZ - 2] = '\0'; + while (fgets (buff, BUFSIZ - 2, stdin) && buff[0]) { - struct timespec d; + struct timespec d, ref = { 0, 0 }; struct tm const *tm; - if (! get_date (&d, buff, NULL)) + char *arrow; + arrow = strstr (buff, "->"); + if (arrow) + *arrow = 0; + else { + arrow = buff + strlen (buff); + while (arrow > buff && *(arrow-1) == '\n') + arrow--; + *arrow-- = 0; + if (arrow >= buff && ( *arrow != ' ' || *arrow != '\t' )) + strcat (buff, " "); + } + if (ac > 1) { + struct tm tm_arg; + memset (&tm_arg, 0, sizeof (tm_arg)); + strptime (av[1], "%Y-%m-%d %H:%M", &tm_arg); + ref.tv_sec = mktime (&tm_arg); + } else + clock_gettime (CLOCK_REALTIME, &ref); + if (! get_date (&d, buff, &ref)) printf ("Bad format - couldn't convert.\n"); else if (! (tm = localtime (&d.tv_sec))) { @@ -3492,13 +3523,10 @@ main (int ac, char **av) } else { - int ns = d.tv_nsec; - printf ("%04ld-%02d-%02d %02d:%02d:%02d.%09d\n", + printf ("%s-> %04ld-%02d-%02d %02d:%02d:%02d\n", buff, tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, ns); + tm->tm_hour, tm->tm_min, tm->tm_sec); } - printf ("\t> "); - fflush (stdout); } return 0; } diff --git a/lib/getdate.y b/lib/getdate.y index d423f5b..657416e 100644 --- a/lib/getdate.y +++ b/lib/getdate.y @@ -1547,19 +1547,50 @@ get_date (struct timespec *result, char const *p, struct timespec const *now) #if TEST int +_internal_error (const char *format, ...) +{ + va_list va_args; + + va_start (va_args, format); + + fprintf (stderr, "Internal error: "); + vfprintf (stderr, format, va_args); + + exit (1); + + return 1; +} + +int main (int ac, char **av) { char buff[BUFSIZ]; - printf ("Enter date, or blank line to exit.\n\t> "); - fflush (stdout); - - buff[BUFSIZ - 1] = '\0'; - while (fgets (buff, BUFSIZ - 1, stdin) && buff[0]) + buff[BUFSIZ - 2] = '\0'; + while (fgets (buff, BUFSIZ - 2, stdin) && buff[0]) { - struct timespec d; + struct timespec d, ref = { 0, 0 }; struct tm const *tm; - if (! get_date (&d, buff, NULL)) + char *arrow; + arrow = strstr (buff, "->"); + if (arrow) + *arrow = 0; + else { + arrow = buff + strlen (buff); + while (arrow > buff && *(arrow-1) == '\n') + arrow--; + *arrow-- = 0; + if (arrow >= buff && ( *arrow != ' ' || *arrow != '\t' )) + strcat (buff, " "); + } + if (ac > 1) { + struct tm tm_arg; + memset (&tm_arg, 0, sizeof (tm_arg)); + strptime (av[1], "%Y-%m-%d %H:%M", &tm_arg); + ref.tv_sec = mktime (&tm_arg); + } else + clock_gettime (CLOCK_REALTIME, &ref); + if (! get_date (&d, buff, &ref)) printf ("Bad format - couldn't convert.\n"); else if (! (tm = localtime (&d.tv_sec))) { @@ -1568,13 +1599,10 @@ main (int ac, char **av) } else { - int ns = d.tv_nsec; - printf ("%04ld-%02d-%02d %02d:%02d:%02d.%09d\n", + printf ("%s-> %04ld-%02d-%02d %02d:%02d:%02d\n", buff, tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, ns); + tm->tm_hour, tm->tm_min, tm->tm_sec); } - printf ("\t> "); - fflush (stdout); } return 0; } diff --git a/test/Makefile.local b/test/Makefile.local index 302482b..72c5d74 100644 --- a/test/Makefile.local +++ b/test/Makefile.local @@ -8,10 +8,15 @@ $(dir)/smtp-dummy: $(dir)/smtp-dummy.c $(dir)/qparser-test: $(dir)/qparser-test.o notmuch-config.o query-string.o lib/libnotmuch.a $(call quiet,CXX $(CXXFLAGS)) $^ $(FINAL_LIBNOTMUCH_LDFLAGS) -o $@ +getdate-src := getdate.c xutil.c c-ctype.c gettime.c +$(dir)/getdate-test: $(getdate-src:%=lib/%) + $(call quiet,CC $(CFLAGS)) $(FINAL_CFLAGS) -lrt -DTEST $^ -o $@ + + .PHONY: test check -test: all $(dir)/smtp-dummy $(dir)/qparser-test +test: all $(dir)/smtp-dummy $(dir)/qparser-test $(dir)/getdate-test @${dir}/notmuch-test $(OPTIONS) check: test -CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/qparser-test.o $(dir)/qparser-test +CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/qparser-test.o $(dir)/qparser-test $(dir)/getdate-test diff --git a/test/basic b/test/basic index 3191bcc..bbf452f 100755 --- a/test/basic +++ b/test/basic @@ -52,7 +52,7 @@ test_expect_code 2 'failure to clean up causes the test to fail' ' # Ensure that all tests are being run test_begin_subtest 'Ensure that all available tests will be run by notmuch-test' tests_in_suite=$(grep TESTS= ../notmuch-test | sed -e "s/TESTS=\"\(.*\)\"/\1/" | tr " " "\n" | sort) -available=$(ls -1 ../ | grep -v -E "^(aggregate-results.sh|Makefile|Makefile.local|notmuch-test|README|test-lib.sh|test-results|tmp.*|valgrind|corpus*|emacs.expected-output|smtp-dummy|smtp-dummy.c|test-verbose|test.expected-output|qparser-test.*|qparser-test|qparser.expected-output)" | sort) +available=$(ls -1 ../ | grep -v -E "^(aggregate-results.sh|Makefile|Makefile.local|notmuch-test|README|test-lib.sh|test-results|tmp.*|valgrind|corpus*|emacs.expected-output|smtp-dummy|smtp-dummy.c|test-verbose|test.expected-output|qparser-test.*|qparser-test|qparser.expected-output|getdate-test)" | sort) test_expect_equal "$tests_in_suite" "$available" EXPECTED=../test.expected-output diff --git a/test/date-parser b/test/date-parser new file mode 100755 index 0000000..1c13748 --- /dev/null +++ b/test/date-parser @@ -0,0 +1,37 @@ +#!/bin/bash +test_description="date parser" +. ./test-lib.sh + +# Note: 2011-1-11 is Tuesday + +test_begin_subtest "Date/time parsing" +cat > input < 2011-01-11 11:11:00 +2010-1-1 -> 2010-01-01 00:00:00 +Jan 2 -> 2011-01-02 00:00:00 +last Friday -> 2011-01-07 00:00:00 +2 hours ago -> 2011-01-11 09:11:00 +last month -> 2010-12-11 11:11:00 +month ago -> 2010-12-11 11:11:00 +8am -> 2011-01-11 08:00:00 +9:15 -> 2011-01-11 09:15:00 +12:34 -> 2011-01-11 12:34:00 +EOF +output=$(../getdate-test "2011-1-11 11:11" < input) +test_expect_equal "$output" "$(cat input)" + + +test_begin_subtest "Broken - implicitely, we mean the past" +cat > input < 2011-01-10 00:00:00 +yesterday -> 2011-01-10 00:00:00 +tomorrow -> 2011-01-12 00:00:00 +EOF +output=$(../getdate-test "2011-1-11 11:11" < input) +test_expect_equal "$output" "$(cat input)" + +# TODO: Some values should depend whether used with after/before (e.g. +# query "before:yesterday or after:yesterday" should return everything +# except mails from yesterday) + +test_done diff --git a/test/notmuch-test b/test/notmuch-test index 1e331b3..167e920 100755 --- a/test/notmuch-test +++ b/test/notmuch-test @@ -16,7 +16,7 @@ fi cd $(dirname "$0") -TESTS="basic new qparser search search-output json thread-naming raw reply dump-restore uuencode thread-order author-order from-guessing long-id encoding emacs maildir-sync" +TESTS="basic new qparser date-parser search search-output json thread-naming raw reply dump-restore uuencode thread-order author-order from-guessing long-id encoding emacs maildir-sync" # Clean up any results from a previous run rm -r test-results >/dev/null 2>/dev/null -- 1.7.2.3