public void ParsePrimitiveConstant() { Expression<Func<int>> f = () => 5; var p = new ExpressionParser(f.Body); ConstantNode constantNode = p.Parse() as ConstantNode; Assert.AreEqual("5", constantNode.Text); }
public void ParseStringConstant() { Expression<Func<string>> f = () => "foo"; var p = new ExpressionParser(f.Body); ConstantNode constantNode = p.Parse() as ConstantNode; Assert.AreEqual("\"foo\"", constantNode.Text); }
static bool CheckArgument(ExpressionParser parser, MethodCallExpression methE, bool left, out string hint) { int ix1 = left ? 0 : 1; int ix2 = left ? 1 : 0; if (typeof (Delegate).IsAssignableFrom(methE.Arguments[ix1].Type)) { object leftR; try { leftR = parser.DynamicInvoke(Expression.Invoke(methE.Arguments[ix1])); } catch (InvalidOperationException) // delegate needs arguments { hint = null; return false; } if (Equals(leftR, parser.DynamicInvoke(methE.Arguments[ix2]))) { hint = string.Format(", but would have been True if you had invoked '{0}'", NodeFormatter.PrettyPrint(parser.Parse(methE.Arguments[ix1]))); return true; } } hint = null; return false; }
public void ParseMember() { int x = 5; Expression<Func<int>> f = () => x; var p = new ExpressionParser(f.Body); Node constantNode = p.Parse(); Assert.AreEqual(new ConstantNode {Text = "x", Value = "5"}, constantNode); }
public void ParsePrimitiveStaticField() { Expression<Func<int>> f = () => field; var p = new ExpressionParser(f.Body); ConstantNode constantNode = p.Parse() as ConstantNode; Assert.AreEqual("field", constantNode.Text); Assert.AreEqual("5", constantNode.Value); }
public void ParseMemberAccess() { DateTime d = new DateTime(2010, 12, 25); Expression<Func<int>> f = () => d.Day; var p = new ExpressionParser(f.Body); MemberAccessNode node = (MemberAccessNode) p.Parse(); MemberAccessNode expected = new MemberAccessNode { Container = new ConstantNode {Text = "d", Value = d.ToString()}, MemberName = "Day", MemberValue = "25" }; Assert.AreEqual(expected, node); }
public void ParseMethodAccess() { string s = "hello"; Expression<Func<string>> f = () => s.Substring(1); var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new MethodCallNode { Container = new ConstantNode {Text = "s", Value = @"""hello"""}, MemberName = "Substring", MemberValue = @"""ello""", Parameters = new List<Node>() {new ConstantNode {Text = "1"}} }; Assert.AreEqual(expected, node); }
public void ParseNewAnonymousType() { int x = 1; Expression<Func<int>> f = () => new { Value = x, x }.Value; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new MemberAccessNode { Container = new NewAnonymousTypeNode { Parameters = new[] { new MemberAssignmentNode { MemberName = "Value", Value = new ConstantNode {Text = "x", Value="1" } }, new MemberAssignmentNode { MemberName = "x", Value = new ConstantNode {Text = "x", Value="1" } } }.ToList(), Value = "{ Value = 1, x = 1 }" }, MemberName = "Value", MemberValue = "1" }; Assert.AreEqual(expected, node); }
public void ParseCoalesce() { string x = null; Expression<Func<string>> f = () => x ?? "foo"; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new BinaryNode { Left = new ConstantNode { Text = "x", Value = "null" }, Right = new ConstantNode { Text = "\"foo\"" }, Operator = "??", Value = "\"foo\"" }; Assert.AreEqual(expected, node); }
public void ParseArrayCreateAndIndex() { Expression<Func<int>> f = () => new int[] {1, 2, 3}[1]; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new ArrayIndexNode { Array = new NewArrayNode { Type = "int", Items = new List<Node> { new ConstantNode {Text = "1"}, new ConstantNode {Text = "2"}, new ConstantNode {Text = "3"}, } }, Index = new ConstantNode {Text = "1"}, Value = "2" }; Assert.AreEqual(expected, node); }
Node ParseExpression(LambdaExpression e) { var parser = new ExpressionParser(e.Body, textOnly:true, baseParamIndex:_nextParamIndex, testClass:TestClass); string parameters; if (e.Parameters.Count == 0) { parameters = "()"; } else if (e.Parameters.Count == 1) { parameters = parser.GetParamName(e.Parameters[0]); } else { parameters = "(" + string.Join(", ", e.Parameters.Select(GetParamName)) + ")"; } return new BinaryNode { Operator = "=>", Left = new ConstantNode { Text = parameters }, Right = parser.Parse(), }; }
public void ParseUnaryNegate() { var v = 5; Expression<Func<int>> f = () => -v; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new UnaryNode { Prefix = "-", PrefixValue = "-5", Operand = new ConstantNode() {Text = "v", Value = "5"}, }; Assert.AreEqual(expected, node); }
public void ParseUnaryNotWrappingBinaryOperation() { var v = true; Expression<Func<bool>> f = () => !(v == false); var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new UnaryNode { Prefix = "!", PrefixValue = "True", Operand = new BinaryNode { Left = new ConstantNode {Text = "v", Value = "True"}, Right = new ConstantNode {Text = "False", Value = null}, Operator = "==", Value = "False" } }; Assert.AreEqual(expected, node); }
public void ParseUnaryNot() { var v = true; Expression<Func<bool>> f = () => !v; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new UnaryNode { Prefix = "!", PrefixValue = "False", Operand = new ConstantNode() {Text = "v", Value = "True"}, }; Assert.AreEqual(expected, node); }
public void ParseIsOperator() { object x = "xValue"; Expression<Func<bool>> f = () => x is string; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new BinaryNode() { Left = new ConstantNode() {Text = "x", Value = "\"xValue\""}, Operator = "is", Right = new ConstantNode() {Text = "string"}, Value = "True" }; Assert.AreEqual(expected, node); }
public void ParseCast() { double x = 5.1; Expression<Func<int>> f = () => (int) x; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new UnaryNode { Prefix = "(int)", PrefixValue = "5", Operand = new ConstantNode() {Text = "x", Value = 5.1M.ToString()}, }; Assert.AreEqual(expected, node); }
public void ParseMethodWithException() { Expression<Func<int>> f = () => ThrowException(); var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new MethodCallNode { Container = new ConstantNode {Text = "PowerAssertTests.ParserTest"}, MemberName = "ThrowException", MemberValue = @"(threw DivideByZeroException)", }; Assert.AreEqual(expected, node); }
public void ParseEnumerableWithNulls() { var v = new List<int?> {1, 2, null, 4}; Expression<Func<List<int?>>> f = () => v; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new ConstantNode() { Text = "v", Value = "[1, 2, null, 4]" }; Assert.AreEqual(expected, node); }
public void ParseBinaryWithNullRightHandSide() { string rightHandSide = null; Expression<Func<bool>> f = () => "foo" == rightHandSide; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new BinaryNode { Left = new ConstantNode {Text = "\"foo\""}, Right = new ConstantNode {Text = "rightHandSide", Value = "null"}, Operator = "==", Value = "False" }; Assert.AreEqual(expected, node); }
public void ParseConditional() { bool b = false; Expression<Func<int>> f = () => b ? 1 : 2; var p = new ExpressionParser(f.Body); var node = p.Parse(); var expected = new ConditionalNode { Condition = new ConstantNode {Text = "b", Value = "False"}, TrueNode = new ConstantNode {Text = "1"}, TrueValue = "1", FalseNode = new ConstantNode {Text = "2"}, FalseValue = "2", }; Assert.AreEqual(expected, node); }