private static string AsName(IExpression expression) { if (expression is VariableExpression) { return(((VariableExpression)expression).VariableName); } if (expression is DotExpression) { DotExpression dot = (DotExpression)expression; return(AsName(dot.Expression) + "." + dot.Name); } return(null); }
private static IDictionary ResolveToDictionary(DotExpression expression, IBindingEnvironment environment) { object obj = ResolveToObject(expression.Expression, environment); if (obj is DynamicObject) { DynamicObject dynobj = (DynamicObject)obj; obj = dynobj.GetValue(expression.Name); if (obj == null) { obj = new Hashtable(); dynobj.SetValue(expression.Name, obj); } return (IDictionary)obj; } return (IDictionary)ObjectUtilities.GetValue(obj, expression.Name); }
public void ResolveDotExpressionToObject() { BindingEnvironment environment = new BindingEnvironment(); IExpression expression = new DotExpression(new VariableExpression("Project"), "Title"); object obj = ExpressionUtilities.ResolveToObject(expression, environment); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IObject)); object project = environment.GetValue("Project"); Assert.IsNotNull(project); Assert.IsInstanceOfType(project, typeof(IObject)); object title = ((IObject)project).GetValue("Title"); Assert.IsNotNull(title); Assert.IsInstanceOfType(title, typeof(IObject)); Assert.AreEqual(obj, title); }
public void ResolveDotExpressionToDictionary() { BindingEnvironment environment = new BindingEnvironment(); IExpression expression = new DotExpression(new VariableExpression("Project"), "Entities"); object obj = ExpressionUtilities.ResolveToDictionary(expression, environment); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IDictionary)); object project = environment.GetValue("Project"); Assert.IsNotNull(project); Assert.IsInstanceOfType(project, typeof(IObject)); object entities = ((IObject)project).GetValue("Entities"); Assert.IsNotNull(entities); Assert.IsInstanceOfType(entities, typeof(IDictionary)); Assert.AreEqual(obj, entities); }
private static void SetValue(DotExpression expression, object value, IBindingEnvironment environment) { if (expression.Arguments != null) throw new InvalidOperationException("Invalid left value"); object obj = ResolveToObject(expression.Expression, environment); ObjectUtilities.SetValue(obj, expression.Name, value); }
private static object ResolveToObject(DotExpression expression, IBindingEnvironment environment) { object obj = ResolveToObject(expression.Expression, environment); if (obj is DynamicObject) { DynamicObject dynobj = (DynamicObject)obj; obj = dynobj.GetValue(expression.Name); if (obj == null) { obj = new DynamicObject(); dynobj.SetValue(expression.Name, obj); } return obj; } return ObjectUtilities.GetValue(obj, expression.Name); }
public void EvaluateDotExpressionOnString() { IExpression expression = new DotExpression(new ConstantExpression("foo"), "Length"); Assert.AreEqual(3, expression.Evaluate(null)); }
public void EvaluateDotExpressionOnInteger() { IExpression expression = new DotExpression(new ConstantExpression(1), "ToString", new List<IExpression>()); Assert.AreEqual("1", expression.Evaluate(null)); }
public void EvaluateDotExpressionAsTypeInvocation() { DotExpression dot = new DotExpression(new DotExpression(new DotExpression(new VariableExpression("System"), "IO"), "File"), "Exists", new IExpression[] { new ConstantExpression("unknown.txt") }); Assert.IsFalse((bool)dot.Evaluate(new BindingEnvironment())); }
private IExpression ParseTermExpression() { IExpression expression = this.ParseSimpleTermExpression(); while (this.TryParse(TokenType.Operator, ".") || this.TryParse(TokenType.Separator, "[", "(")) { if (this.TryParse(TokenType.Separator, "(")) expression = new InvokeExpressionExpression(expression, this.ParseArgumentList()); else if (this.TryParse(TokenType.Operator, ".")) { this.lexer.NextToken(); string name = this.ParseName(); List<IExpression> arguments = null; if (this.TryParse(TokenType.Separator, "(")) arguments = this.ParseArgumentList(); expression = new DotExpression(expression, name, arguments); } else expression = new ArrayExpression(expression, this.ParseArrayArgumentList()); } return expression; }
public void ExecuteSetCommandWithObjectProperty() { Machine machine = new Machine(); TextWriter outwriter = new StringWriter(); machine.Environment.SetValue("machine", machine); machine.Environment.SetValue("out", outwriter); DotExpression dotexpr = new DotExpression(new VariableExpression("machine"), "Out"); SetCommand command = new SetCommand(dotexpr, new VariableExpression("out")); command.Execute(machine.Environment); Assert.AreEqual(machine.Out, outwriter); }
public void ExecuteSetCommandWithDotExpression() { BindingEnvironment environment = new BindingEnvironment(); DotExpression dotexpr = new DotExpression(new VariableExpression("foo"), "FirstName"); SetCommand command = new SetCommand(dotexpr, new ConstantExpression("bar")); command.Execute(environment); object obj = environment.GetValue("foo"); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(DynamicObject)); DynamicObject dynobj = (DynamicObject)obj; Assert.AreEqual("bar", dynobj.GetValue("FirstName")); }
public void ExecuteSetArrayCommandWithDotExpression() { BindingEnvironment environment = new BindingEnvironment(); DotExpression dotexpr = new DotExpression(new VariableExpression("foo"), "Values"); SetArrayCommand command = new SetArrayCommand(dotexpr, new IExpression[] { new ConstantExpression(0) }, new ConstantExpression("bar")); command.Execute(environment); object obj = environment.GetValue("foo"); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(DynamicObject)); DynamicObject dynobj = (DynamicObject)obj; object obj2 = dynobj.GetValue("Values"); Assert.IsNotNull(obj2); Assert.IsInstanceOfType(obj2, typeof(IList)); IList list = (IList)obj2; Assert.AreEqual(1, list.Count); Assert.AreEqual("bar", list[0]); }