[PATCH 4/4] Add first date parser tests

Michal Sojka sojkam1 at fel.cvut.cz
Sun Jan 23 03:47:27 PST 2011


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 <<EOF
+now         -> 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 <<EOF
+monday    -> 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



More information about the notmuch mailing list