public void CreateActionTest()
        {
            var collection = new ActionCollection();
            var action     = new MockAction();

            action.NameId = "someaction";
            collection.Add(action);
            var newAction = collection.Create("someaction");

            Assert.AreEqual(action.NameId, newAction.NameId);
            Assert.AreEqual(action.ActionStatus, newAction.ActionStatus);
            Assert.AreEqual(action.InCooldown, newAction.InCooldown);
            Assert.AreNotEqual(action, newAction);
        }
Example #2
0
    [Test] public void Sequence_FirstChildRunningAndNextChildSuccess()
    {
        MockAction mockActionA = new MockAction(BehaviorStatus.Running);

        BehaviorTree behaviorTree = new BehaviorTreeBuilder<BehaviorTree>(new Sequence())
            .Action(mockActionA)
            .Condition(new MockCondition(BehaviorStatus.Success))
        .Build();

        Assert.IsTrue(behaviorTree.Tick() == BehaviorStatus.Running);

        mockActionA.status = BehaviorStatus.Failure;
        Assert.IsTrue(behaviorTree.Tick() == BehaviorStatus.Failure);
    }
        void SetupActionsAndConsiderations()
        {
            _aiConstructor.AIs.ClearAll();

            var tmpa = new MockAction("a1", _aiConstructor.Actions);

            tmpa = new MockAction("a2", _aiConstructor.Actions);
            tmpa = new MockAction("a3", _aiConstructor.Actions);
            tmpa = new MockAction("a4", _aiConstructor.Actions);

            var tmpc = new OptionConsideration1("c1", _aiConstructor.Considerations);

            tmpc = new OptionConsideration1("c2", _aiConstructor.Considerations);
            tmpc = new OptionConsideration1("c3", _aiConstructor.Considerations);
            tmpc = new OptionConsideration1("c4", _aiConstructor.Considerations);
            tmpc = new OptionConsideration1("c5", _aiConstructor.Considerations);
        }
Example #4
0
        public void BehaviourSelectionTest()
        {
            var ai      = new UtilityAi();
            var context = new UaiContext();
            var b1      = new Behaviour();

            b1.NameId = "b1";
            var b2 = new Behaviour();

            b2.NameId = "b2";
            var consideration1 = new BehaviourConsideration1();
            var consideration2 = new BehaviourConsideration2();

            b1.AddConsideration(consideration1);
            b2.AddConsideration(consideration2);

            var a1 = new MockAction();

            a1.NameId = "a1";
            var fo1 = new ConstantUtilityOption();

            fo1.SetAction(a1);
            b1.AddOption(fo1);
            var a2 = new MockAction();

            a2.NameId = "a2";
            var fo2 = new ConstantUtilityOption();

            fo2.SetAction(a2);
            b2.AddOption(fo2);

            ai.AddBehaviour(b1);
            ai.AddBehaviour(b2);

            context.Val1 = 1.0f;
            context.Val2 = 0.0f;
            var sel1 = ai.Select(context);

            Assert.That(sel1 == a1);
            context.Val1 = 0.0f;
            context.Val2 = 1.0f;
            var sel2 = ai.Select(context);

            Assert.That(sel2 == a2);
        }
        public void ShouldFireTriggersOnPropertyChanged()
        {
            PropertyTrigger trigger = new PropertyTrigger()
            {
                PropertyName = "HorizontalAlignment"
            };

            MockAction action = new MockAction();
            trigger.Actions.Add(action);
            FrameworkElement element = new FrameworkElement();

            System.Windows.Interactivity.TriggerCollection triggers = Interaction.GetTriggers(element);
            triggers.Add(trigger);

            element.HorizontalAlignment = HorizontalAlignment.Left;

            Assert.IsTrue(action.hasExecuted);
            Assert.IsTrue(action.IsAttached);
        }
 protected override void DefineActions()
 {
     A = new MockAction(EatAction, Actions)
     {
         Message = "Eating..."
     };
     A = new MockAction(DrinkAction, Actions)
     {
         Message = "Drinking..."
     };
     A = new MockAction(ToiletAction, Actions)
     {
         Message = "Toilet..."
     };
     A = new MockAction(ShowerAction, Actions)
     {
         Message = "Shower..."
     };
 }
Example #7
0
        public void CloneTests(ActionSequence action, int numActions)
        {
            for (int i = 0; i < numActions; i++)
            {
                var a = new MockAction();
                a.NameId = string.Format("Name{0}", i);
                action.Actions.Add(a);
            }

            var newAction = action.Clone() as ActionSequence;

            Assert.AreEqual(action.Actions.Count, newAction.Actions.Count);
            Assert.AreEqual(action.NameId, newAction.NameId);
            for (int i = 0; i < numActions; i++)
            {
                var name = string.Format("Name{0}", i);
                Assert.AreEqual(action.Actions[i].NameId, newAction.Actions[i].NameId);
            }
        }
Example #8
0
        [Test] public void UnregisterAction()
        {
            MockAction action = new MockAction();

            _actionManager.RegisterAction(action, null, ListAnchor.Last,
                                          (Image)null, "My Action", "", null, null);
            _actionManager.UpdateToolbarActions();

/*
 *          Assert.AreEqual( 2, _toolBar.Buttons.Count );
 *          Assert.AreEqual( ToolBarButtonStyle.Separator, _toolBar.Buttons [1].Style );
 *          Assert.AreEqual( false, _toolBar.Buttons [1].Visible );
 */
            Assert.AreEqual(2, _toolBar.Items.Count);
//            Assert.AreEqual( ToolBarButtonStyle.Separator, _toolBar.Items [1].Style );
            Assert.AreEqual(false, _toolBar.Items [1].Visible);

            _actionManager.UnregisterAction(action);
            Assert.AreEqual(1, _toolBar.Items.Count);
//            Assert.AreEqual( ToolBarButtonStyle.Separator, _toolBar.Items [0].Style );
        }
Example #9
0
 private MockEngineResponse BuildResponse(V8ScriptEngine scriptEngine, string scenarioName, MockAction action)
 {
     try
     {
         var response     = BuildMockEngineResponse(scriptEngine, scenarioName, action);
         var mockResponse = action.Response;
         if (mockResponse.Body != null || mockResponse.BodyXml != null)
         {
             Type responseType;
             if (mockResponse.BodyType == null)
             {
                 responseType = typeof(object);
             }
             else
             {
                 responseType = _typeResolver.Resolve(action.Response.BodyType, true);
             }
             if (mockResponse.Body == null)
             {
                 if (mockResponse.BodyXml == null)
                 {
                     _logProvider.Warning("response body or xml was not defined");
                 }
                 else
                 {
                     var processedXml = ApplyExpressions(scriptEngine, mockResponse.BodyXml, false);
                     response.Content = DeserializeXml(processedXml, responseType);
                 }
             }
             else
             {
                 var processedBody = ApplyYamlExpressions(scriptEngine, mockResponse.Body);
                 if (responseType == typeof(object))
                 {
                     response.Content = processedBody;
                 }
                 else
                 {
                     var processedYaml = _scenarioManager.Serialize(processedBody);
                     response.Content = _scenarioManager.Deserialize(processedYaml, responseType);
                 }
             }
         }
         return(response);
     }
     catch (TypeResolverException e)
     {
         var message = $"response type \"{e.TypeName}\" not found: {e.Message}";
         _logProvider.Error(e, "response type {typeName} not found: {reason}", e.TypeName, e.Message);
         throw new MockEngineException(this.Name, scenarioName, message);
     }
     catch (YamlDotNet.Core.SyntaxErrorException e)
     {
         var message = "syntax error building response: {e.Message}";
         _logProvider.Error(e, "syntax error building response: {reason}", e.Message);
         throw new MockEngineException(this.Name, scenarioName, message, e);
     }
     catch (YamlException e)
     {
         var message = $"error deserializing response: {e.InnerException.Message} at {e.Source}";
         _logProvider.Error("error deserializing response: {reason} at {source}", e.InnerException.Message, e.Source);
         throw new MockEngineException(this.Name, scenarioName, message, e);
     }
     catch (Exception e)
     {
         var message = $"error building response: {e.Message}";
         _logProvider.Error("error building response: {reason}", e.Message);
         throw new MockEngineException(this.Name, scenarioName, message, e);
     }
 }
Example #10
0
        private MockEngineResponse BuildMockEngineResponse(V8ScriptEngine scriptEngine, string scenarioName, MockAction action)
        {
            var mockResponse = action.Response;
            var response     = new MockEngineResponse()
            {
                Success = true
            };

            if (!string.IsNullOrWhiteSpace(mockResponse.Reason))
            {
                response.ReasonPhrase = mockResponse.Reason;
            }
            if (string.IsNullOrWhiteSpace(mockResponse.StatusCode))
            {
                response.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                HttpStatusCode statusCode;
                int            intStatusCode;
                if (Enum.TryParse <HttpStatusCode>(mockResponse.StatusCode, true, out statusCode))
                {
                    response.StatusCode = statusCode;
                }
                else if (int.TryParse(mockResponse.StatusCode, out intStatusCode))
                {
                    var value = Enum.ToObject(typeof(HttpStatusCode), intStatusCode);
                    if (Enum.IsDefined(typeof(HttpStatusCode), value))
                    {
                        response.StatusCode = (HttpStatusCode)value;
                    }
                    else
                    {
                        response.StatusCode   = HttpStatusCode.InternalServerError;
                        response.ReasonPhrase = $"action {action} specified an invalid status code \"{mockResponse.StatusCode}\"";
                        _logProvider.Warning("action {action} specified an invalid status code {statusCode}", action, mockResponse.StatusCode);
                    }
                }
                else
                {
                    response.StatusCode = HttpStatusCode.InternalServerError;
                    _logProvider.Warning("action {action} specified an invalid status code {statusCode}", action, mockResponse.StatusCode);
                }
            }
            return(response);
        }
Example #11
0
        private MockEngineResponse ExecuteAction(V8ScriptEngine scriptEngine, string scenarioName, MockAction action)
        {
            _logProvider.Information("executing action: {action}", action);
            var response = new MockEngineResponse()
            {
                Success = true
            };

            if (action.Before != null)
            {
                try
                {
                    scriptEngine.Execute(action.Before);
                }
                catch (Exception e)
                {
                    throw new MockEngineException(this.Name, scenarioName, $"error executing Before code: {e.Message}", e);
                }
            }
            if (action.Response != null)
            {
                response = BuildResponse(scriptEngine, scenarioName, action);
                if (response != null)
                {
                    scriptEngine.AddHostObject("response", response);
                }
            }
            if (action.After != null)
            {
                try
                {
                    scriptEngine.Execute(action.After);
                }
                catch (Exception e)
                {
                    throw new MockEngineException(this.Name, scenarioName, $"error executing After code: {e.Message}", e);
                }
            }
            if (action.Log != null)
            {
                _logProvider.Information(ApplyExpressions(scriptEngine, action.Log, false));
            }
            return(response);
        }