Ejemplo n.º 1
0
        public void when_nested_variable_is_missing_error_is_returned()
        {
            //given
            var expressionStr = "var1.var2";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                var1 = new
                {
                    not_var2 = (object)null
                }
            }));

            result.Should().Be(
                new Jiml.Result(new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors: new VariableWasNotFound("var1.var2"))
            }));
        }
Ejemplo n.º 2
0
        public void elements_which_conditions_are_met_are_appended_to_the_array()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "? true -> 2," +
                "? false -> 3," +
                "? true -> 4," +
                "? false -> 5" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 2, 4 }
            })));
        }
Ejemplo n.º 3
0
        public void can_get_deeply_nested_variable_when_present()
        {
            //given
            var expressionStr = "var1.var2.var3.var4";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                var1 = new
                {
                    var2 = new
                    {
                        var3 = new
                        {
                            var4 = "var4 value"
                        }
                    }
                }
            }));

            result.Should().Be(
                new Jiml.Result("var4 value"));
        }
Ejemplo n.º 4
0
        public void can_chain_iterators()
        {
            //given
            var expressionStr =
                "{" +
                "\"value\": [3,2,1] " +
                "?> (x) -> ( x != 2 ) " +
                ">> (x) -> ( x * 2 )" +
                ">< [10.0, 14.0], (acc, x) -> ( [x, ...acc] )" +
                "}";

            //when
            var expression = Jiml.Parse(expressionStr);
            var input      = Json.From(new { });

            //then
            var result = expression.Evaluate(input);

            result.Should().Be(
                new Jiml.Result(Json.From(
                                    new
            {
                value = new[] { 2.0, 6.0, 10.0, 14.0 }
            })));
        }
Ejemplo n.º 5
0
        public void when_deeply_nested_parent_variable_is_null_error_is_returned()
        {
            //given
            var expressionStr = "var1.var2.var3.var4";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                var1 = new
                {
                    var2 = (object)null
                }
            }));

            result.Should().Be(
                new Jiml.Result(new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors: new ParentVariableWasNull(
                        currentPath: "var1.var2.var3",
                        previousPath: "var1.var2")),
            }));
        }
Ejemplo n.º 6
0
        public void can_chain_and_nest_iterators()
        {
            //given
            var expressionStr =
                @"{
                ""value"": [3,2,1] ?> (x) -> (x != 2)
                                   >> (x) -> ({
                                        ""a"": [x*2, x*3, x*4],
                                        ""b"": [x, x+1, x+2]   
                                      })
                                   >< [10.0, 14.0], (acc, x) -> ([
                                        x.a >< 0, (aAcc, value) -> (aAcc + value),
                                        ...(x.b >> (value) -> (value * 2)),
                                        ...acc
                                      ])
                                   >< 0, (acc, x) -> (acc + x)
            }";

            //when
            var expression = Jiml.Parse(expressionStr);
            var input      = Json.From(new { });

            //then
            var result = expression.Evaluate(input);

            result.Should().Be(
                new Jiml.Result(Json.From(
                                    new
            {
                value = 96.0
            })));
        }
Ejemplo n.º 7
0
        public void when_condition_is_met_then_property_is_appended_to_the_object()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "? true -> \"b\": var[1:3]" +
                "}";

            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 2 }
            })));
        }
Ejemplo n.º 8
0
        public void can_spread_inline_array_with_conditional_elements()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "...[? true -> 2, ? false -> 3, 4]" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var1 = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 2, 4 }
            })));
        }
Ejemplo n.º 9
0
        public void if_else_logic_can_be_used_with_spread_operator()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "...var1," +
                "? false -> ...var2[0:2] " +
                "| ...var3[2:]" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var1 = new[] { 0, 1, 2, 3 },
                var2 = new[] { 4, 5, 6, 7 },
                var3 = new[] { 8, 9, 10, 11 },
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 0, 1, 2, 3, 10, 11 }
            })));
        }
Ejemplo n.º 10
0
        public void property_provided_by_first_else_if_condition_which_is_met_is_appended_to_the_object()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "? false -> \"b\": var[1:3]" +
                "|? false -> \"c\": var[1:3]" +
                "|? true -> \"d\": var[1:3]" +
                "|? true -> \"e\": var[1:3]" +
                "}";

            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                d = new[] { 1, 2 }
            })));
        }
Ejemplo n.º 11
0
        public void can_spread_ifElse_value_when_result_is_not_an_array()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "... ? false -> [2,3,4] | 5" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var1 = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 5 }
            })));
        }
Ejemplo n.º 12
0
        public void index_cannot_be_greater_or_equal_to_collection_count()
        {
            //given
            var expressionStr = "var1[4]";
            var json          = Json.From(new
            {
                var1 = new[] { 1, 2, 3, 4 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors: new IndexWasOutOfRange(
                        "var1[4]",
                        "var1",
                        4,
                        new [] { 4 }))
            }));
        }
Ejemplo n.º 13
0
        public void when_condition_is_not_met_but_else_is_provided_then_it_is_added_to_the_array()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "? false -> 2 | 3" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 3 }
            })));
        }
Ejemplo n.º 14
0
        public void indexed_variable_cannot_be_a_jobject()
        {
            //given
            var expressionStr = "var1[4]";
            var json          = Json.From(new
            {
                var1 = new
                {
                    some_variable = "some value"
                }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors: new VariableWasNotAnArray(
                        "var1[4]",
                        "var1",
                        JObject.FromObject(new
                {
                    some_variable = "some value"
                }))),
            }));
        }
Ejemplo n.º 15
0
        public void when_parent_variable_is_jArray_error_is_returned()
        {
            //given
            var expressionStr = "var1.var2";
            var json          = Json.From(new
            {
                var1 = new[] { 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors:  new ParentVariableWasNotAnObject(
                        currentPath: "var1.var2",
                        previousPath: "var1",
                        parentValue: JArray.FromObject(new[] { 1, 2, 3 })))
            }));
        }
Ejemplo n.º 16
0
        public void element_provided_by_first_else_if_condition_which_is_met_is_added_to_the_array()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "? false -> 2 " +
                "|? false -> 3" +
                "|? true -> 4" +
                "|? true -> 5" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 4 }
            })));
        }
Ejemplo n.º 17
0
        public void when_deeply_nested_parent_variable_is_jValue_error_is_returned()
        {
            //given
            var expressionStr = "var1.var2.var3.var4";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                var1 = new
                {
                    var2 = "some value"
                }
            }));

            result.Should().Be(
                new Jiml.Result(new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors:  new ParentVariableWasNotAnObject(
                        currentPath: "var1.var2.var3",
                        previousPath: "var1.var2",
                        parentValue: JValue.CreateString("some value")))
            }));
        }
Ejemplo n.º 18
0
        public void spreading_element_which_is_not_an_jArray_returns_that_element_itself()
        {
            //given
            var expressionStr =
                "{" +
                "\"a\": \"some constant string\"," +
                "\"b\": [" +
                "1," +
                "...var1[0]" +
                "]" +
                "}";

            var json = Json.From(new
            {
                var1 = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    Json.From(new
            {
                a = "some constant string",
                b = new[] { 1, 0 }
            })));
        }
Ejemplo n.º 19
0
        public void when_more_than_one_index_is_incorrect_all_are_returned_in_error_message()
        {
            //given
            var expressionStr = "var1[0,1,10,-10,200]";
            var json          = Json.From(new
            {
                var1 = new[] { 1, 2, 3, 4 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors: new IndexWasOutOfRange(
                        "var1[0,1,10,-10,200]",
                        "var1",
                        4,
                        new [] { 10, -10, 200 }))
            }));
        }
Ejemplo n.º 20
0
        public void can_use_less_or_equal_to_operator_on_two_numbers()
        {
            //given
            var expressionStr =
                "{" +
                "? 1 <= 2 -> \"b\": 1," +
                "? 2 <= 1 -> \"c\": 2," +
                "? 2 <= 2 -> \"d\": 3" +
                "}";

            //when
            var expression = Jiml.Parse(expressionStr);
            var input      = Json.From(new { });

            //then
            var result = expression.Evaluate(input);

            result.Should().Be(
                new Jiml.Result(Json.From(
                                    new
            {
                b = 1,
                d = 3
            })));
        }
        public void indexed_variable_cannot_be_a_jvalue()
        {
            //given
            var expressionStr = "var[:]";
            var json          = Json.From(new
            {
                var = "some value"
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    new[]
            {
                new CompositionFailed(
                    compositionPath: Path.Root,
                    innerErrors: new VariableWasNotAnArray(
                        "var[:]",
                        "var",
                        JValue.CreateString("some value"))),
            }));
        }
Ejemplo n.º 22
0
        public void correct_variable_name_should_be_parsed_without_errors(string variableName)
        {
            //when
            Action parsing = () => Jiml.Parse(variableName);

            //then
            parsing.Should().NotThrow <AntlrException>();
        }
Ejemplo n.º 23
0
        public void can_use_index(string expressionStr, string expectedItem)
        {
            //given
            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(expectedItem));
        }
Ejemplo n.º 24
0
        public void can_get_variable_from_input()
        {
            //given
            var expressionStr = "variable";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                variable = "some variable value"
            }));

            result.Should().Be(
                new Jiml.Result("some variable value"));
        }
Ejemplo n.º 25
0
        public void when_variable_is_present_in_the_input_but_is_null_then_null_is_returned()
        {
            //given
            var expressionStr = "variable";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                variable = (object)null
            }));


            result.Should().Be(
                new Jiml.Result(
                    JValue.CreateNull()));
        }
Ejemplo n.º 26
0
        public void can_negate_constant_true()
        {
            //given
            var expressionStr =
                "{" +
                "? !true -> \"b\": 1" +
                "}";

            //when
            var expression = Jiml.Parse(expressionStr);
            var input      = Json.From(new { });

            //then
            var result = expression.Evaluate(input);

            result.Should().Be(
                new Jiml.Result(Json.From(
                                    new { })));
        }
        public void range_index_works(
            string expressionStr,
            string expectedArrayStr)
        {
            //given
            var json = Json.From(new
            {
                var = new[] { 0, 1, 2, 3 }
            });

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(json);

            result.Should().Be(
                new Jiml.Result(
                    JArray.Parse(expectedArrayStr)));
        }
Ejemplo n.º 28
0
        public void when_nested_variable_is_null_then_null_is_returned()
        {
            //given
            var expressionStr = "var1.var2";

            //when
            var expression = Jiml.Parse(expressionStr);

            //then
            var result = expression.Evaluate(Json.From(new
            {
                var1 = new
                {
                    var2 = (object)null
                }
            }));

            result.Should().Be(
                new Jiml.Result(
                    JValue.CreateNull()));
        }
Ejemplo n.º 29
0
        public void false_or_false_is_false()
        {
            //given
            var expressionStr =
                "{" +
                "? false || false -> \"b\": 1" +
                "}";

            //when
            var expression = Jiml.Parse(expressionStr);
            var input      = Json.From(new { });

            //then
            var result = expression.Evaluate(input);

            result.Should().Be(
                new Jiml.Result(Json.From(
                                    new
            {
            })));
        }
Ejemplo n.º 30
0
        public void can_perform_math_operation(string operation, decimal expectedResult)
        {
            //given
            var expressionStr =
                "{" +
                $"\"value\": {operation}" +
                "}";

            //when
            var expression = Jiml.Parse(expressionStr);
            var input      = Json.From(new { });

            //then
            var result = expression.Evaluate(input);

            result.Should().Be(
                new Jiml.Result(Json.From(
                                    new
            {
                value = expectedResult
            })));
        }