[gnome-calculator] GCalc: handle null parameters



commit a7948cfc3b6a80d695794f6920eeb936580f7b20
Author: Daniel Espinosa <esodan gmail com>
Date:   Sun Nov 3 22:40:03 2019 -0600

    GCalc: handle null parameters
    
    Null parameters to string return its name and
    evaluate to 0.0

 gcalc/gcalc-math-parameter.vala |  4 +--
 gcalc/gcalc-math-term.vala      |  1 -
 gcalc/gcalc-math-variable.vala  |  3 +++
 gcalc/gcalc-parameter.vala      | 13 +++++++---
 gcalc/gcalc-variable.vala       |  2 +-
 tests/gcalc-parsing.vala        |  2 +-
 tests/gcalc-solving-basic.vala  | 54 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 71 insertions(+), 8 deletions(-)
---
diff --git a/gcalc/gcalc-math-parameter.vala b/gcalc/gcalc-math-parameter.vala
index cb1bbcc3..692d9df9 100644
--- a/gcalc/gcalc-math-parameter.vala
+++ b/gcalc/gcalc-math-parameter.vala
@@ -26,6 +26,6 @@
  * Currently the value will be converted to a {@link MathConstant} if possible.
  */
 public interface GCalc.MathParameter : Object, MathExpression, MathVariable {
-  public abstract void set_value (GLib.Value val) throws GLib.Error;
-  public abstract GLib.Value get_value ();
+  public abstract void set_value (GLib.Value? val) throws GLib.Error;
+  public abstract GLib.Value? get_value ();
 }
diff --git a/gcalc/gcalc-math-term.vala b/gcalc/gcalc-math-term.vala
index 6bdb7066..6f14426e 100644
--- a/gcalc/gcalc-math-term.vala
+++ b/gcalc/gcalc-math-term.vala
@@ -110,7 +110,6 @@ public interface GCalc.MathTerm : Object, MathExpression {
   public static MathExpression evaluate_constants (MathConstant c1, MathConstant c2, MathOperator op)
     throws GLib.Error
   {
-      message ("Eval: %s : %s", c1.to_string (), c2.to_string ());
     MathExpression res = null;
     if (op is MathMinus) {
       res = c1.multiply (c2);
diff --git a/gcalc/gcalc-math-variable.vala b/gcalc/gcalc-math-variable.vala
index d6f6c17c..7143a3b6 100644
--- a/gcalc/gcalc-math-variable.vala
+++ b/gcalc/gcalc-math-variable.vala
@@ -32,6 +32,9 @@ public interface GCalc.MathVariable : Object, MathExpression {
       return bind.evaluate ();
     }
     if (this is MathParameter) {
+      if (@value == null) {
+        return new Constant.@double (0.0);
+      }
       return @value;
     }
     if (parent == null) {
diff --git a/gcalc/gcalc-parameter.vala b/gcalc/gcalc-parameter.vala
index 6434d208..a95eaf10 100644
--- a/gcalc/gcalc-parameter.vala
+++ b/gcalc/gcalc-parameter.vala
@@ -27,7 +27,11 @@ public class GCalc.Parameter : GCalc.Variable, MathParameter {
     base (name);
   }
 
-  internal void set_value (GLib.Value val) throws GLib.Error {
+  internal void set_value (GLib.Value? val) throws GLib.Error {
+    if (val == null) {
+      @value = null;
+      return;
+    }
     MathConstant c = new Constant.integer (0);
     if (val.holds (GLib.Type.INT)) {
       c = new Constant.integer ((int) val);
@@ -41,7 +45,10 @@ public class GCalc.Parameter : GCalc.Variable, MathParameter {
     @value = c;
   }
 
-  internal GLib.Value get_value () {
+  internal GLib.Value? get_value () {
+    if (@value == null) {
+      return null;
+    }
     var v = GLib.Value (typeof (GCalc.MathConstant));
     v = @value;
     return v;
@@ -49,7 +56,7 @@ public class GCalc.Parameter : GCalc.Variable, MathParameter {
   // Expression
   internal override string to_string () {
     if (@value == null) {
-      return "0";
+      return name;
     }
     return @value.to_string ();
   }
diff --git a/gcalc/gcalc-variable.vala b/gcalc/gcalc-variable.vala
index f40d72fe..c46b2705 100644
--- a/gcalc/gcalc-variable.vala
+++ b/gcalc/gcalc-variable.vala
@@ -28,7 +28,7 @@ public class GCalc.Variable : Expression, MathVariable, Hashable {
   internal MathVariable bind { get; set; }
 
   construct {
-    _value = new Constant.@double (0.0);
+    _value = null;
   }
   internal Variable (string name) {
     this.name = name;
diff --git a/tests/gcalc-parsing.vala b/tests/gcalc-parsing.vala
index 6cf0f5d0..b102ac48 100644
--- a/tests/gcalc-parsing.vala
+++ b/tests/gcalc-parsing.vala
@@ -1138,7 +1138,7 @@ class Tests {
         var v2 = t2.expressions.get_item (0) as MathVariable;
         assert (v2 != null);
         message (eq.to_string ());
-        assert (eq.to_string () == "x=0");
+        assert (eq.to_string () == "x=param1");
         p.set_value (10.0);
         message (eq.to_string ());
         assert (eq.to_string () == "x=10");
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index cae20a6c..73c7d08b 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -1571,6 +1571,60 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
+    Test.add_func ("/gcalc/solve/equations/solve/variable/parameter",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new EquationManager ();
+        parser.parse ("3*$p", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        var res = eq.solve ();
+        if (res is ErrorResult) {
+          warning ("Error: %s", (res as ErrorResult).message);
+        }
+        assert (res.expression != null);
+        assert (res.expression is MathConstant);
+        message ("Result: %s", res.expression.to_string ());
+        var c = res.expression as MathConstantNumber;
+        assert (c != null);
+        assert (c.@value () == 0.0);
+        var p = eq.variables.find_named ("p") as MathParameter;
+        assert (p != null);
+        assert (p is MathParameter);
+        message ("Param without value to string: %s", p.to_string ());
+        assert (p.to_string () == "p");
+        p.set_value (3.0);
+        message ("Param with value to string: %s", p.to_string ());
+        assert (p.to_string () == "3");
+        var res2 = eq.solve ();
+        if (res2 is ErrorResult) {
+          warning ("Error: %s", (res2 as ErrorResult).message);
+        }
+        assert (res2.expression != null);
+        assert (res2.expression is MathConstant);
+        message ("Result: %s", res2.expression.to_string ());
+        var c2 = res2.expression as MathConstantNumber;
+        assert (c2 != null);
+        assert (c2.@value () == 9.0);
+        p.set_value (null);
+        message ("Param without value to string: %s", p.to_string ());
+        assert (p.to_string () == "p");
+        var res3 = eq.solve ();
+        if (res3 is ErrorResult) {
+          warning ("Error: %s", (res3 as ErrorResult).message);
+        }
+        assert (res3.expression != null);
+        assert (res3.expression is MathConstant);
+        message ("Result: %s", res3.expression.to_string ());
+        var c3 = res3.expression as MathConstantNumber;
+        assert (c3 != null);
+        assert (c3.@value () == 0.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]