Files
spark/src/DataStreamSuite/BooleanExpressionsInterpreter/tests/BooleanExpressionTests.cs
T
John Colvin 9e221cabc3 build: Add BooleanExpressionsInterpreter csproj
For the DataStream port

* Add Lexer/Parser build targets
* Update externals w/ lexer/parser generator deps
2023-09-01 01:28:40 -04:00

86 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using NUnit.Framework;
using System.Diagnostics;
namespace BCG
{
[TestFixture]
public class BooleaExpressionTests
{
[Test]
public void baseTest()
{
var interp =
new BooleanExpressionsInterpreter("1=1");
Assert.IsTrue(interp.eval(new Dictionary<string, object>()));
interp = new BooleanExpressionsInterpreter(@"define a() { true }
define f() { a and b and c}
define g(a,b,c) { a and b and c}
true");
Assert.IsTrue(interp.eval(new Dictionary<string, object>()));
interp = new BooleanExpressionsInterpreter("id>0");
Assert.IsTrue(interp.eval(new Dictionary<string, object> { { "id", 1 } }));
}
[Test]
public void getAllIdentifiersWorks()
{
BooleanExpressionsInterpreter.ParseTree pt;
var interp =
new BooleanExpressionsInterpreter("a=12/12/1912 and 2 = null or b=2 and not c=1 and b > a and f(1,2) > y(2,3)",out pt);
Dictionary<string, bool> ids = interp.getAllIdentifiers(pt);
Assert.AreEqual(ids.Count, 5);
Assert.IsTrue(ids["a"]);
Assert.IsTrue(ids["b"]);
Assert.IsTrue(ids["c"]);
Assert.IsFalse(ids["f"]);
Assert.IsFalse(ids["y"]);
}
[Test]
public void parameterPassingWorks()
{
var interp = new BooleanExpressionsInterpreter(@"define inOrder(a,b,c) { a >= b and b >= c}
inOrder(x,y,z)");
Assert.IsTrue(interp.eval(new Dictionary<string, object>{{"x",3},{"y",2},{"z",1}}));
Assert.IsFalse(interp.eval(new Dictionary<string, object> { { "x", 1 }, {"y", 2 }, {"z", 3} }));
}
[Test]
public void parametersHaveCorretScope()
{
var interp = new BooleanExpressionsInterpreter(@"define inOrder(z,y,x) { z >= y and y >= x}
inOrder(x,y,z)");
Assert.IsTrue(interp.eval(new Dictionary<string, object> { { "x", 3 }, { "y", 2 }, { "z", 1 } }));
Assert.IsFalse(interp.eval(new Dictionary<string, object> { { "x", 1 }, { "y", 2 }, { "z", 3 } }));
}
[Test]
public void functionsCanCallOtherFunctions()
{
var interp = new BooleanExpressionsInterpreter(
@"define ascending(x,y,z) { x <= y and y <= z}
define descending(x,y,z) { ascending(z,y,x)}
descending(x,y,z)");
Assert.IsTrue(interp.eval(new Dictionary<string, object> { { "x", 3 }, { "y", 2 }, { "z", 1 } }));
Assert.IsFalse(interp.eval(new Dictionary<string, object> { { "x", 1 }, { "y", 2 }, { "z", 3 } }));
}
[Test]
public void evaluationHasDynamicScope()
{
var interp = new BooleanExpressionsInterpreter(
@"define isEquity(){ group='Equity'}
isEquity()");
Assert.IsTrue(interp.eval(new Dictionary<string, object> { { "group", "Equity" } }));
Assert.IsFalse(interp.eval(new Dictionary<string, object> { { "group", "NotEquity" } }));
}
}
}