[Gnucash-changes] r13715 - gnucash/trunk - Bugs#308554, 334811: Add basic validation and test-cases for invalid expressions.

Joshua Sled jsled at cvs.gnucash.org
Thu Mar 30 14:48:30 EST 2006


Author: jsled
Date: 2006-03-30 14:48:30 -0500 (Thu, 30 Mar 2006)
New Revision: 13715
Trac: http://svn.gnucash.org/trac/changeset/13715

Modified:
   gnucash/trunk/ChangeLog
   gnucash/trunk/src/app-utils/test/test-exp-parser.c
   gnucash/trunk/src/calculation/expression_parser.c
   gnucash/trunk/src/calculation/finvar.h
Log:
Bugs#308554, 334811: Add basic validation and test-cases for invalid expressions. 


Modified: gnucash/trunk/ChangeLog
===================================================================
--- gnucash/trunk/ChangeLog	2006-03-30 18:00:44 UTC (rev 13714)
+++ gnucash/trunk/ChangeLog	2006-03-30 19:48:30 UTC (rev 13715)
@@ -13,6 +13,11 @@
 
 2006-03-30  Joshua Sled  <jsled at asynchronous.org>
 
+	* src/calculation/expression_parser.c (primary_exp): 
+	* src/app-utils/test/test-exp-parser.c (test_parser): Add basic
+	validation and test-cases for invalid expressions.  Bugs#308554,
+	334811.
+	
 	* accounts/C/acctchrt_checkbook.gnucash-xea: Revoke placeholder
 	status from leaf Expense account in Simple Checkbook account
 	tree.  Bug#334777.

Modified: gnucash/trunk/src/app-utils/test/test-exp-parser.c
===================================================================
--- gnucash/trunk/src/app-utils/test/test-exp-parser.c	2006-03-30 18:00:44 UTC (rev 13714)
+++ gnucash/trunk/src/app-utils/test/test-exp-parser.c	2006-03-30 19:48:30 UTC (rev 13715)
@@ -14,18 +14,18 @@
 typedef struct
 {
   const char * test_name;
-
   const char * exp;
-
   gboolean should_succeed;
-
   gnc_numeric expected_result;
-
   int expected_error_offset;
+  const char * file;
+  int line;
 } TestNode;
 
+#define add_pass_test(n, e, r) _add_pass_test((n), (e), (r), __FILE__, __LINE__)
+
 static void
-add_pass_test (const char *test_name, const char *exp, gnc_numeric result)
+_add_pass_test (const char *test_name, const char *exp, gnc_numeric result, char *file, int line)
 {
   TestNode *node = g_new0 (TestNode, 1);
 
@@ -33,13 +33,16 @@
   node->exp = exp ? exp : test_name;
   node->should_succeed = TRUE;
   node->expected_result = result;
+  node->file = file;
+  node->line = line;
 
   tests = g_list_append (tests, node);
 }
 
+#define add_fail_test(n,e,o) _add_fail_test((n), (e), (o), __FILE__, __LINE__)
+
 static void
-add_fail_test (const char *test_name, const char *exp,
-               int expected_error_offset)
+_add_fail_test (const char *test_name, const char *exp, int expected_error_offset, char *file, int line)
 {
   TestNode *node = g_new0 (TestNode, 1);
 
@@ -47,6 +50,8 @@
   node->exp = exp;
   node->should_succeed = FALSE;
   node->expected_error_offset = expected_error_offset;
+  node->file = file;
+  node->line = line;
 
   tests = g_list_append (tests, node);
 }
@@ -59,7 +64,7 @@
   char *error_loc;
 
   result = gnc_numeric_error( -1 );
-  printf( "Running test \"%s\" =  ", node->test_name );
+  printf("Running test \"%s\" [%s] = ", node->test_name, node->exp);
   succeeded = gnc_exp_parser_parse (node->exp, &result, &error_loc);
   {
     int pass;
@@ -74,7 +79,7 @@
 
   if (succeeded != node->should_succeed)
   {
-    failure_args (node->test_name, __FILE__, __LINE__,
+    failure_args (node->test_name, node->file, node->line,
                   "parser %s on \"%s\"", 
                   succeeded ? "succeeded" : "failed",
                   node->exp);
@@ -85,7 +90,7 @@
   {
     if (!gnc_numeric_equal (result, node->expected_result))
     {
-      failure_args (node->test_name, __FILE__, __LINE__, "wrong result");
+      failure_args (node->test_name, node->file, node->line, "wrong result");
       return;
     }
   }
@@ -93,7 +98,8 @@
   {
     if (error_loc != node->exp + node->expected_error_offset)
     {
-      failure_args (node->test_name, __FILE__, __LINE__, "wrong offset");
+      failure_args (node->test_name, node->file, node->line, "wrong offset; expected %d, got %d",
+                    node->expected_error_offset, (error_loc - node->exp));
       return;
     }
   }
@@ -121,8 +127,11 @@
   add_fail_test ("whitespace", "  \t\n", 4);
   add_fail_test ("bad expression", "\\", 0);
   add_fail_test ("bad expression", "1 +", 3);
-  /* FIXME: This should be a failure test... */
-  //add_fail_test ("bad expression", "1 2", 2);
+  /* Bug#334811 - http://bugzilla.gnome.org/show_bug.cgi?id=334811 */
+  add_fail_test ("bad expression", "1 2", 3);
+  /* Bug#308554 - http://bugzilla.gnome.org/show_bug.cgi?id=308554 */
+  add_fail_test ("bad expression", "1 ç", 2);
+  add_fail_test ("bad expression", "1 asdf", 6);
   add_fail_test ("bad expression", "  (5 + 23)/   ", 14);
   add_fail_test ("bad expression", "  ((((5 + 23)/   ", 17);
   add_fail_test ("divide by zero", "  4 / (1 - 1)", -1);
@@ -150,6 +159,7 @@
                  "- 42.72 + 13.32 + 15.48 + 23.4 + 115.4",
                  gnc_numeric_create(35897, 100) );
 
+  // This must be defined for the function-parsing to work.
   scm_c_eval_string("(define (gnc:error->string tag args)   (define (write-error port)     (if (and (list? args) (not (null? args)))         (let ((func (car args)))           (if func               (begin                 (display \"Function: \" port)                 (display func port)                 (display \", \" port)                 (display tag port)                 (display \"\n\n\" port)))))     (false-if-exception      (apply display-error (fluid-ref the-last-stack) port args))     (display-backtrace (fluid-ref the-last-stack) port)     (force-output port))   (false-if-exception    (call-with-output-string write-error)))");
 
   scm_c_eval_string( "(define (gnc:plus a b) (+ a b))" );

Modified: gnucash/trunk/src/calculation/expression_parser.c
===================================================================
--- gnucash/trunk/src/calculation/expression_parser.c	2006-03-30 18:00:44 UTC (rev 13714)
+++ gnucash/trunk/src/calculation/expression_parser.c	2006-03-30 19:48:30 UTC (rev 13715)
@@ -1136,6 +1136,16 @@
       if (pe->error_code)
         return;
 
+      // Bug#334811, 308554: apply some basic grammar constraints.
+      if (pe->Token == VAR_TOKEN
+          || pe->Token == STR_TOKEN
+          || pe->Token == NUM_TOKEN)
+      {
+        add_token(pe, EOS);
+        pe->error_code = EXPRESSION_ERROR;
+        return;
+      }
+
       rslt->value = pe->numeric_value;
       pe->numeric_value = NULL;
       break;

Modified: gnucash/trunk/src/calculation/finvar.h
===================================================================
--- gnucash/trunk/src/calculation/finvar.h	2006-03-30 18:00:44 UTC (rev 13714)
+++ gnucash/trunk/src/calculation/finvar.h	2006-03-30 19:48:30 UTC (rev 13715)
@@ -48,6 +48,7 @@
   NOT_A_FUNC,
   PARSER_OUT_OF_MEMORY,
   NUMERIC_ERROR,
+  EXPRESSION_ERROR,
   PARSER_NUM_ERRORS
 }
 ParseError;



More information about the gnucash-changes mailing list