private void CompareMaybeNone(string exprString, object o = null) { var expr = VCExpression.ParseMaybe(exprString).Expression; var result = expr.Evaluate(o ?? new { }); Assert.That(result.HasValue, Is.EqualTo(false)); }
public void TernaryMaybeNone(string exprStr, object a, object b, object c) { var expr = VCExpression.ParseMaybe(exprStr).Expression; var result = expr.Evaluate(new { A = a, B = b, C = c }); Assert.That(result.HasValue, Is.False); }
public void GuardWithoutOtherwise(double a, double expected) { var exprStr = @" match | A < 0.03 = 0.01 | A < 0.1 = 0.05 | A < 0.225 = 0.15 | A < 0.4 = 0.3 | A < 0.6 = 0.5 | A < 0.775 = 0.7 | A < 0.9 = 0.85 | A < 0.97 = 0.95 | A >= 0.97 = 0.99"; var expr = VCExpression.ParseMaybe(exprStr).Expression; var result = expr.Evaluate(new { A = a }); Assert.That(result.HasValue); Assert.That(result.Value, Is.EqualTo(expected)); foreach (var parseResult in CompositeExpression.ParseMultiple(exprStr)) { var expr2 = parseResult.Expression; var result2 = expr2.Evaluate(new { A = a }); Assert.That(result2, Is.EqualTo(expected)); } }
public void StringLetGuard(double a, double l, string expected) { var exprStr = @" let p = a / l in (match | p < 0.5 = 'Low' | p < 0.75 = 'Normal' | p < 1.0 = 'Near Breach' | p < 1.25 = 'Breach' | otherwise 'Critical') "; var expr = VCExpression.ParseMaybe(exprStr).Expression; var result = expr.Evaluate(new { a, l }); Assert.That(result.HasValue); Assert.That(result.Value, Is.EqualTo(expected)); foreach (var parseResult in CompositeExpression.ParseMultiple(exprStr)) { var expr2 = parseResult.Expression; var result2 = expr2.Evaluate(new { a, l }); Assert.That(result2, Is.EqualTo(expected)); } }
public void NaNTest() { var expr = VCExpression.ParseMaybe("A / B / C * 100"); var res = expr.Expression.Evaluate(new { A = 1.0, B = 1.0, C = double.NaN }); Assert.That(res.Value, Is.EqualTo(double.NaN)); }
public void CompareNoneAfterPropertyCache() { var o = new { }; var expr = VCExpression.ParseMaybe("A + B"); expr.Expression.Evaluate(o); // First Eval to Cache Property Infos var res = expr.Expression.Evaluate(o); Assert.That(res.HasValue, Is.False); }
public void InInvalid(string exprString) { foreach (var parseResult in CompositeExpression.ParseMultiple(exprString)) { Assert.False(parseResult.Success); } var parsedMaybe = VCExpression.ParseMaybe(exprString); Assert.False(parsedMaybe.Success); }
public void InMaybe(string exprString) { foreach (var parseResult in CompositeExpression.ParseMultiple(exprString)) { Assert.True(parseResult.Success); } var parsedMaybe = VCExpression.ParseMaybe(exprString); Assert.True(parsedMaybe.Success); var maybeResult = parsedMaybe.Expression.Evaluate(new { }); Assert.False(maybeResult.HasValue); }
public void TernaryOp(string exprStr, object a, object b, object c, object expected) { var expr = VCExpression.ParseMaybe(exprStr).Expression; var result = expr.Evaluate(new { A = a, B = b, C = c }); Assert.That(result.HasValue); Assert.That(result.Value, Is.EqualTo(expected)); foreach (var parseResult in CompositeExpression.ParseMultiple(exprStr)) { var expr2 = parseResult.Expression; var result2 = expr2.Evaluate(new { A = a, B = b, C = c }); Assert.That(result2, Is.EqualTo(expected)); } }
public void LetOp() { var exprStr = "let x = 1, y = 2 in x + y"; var expr = VCExpression.ParseMaybe(exprStr).Expression; var result = expr.Evaluate(new object()); Assert.That(result.HasValue); Assert.That(result.Value, Is.EqualTo(3)); foreach (var parseResult in CompositeExpression.ParseMultiple(exprStr)) { var expr2 = parseResult.Expression; var result2 = expr2.Evaluate(new object()); Assert.That(result2, Is.EqualTo(3)); } }
private void CompareResult(string exprString, bool expected, object o) { foreach (var parseResult in CompositeExpression.ParseMultiple(exprString)) { Assert.True(parseResult.Success); var result = parseResult.Expression.Evaluate(o ?? new { }); Assert.AreEqual(expected, result); } var maybeExpr = VCExpression.ParseMaybe(exprString); Assert.True(maybeExpr.Success); var maybeResult = maybeExpr.Expression.Evaluate(o ?? new { }); Assert.True(maybeResult.HasValue); Assert.AreEqual(expected, maybeResult.Value); }
private void Compare(string exprString, bool expected, object o = null) { foreach (var parseResult in CompositeExpression.ParseMultiple(exprString)) { Assert.True(parseResult.Success, "Default expression parse"); var result = parseResult.Expression.Evaluate(o ?? new { }); Assert.That(result, Is.EqualTo(expected).Within(0.0001), "Default expression evaluated"); } var maybeExpr = VCExpression.ParseMaybe(exprString); Assert.True(maybeExpr.Success, "Maybe expression parse"); var maybeResult = maybeExpr.Expression.Evaluate(o ?? new { }); Assert.True(maybeResult.HasValue, "Maybe expression evaluate has value"); Assert.That(maybeResult.Value, Is.EqualTo(expected).Within(0.0001), "Maybe expression evaluated"); }
public void TernaryNullCase(string exprStr, object a, object c) { var expr = VCExpression.ParseMaybe(exprStr).Expression; var result = expr.Evaluate(new { A = a }); Assert.That(result.HasValue, Is.True); if (result.HasValue) { Assert.AreEqual(c, result.Value); } foreach (var parseResult in CompositeExpression.ParseMultiple(exprStr)) { var expr2 = parseResult.Expression; var result2 = expr2.Evaluate(new { A = a }); Assert.AreEqual(result2, c); } }
public void ShouldEvaluate(string exprStr, object expected) { foreach (var parseResult in CompositeExpression.ParseMultiple(exprStr)) { Assert.That(parseResult.Success, Is.True, "Is successful"); var expr = parseResult.Expression; var result = expr.Evaluate(new { }); Assert.That(result, Is.EqualTo(expected)); } var parseResult2 = VCExpression.ParseMaybe(exprStr); Assert.That(parseResult2.Success, Is.True, "Is successful"); var expr2 = parseResult2.Expression; var result2 = expr2.Evaluate(new { }); Assert.That(result2.HasValue, Is.True, "Have value"); Assert.That(result2.Value, Is.EqualTo(expected)); }
public void Eval(string exprStr, bool hasValue = false, object expected = null) { // Verifies that these expressions all return Maybe.Nothing when called with an empty expression var parseResult = VCExpression.ParseMaybe(exprStr); if (!parseResult.Success) { var errorString = string.Join("\n", parseResult.ParseErrors.Select(p => $"Error parsing at token '{p.Token}' in {p.GetExprError(exprStr)}")); Console.WriteLine(errorString); } Assert.That(parseResult.Success, Is.True); var expr = parseResult.Expression; var result = expr.Evaluate(new { }); Assert.That(result.HasValue, Is.EqualTo(hasValue)); if (hasValue) { Assert.That(result.Value, Is.EqualTo(expected)); } }