示例#1
0
        private static IEnumerable <TestItem> TryCatchTests()
        {
            yield return(new TestItem("(1, 2, 3)?", new TryCatch {
                TryBody = Concat.AllParams(
                    new Literal(1),
                    new Literal(2),
                    new Literal(3)
                    )
            }));

            yield return(new TestItem("-1?", new FunctionCall(
                                          Minus.Builtin_1,
                                          new TryCatch {
                TryBody = new Literal(1)
            })));

            yield return(new TestItem("try 1", new TryCatch {
                TryBody = new Literal(1)
            }));

            yield return(new TestItem("try 1 catch 2", new TryCatch {
                TryBody = new Literal(1),
                CatchBody = new Literal(2)
            }));
        }
示例#2
0
        public void LetObjectMatcher()
        {
            // Arrange
            var data = "{\"a\": 1, \"b\": 2}".AsJson();
            var op   = new Let
            {
                Value   = new Identity(),
                Matcher = new ObjectMatcher(
                    new ObjectMatcherProperty(new Literal("a"), new ValueMatcher("a")),
                    new ObjectMatcherProperty(new Literal("b"), new ValueMatcher("b")),
                    new ObjectMatcherProperty(new Literal("c"), new ValueMatcher("c"))),
                Body = new ConstructArray
                {
                    Elements = Concat.AllParams(
                        new GetVariable {
                        Name = "a"
                    },
                        new GetVariable {
                        Name = "b"
                    },
                        new GetVariable {
                        Name = "c"
                    })
                }
            };

            // Act
            var result = op.RunAsScalar(data);

            // Assert
            result.DeepEqual("[1,2,null]".AsJson()).Should().BeTrue();
        }
示例#3
0
        public void WithArguments()
        {
            // Arrange
            var data = Utils.JsonNumberArray(1, 2, 3);
            var op   = Compose.AllParams(
                new FunctionDefinition {
                Name      = "x",
                Arguments = new List <string>()
                {
                    "test1",
                    "test2"
                },
                Body = new ConstructArray {
                    Elements = Concat.AllParams(
                        FunctionCall.ZeroArity("test1"),
                        FunctionCall.ZeroArity("test2"))
                }
            },
                new FunctionCall(new FunctionName("x", 2), new Enumerate(), new Identity()));

            // Act
            var result = op.RunAsScalar(data);

            // Assert
            var expectedValues = Json.ArrayParams(
                Json.Number(1),
                Json.Number(2),
                Json.Number(3),
                Utils.JsonNumberArray(1, 2, 3));

            result.DeepEqual(expectedValues).Should().BeTrue();
        }
示例#4
0
        private static IEnumerable <TestItem> CommaTests()
        {
            yield return(new TestItem("1, 2", Concat.AllParams(
                                          new Literal(1),
                                          new Literal(2)
                                          )));

            yield return(new TestItem("1, 2, \"a\"", Concat.AllParams(
                                          new Literal(1),
                                          new Literal(2),
                                          new Literal("a")
                                          )));

            yield return(new TestItem(
                             "1, 2, \"a\" | . | 3, 4",
                             Compose.AllParams(
                                 Concat.AllParams(
                                     new Literal(1),
                                     new Literal(2),
                                     new Literal("a")),
                                 new Identity(),
                                 Concat.AllParams(
                                     new Literal(3),
                                     new Literal {
                Value = Json.Number(4)
            }))));
        }
示例#5
0
        public void LabelBreakPseudoNestedTest()
        {
            // Arrange
            var data = Json.Null;
            var op   = new Label {
                Name = "out",
                Body = Concat.AllParams(
                    new Literal(1),
                    new Literal(2),
                    new Literal(3),
                    new Label
                {
                    Name = "inner",
                    Body = new Break {
                        Label = "inner"
                    }
                },
                    new Literal(4)
                    )
            };

            // Act
            var result = op.RunAsSequence(data);

            // Assert
            Json.Array(result).DeepEqual("[1,2,3,4]".AsJson()).Should().BeTrue();
        }
示例#6
0
        public void ConcatEnumerations()
        {
            // Arrange
            var data = MakeArray();
            var op   = Concat.AllParams(new Enumerate(), new Enumerate());

            // Act
            var result = op.RunAsSequence(data);

            // Assert
            Json.Array(result)
            .DeepEqual(Utils.JsonNumberArray(1, 2, 3, 1, 2, 3))
            .Should().BeTrue();
        }
示例#7
0
        public void AlternativeMatcherFirstFailsExtendProperties()
        {
            // Arrange
            var data = "[1, {\"a\": 2, \"b\": 3}]".AsJson();
            var op   = new Let
            {
                Value   = new Identity(),
                Matcher = new AlternativeMatcher {
                    First = new ObjectMatcher(
                        new ObjectMatcherProperty(new Literal("d"), new ValueMatcher("d"))),
                    Second = new ArrayMatcher(
                        new ValueMatcher("a"),
                        new ObjectMatcher(
                            new ObjectMatcherProperty(
                                new Literal("a"),
                                new ValueMatcher("b")),
                            new ObjectMatcherProperty(
                                new Literal("b"),
                                new ValueMatcher("c")))),
                },
                Body = new ConstructArray
                {
                    Elements = Concat.AllParams(
                        new GetVariable {
                        Name = "a"
                    },
                        new GetVariable {
                        Name = "b"
                    },
                        new GetVariable {
                        Name = "c"
                    },
                        new GetVariable {
                        Name = "d"
                    })
                }
            };

            // Act
            var result = op.RunAsScalar(data);

            // Assert
            result.DeepEqual("[1,2,3,null]".AsJson()).Should().BeTrue();
        }
示例#8
0
        private static IEnumerable <TestItem> LabelBreakTests()
        {
            yield return(new TestItem("label $out | break $out", new Label {
                Name = "out",
                Body = new Break {
                    Label = "out"
                },
            }));

            yield return(new TestItem("label $out | 1, 2, break $out, 3", new Label {
                Name = "out",
                Body = Concat.AllParams(
                    new Literal(1),
                    new Literal(2),
                    new Break {
                    Label = "out"
                },
                    new Literal(3))
            }));
        }
示例#9
0
        private static IEnumerable <TestItem> ArrayConstructionTests()
        {
            yield return(new TestItem("[]", new ConstructArray {
            }));

            yield return(new TestItem("[1]", new ConstructArray {
                Elements = new Literal(1)
            }));

            yield return(new TestItem("[1, 2]", new ConstructArray {
                Elements = Concat.AllParams(
                    new Literal(1),
                    new Literal(2))
            }));

            yield return(new TestItem("[1, 2, \"a\"]", new ConstructArray {
                Elements = Concat.AllParams(
                    new Literal(1),
                    new Literal(2),
                    new Literal("a"))
            }));
        }
示例#10
0
        private static IEnumerable <TestItem> RangeTests()
        {
            yield return(new TestItem(
                             new FunctionCall(Range.Builtin_1, new Literal(3)),
                             "null",
                             "[0, 1, 2]"));

            yield return(new TestItem(
                             new FunctionCall(
                                 Range.Builtin_1,
                                 Concat.AllParams(new Literal(3), new Literal(4))),
                             "null",
                             "[0, 1, 2, 0, 1, 2, 3]"));

            yield return(new TestItem(
                             new FunctionCall(Range.Builtin_2, new Literal(1), new Literal(3)),
                             "null",
                             "[1, 2]"));

            yield return(new TestItem(
                             new FunctionCall(Range.Builtin_3, new Literal(1), new Literal(6), new Literal(2)),
                             "null",
                             "[1, 3, 5]"));
        }
示例#11
0
        public void SelectorTestSequence()
        {
            // Arrange
            var data = MakeArray();
            var op   = new Selector {
                Index = Concat.AllParams(
                    new Literal(1),
                    new Literal {
                    Value = Json.Number(2)
                },
                    new Literal {
                    Value = Json.Number(0)
                }
                    )
            };

            // Act
            var result = op.RunAsSequence(data);

            // Assert
            Json.Array(result)
            .DeepEqual(Utils.JsonNumberArray(2, 3, 1))
            .Should().BeTrue();
        }
示例#12
0
        private static IEnumerable <TestItem> FunctionTests()
        {
            yield return(new TestItem(
                             "def test: 1;",
                             new FunctionDefinition {
                Name = "test",
                Body = new Literal(1)
            }));

            yield return(new TestItem(
                             "def test: 1; test",
                             Compose.AllParams(
                                 new FunctionDefinition {
                Name = "test",
                Body = new Literal(1)
            },
                                 new FunctionCall(new FunctionName("test", 0)))));

            yield return(new TestItem(
                             "def test(a): 1 + a;",
                             new FunctionDefinition {
                Name = "test",
                Arguments = new List <string> {
                    "a"
                },
                Body = new FunctionCall(
                    Plus.Builtin,
                    new Literal(1),
                    new FunctionCall(new FunctionName("a", 0)))
            }));

            yield return(new TestItem(
                             "def test($a): 1 + $a;",
                             new FunctionDefinition {
                Name = "test",
                Arguments = new List <string> {
                    "a"
                },
                Body = new Let {
                    Matcher = new ValueMatcher("a"),
                    Value = new FunctionCall(new FunctionName("a", 0)),
                    Body = new FunctionCall(
                        Plus.Builtin,
                        new Literal(1),
                        new GetVariable {
                        Name = "a"
                    })
                }
            }));

            yield return(new TestItem(
                             "def point(x; y): [x, y];",
                             new FunctionDefinition {
                Name = "point",
                Arguments = new List <string> {
                    "x", "y"
                },
                Body = new ConstructArray {
                    Elements = Concat.AllParams(
                        new FunctionCall(new FunctionName("x", 0)),
                        new FunctionCall(new FunctionName("y", 0)))
                }
            }));

            yield return(new TestItem(
                             "def point($x; $y): [$x, $y];",
                             new FunctionDefinition {
                Name = "point",
                Arguments = new List <string> {
                    "x", "y"
                },
                Body = new Let {
                    Matcher = new ValueMatcher("x"),
                    Value = new FunctionCall(new FunctionName("x", 0)),
                    Body = new Let {
                        Matcher = new ValueMatcher("y"),
                        Value = new FunctionCall(new FunctionName("y", 0)),
                        Body = new ConstructArray {
                            Elements = Concat.AllParams(
                                new GetVariable {
                                Name = "x"
                            },
                                new GetVariable {
                                Name = "y"
                            })
                        }
                    }
                }
            }));

            yield return(new TestItem(
                             "def point(x; y; z): [x, y, z];",
                             new FunctionDefinition {
                Name = "point",
                Arguments = new List <string> {
                    "x", "y", "z"
                },
                Body = new ConstructArray {
                    Elements = Concat.AllParams(
                        new FunctionCall(new FunctionName("x", 0)),
                        new FunctionCall(new FunctionName("y", 0)),
                        new FunctionCall(new FunctionName("z", 0)))
                }
            }));

            yield return(new TestItem(
                             "def point(x; y): [x, y]; point(1; 2)",
                             Compose.AllParams(
                                 new FunctionDefinition {
                Name = "point",
                Arguments = new List <string> {
                    "x", "y"
                },
                Body = new ConstructArray {
                    Elements = Concat.AllParams(
                        new FunctionCall(new FunctionName("x", 0)),
                        new FunctionCall(new FunctionName("y", 0)))
                }
            },
                                 new FunctionCall(
                                     new FunctionName("point", 2),
                                     new Literal(1),
                                     new Literal(2)))));
        }