public void parse_expression_parses_variable_expression() { using (var stringReader = new StringReader("{0}")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(3, expression.Evaluate(NodeEvaluationContext.Create(3))); } }
/// <summary> /// Attempts to convert the specified values. /// </summary> /// <param name="values"> /// The values to convert. /// </param> /// <param name="targetType"> /// The type of the binding target property. /// </param> /// <param name="parameter"> /// The converter parameter to use. /// </param> /// <param name="culture"> /// The culture to use in the converter. /// </param> /// <returns> /// A converted value. /// </returns> public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { this.EnsureExpressionNode(); exceptionHelper.ResolveAndThrowIf(this.expressionNode == null, "NoExpression"); return this.expressionNode.Evaluate(NodeEvaluationContext.Create(values)); }
public void parse_expression_parses_complex_expressions() { using (var stringReader = new StringReader("1 + (8/3d) * (50 >> (3 - 1)) * 1e4")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(320001d, expression.Evaluate(NodeEvaluationContext.Empty)); } using (var stringReader = new StringReader("\"a\"+\"b\" + \"c\" +\"def\" + \"\\nghi\" == {0}")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(true, expression.Evaluate(NodeEvaluationContext.Create("abcdef\nghi"))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create("abcdefghi"))); } using (var stringReader = new StringReader("23543L & 3448 | (1024 * 56) ^ 8948 ^ (548395 % 34853)")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(45044L, expression.Evaluate(NodeEvaluationContext.Empty)); } using (var stringReader = new StringReader("~{0} * -{1} / {2}")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(177m, expression.Evaluate(NodeEvaluationContext.Create(235, 3m, 4))); Assert.Equal(-279, expression.Evaluate(NodeEvaluationContext.Create(-32, 18, 2))); } using (var stringReader = new StringReader(@"{0} > 100 ? ({1} ?? {2} ?? ""default"") : ""small number""")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal("small number", expression.Evaluate(NodeEvaluationContext.Create(99, "first", "second"))); Assert.Equal("first", expression.Evaluate(NodeEvaluationContext.Create(101, "first", "second"))); Assert.Equal("second", expression.Evaluate(NodeEvaluationContext.Create(101, null, "second"))); Assert.Equal("default", expression.Evaluate(NodeEvaluationContext.Create(101, null, null))); } using (var stringReader = new StringReader(@"{0} == {1}")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(true, expression.Evaluate(NodeEvaluationContext.Create(ConsoleKey.A, ConsoleKey.A))); Assert.Equal(true, expression.Evaluate(NodeEvaluationContext.Create(ConsoleKey.A, (int)ConsoleKey.A))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create(ConsoleKey.A, ConsoleKey.B))); Assert.Equal(true, expression.Evaluate(NodeEvaluationContext.Create(this, this))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create(this, expression))); } using (var stringReader = new StringReader(@"{0} != null && {1} == {2}")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(true, expression.Evaluate(NodeEvaluationContext.Create(this, ConsoleKey.A, ConsoleKey.A))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create(null, ConsoleKey.A, ConsoleKey.A))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create(this, ConsoleKey.A, ConsoleKey.B))); } using (var stringReader = new StringReader(@"{0} != null && {1} == {2}")) using (var tokenizer = new Tokenizer(stringReader)) using (var parser = new Parser(tokenizer)) { var expression = parser.ParseExpression(); Assert.Equal(true, expression.Evaluate(NodeEvaluationContext.Create(this, ConsoleKey.A, ConsoleKey.A))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create(null, ConsoleKey.A, ConsoleKey.A))); Assert.Equal(false, expression.Evaluate(NodeEvaluationContext.Create(this, ConsoleKey.A, ConsoleKey.B))); } }