[gnome-calculator/60-split-out-a-backend-library] gcalc: evalutes simple constants in terms



commit 952e4beefb36c491468c6f53460d2a157e00c1df
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Thu Jan 3 18:03:17 2019 -0600

    gcalc: evalutes simple constants in terms

 gcalc/gcalc-gerror-result.vala |  2 +-
 gcalc/gcalc-gterm.vala         | 11 +++++++++++
 gcalc/gcalc-term.vala          |  3 +++
 tests/gcalc-solving-basic.vala | 25 +++++++++++++++++++++++++
 4 files changed, 40 insertions(+), 1 deletion(-)
---
diff --git a/gcalc/gcalc-gerror-result.vala b/gcalc/gcalc-gerror-result.vala
index 61aca945..f99f1699 100644
--- a/gcalc/gcalc-gerror-result.vala
+++ b/gcalc/gcalc-gerror-result.vala
@@ -18,7 +18,7 @@
  * Authors:
  *      Daniel Espinosa <esodan gmail com>
  */
-public class GCalc.GErrorResult : Object {
+public class GCalc.GErrorResult : Object, ErrorResult {
   private string msg = "";
   public GErrorResult (string msg) {
     this.msg = msg;
diff --git a/gcalc/gcalc-gterm.vala b/gcalc/gcalc-gterm.vala
index 8bb18227..8f9c53b8 100644
--- a/gcalc/gcalc-gterm.vala
+++ b/gcalc/gcalc-gterm.vala
@@ -19,5 +19,16 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 public class GCalc.GTerm : GExpression, Term {
+  public override Result solve () {
+    Result res = null;
+    try {
+      var e = evaluate ();
+      res = new GResult (e) as Result;
+    } catch (GLib.Error err) {
+      var nerr = new GErrorResult (err.message);
+      res = new GResult.with_error ((Expression) new GExpression (), (ErrorResult) nerr) as Result;
+    }
+    return res;
+  }
 }
 
diff --git a/gcalc/gcalc-term.vala b/gcalc/gcalc-term.vala
index 2167ee3f..25e517ec 100644
--- a/gcalc/gcalc-term.vala
+++ b/gcalc/gcalc-term.vala
@@ -23,6 +23,9 @@ public interface GCalc.Term : Object, Expression {
     if (t.expressions.get_n_items () == 0) {
       return this;
     }
+    return this;
+  }
+  public virtual Expression evaluate () throws GLib.Error {
     Expression current = null;
     Operator current_operator = null;
     bool first = true;
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 76372fdf..9de1c330 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -102,6 +102,31 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
+    Test.add_func ("/gcalc/solve/term/constant",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("1", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        var e = eq.expressions.get_item (0) as Polynomial;
+        assert (e != null);
+        var t = e.expressions.get_item (0) as Term;
+        var res = t.solve ();
+        assert (res != null);
+        assert (res.expression != null);
+        message ("Result type: %s", res.expression.get_type ().name ());
+        assert (res.is_valid);
+        var rc = res.expression as Constant;
+        assert (rc != null);
+        message ("Constant Result: %s", rc.to_string ());
+        assert (rc.real () == 1.0);
+      } catch (GLib.Error e) {
+        warning ("Error: %s", e.message);
+      }
+    });
     return Test.run ();
   }
 }


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