private static double EvaluateSuccess(string input) { var evaluator = new StringEvaluator(); var result = evaluator.Evaluate(input); result.IsSuccessful.Should().BeTrue(); return(result.Result); }
public static T Evaluate <T>(RPNExpression expression) { try { var context = new RPNContext(expression); while (context.CanMove) { context.MoveNext(); if (DefaultEvaluator.Evaluate(context)) { continue; } if (BasicEvaluator.Evaluate(context)) { continue; } if (MathEvaluator.Evaluate(context)) { continue; } if (LogicEvaluator.Evaluate(context)) { continue; } if (StringEvaluator.Evaluate(context)) { continue; } if (RegexEvaluator.Evaluate(context)) { continue; } if (DateTimeEvaluator.Evaluate(context)) { continue; } if (ControlEvaluator.Evaluate <T>(context)) { continue; } context.Stack.Push(context.Current); } return(context.GetResult <T>()); } catch (RPNException) { throw; } catch { throw new ParsingException(expression.Expression); } }
public void Variable_not_found() { var sut = new StringEvaluator(); var dict = new Dictionary <string, object>(); var actual = sut.Evaluate("This will {fail}", dict); var expected = Either.Error <RuntimeErrors, string>(new RuntimeErrors(new ReferenceToUnsetVariable("fail"))); actual.Should().BeEquivalentTo(expected, options => options .ComparingByMembers(typeof(Either <,>)) .ComparingByMembers(typeof(Option <RuntimeErrors>))); }
public void All_variables_should_be_found() { var sut = new StringEvaluator(); var dict = new Dictionary <string, object> { { "replaced", "mate" }, { "replaced_between_braces", "boy!" }, { "non_replaced", "will not appear on resulting string" }, }; var actual = sut.Evaluate("Hello {replaced}, {{non_replaced}}, {{{replaced_between_braces}}}", dict); var expected = Either.Success <RuntimeErrors, string>("Hello mate, {non_replaced}, {boy!}"); actual.Should().BeEquivalentTo(expected); }