Ejemplo n.º 1
0
 public void ValueTest()
 {
     var item = new TestSwitch();
     item.SwitchValue = "1";
     item.SwitchValue = "0";
     item.SwitchValue = "-1";
 }
Ejemplo n.º 2
0
 public void DescriptionTest()
 {
     var item = new TestSwitch("Desc");
     Assert.Equal("Desc", item.Description);
     item = new TestSwitch();
     Assert.Equal("", item.Description);
 }
Ejemplo n.º 3
0
        public void SwitchStandAlone()
        {
            //  SwitchNoDefaultSimple switch case scenario
            //  Test case description:
            //  Simple switch case scenario

            TestSwitch <int> switchAct = new TestSwitch <int>
            {
                DisplayName = "standAloneSwitch",
                Expression  = 23
            };

            switchAct.AddCase(12, new TestProductWriteline {
                Text = "in case 12"
            });
            switchAct.AddCase(23, new TestProductWriteline {
                Text = "in case 23"
            });
            switchAct.AddCase(123, new TestProductWriteline {
                Text = "in case 123"
            });
            switchAct.AddCase(234, new TestProductWriteline {
                Text = "in case 234"
            });

            switchAct.Hints.Add(1);
            TestRuntime.RunAndValidateWorkflow(switchAct);
        }
Ejemplo n.º 4
0
        public void SwitchDefault()
        {
            //  SwitchDefaultSimple switch case default
            //  Test case description:
            //  Simple switch case default
            TestSwitch <int> switchAct        = new TestSwitch <int>();
            Variable <int>   switchExpression = new Variable <int>("switchExpression", 444);
            TestSequence     seq = new TestSequence();

            seq.Variables.Add(switchExpression);

            switchAct.ExpressionVariable = switchExpression;
            switchAct.AddCase(12, new TestProductWriteline {
                Text = "in case 12"
            });
            switchAct.AddCase(23, new TestProductWriteline {
                Text = "in case 23"
            });
            switchAct.AddCase(123, new TestProductWriteline {
                Text = "in case 123"
            });
            switchAct.AddCase(234, new TestProductWriteline {
                Text = "in case 234"
            });
            switchAct.Default = new TestProductWriteline {
                Text = "in default"
            };

            switchAct.Hints.Add(-1);
            seq.Activities.Add(switchAct);
            TestRuntime.RunAndValidateWorkflow(seq);
        }
Ejemplo n.º 5
0
        public void DescriptionTest()
        {
            var item = new TestSwitch("Desc");

            Assert.Equal("Desc", item.Description);
            item = new TestSwitch();
            Assert.Equal("", item.Description);
        }
Ejemplo n.º 6
0
        public void ValueTest()
        {
            var item = new TestSwitch();

            item.SwitchValue = "1";
            item.SwitchValue = "0";
            item.SwitchValue = "-1";
        }
Ejemplo n.º 7
0
        public void EmptySwitch()
        {
            TestSwitch <string> switchAct = new TestSwitch <string>();

            switchAct.Hints.Add(-1);
            switchAct.Expression = "";
            TestRuntime.RunAndValidateWorkflow(switchAct);
        }
    private static void Main()
    {
        TestSwitch test = new TestSwitch();

        test.SelectLanguage();

        Console.ReadKey();
    }
Ejemplo n.º 9
0
 public void PruneTest()
 {
     var strongSwitch = new TestSwitch();
     var weakSwitch = new WeakReference(new TestSwitch());
     Assert.True(weakSwitch.IsAlive);
     GC.Collect(2);
     Trace.Refresh();
     Assert.False(weakSwitch.IsAlive);
     GC.Collect(2);
     Trace.Refresh();
 }
Ejemplo n.º 10
0
        public void PruneTest()
        {
            var strongSwitch = new TestSwitch();
            var weakSwitch   = PruneMakeRef();

            GC.Collect(2);
            Trace.Refresh();
            Assert.False(weakSwitch.IsAlive);
            GC.Collect(2);
            Trace.Refresh();
        }
Ejemplo n.º 11
0
        public void PruneTest()
        {
            var strongSwitch = new TestSwitch();
            var weakSwitch   = new WeakReference(new TestSwitch());

            Assert.True(weakSwitch.IsAlive);
            GC.Collect(2);
            Trace.Refresh();
            Assert.False(weakSwitch.IsAlive);
            GC.Collect(2);
            Trace.Refresh();
        }
Ejemplo n.º 12
0
        public void Return_No_Consumption_If_Switch_Does_Not_Match()
        {
            // arrange
            var switch0 = new TestSwitch(new Parser("a"), 't', "test", (o, strings) => { });
            var info    = new IterationInfo("-x a b c d e d f g".Split(' '));

            // act
            var result = switch0.CanConsume("", info);

            // assert
            result.NumConsumed.Should().Be(0);
        }
Ejemplo n.º 13
0
        public void Return_A_Human_Friendly_ToString()
        {
            // arrange
            var switch0 = new TestSwitch(new Parser("a"), 't', "test", (o, strings) => { });
            var switch1 = new TestSwitch(new Parser("a"), null, "test", (o, strings) => { });
            var switch2 = new TestSwitch(new Parser("a"), 't', null, (o, strings) => { });

            // act
            // assert
            switch0.ToString().Should().Be($"-t, --test");
            switch1.ToString().Should().Be($"--test");
            switch2.ToString().Should().Be($"-t");
        }
Ejemplo n.º 14
0
        public void SwitchEvaluatingNullCase()
        {
            TestSwitch <string> sw = new TestSwitch <string>();
            string s = null;

            sw.AddCase(null, new TestWriteLine()
            {
                Message = "Hi"
            });
            sw.Hints.Add(0);
            sw.Expression = s;

            TestRuntime.RunAndValidateWorkflow(sw);
        }
Ejemplo n.º 15
0
        public void SwitchWithEmptyCase()
        {
            TestSwitch <string> switchAct = new TestSwitch <string>();

            switchAct.AddCase("12", new TestProductWriteline {
                Text = "in case 12"
            });
            switchAct.AddCase("", new TestProductWriteline {
                Text = "in case 12"
            });
            switchAct.Hints.Add(1);
            switchAct.Expression = "";
            TestRuntime.RunAndValidateWorkflow(switchAct);
        }
Ejemplo n.º 16
0
        public void ThrowExceptionInCase()
        {
            TestSwitch <float> switchAct = new TestSwitch <float>();

            switchAct.DisplayName = "Switch Act";
            switchAct.AddCase(123, new TestThrow <InvalidCastException>("THrow invalid cast")
            {
                ExpectedOutcome = Outcome.UncaughtException(typeof(InvalidCastException))
            });
            switchAct.Expression = 123;
            switchAct.Hints.Add(0);

            TestRuntime.RunAndValidateAbortedException(switchAct, typeof(InvalidCastException), new Dictionary <string, string>());
        }
Ejemplo n.º 17
0
        public void SwitchWithNoExpressionSet()
        {
            TestSwitch <string> switchAct = new TestSwitch <string>();

            switchAct.Hints.Add(-1);
            switchAct.AddCase("1", new TestProductWriteline {
                Text = "in case 1"
            });
            ((CoreWf.Statements.Switch <string>)switchAct.ProductActivity).Expression = null;

            string exceptionMessage = string.Format(ErrorStrings.RequiredArgumentValueNotSupplied, "Expression");

            TestRuntime.ValidateWorkflowErrors(switchAct, new List <TestConstraintViolation>(), typeof(ArgumentException), exceptionMessage);
        }
Ejemplo n.º 18
0
        public void ThrowExcInCaseWhichWontExecute()
        {
            TestSwitch <float> switchAct = new TestSwitch <float>();

            switchAct.AddCase(345678, new TestWriteLine("Writeline")
            {
                Message = "345678"
            });
            switchAct.AddCase(123, new TestThrow <InvalidCastException>("Throw invalid cast"));
            switchAct.Expression = 345678;
            switchAct.Hints.Add(0);

            TestRuntime.RunAndValidateWorkflow(switchAct);
        }
Ejemplo n.º 19
0
        public void Return_No_Consumption_If_Minimum_Number_Of_Values_Are_Not_Met()
        {
            // arrange
            var switch0 = new TestSwitch(new Parser("a"), 't', "test", (o, strings) => { })
            {
                MinRequired = 50
            };
            var info = new IterationInfo("-t a b c d e d f g".Split(' '));

            // act
            var result = switch0.CanConsume("", info);

            // assert
            result.NumConsumed.Should().Be(0);
        }
Ejemplo n.º 20
0
        public void CompositeProcedurals()
        {
            Variable <bool> cond = new Variable <bool> {
                Default = true
            };
            Variable <string> value = new Variable <string> {
                Default = "Apple"
            };
            DelegateInArgument <string> arg = new DelegateInArgument <string> {
                Name = "Apple"
            };

            string[] values = { "a", "b" };

            TestSwitch <string> switchAct = new TestSwitch <string>
            {
                ExpressionVariable = value
            };

            switchAct.AddCase("Apple", new TestWriteLine("Apple", "this is an apple"));
            switchAct.AddCase("Orange", new TestWriteLine("Orange", "this is an orange"));
            switchAct.Hints.Add(0);

            TestIf ifAct = new TestIf(HintThenOrElse.Then)
            {
                ConditionVariable = cond,
                ThenActivity      = new TestWriteLine("W", "Yes thats true"),
                ElseActivity      = new TestWriteLine("W", "No thats not true")
            };

            TestForEach <string> forEachAct = new TestForEach <string>
            {
                Values             = values,
                CurrentVariable    = arg,
                HintIterationCount = 2,
                Body = new TestWriteLine {
                    DisplayName = "w1", MessageExpression = context => arg.Get(context), HintMessageList = { "a", "b" }
                }
            };

            TestSequence seq = new TestSequence
            {
                Variables  = { cond, value },
                Activities = { switchAct, ifAct, forEachAct }
            };

            TestRuntime.RunAndValidateWorkflow(seq);
        }
Ejemplo n.º 21
0
        public void ThrowExceptionInExpression()
        {
            TestSwitch <int> switchAct = new TestSwitch <int>();
            Variable <int>   temp      = VariableHelper.CreateInitialized <int>("temp", 3);
            TestSequence     seq       = new TestSequence();

            seq.Activities.Add(switchAct);
            seq.Variables.Add(temp);

            switchAct.AddCase(123, new TestSequence("Seq"));
            switchAct.ExpressionExpression = (env) => (int)(1 / (temp.Get(env) - 3));
            switchAct.Hints.Add(-1);
            switchAct.ExpectedOutcome = Outcome.UncaughtException(typeof(DivideByZeroException));

            TestRuntime.RunAndValidateAbortedException(seq, typeof(DivideByZeroException), new Dictionary <string, string>());
        }
Ejemplo n.º 22
0
        public void Indicate_It_Can_Consume_The_Max_Allowed_When_Current_Arg_Is_A_Match()
        {
            // arrange
            var switch0 = new TestSwitch(new Parser("a"), 't', "test", (o, strings) => { })
            {
                MaxAllowed = 3
            };
            var info = new IterationInfo("-t a b c d e d f g".Split(' '));

            // act
            // assert
            var result = switch0.CanConsume("", info);

            result.NumConsumed.Should().Be(3);
            result.Info.Current.Should().Be("c");
        }
Ejemplo n.º 23
0
        public void SwitchThrowInDefault()
        {
            TestSwitch <float> switchAct = new TestSwitch <float>();

            switchAct.AddCase(123, new TestThrow <InvalidCastException>("Throw invalid cast")
            {
                ExpectedOutcome = Outcome.None
            });
            switchAct.Default = new TestThrow <TestCaseException>("Op cancelled")
            {
                ExpectedOutcome = Outcome.UncaughtException(typeof(TestCaseException))
            };
            switchAct.Expression = 456;
            switchAct.Hints.Add(-1);
            TestRuntime.RunAndValidateAbortedException(switchAct, typeof(TestCaseException), new Dictionary <string, string>());
        }
Ejemplo n.º 24
0
        public void SwitchWithEnums()
        {
            TestSwitch <OrderStatus> order = new TestSwitch <OrderStatus>();

            order.AddCase(OrderStatus.NewOrder, new TestWriteLine("We have received a new order")
            {
                Message = "New Order"
            });
            order.AddCase(OrderStatus.Processing, new TestWriteLine("Order is in processing state")
            {
                Message = "Processing"
            });
            order.AddCase(OrderStatus.Shipped, new TestSequence {
                Activities = { new TestWriteLine("Order is shipped to you")
                               {
                                   Message = "Order shipped"
                               } }
            });
            order.Hints.Add(0);
            order.Hints.Add(1);
            order.Hints.Add(2);

            List <OrderStatus> values = new List <OrderStatus>()
            {
                OrderStatus.NewOrder, OrderStatus.Processing, OrderStatus.Shipped
            };
            DelegateInArgument <OrderStatus> var = new DelegateInArgument <OrderStatus> {
                Name = "var"
            };

            TestForEach <OrderStatus> forEachAct = new TestForEach <OrderStatus>("ForEachAct")
            {
                Values          = values,
                CurrentVariable = var
            };
            TestSequence seq = new TestSequence("Seq in For Each");

            seq.Activities.Add(order);
            forEachAct.Body            = seq;
            order.ExpressionExpression = (env) => (OrderStatus)var.Get(env);

            forEachAct.HintIterationCount = 3;

            TestRuntime.RunAndValidateWorkflow(forEachAct);
        }
Ejemplo n.º 25
0
        public void Return_True_When_Current_Arg_Matches()
        {
            // arrange
            var switch0 = new TestSwitch(new Parser("a"), 't', "test", (o, strings) => { });
            var switch1 = new TestSwitch(new Parser("a"), null, "test", (o, strings) => { });
            var switch2 = new TestSwitch(new Parser("a"), 't', null, (o, strings) => { });

            // act
            // assert
            switch0.IsLetterMatch(new IterationInfo("-t".Split(' '), 0)).Should().BeTrue();
            switch0.IsWordMatch(new IterationInfo("--test".Split(' '), 0)).Should().BeTrue();

            switch1.IsLetterMatch(new IterationInfo("-t".Split(' '), 0)).Should().BeFalse();
            switch1.IsWordMatch(new IterationInfo("--test".Split(' '), 0)).Should().BeTrue();

            switch2.IsLetterMatch(new IterationInfo("-t".Split(' '), 0)).Should().BeTrue();
            switch2.IsWordMatch(new IterationInfo("--test".Split(' '), 0)).Should().BeFalse();
        }
Ejemplo n.º 26
0
        public Switch(FlightControlSystem fcs, XmlElement element)
            : base(fcs, element)
        {
            string     value;
            TestSwitch current_test;

            Bind(element); // Bind() this component here in case it is used in its own
                           // definition for a sample-and-hold
            XmlElement test_element = element.FindElement("default");

            if (test_element != null)
            {
                current_test = new TestSwitch();
                value        = test_element.GetAttribute("value");
                current_test.SetTestValue(value, name, propertyManager);
                current_test.Default = true;
                double tmp;
                if (delay > 0 && double.TryParse(value, out tmp))
                {                              // If there is a delay, initialize the
                    for (int i = 0; i < delay - 1; i++)
                    {                          // delay buffer to the default value
                        output_array[i] = tmp; // for the switch if that value is a number.
                    }
                }
                tests.Add(current_test);
            }

            var nodeList = element.GetElementsByTagName("test");

            foreach (var elem in nodeList)
            {
                if (elem is XmlElement)
                {
                    test_element           = elem as XmlElement;
                    current_test           = new TestSwitch();
                    current_test.condition = new Condition(test_element, propertyManager);
                    value = test_element.GetAttribute("value");
                    current_test.SetTestValue(value, name, propertyManager);
                    tests.Add(current_test);
                }
            }

            Debug(0);
        }
Ejemplo n.º 27
0
        public void SwitchWithWorkflowInvoker()
        {
            TestSwitch <int> switchAct = new TestSwitch <int>();

            switchAct.AddCase(12, new TestProductWriteline {
                Text = "in case 12"
            });
            switchAct.AddCase(23, new TestProductWriteline {
                Text = "in case 23"
            });
            switchAct.AddCase(123, new TestProductWriteline {
                Text = "in case 123"
            });
            switchAct.AddCase(234, new TestProductWriteline {
                Text = "in case 234"
            });
            switchAct.Hints.Add(2);

            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("Expression", 123);
            TestRuntime.RunAndValidateUsingWorkflowInvoker(switchAct, dic, null, null);
        }
Ejemplo n.º 28
0
        public void ThrowWithCaseInfinity()
        {
            TestSwitch <float> switchAct = new TestSwitch <float>();
            Variable <float>   temp      = VariableHelper.CreateInitialized <float>("temp", 3);
            float        temp1           = 1;
            float        temp2           = 0;
            TestSequence seq             = new TestSequence();

            seq.Activities.Add(switchAct);
            seq.Variables.Add(temp);

            switchAct.AddCase(123, new TestWriteLine("Seq")
            {
                Message = ""
            });
            switchAct.AddCase(temp1 / temp2, new TestWriteLine()
            {
                Message = "Infinity is the value"
            });
            switchAct.ExpressionExpression = (env) => (float)(1 / (temp.Get(env) - 3));
            switchAct.Hints.Add(1);

            TestRuntime.RunAndValidateWorkflow(seq);
        }
Ejemplo n.º 29
0
        public void DifferentArguments()
        {
            //Testing Different argument types for Switch.Expression
            // DelegateInArgument
            // DelegateOutArgument
            // Activity<T>
            // Variable<T> , Activity<T> and Expression is already implemented.

            DelegateInArgument <string>  delegateInArgument  = new DelegateInArgument <string>("Input");
            DelegateOutArgument <string> delegateOutArgument = new DelegateOutArgument <string>("Output");

            TestCustomActivity <InvokeFunc <string, string> > invokeFunc = TestCustomActivity <InvokeFunc <string, string> > .CreateFromProduct(
                new InvokeFunc <string, string>
            {
                Argument = "PassedInValue",
                Func     = new ActivityFunc <string, string>
                {
                    Argument = delegateInArgument,
                    Result   = delegateOutArgument,
                    Handler  = new CoreWf.Statements.Sequence
                    {
                        DisplayName = "Sequence1",
                        Activities  =
                        {
                            new CoreWf.Statements.Switch <string>
                            {
                                DisplayName = "Switch1",
                                Expression  = delegateInArgument,
                                Cases       =
                                {
                                    {
                                        "PassedInValue",
                                        new CoreWf.Statements.Assign <string>
                                        {
                                            DisplayName = "Assign1",
                                            To          = delegateOutArgument,
                                            Value       = "OutValue",
                                        }
                                    },
                                },
                                Default = new Test.Common.TestObjects.CustomActivities.WriteLine{
                                    DisplayName = "W1", Message = "This should not be printed"
                                },
                            },
                            new CoreWf.Statements.Switch <string>
                            {
                                DisplayName = "Switch2",
                                Expression  = delegateOutArgument,
                                Cases       =
                                {
                                    {
                                        "OutValue",
                                        new Test.Common.TestObjects.CustomActivities.WriteLine {
                                            DisplayName = "W2", Message = delegateOutArgument
                                        }
                                    }
                                },
                                Default = new Test.Common.TestObjects.CustomActivities.WriteLine{
                                    DisplayName = "W3", Message = "This should not be printed"
                                },
                            }
                        }
                    }
                }
            }
                );

            TestSwitch <string> switch1 = new TestSwitch <string>
            {
                DisplayName = "Switch1",
                Hints       = { 0 }
            };

            switch1.AddCase("PassedInValue", new TestAssign <string> {
                DisplayName = "Assign1"
            });
            switch1.Default = new TestWriteLine {
                DisplayName = "W1"
            };

            TestSwitch <string> switch2 = new TestSwitch <string>
            {
                DisplayName = "Switch2",
                Hints       = { 0 }
            };

            switch2.AddCase("OutValue", new TestWriteLine {
                DisplayName = "W2", HintMessage = "OutValue"
            });
            switch2.Default = new TestWriteLine {
                DisplayName = "W3"
            };

            TestSequence sequenceForTracing = new TestSequence
            {
                DisplayName = "Sequence1",
                Activities  =
                {
                    switch1,
                    switch2,
                }
            };

            invokeFunc.CustomActivityTraces.Add(sequenceForTracing.GetExpectedTrace().Trace);

            TestRuntime.RunAndValidateWorkflow(invokeFunc);
        }
Ejemplo n.º 30
0
        public Switch(FlightControlSystem fcs, XmlElement element)
            : base(fcs, element)
        {
            string     value, logic;
            TestSwitch current_test;

            foreach (XmlNode currentNode in element.ChildNodes)
            {
                if (currentNode.NodeType == XmlNodeType.Element)
                {
                    XmlElement currentElement = (XmlElement)currentNode;

                    current_test = new TestSwitch();
                    if (currentElement.LocalName.Equals("default"))
                    {
                        tests.Add(current_test);
                        current_test.Logic = eLogic.eDefault;
                    }
                    else if (currentElement.LocalName.Equals("test"))
                    {
                        tests.Add(current_test);
                        logic = currentElement.GetAttribute("logic");
                        if (logic.Equals("OR"))
                        {
                            current_test.Logic = eLogic.eOR;
                        }
                        else if (logic.Equals("AND"))
                        {
                            current_test.Logic = eLogic.eAND;
                        }
                        else if (logic.Length == 0)
                        {
                            current_test.Logic = eLogic.eAND; // default
                        }
                        else
                        { // error
                            if (log.IsErrorEnabled)
                            {
                                log.Error("Unrecognized LOGIC token " + logic + " in switch component: " + name);
                            }
                        }

                        ReaderText rtxt = new ReaderText(new StringReader(currentElement.InnerText));
                        while (rtxt.Done)
                        {
                            string tmp = rtxt.ReadLine().Trim();
                            if (tmp.Length != 0)
                            {
                                current_test.conditions.Add(new Condition(tmp, propertyManager));
                            }
                        }

                        foreach (XmlNode currentNode2 in currentElement.ChildNodes)
                        {
                            if (currentNode2.NodeType == XmlNodeType.Element)
                            {
                                current_test.conditions.Add(new Condition(currentNode2 as XmlElement, propertyManager));
                            }
                        }
                    }

                    if (!currentElement.LocalName.Equals("output"))
                    {
                        value = currentElement.GetAttribute("value");
                        if (value.Length == 0)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error("No VALUE supplied for switch component: " + name);
                            }
                        }
                        else
                        {
                            Match match = testRegex.Match(value);
                            if (match.Success)
                            {
                                if (match.Groups["prop"].Value == "") // if true (and execution falls into this block), "value" is a number.
                                {
                                    current_test.OutputVal = double.Parse(value, FormatHelper.numberFormatInfo);
                                }
                                else
                                {
                                    // "value" must be a property if execution passes to here.
                                    if (value[0] == '-')
                                    {
                                        current_test.sign = -1.0;
                                        value             = value.Remove(0, 1);
                                    }
                                    else
                                    {
                                        current_test.sign = 1.0;
                                    }
                                    current_test.OutputProp = propertyManager.GetPropertyNode(value);
                                }
                            }
                        }
                    }
                }
            }
            base.Bind();
        }