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) })); }
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(); }
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(); }
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) })))); }
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(); }
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(); }
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(); }
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)) })); }
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")) })); }
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]")); }
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(); }
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))))); }