示例#1
0
        public void Does_Outer_Scope_Variable_ReAssignment_On_Multi_Link_Scope_Chain_Work_Correctly()
        {
            //given
            ScopeChain chain = new ScopeChain();

            chain.Push("link1");
            chain.SetVariable(":p1FirstName", "Charlie");
            chain.SetVariable(":p1LastName", "Brown");
            chain.SetVariable(":p1Age", 8);

            chain.Push("link2");
            chain.SetVariable(":p2FirstName", "Susie");
            chain.SetVariable(":p2LastName", "Derkins");
            chain.SetVariable(":p2Age", 6);

            chain.Push("link3");
            chain.SetVariable(":p3FirstName", "GI");
            chain.SetVariable(":p3LastName", "Joe");
            chain.SetVariable(":p3Age", 32);

            chain.Push("link4");
            chain.Push("link5");
            chain.Push("link6");
            chain.Push("link7");
            chain.Push("link8");
            chain.Push("link9");
            chain.Push("link10");

            //when
            chain.UpdateVariable(":p2Age", ((int)chain.AccessVariable(":p2Age")) + 1); //increment age to 7

            //then
            Assert.Equal(7, (int)chain.AccessVariable(":p2Age"));
        }
示例#2
0
        public void Does_scope_chain_pop_properly_dispose_chain_links()
        {
            //given
            ScopeChain chain = new ScopeChain();
            int        cnt   = 20;

            for (int i = 0; i < cnt; i++)
            {
                chain.Push(new { Index = i });
            }

            //when
            int[] actual = new int[cnt];
            for (int i = (cnt - 1); i > -1; i--)
            {
                actual[i] = (int)ReflectionHelper.Expression.ReflectItem(chain.Peek(), "Index");
                chain.Pop();
            }

            //then
            int[] expected = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {
                expected[i] = i;
            }

            Assert.Equal(expected, actual);
        }
示例#3
0
        public void Does_multi_link_scope_chain_reflect_correct_context_item()
        {
            //given
            ScopeChain chain = new ScopeChain();
            var        p     = new Person()
            {
                Name = new Name()
                {
                    First = "Charlie", Last = "Brown"
                }
            };

            chain.Push(p);
            var p2 = new { Name = new { First = "Susie", Last = "Derkins" } };

            chain.Push(p2);
            var a = new Address()
            {
                Line1 = "111 Main St.", Line2 = "", City = "Dallas", State = "TX", Zip = "75075"
            };

            chain.Push(a);
            var p3 = new Name {
                First = "Spider", Last = "Man"
            };

            chain.Push(p3);

            //when
            var actual = ReflectionHelper.Expression.ReflectItem(chain.Peek(), "First");

            //then
            Assert.Equal("Spider", actual);
        }
示例#4
0
        public void TestMapParams()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("+", new TestSum());
            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);
            ValueDelimiter curly = new ValueDelimiter("}", DelimiterType.AsArray, new CreateMap());
            scope.SetValue("{", curly);

            {	// create postfix function that takes { :a :b }
                Map map = new Map();
                map["a"] = PatternData.Single("a", ValueType.Int);
                map["b"] = PatternData.Single("b", ValueType.Int);
                ValueMap vMap = new ValueMap(map);

                List<string> body = new List<string>();
                body.Add("a + b");

                CreateFunction.Do(scope, "add", null, vMap, body);
            }
            {	// try out the function
                DelimiterList list = ParseLine.Do("add { :a 2 :b 5 }", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(7, value.AsInt);
            }

            {	// try it as a partial
                DelimiterList list = ParseLine.Do("add { :a 2 } { :b 5 }", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(7, value.AsInt);
            }
        }
示例#5
0
        public void Does_variable_accessor_crawl_a_multi_link_scope_chain_correctly()
        {
            //given
            ScopeChain chain = new ScopeChain();
            var        p     = new Person()
            {
                Name = new Name()
                {
                    First = "Charlie", Last = "Brown"
                }
            };

            chain.Push(p);
            chain.SetVariable("test", "t1");
            var p2 = new { Name = new { First = "Susie", Last = "Derkins" } };

            chain.Push(p2);
            chain.SetVariable("test", "t2");
            var a = new Address()
            {
                Line1 = "111 Main St.", Line2 = "", City = "Dallas", State = "TX", Zip = "75075"
            };

            chain.Push(a);
            var p3 = new Name {
                First = "Spider", Last = "Man"
            };

            chain.Push(p3);
            var p4 = new Name {
                First = "Luke", Last = "Skywalker"
            };

            chain.Push(p4);
            var p5 = new { Name = new { First = "Peter", Last = "Pan" } };

            chain.Push(p5);
            var p6 = new Name {
                First = "Duke", Last = "Caboom"
            };

            chain.Push(p6);
            var p7 = new Name {
                First = "Buzz", Last = "Lightyear"
            };

            chain.Push(p7);

            //when
            var actual = chain.AccessVariable("test");

            //then
            Assert.Equal("t2", actual);
        }
示例#6
0
        static IScope CreateScope()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("subtract", new SubtractArray());
            scope.SetValue("add", new AddUpBody());

            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);

            return scope;
        }
示例#7
0
        public void Does_multi_link_scope_chain_return_correct_item_on_reach_back()
        {
            //given
            ScopeChain chain = new ScopeChain();
            var        p     = new Person()
            {
                Name = new Name()
                {
                    First = "Charlie", Last = "Brown"
                }
            };

            chain.Push(p);
            var p2 = new { Name = new { First = "Susie", Last = "Derkins" } };

            chain.Push(p2);
            var a = new Address()
            {
                Line1 = "111 Main St.", Line2 = "", City = "Dallas", State = "TX", Zip = "75075"
            };

            chain.Push(a);
            var p3 = new Name {
                First = "Spider", Last = "Man"
            };

            chain.Push(p3);
            var p4 = new Name {
                First = "Luke", Last = "Skywalker"
            };

            chain.Push(p4);
            var p5 = new { Name = new { First = "Peter", Last = "Pan" } };

            chain.Push(p5);
            var p6 = new Name {
                First = "Duke", Last = "Caboom"
            };

            chain.Push(p6);
            var p7 = new Name {
                First = "Buzz", Last = "Lightyear"
            };

            chain.Push(p7);

            //when
            var actual = ReflectionHelper.Expression.ReflectItem(chain.Peek(5), "City");


            //then
            Assert.Equal("Dallas", actual);
        }
示例#8
0
        static IScope CreateScope()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("subtract-array", new SubtractArray());
            scope.SetValue("subtract-map", new SubtractMap());
            scope.SetValue("create-map", new CreateMap());

            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);
            ValueDelimiter curly = new ValueDelimiter("}", DelimiterType.AsArray, new CreateMap());
            scope.SetValue("{", curly);

            return scope;
        }
示例#9
0
        public void Does_Variable_ReAssignment_Work_Correctly()
        {
            //given
            ScopeChain chain = new ScopeChain();

            chain.Push("link1");
            chain.SetVariable("v1", "foo");

            //when
            chain.UpdateVariable("v1", "bar");
            string v1 = (string)chain.AccessVariable("v1");

            //then
            Assert.Equal("bar", v1);
        }
示例#10
0
        public void Does_overreaching_back_into_scope_chain_throw_argument_exception()
        {
            //given
            var data1 = new { Name = new { First = "Charlie", Last = "Brown" } };
            var data2 = new { Name = new { First = "Super", Last = "Man" } };

            ScopeChain chain = new ScopeChain();

            chain.Push(data1);
            chain.Push(data2);

            //when
            Exception ex = Assert.Throws <ArgumentException>(() => chain.Peek(2));

            //then
            Assert.Equal("value must be < ScopeChain.Depth (Parameter 'back')", ex.Message);
        }
示例#11
0
        public void Does_Variable_ReAssignment_On_Multi_Link_Scope_Chain_Work_Correctly()
        {
            //given
            ScopeChain chain = new ScopeChain();

            chain.Push("link1");
            chain.Push("link2");
            chain.Push("link3");
            int age = 99;

            chain.SetVariable(":age", age);

            //when
            chain.UpdateVariable(":age", ++age); //increment age to 100

            //then
            Assert.Equal(100, (int)chain.AccessVariable(":age"));
        }
示例#12
0
        public void TestPartialIn()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("+", new TestSum());
            scope.SetValue("(", new ValueDelimiter(")", DelimiterType.AsValue));

            // create infix function because we're testing that CreateFunction.UserFunction.Eval
            // knows how to create partial functions
            List<string> body = new List<string>();
            body.Add("x + y");
            CreateFunction.Do(scope, "add", new ValueString("x"), new ValueString("y"), body);

            {	// test that it creates a prefix function when only the post parameter is specified
                DelimiterList list = ParseLine.Do("(add 3)", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(ValueType.Function, value.Type);
                Map meta = value.Metadata;
                Assert.IsTrue(meta.ContainsKey(ValueFunction.keyPreviousPattern));
                Assert.IsFalse(meta.ContainsKey(ValueFunction.keyNextPattern));
            }
            {
                DelimiterList list = ParseLine.Do("2 (add 3)", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(5, value.AsInt);
            }

            {	// test that it creates a postfix function when only the pre parameter is specified
                DelimiterList list = ParseLine.Do("(4 add)", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(ValueType.Function, value.Type);
                Map meta = value.Metadata;
                Assert.IsFalse(meta.ContainsKey(ValueFunction.keyPreviousPattern));
                Assert.IsTrue(meta.ContainsKey(ValueFunction.keyNextPattern));
            }
            {
                DelimiterList list = ParseLine.Do("(4 add) 5", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(9, value.AsInt);
            }
        }
示例#13
0
        public void Does_multi_link_scope_chain_return_closest_scoped_variable_by_name()
        {
            //given
            ScopeChain chain = new ScopeChain();
            var        p     = new Person()
            {
                Name = new Name()
                {
                    First = "Charlie", Last = "Brown"
                }
            };

            chain.Push(p);
            chain.SetVariable("test", "t1");
            var p2 = new { Name = new { First = "Susie", Last = "Derkins" } };

            chain.Push(p2);
            chain.SetVariable("test", "t2");
            var a = new Address()
            {
                Line1 = "111 Main St.", Line2 = "", City = "Dallas", State = "TX", Zip = "75075"
            };

            chain.Push(a);
            chain.SetVariable("test", "t3");
            var p3 = new Name {
                First = "Spider", Last = "Man"
            };

            chain.Push(p3);
            chain.SetVariable("test", "t4");

            //when
            var actual = chain.AccessVariable("test");

            //then
            Assert.Equal("t4", actual);
        }
示例#14
0
        public void Does_Variable_Scope_Multi_Demarcation_Work_Correctly()
        {
            //given
            ScopeChain chain = new ScopeChain();

            chain.Push("link1");
            chain.Push("link2");
            chain.SetVariable("v2", "x");
            chain.ApplyVariableScopeMarker();
            chain.SetVariable("v2", "y");
            chain.ApplyVariableScopeMarker();
            chain.ApplyVariableScopeMarker();
            chain.ApplyVariableScopeMarker();

            //when
            chain.DereferenceVariableScope();
            chain.DereferenceVariableScope();
            chain.DereferenceVariableScope();
            chain.DereferenceVariableScope();
            var v2 = (string)chain.AccessVariable("v2");

            //then
            Assert.Equal("x", v2);
        }
示例#15
0
        public void TestArrayParams()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("+", new TestSum());
            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);

            {	// create postfix function that takes [ a b ]
                List<Value> list = new List<Value>();
                list.Add(PatternData.Single("a", ValueType.Int));
                list.Add(PatternData.Single("b", ValueType.Int));
                ValueArray array = new ValueArray(list);

                List<string> body = new List<string>();
                body.Add("a + b");

                CreateFunction.Do(scope, "add", null, array, body);
            }
            {	// try out the function
                DelimiterList list = ParseLine.Do("add [ 2 5 ]", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(7, value.AsInt);
            }
        }
示例#16
0
        public void TestSimple()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("+", new TestSum());

            {	// create postfix function
                List<string> body = new List<string>();
                body.Add("x + 5");
                CreateFunction.Do(scope, "5+", null, new ValueString("x"), body);
            }
            {
                DelimiterList list = ParseLine.Do("5+ 3", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(8, value.AsInt);
            }

            {	// create prefix function
                List<string> body = new List<string>();
                body.Add("5 + x");
                CreateFunction.Do(scope, "+5", new ValueString("x"), null, body);
            }
            {
                DelimiterList list = ParseLine.Do("4 +5", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(9, value.AsInt);
            }

            {	// test both
                DelimiterList list = ParseLine.Do("5+ 2 +5", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(12, value.AsInt);
            }
        }