Beispiel #1
0
        public void ExpressionCondition_Expression()
        {
            var group = new ConditionGroup();

            CommandHelper.TestUndoableCommand(
                () => Assert.IsNull(group.If.Expression),
                () => group.If.Expression = "true",
                () => Assert.AreEqual("true", group.If.Expression)
            );
        }
        public void AbstractConditionGroup_AddElse()
        {
            var group = new ConditionGroup();

            CommandHelper.TestUndoableCommand(
                () => Assert.IsNull(group.Else),
                () => group.AddElseCommand.Execute(null),
                () => Assert.IsNotNull(group.Else)
            );
        }
        public void AbstractConditionGroup_AddElseIf()
        {
            var group = new ConditionGroup();

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(0, group.Statements.Count),
                () => group.AddElseIfCommand.Execute(new ExpressionCondition()),
                () => Assert.AreEqual(1, group.Statements.Count)
            );
        }
        public void AddElseIfCondition()
        {
            bool collectionChanged = false;

            ConditionGroup group = new ConditionGroup();
            group.Statements.CollectionChanged += (o, e) => collectionChanged = true;

            ExpressionCondition condition = new ExpressionCondition() { Expression = "test > 1" };
            group.AddStatement(condition);

            Assert.IsTrue(collectionChanged);
            Assert.AreEqual("test > 1", condition.Expression);
            Assert.AreEqual(1, group.Statements.Count);
        }
        public void AddConditionGroup()
        {
            bool collectionChanged = false;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            evt.Statements.CollectionChanged += (o, e) => collectionChanged = true;

            ConditionGroup group = new ConditionGroup();
            evt.AddStatement(group);

            Assert.IsTrue(collectionChanged);
            Assert.AreEqual(1, evt.Statements.Count);
            Assert.IsNotNull(group.If);
            Assert.IsNull(group.Else);
        }
        public void AbstractConditionGroup_RemoveStatement()
        {
            var entity = new Entity();
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent)));
            var group = new ConditionGroup();
            var elif = new ExpressionCondition();
            var el = new BasicCondition();
            group.AddStatement(elif);
            group.Else = el;
            evt.AddStatement(group);

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(elif, group.Statements.First()),
                () => entity.DeleteStatementCommand.Execute(elif),
                () => Assert.AreEqual(0, group.Statements.Count)
            );

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(el, group.Else),
                () => entity.DeleteStatementCommand.Execute(el),
                () => Assert.IsNull(group.Else)
            );
        }
        public override sealed AbstractStatement DeepCopyStatement()
        {
            ConditionGroup copy = new ConditionGroup() { If = this.If.DeepCopyCondition() };

            foreach (AbstractStatement statement in Statements)
            {
                copy.AddStatement(statement.DeepCopyStatement());
            }

            if (null != Else)
            {
                copy.Else = Else.DeepCopyCondition();
            }

            return copy;
        }
        private void CreateStatement(ParseTreeNode statementNode, Event evt, CompositeStatement compositeStatement = null)
        {
            AbstractStatement statement = null;
            if (statementNode.Term == grammar.Action)
            {
                statement = createAction(statementNode);
            }
            else if (statementNode.Term == grammar.Condition)
            {
                ConditionGroup conditionGroup = new ConditionGroup();
                conditionGroup.If.Expression = getStrVal(statementNode.ChildNodes.First(child => child.Term == grammar.Expr));
                foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions)) CreateStatement(child, evt, conditionGroup.If);
                foreach (ParseTreeNode elseNode in grammar.GetOfType(statementNode, grammar.Else))
                {
                    string expr = getStrVal(elseNode.ChildNodes.FirstOrDefault(child => child.Term == grammar.Expr));
                    AbstractCondition cond;

                    if (null == expr)
                    {
                        conditionGroup.Else = new BasicCondition();
                        cond = conditionGroup.Else;
                    }
                    else
                    {
                        cond = new ExpressionCondition() { Expression = expr };
                        conditionGroup.AddStatement(cond);
                    }

                    foreach (ParseTreeNode child in grammar.GetOfType(elseNode, grammar.Actions)) CreateStatement(child, evt, cond);
                }
                statement = conditionGroup;
            }
            else if (statementNode.Term == grammar.Assignment)
            {
                Assignment assignment = new Assignment();
                assignment.Key = getStrVal(statementNode.ChildNodes[0]);
                assignment.Value = getStrVal(statementNode.ChildNodes.Last());

                assignment.Operator = statementNode.ChildNodes.Count == 2 ? AssignmentOperator.Assign :
                    AbstractAssignment.AssignmentValues[grammar.OpLookup[statementNode.ChildNodes[1].Term]];

                statement = assignment;
            }
            else
            {
                CompositeStatement loop = null;
                switch (statementNode.ChildNodes.Count)
                {
                    case 2:
                        WhileLoop wl = new WhileLoop();
                        wl.Expression = getStrVal(statementNode.ChildNodes[0]);
                        statement = loop = wl;
                        break;
                    case 3:
                        ForLoop fl = new ForLoop();
                        fl.Expression = getStrVal(statementNode.ChildNodes[0]);
                        //First assignment, expression, assignment, actions
                        fl.PostExpression = getStrVal(statementNode.ChildNodes[1]);
                        statement = loop = fl;
                        break;
                    case 4:
                        ForLoop forLoop = new ForLoop();
                        forLoop.Expression = getStrVal(statementNode.ChildNodes[1]);
                        //First assignment, expression, assignment, actions
                        forLoop.PostExpression = getStrVal(statementNode.ChildNodes[2]);
                        forLoop.PreExpression = getStrVal(statementNode.ChildNodes[0]);
                        statement = loop = forLoop;
                        break;
                }
                foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions))
                {
                    // TODO: This is a hack to skip the Actions found in the first line of the for() statement.
                    if (!statementNode.ChildNodes.Contains(child))
                    {
                        CreateStatement(child, evt, loop);
                    }
                }
            }
            if (compositeStatement == null) evt.AddStatement(statement);
            else compositeStatement.AddStatement(statement);
        }
        public void RemoveElseIfCondition()
        {
            int eventsFired = 0;

            ConditionGroup group = new ConditionGroup();
            group.Statements.CollectionChanged += (o, e) => eventsFired++;

            ExpressionCondition condition = new ExpressionCondition() { Expression = "test > 1" };
            group.AddStatement(condition);
            group.RemoveStatement(condition);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(0, group.Statements.Count);
        }
        public void RemoveConditionGroup()
        {
            int eventsFired = 0;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            evt.Statements.CollectionChanged += (o, e) => eventsFired++;

            ConditionGroup group = new ConditionGroup();
            evt.AddStatement(group);
            evt.RemoveStatement(group);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(0, evt.Statements.Count);
        }
        public void AddReadOnlyElseIfCondition()
        {
            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            ReadOnlyEvent childEvent = new ReadOnlyEvent(parentEvent);

            ConditionGroup parentGroup = new ConditionGroup();
            parentEvent.AddStatement(parentGroup);

            parentGroup.AddStatement(new ExpressionCondition() { Expression = "test > 1" });

            Assert.AreEqual(1, childEvent.Statements.Count);

            ReadOnlyConditionGroup childGroup = childEvent.Statements.OfType<ReadOnlyConditionGroup>().Single();

            Assert.IsNotNull(childGroup);

            ReadOnlyExpressionCondition childCondition = childGroup.Statements.OfType<ReadOnlyExpressionCondition>().Single();

            Assert.AreEqual("test > 1", childCondition.Expression);
        }