[gnome-calculator/60-split-out-a-backend-library] gcalc: support adding in the middle constant to term



commit ae051c065d4396aac9ff90b07e22ac13ec3c7f35
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Thu Jan 3 09:58:29 2019 -0600

    gcalc: support adding in the middle constant to term

 gcalc/gcalc-parser.vala          |  4 +---
 tests/gcalc-main-interfaces.vala | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)
---
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 160734af..ae805d97 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -179,9 +179,7 @@ public class GCalc.Parser : Object {
           } else if ((current is Operator || current is Term) && current_parent is Term && top_parent is 
Polynomial) {
             current_parent.expressions.add (cexp);
             expected.clear ();
-            current = current_parent;
-            current_parent = top_parent;
-            top_parent = null;
+            current = cexp;
           }
           break;
         case Vala.TokenType.PERCENT:
diff --git a/tests/gcalc-main-interfaces.vala b/tests/gcalc-main-interfaces.vala
index 5339b95b..89279370 100644
--- a/tests/gcalc-main-interfaces.vala
+++ b/tests/gcalc-main-interfaces.vala
@@ -222,6 +222,50 @@ class Tests {
         warning ("Error: %s", error.message);
       }
     });
+    Test.add_func ("/gcalc/parser/term/complex/constant+variable",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("-1+B+3+A", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        assert (eq.expressions.get_n_items () == 1);
+        var p = eq.expressions.get_item (0) as Polynomial;
+        assert (p != null);
+        message ("Terms: %u", p.expressions.get_n_items ());
+        assert (p.expressions.get_n_items () == 4);
+        var t1 = p.expressions.get_item (0) as Term;
+        assert (t1 != null);
+        assert (t1.expressions.get_n_items () == 1);
+        var c1 = t1.expressions.get_item (0) as Constant;
+        assert (c1 != null);
+        var t2 = p.expressions.get_item (1) as Term;
+        assert (t2 != null);
+        assert (t2.expressions.get_n_items () == 2);
+        var plus = t2.expressions.get_item (0) as Plus;
+        assert (plus != null);
+        var c2 = t2.expressions.get_item (1) as Variable;
+        assert (c2 != null);
+        var t3 = p.expressions.get_item (2) as Term;
+        assert (t3 != null);
+        assert (t3.expressions.get_n_items () == 2);
+        var plus2 = t3.expressions.get_item (0) as Plus;
+        assert (plus2 != null);
+        var c3 = t3.expressions.get_item (1) as Constant;
+        assert (c3 != null);
+        var t4 = p.expressions.get_item (3) as Term;
+        assert (t4 != null);
+        assert (t4.expressions.get_n_items () == 2);
+        var plus3 = t4.expressions.get_item (0) as Plus;
+        assert (plus3 != null);
+        var c4 = t4.expressions.get_item (1) as Variable;
+        assert (c4 != null);
+      } catch (GLib.Error error) {
+        warning ("Error: %s", error.message);
+      }
+    });
     return Test.run ();
   }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]