Beispiel #1
0
        public void AddLocalEvent()
        {
            bool collectionChanged = false;

            Entity entity = new Entity();
            entity.Events.CollectionChanged += (o, e) => collectionChanged = true;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            entity.AddEvent(evt);

            Assert.IsTrue(collectionChanged);
            Assert.IsTrue(evt.IsEditable);
            Assert.AreEqual(1, entity.Events.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);
        }
Beispiel #3
0
        public void AddLocalAction()
        {
            int eventsRaised = 0;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));

            evt.Statements.CollectionChanged += (sender, e) => eventsRaised++;

            Action action = new Action(Workspace.Instance.GetPlugin(FireTriggerActionType));
            evt.AddStatement(action);

            Assert.AreEqual(1, eventsRaised);
            Assert.IsTrue(action.IsEditable);
            Assert.AreEqual(1, evt.Statements.Count);
        }
Beispiel #4
0
        public void CannotRemoveReadOnlyEventFromInheritingEntity()
        {
            bool collectionChanged = false;

            Entity parent = new Entity() { Name = "parent" };

            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            parentEvent.SetProperty("Trigger", new Value("test"));
            parent.AddEvent(parentEvent);

            Entity child = new Entity();
            child.AddPrototype(parent);

            child.Events.CollectionChanged += (o, e) => collectionChanged = true;

            AbstractEvent childEvent = child.Events.Single();
            child.RemoveEvent(childEvent);

            Assert.IsFalse(collectionChanged);
            Assert.AreEqual(1, child.Events.Count);
        }
Beispiel #5
0
        public void AddInheritedAction()
        {
            int parentEventsRaised = 0;
            int childEventsRaised = 0;

            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            parentEvent.Statements.CollectionChanged += (sender, e) => parentEventsRaised++;

            parentEvent.AddStatement(new Action(Workspace.Instance.GetPlugin(FireTriggerActionType)));

            ReadOnlyEvent childEvent = new ReadOnlyEvent(parentEvent);
            childEvent.Statements.CollectionChanged += (sender, e) => childEventsRaised++;

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

            parentEvent.AddStatement(new Action(Workspace.Instance.GetPlugin(FireTriggerActionType)));

            Assert.AreEqual(2, childEvent.Statements.Count);
            Assert.AreEqual(2, parentEventsRaised);
            Assert.AreEqual(1, childEventsRaised);
        }
        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 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);
        }
Beispiel #8
0
        public void SetEventProperty()
        {
            bool propertyChanged = false;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));

            AbstractProperty property = evt.GetProperty("Trigger");
            property.PropertyChanged += (o, e) => propertyChanged |= (e.PropertyName == "Value");

            evt.SetProperty("Trigger", new Value("test", true));

            Assert.IsTrue(propertyChanged);
            Assert.AreEqual("test", property.Value.Reader.GetStrValue());
        }
Beispiel #9
0
        public void RemoveReadOnlyEvent()
        {
            int eventsFired = 0;

            Entity parent = new Entity() { Name = "parent" };

            Entity child = new Entity();
            child.Events.CollectionChanged += (o, e) => eventsFired++;

            child.AddPrototype(parent);

            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            parent.AddEvent(parentEvent);
            parent.RemoveEvent(parentEvent);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(0, parent.Events.Count);
            Assert.AreEqual(0, child.Events.Count);
        }
Beispiel #10
0
        public void RemoveLocalEvent()
        {
            int eventsFired = 0;

            Entity entity = new Entity();
            entity.Events.CollectionChanged += (o, e) => eventsFired++;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            entity.AddEvent(evt);
            entity.RemoveEvent(evt);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(0, entity.Events.Count);
        }
Beispiel #11
0
        public void EventsInheritedFromPrototype()
        {
            bool collectionChanged = false;

            Entity parent = new Entity() { Name = "parent" };

            Entity child = new Entity();
            child.Events.CollectionChanged += (o, e) => collectionChanged = true;

            child.AddPrototype(parent);

            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            parent.AddEvent(parentEvent);

            Assert.IsTrue(collectionChanged);
            Assert.AreEqual(1, parent.Events.Count(x => x.IsEditable));
            Assert.AreEqual(1, child.Events.Count(x => x.IsReadOnly));
        }
        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);
        }
Beispiel #13
0
        public void AddMultipleActionsOfSameType()
        {
            int eventsRaised = 0;

            Event evt = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));

            evt.Statements.CollectionChanged += (sender, e) => eventsRaised++;

            evt.AddStatement(new Action(Workspace.Instance.GetPlugin(FireTriggerActionType)));
            evt.AddStatement(new Action(Workspace.Instance.GetPlugin(FireTriggerActionType)));

            Assert.AreEqual(2, eventsRaised);
            Assert.AreEqual(2, evt.Statements.Count);
        }
Beispiel #14
0
        public void CannotSetReadOnlyEventProperty()
        {
            Entity parent = new Entity() { Name = "parent" };

            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            parentEvent.SetProperty("Trigger", new Value("test", true));
            parent.AddEvent(parentEvent);

            Entity child = new Entity();
            child.AddPrototype(parent);

            AbstractProperty childProperty = child.Events.Single().GetProperty("Trigger");
            childProperty.Value = new Value("test2", true);

            Assert.AreEqual("test", childProperty.Value.Reader.GetStrValue());
        }
Beispiel #15
0
        public void Event_AddStatement()
        {
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent).FullName));

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(0, evt.Statements.Count),
                () => evt.AddStatementCommand.Execute(new WhileLoop()),
                () => Assert.AreEqual(1, evt.Statements.Count)
            );
        }
Beispiel #16
0
        public void Entity_RemoveEvent()
        {
            var entity = new Entity();
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent).FullName));
            entity.AddEvent(evt);

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(1, entity.Events.Count),
                () => entity.RemoveEventCommand.Execute(evt),
                () => Assert.AreEqual(0, entity.Events.Count)
            );
        }
Beispiel #17
0
        public void Entity_DeleteStatement()
        {
            var entity = new Entity();
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent).FullName));
            entity.AddEvent(evt);
            var loop = new WhileLoop();
            evt.AddStatement(loop);

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(1, evt.Statements.Count),
                () => entity.DeleteStatementCommand.Execute(loop),
                () => Assert.AreEqual(0, evt.Statements.Count)
            );
        }
Beispiel #18
0
        public void AbstractStatement_InsertBefore()
        {
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent).FullName));
            var loop = new WhileLoop();
            evt.AddStatement(loop);
            var stmt = new ForLoop();

            CommandHelper.TestUndoableCommand(
                () =>
                {
                    Assert.AreEqual(1, evt.Statements.Count);
                    Assert.AreEqual(0, evt.IndexOf(loop));
                    Assert.AreEqual(-1, evt.IndexOf(stmt));
                },
                () => loop.InsertBeforeCommand.Execute(stmt),
                () =>
                {
                    Assert.AreEqual(2, evt.Statements.Count);
                    Assert.AreEqual(1, evt.IndexOf(loop));
                    Assert.AreEqual(0, evt.IndexOf(stmt));
                }
            );
        }
        public void AddReadOnlyConditionGroup()
        {
            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));
            ReadOnlyEvent childEvent = new ReadOnlyEvent(parentEvent);

            parentEvent.AddStatement(new ConditionGroup());

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

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

            Assert.IsNotNull(childGroup);
        }
Beispiel #20
0
        public void InheritedActionPropertyFollowsSource()
        {
            bool childPropertyChanged = false;

            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));

            Action parentAction = new Action(Workspace.Instance.GetPlugin(FireTriggerActionType));
            parentAction.SetProperty("Name", new Value("test", true));
            parentEvent.AddStatement(parentAction);

            ReadOnlyEvent childEvent = new ReadOnlyEvent(parentEvent);

            AbstractProperty childProperty = ((AbstractAction)childEvent.Statements.Single()).GetProperty("Name");
            childProperty.PropertyChanged += (sender, e) => childPropertyChanged |= (e.PropertyName == "Value");

            parentAction.SetProperty("Name", new Value("test2", true));

            Assert.AreEqual("test2", childProperty.Value.Reader.GetStrValue());
            Assert.IsTrue(childPropertyChanged);
        }
 private Event CreateEvent(ParseTreeNode node)
 {
     Event evt = new Event(game.GetPlugin(grammar.GetName(node)));
     foreach (Tuple<string, object> property in GetProperties(node))
     {
         ParseTreeNode propertyNode = (ParseTreeNode)property.Item2;
         evt.SetProperty(property.Item1, new Value(getStrVal(propertyNode)));
     }
     foreach (ParseTreeNode actionNode in grammar.GetOfType(node, grammar.Actions)) CreateStatement(actionNode, evt, null);
     return evt;
 }
        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);
        }
Beispiel #23
0
        public void Property_Value()
        {
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent).FullName));
            var property = evt.Properties.First();
            var val = new Value("5");

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(property.PluginProperty.DefaultValue, property.Value),
                () => property.Value = val,
                () => Assert.AreEqual(val, property.Value)
            );
        }
Beispiel #24
0
        public void CannotRemoveInheritedActionFromInheritingEvent()
        {
            Event parentEvent = new Event(Workspace.Instance.GetPlugin(TriggerOccursEventType));

            Action parentAction = new Action(Workspace.Instance.GetPlugin(FireTriggerActionType));
            parentEvent.AddStatement(parentAction);

            ReadOnlyEvent childEvent = new ReadOnlyEvent(parentEvent);

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

            AbstractStatement childAction = childEvent.Statements.Single();
            childEvent.RemoveStatement(childAction);

            Assert.AreEqual(1, childEvent.Statements.Count);
        }
        public Event DeepCopy()
        {
            Event copy = new Event(Plugin);

            foreach (AbstractProperty property in Properties)
            {
                copy.SetProperty(property.Name, property.Value);
            }

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

            return copy;
        }