Exemplo n.º 1
0
        private static IEnumerable <TestItem> ParenTests()
        {
            yield return(new TestItem(
                             "1 + (1 - 1)",
                             new FunctionCall(
                                 Plus.Builtin,
                                 new Literal(1),
                                 new FunctionCall(
                                     Minus.Builtin_2,
                                     new Literal(1),
                                     new Literal(1)))));

            yield return(new TestItem("(. | .) | .", Compose.AllParams(
                                          Compose.AllParams(
                                              new Identity(),
                                              new Identity()),
                                          new Identity()
                                          )));

            yield return(new TestItem(
                             "(1 + 2) * 2",
                             new FunctionCall(
                                 Times.Builtin,
                                 new FunctionCall(
                                     Plus.Builtin,
                                     new Literal(1),
                                     new Literal(2)),
                                 new Literal(2))));
        }
Exemplo n.º 2
0
        private static IEnumerable <TestItem> PipeTests()
        {
            yield return(new TestItem(". | .", Compose.AllParams(
                                          new Identity(),
                                          new Identity())));

            yield return(new TestItem(". | . | .", Compose.AllParams(
                                          new Identity(),
                                          new Identity(),
                                          new Identity())));

            yield return(new TestItem(". | . | . | .", Compose.AllParams(
                                          new Identity(),
                                          new Identity(),
                                          new Identity(),
                                          new Identity())));

            yield return(new TestItem(".a | .[].c | .d", Compose.AllParams(
                                          new StringSelector {
                Key = "a"
            },
                                          Compose.AllParams(
                                              new Enumerate(),
                                              new StringSelector {
                Key = "c"
            }
                                              ),
                                          new StringSelector {
                Key = "d"
            })));
        }
Exemplo n.º 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();
        }
Exemplo n.º 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)
            }))));
        }
Exemplo n.º 5
0
        private static IEnumerable <TestItem> BooleanTests()
        {
            yield return(new TestItem(
                             ". | not",
                             Compose.AllParams(new Identity(), new FunctionCall(new FunctionName("not", 0)))));

            yield return(new TestItem(
                             "true or false",
                             new FunctionCall(Or.Builtin, new Literal(true), new Literal(false))));

            yield return(new TestItem(
                             "true and false",
                             new FunctionCall(And.Builtin, new Literal(true), new Literal(false))));

            yield return(new TestItem(
                             "true or false and true",
                             new FunctionCall(
                                 Or.Builtin,
                                 new Literal(true),
                                 new FunctionCall(
                                     And.Builtin,
                                     new Literal(false),
                                     new Literal(true)))));

            yield return(new TestItem(
                             "true or false or true",
                             new FunctionCall(
                                 Or.Builtin,
                                 new FunctionCall(
                                     Or.Builtin,
                                     new Literal(true),
                                     new Literal(false)),
                                 new Literal(true))));

            yield return(new TestItem(
                             "true and false and true",
                             new FunctionCall(
                                 And.Builtin,
                                 new FunctionCall(
                                     And.Builtin,
                                     new Literal(true),
                                     new Literal(false)),
                                 new Literal(true))));

            yield return(new TestItem(
                             "true and false or false and true",
                             new FunctionCall(
                                 Or.Builtin,
                                 new FunctionCall(
                                     And.Builtin,
                                     new Literal(true),
                                     new Literal(false)),
                                 new FunctionCall(
                                     And.Builtin,
                                     new Literal(false),
                                     new Literal(true)))));
        }
Exemplo n.º 6
0
        public void IdentityInSequence()
        {
            // Arrange
            var data = MakeArray();
            var op   = Compose.AllParams(new Identity());

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

            // Assert
            result.DeepEqual(data).Should().BeTrue();
        }
Exemplo n.º 7
0
        public void TestEmptySecondInComposition()
        {
            // Arrange
            var data = MakeNestedArray();
            var op   = Compose.AllParams(new Identity(), new FunctionCall(Empty.Builtin));

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

            // Assert
            result.Count().Should().Be(0);
        }
Exemplo n.º 8
0
        public void EmptySequence()
        {
            // Arrange
            Json data = MakeArray();
            var  op   = Compose.AllParams();

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

            // Assert
            result.DeepEqual(data).Should().BeTrue();
        }
Exemplo n.º 9
0
        public void ComposeArrayEnumerations()
        {
            // Arrange
            var data = MakeNestedArray();
            var op   = Compose.AllParams(new Enumerate(), new Enumerate());

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

            // Assert
            Json.Array(result)
            .DeepEqual(Utils.JsonNumberArray(1, 2, 3, 4, 5, 6, 7, 8, 9))
            .Should().BeTrue();
        }
Exemplo n.º 10
0
        public void IsNormalTest()
        {
            // Arrange
            var data = Json.ArrayParams(Json.Number(0));
            var op   = Compose.AllParams(
                new Enumerate(),
                new FunctionCall(IsNormal.Builtin)
                );

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

            // Assert
            Json.Array(result).DeepEqual("[false]".AsJson()).Should().BeTrue();
        }
Exemplo n.º 11
0
        public void ErrorTest()
        {
            // Arrange
            var data = Json.ArrayParams(Json.Number(0));
            var op   = Compose.AllParams(
                new Literal("not good"),
                new FunctionCall(Error.Builtin)
                );

            // Act
            Action action = () => op.RunAsSequence(data).ToList();

            // Assert
            action.Should().Throw <JsonMasherException>().Where(ex => ex.Message == "not good");
        }
Exemplo n.º 12
0
        public void OuterIterateNumbers()
        {
            // Arrange
            var data = Utils.JsonNumberArray(1, 2, 3);
            var op   = Compose.AllParams(
                new Enumerate(),
                new FunctionCall(Plus.Builtin, new Identity(), new Identity()));

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

            // Assert
            Json.Array(result)
            .DeepEqual(Utils.JsonNumberArray(2, 4, 6))
            .Should().BeTrue();
        }
Exemplo n.º 13
0
        public void ConcatComponseEnumerations()
        {
            // Arrange
            var data = MakeArray();
            var op   = Compose.AllParams(
                new Enumerate(),
                Concat.AllParams(
                    new Identity(),
                    new Identity()));

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

            // Assert
            Json.Array(result)
            .DeepEqual(Utils.JsonNumberArray(1, 1, 2, 2, 3, 3))
            .Should().BeTrue();
        }
Exemplo n.º 14
0
        public void IsInfiniteTest()
        {
            // Arrange
            var data = Json.ArrayParams(
                Json.Number(double.NegativeInfinity),
                Json.Number(double.PositiveInfinity),
                Json.Number(0));
            var op = Compose.AllParams(
                new Enumerate(),
                new FunctionCall(IsInfinite.Builtin)
                );

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

            // Assert
            Json.Array(result).DeepEqual("[true, true, false]".AsJson()).Should().BeTrue();
        }
Exemplo n.º 15
0
        public void ArrayDoubleEnumerationAssignment()
        {
            // Arrange
            var data = "[[1, 2], [3, 4]]".AsJson();
            var op   = new Assignment {
                PathExpression = Compose.AllParams(
                    new Enumerate(),
                    new Enumerate()),
                Masher = new FunctionCall(
                    Plus.Builtin, new Identity(), new Literal(2))
            };

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

            // Assert
            Json.Array(result)
            .DeepEqual("[[[3, 4], [5, 6]]]".AsJson())
            .Should().BeTrue();
        }
Exemplo n.º 16
0
        public void ConstructObjectTest()
        {
            // Arrange
            var data = MakeArray();
            var op   = Compose.AllParams(
                new Enumerate(),
                new ConstructObject(
                    new PropertyDescriptor(new Literal("a"), new Identity())));

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

            // Assert
            Json.Array(result)
            .DeepEqual(Json.ArrayParams(
                           Json.ObjectParams(new JsonProperty("a", Json.Number(1))),
                           Json.ObjectParams(new JsonProperty("a", Json.Number(2))),
                           Json.ObjectParams(new JsonProperty("a", Json.Number(3)))))
            .Should().BeTrue();
        }
Exemplo n.º 17
0
        public void ObjectIndexSelectorAssignment()
        {
            // Arrange
            var data = "[{ \"a\": 1, \"b\": 2 }, { \"a\": 3, \"b\": 4 }]".AsJson();
            var op   = new Assignment {
                PathExpression = Compose.AllParams(
                    new Enumerate(),
                    new Selector {
                    Index = new Literal {
                        Value = Json.String("b")
                    }
                }),
                Masher = new FunctionCall(
                    Plus.Builtin, new Identity(), new Literal(2))
            };

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

            // Assert
            Json.Array(result)
            .DeepEqual("[[{ \"a\": 1, \"b\": 4 }, { \"a\": 3, \"b\": 6 }]]".AsJson())
            .Should().BeTrue();
        }
Exemplo n.º 18
0
        public void ObjectMultiKeyPathAssignment()
        {
            // Arrange
            var data = "{ \"a\": { \"c\": 1 }, \"b\": 2 }".AsJson();
            var op   = new Assignment {
                PathExpression = Compose.AllParams(
                    new StringSelector {
                    Key = "a"
                },
                    new StringSelector {
                    Key = "c"
                }),
                Masher = new FunctionCall(
                    Plus.Builtin, new Identity(), new Literal(2))
            };

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

            // Assert
            Json.Array(result)
            .DeepEqual("[{ \"a\": { \"c\": 3 }, \"b\": 2 }]".AsJson())
            .Should().BeTrue();
        }
Exemplo n.º 19
0
        public void NoArguments()
        {
            // Arrange
            var data = Utils.JsonNumberArray(1, 2, 3);
            var op   = Compose.AllParams(
                new FunctionDefinition {
                Name      = "test",
                Arguments = new List <string>(),
                Body      = new FunctionCall(
                    Plus.Builtin, new Identity(), new Literal {
                    Value = Json.Number(2)
                })
            },
                new Enumerate(),
                FunctionCall.ZeroArity("test"));

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

            // Assert
            Json.Array(result)
            .DeepEqual(Utils.JsonNumberArray(3, 4, 5))
            .Should().BeTrue();
        }
Exemplo n.º 20
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)))));
        }
Exemplo n.º 21
0
        private static IEnumerable <TestItem> DotTests()
        {
            yield return(new TestItem("", new Identity()));

            yield return(new TestItem(".", new Identity()));

            yield return(new TestItem(".a", new StringSelector {
                Key = "a"
            }));

            yield return(new TestItem(".a?", new StringSelector {
                Key = "a", IsOptional = true
            }));

            yield return(new TestItem(".a.b?", Compose.AllParams(
                                          new StringSelector {
                Key = "a"
            },
                                          new StringSelector {
                Key = "b", IsOptional = true
            })));

            yield return(new TestItem(".\"a\"?", new StringSelector {
                Key = "a", IsOptional = true
            }));

            yield return(new TestItem(".\"a\"", new StringSelector {
                Key = "a"
            }));

            yield return(new TestItem(".a.b", Compose.AllParams(
                                          new StringSelector {
                Key = "a"
            },
                                          new StringSelector {
                Key = "b"
            })));

            yield return(new TestItem(".a[]", Compose.AllParams(
                                          new StringSelector {
                Key = "a"
            },
                                          new Enumerate())));

            yield return(new TestItem(".[]", new Enumerate()));

            yield return(new TestItem(".[].a", Compose.AllParams(
                                          new Enumerate(),
                                          new StringSelector {
                Key = "a"
            })));

            yield return(new TestItem(".[.].a", Compose.AllParams(
                                          new Selector {
                Index = new Identity()
            },
                                          new StringSelector {
                Key = "a"
            })));

            yield return(new TestItem(".[.]?", Compose.AllParams(
                                          new Selector {
                Index = new Identity(), IsOptional = true
            })));

            yield return(new TestItem(".[1:2]", Compose.AllParams(
                                          new SliceSelector {
                From = new Literal(1), To = new Literal(2)
            })));

            yield return(new TestItem(".[1:2]?", Compose.AllParams(
                                          new SliceSelector {
                From = new Literal(1), To = new Literal(2), IsOptional = true
            })));

            yield return(new TestItem(".[1:]", Compose.AllParams(
                                          new SliceSelector {
                From = new Literal(1)
            })));

            yield return(new TestItem(".[1:]?", Compose.AllParams(
                                          new SliceSelector {
                From = new Literal(1), IsOptional = true
            })));

            yield return(new TestItem(".[:2]", Compose.AllParams(
                                          new SliceSelector {
                To = new Literal(2)
            })));

            yield return(new TestItem(".[:2]?", Compose.AllParams(
                                          new SliceSelector {
                To = new Literal(2), IsOptional = true
            })));

            yield return(new TestItem(".[.].a[][]", Compose.AllParams(
                                          new Selector {
                Index = new Identity()
            },
                                          new StringSelector {
                Key = "a"
            },
                                          new Enumerate(),
                                          new Enumerate())));

            yield return(new TestItem(".[][]", Compose.AllParams(
                                          new Enumerate(),
                                          new Enumerate())));

            yield return(new TestItem(".[][][]", Compose.AllParams(
                                          new Enumerate(),
                                          new Enumerate(),
                                          new Enumerate())));

            yield return(new TestItem(".[][][][]", Compose.AllParams(
                                          new Enumerate(),
                                          new Enumerate(),
                                          new Enumerate(),
                                          new Enumerate())));

            yield return(new TestItem("..", new FunctionCall(new FunctionName("recurse", 0))));

            yield return(new TestItem("$obj[.]", new Selector {
                Target = new GetVariable {
                    Name = "obj"
                },
                Index = new Identity()
            }));

            yield return(new TestItem("$obj[1][2]", new Selector {
                Target = new Selector {
                    Target = new GetVariable {
                        Name = "obj"
                    },
                    Index = new Literal(1)
                },
                Index = new Literal(2)
            }));

            yield return(new TestItem("$obj[10:20]", new SliceSelector {
                Target = new GetVariable {
                    Name = "obj"
                },
                From = new Literal(10),
                To = new Literal(20)
            }));

            yield return(new TestItem("$obj[]", new Enumerate {
                Target = new GetVariable {
                    Name = "obj"
                }
            }));
        }
Exemplo n.º 22
0
        private static IEnumerable <TestItem> AssignmentTests()
        {
            yield return(new TestItem(". |= . + 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Plus.Builtin,
                    new Identity(),
                    new Literal(2))
            }));

            yield return(new TestItem(". |= . + 2 | . |= . + 2", Compose.AllParams(
                                          new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Plus.Builtin,
                    new Identity(),
                    new Literal(2))
            },
                                          new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Plus.Builtin,
                    new Identity(),
                    new Literal(2))
            })));

            yield return(new TestItem(". = . + 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Plus.Builtin,
                    new Identity(),
                    new Literal(2)),
                UseWholeValue = true
            }));

            yield return(new TestItem(". += 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Plus.Builtin,
                    new Identity(),
                    new Literal(2)),
            }));

            yield return(new TestItem(". -= 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Minus.Builtin_2,
                    new Identity(),
                    new Literal(2)),
            }));

            yield return(new TestItem(". *= 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Times.Builtin,
                    new Identity(),
                    new Literal(2)),
            }));

            yield return(new TestItem(". /= 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Divide.Builtin,
                    new Identity(),
                    new Literal(2)),
            }));

            yield return(new TestItem(". %= 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new FunctionCall(
                    Modulo.Builtin,
                    new Identity(),
                    new Literal(2)),
            }));

            yield return(new TestItem(". //= 2", new Assignment {
                PathExpression = new Identity(),
                Masher = new Alternative {
                    First = new Identity(),
                    Second = new Literal(2)
                }
            }));
        }