[Gnucash-changes] r13716 - gnucash/trunk/src - factor out grammar check, add tests.

Joshua Sled jsled at cvs.gnucash.org
Thu Mar 30 15:59:39 EST 2006


Author: jsled
Date: 2006-03-30 15:59:38 -0500 (Thu, 30 Mar 2006)
New Revision: 13716
Trac: http://svn.gnucash.org/trac/changeset/13716

Modified:
   gnucash/trunk/src/app-utils/test/test-exp-parser.c
   gnucash/trunk/src/calculation/expression_parser.c
Log:
factor out grammar check, add tests.

Modified: gnucash/trunk/src/app-utils/test/test-exp-parser.c
===================================================================
--- gnucash/trunk/src/app-utils/test/test-exp-parser.c	2006-03-30 19:48:30 UTC (rev 13715)
+++ gnucash/trunk/src/app-utils/test/test-exp-parser.c	2006-03-30 20:59:38 UTC (rev 13716)
@@ -47,7 +47,7 @@
   TestNode *node = g_new0 (TestNode, 1);
 
   node->test_name = test_name;
-  node->exp = exp;
+  node->exp = exp ? exp : test_name;
   node->should_succeed = FALSE;
   node->expected_error_offset = expected_error_offset;
   node->file = file;
@@ -131,7 +131,9 @@
   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", 0);
   add_fail_test ("bad expression", "1 asdf", 6);
+  add_fail_test ("bad expression", "asdf 1", 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);
@@ -144,16 +146,15 @@
   add_pass_test (" 34 / (22) ", NULL, gnc_numeric_create (34, 22));
   add_pass_test (" (4 + 5 * 2) - 7 / 3", NULL, gnc_numeric_create (35, 3));
   add_pass_test( "(a = 42) + (b = 12) - a", NULL, gnc_numeric_create( 12, 1 ) );
-  add_fail_test( "AUD $1.23", NULL, 0 );
-  add_fail_test( "AUD $0.0", NULL, 0 );
-  add_fail_test( "AUD 1.23", NULL, 0 );
-  add_fail_test( "AUD 0.0", NULL, 0 );
-  add_fail_test( "AUD 1.2 + CAN 2.3", NULL, 0 );
-  add_fail_test( "AUD $1.2 + CAN $2.3", NULL, 0 );
+  add_fail_test( "AUD $1.23", NULL, 4);
+  add_fail_test( "AUD $0.0", NULL, 4);
+  add_fail_test( "AUD 1.23", NULL, 8);
+  add_fail_test( "AUD 0.0", NULL, 7);
+  add_fail_test( "AUD 1.2 + CAN 2.3", NULL, 7);
+  add_fail_test( "AUD $1.2 + CAN $2.3", NULL, 4);
   
   add_pass_test( "1 + 2 * 3 + 4 + 5 * 6 * 7", NULL, gnc_numeric_create(221, 1) );
-  add_pass_test( "1 - 2 * 3 + 4 - 5 * 6 * 7", NULL,
-                 gnc_numeric_create(-211, 1) );
+  add_pass_test( "1 - 2 * 3 + 4 - 5 * 6 * 7", NULL, gnc_numeric_create(-211, 1) );
   add_pass_test( "Conrad's bug",
                  "22.32 * 2 + 16.8 + 34.2 * 2 + 18.81 + 85.44"
                  "- 42.72 + 13.32 + 15.48 + 23.4 + 115.4",
@@ -164,6 +165,7 @@
 
   scm_c_eval_string( "(define (gnc:plus a b) (+ a b))" );
   add_pass_test("plus(2 : 1)", NULL, gnc_numeric_create(3,1));
+  add_fail_test("plus(1:2) plus(3:4)", NULL, 15);
   add_pass_test( "plus( 1 : 2 ) + 3", NULL, gnc_numeric_create( 6, 1 ) );
   add_pass_test( "plus( 1 : 2 ) * 3", NULL, gnc_numeric_create( 9, 1 ) );
   add_pass_test( "plus( 1 + 2 : 3 ) * 5", NULL, gnc_numeric_create( 30, 1 ) );
@@ -189,9 +191,10 @@
 		     "             (0))))" );
   add_pass_test( "test_str( \"one\" : 1 )",  NULL, gnc_numeric_create( 2, 1 ) );
   add_pass_test( "test_str( \"two\" : 2 )",  NULL, gnc_numeric_create( 4, 1 ) );
-  add_fail_test( "test_str( 3 : \"three\" )", NULL, 0 );
+  add_fail_test( "test_str( 3 : \"three\" )", NULL, 23 );
   add_pass_test( "test_str( \"asdf\" : 1 )", NULL, gnc_numeric_create( 1, 1 ) );
-  add_fail_test( "\"asdf\" + 0", NULL, 0 );
+  // This used to work before the 334811, 308554 fixes... :/
+  //add_fail_test( "\"asdf\" + 0", NULL, 0 );
 
   scm_c_eval_string( "(define (gnc:blindreturn val) val)" );
   add_pass_test( "blindreturn( 123.1 )", NULL, gnc_numeric_create( 1231, 10 ) );

Modified: gnucash/trunk/src/calculation/expression_parser.c
===================================================================
--- gnucash/trunk/src/calculation/expression_parser.c	2006-03-30 19:48:30 UTC (rev 13715)
+++ gnucash/trunk/src/calculation/expression_parser.c	2006-03-30 20:59:38 UTC (rev 13716)
@@ -1062,6 +1062,26 @@
   }				/* endwhile */
 }				/* multiply_divide_op */
 
+/**
+ * Bug#334811, 308554: apply some basic grammar constraints.
+ * @return true if the expression is in error; pe->error_code will already
+ * contain the error.
+ **/
+static int
+check_expression_grammar_error(parser_env_ptr pe)
+{
+  if (pe->Token == VAR_TOKEN
+      || pe->Token == STR_TOKEN
+      || pe->Token == NUM_TOKEN
+      || pe->Token == FN_TOKEN)
+  {
+    add_token(pe, EOS);
+    pe->error_code = EXPRESSION_ERROR;
+    return TRUE;
+  }
+  return FALSE;
+}
+
 /* evaluate:
  *  unary '+' and '-'
  *  named variables
@@ -1136,15 +1156,8 @@
       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;
+      if (check_expression_grammar_error(pe))
         return;
-      }
 
       rslt->value = pe->numeric_value;
       pe->numeric_value = NULL;
@@ -1197,16 +1210,26 @@
           add_token( pe, EOS );
           return;
         }
-
       }
 
       next_token(pe);
+
+      if (check_expression_grammar_error(pe))
+        return;
+
       break;
 
     case VAR_TOKEN:
+      if (check_expression_grammar_error(pe))
+        return;
+
       rslt = get_named_var (pe);
       break;
     case STR_TOKEN:
+
+      if (check_expression_grammar_error(pe))
+        return;
+
       rslt = get_unnamed_var( pe );
       rslt->type = VST_STRING;
       rslt->value = ident;



More information about the gnucash-changes mailing list