Пример #1
0
    def test_parse_func_var_duplicate(self):
        variables_mapping = {
            "var_1": "abc",
            "var_2": "def",
            "var_3": 123,
            "var_4": {
                "a": 1
            },
            "var_5": True,
            "var_6": None
        }
        check_variables_set = variables_mapping.keys()
        functions_mapping = {"func1": lambda x, y: str(x) + str(y)}
        var = parser.LazyString(
            "ABC${func1($var_1, $var_3)}--${func1($var_1, $var_3)}",
            functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}--{}")
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123--abc123")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}$var_1",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123abc")

        var = parser.LazyString(
            "ABC${func1($var_1, $var_3)}$var_1--${func1($var_1, $var_3)}$var_1",
            functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}{}--{}{}")
        self.assertEqual(var.to_value(variables_mapping),
                         "ABCabc123abc--abc123abc")
Пример #2
0
    def test_parse_function(self):
        variables_mapping = {
            "var_1": "abc",
            "var_2": "def",
            "var_3": 123,
            "var_4": {
                "a": 1
            },
            "var_5": True,
            "var_6": None
        }
        check_variables_set = variables_mapping.keys()
        functions_mapping = {"func1": lambda x, y: str(x) + str(y)}

        var = parser.LazyString("${func1($var_1, $var_3)}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "{}")
        self.assertIsInstance(var._args[0], parser.LazyFunction)
        self.assertEqual(var.to_value(variables_mapping), "abc123")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}DE",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}DE")
        self.assertIsInstance(var._args[0], parser.LazyFunction)
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123DE")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}$var_5",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123True")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}DE$var_4",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}DE{}")
        self.assertEqual(var.to_value(variables_mapping),
                         "ABCabc123DE{'a': 1}")

        var = parser.LazyString("ABC$var_5${func1($var_1, $var_3)}",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var.to_value(variables_mapping), "ABCTrueabc123")
Пример #3
0
    def test_parse_variable(self):
        """ variable format ${var} and $var
        """
        variables_mapping = {
            "var_1": "abc",
            "var_2": "def",
            "var_3": 123,
            "var_4": {
                "a": 1
            },
            "var_5": True,
            "var_6": None
        }
        check_variables_set = variables_mapping.keys()
        functions_mapping = {}

        # format: $var
        var = parser.LazyString("ABC$var_1", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc")

        var = parser.LazyString("ABC$var_1$var_3", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123")

        var = parser.LazyString("ABC$var_1/$var_3", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/{}")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/123")

        var = parser.LazyString("ABC$var_1/", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/")

        var = parser.LazyString("ABC$var_1$", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}$")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc$")

        var = parser.LazyString("ABC$var_1/123$var_1/456", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/123{}/456")
        self.assertEqual(var._args, ["var_1", "var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/123abc/456")

        var = parser.LazyString("ABC$var_1/$var_2/$var_1", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/{}/{}")
        self.assertEqual(var._args, ["var_1", "var_2", "var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/def/abc")

        var = parser.LazyString("func1($var_1, $var_3)", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "func1({}, {})")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "func1(abc, 123)")

        # format: ${var}
        var = parser.LazyString("ABC${var_1}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc")

        var = parser.LazyString("ABC${var_1}${var_3}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123")

        var = parser.LazyString("ABC${var_1}/${var_3}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/{}")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/123")

        var = parser.LazyString("ABC${var_1}/", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/")

        var = parser.LazyString("ABC${var_1}123", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}123")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123")

        var = parser.LazyString("ABC${var_1}/123${var_1}/456",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}/123{}/456")
        self.assertEqual(var._args, ["var_1", "var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/123abc/456")

        var = parser.LazyString("ABC${var_1}/${var_2}/${var_1}",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}/{}/{}")
        self.assertEqual(var._args, ["var_1", "var_2", "var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/def/abc")

        var = parser.LazyString("func1(${var_1}, ${var_3})", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "func1({}, {})")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "func1(abc, 123)")
Пример #4
0
    def test_parse_func_var_abnormal(self):
        variables_mapping = {
            "var_1": "abc",
            "var_2": "def",
            "var_3": 123,
            "var_4": {
                "a": 1
            },
            "var_5": True,
            "var_6": None
        }
        check_variables_set = variables_mapping.keys()
        functions_mapping = {"func1": lambda x, y: str(x) + str(y)}

        # {
        var = parser.LazyString("ABC$var_1{", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}{{")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc{")

        var = parser.LazyString("{ABC$var_1{}a}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "{{ABC{}{{}}a}}")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "{ABCabc{}a}")

        var = parser.LazyString("AB{C$var_1{}a}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "AB{{C{}{{}}a}}")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "AB{Cabc{}a}")

        # }
        var = parser.LazyString("ABC$var_1}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}}}")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc}")

        # $$
        var = parser.LazyString("ABC$$var_1{", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC$var_1{{")
        self.assertEqual(var._args, [])
        self.assertEqual(var.to_value(variables_mapping), "ABC$var_1{")

        # $$$
        var = parser.LazyString("ABC$$$var_1{", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC${}{{")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABC$abc{")

        # $$$$
        var = parser.LazyString("ABC$$$$var_1{", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC$$var_1{{")
        self.assertEqual(var._args, [])
        self.assertEqual(var.to_value(variables_mapping), "ABC$$var_1{")

        # ${
        var = parser.LazyString("ABC$var_1${", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}${{")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc${")

        var = parser.LazyString("ABC$var_1${a", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}${{a")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc${a")

        # $}
        var = parser.LazyString("ABC$var_1$}a", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}$}}a")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc$}a")

        # }{
        var = parser.LazyString("ABC$var_1}{a", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}}}{{a")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc}{a")

        # {}
        var = parser.LazyString("ABC$var_1{}a", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}{{}}a")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc{}a")
Пример #5
0
    def test_lazy_string(self):
        variables_mapping = {
            "var_1": "abc",
            "var_2": "def",
            "var_3": 123,
            "var_4": {
                "a": 1
            },
            "var_5": True,
            "var_6": None
        }
        check_variables_set = variables_mapping.keys()
        functions_mapping = {"func1": lambda x, y: str(x) + str(y)}

        var = parser.LazyString("ABC$var_1", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc")

        var = parser.LazyString("ABC$var_1$var_3", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123")

        var = parser.LazyString("ABC$var_1/$var_3", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/{}")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/123")

        var = parser.LazyString("ABC$var_1/", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/")

        var = parser.LazyString("ABC$var_1$", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}$")
        self.assertEqual(var._args, ["var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc$")

        var = parser.LazyString("ABC$var_1{", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}{")
        self.assertEqual(var._args, ["var_1"])
        # self.assertEqual(var.to_value(variables_mapping), "ABCabc{")

        var = parser.LazyString("ABC$$var_1{", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC${}{")
        self.assertEqual(var._args, ["var_1"])

        var = parser.LazyString("ABC$var_1${", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}${")
        self.assertEqual(var._args, ["var_1"])

        var = parser.LazyString("ABC$var_1${a", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}${a")
        self.assertEqual(var._args, ["var_1"])

        var = parser.LazyString("ABC$var_1/$var_2/$var_1", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "ABC{}/{}/{}")
        self.assertEqual(var._args, ["var_1", "var_2", "var_1"])
        self.assertEqual(var.to_value(variables_mapping), "ABCabc/def/abc")

        var = parser.LazyString("func1($var_1, $var_3)", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "func1({}, {})")
        self.assertEqual(var._args, ["var_1", "var_3"])
        self.assertEqual(var.to_value(variables_mapping), "func1(abc, 123)")

        var = parser.LazyString("${func1($var_1, $var_3)}", functions_mapping,
                                check_variables_set)
        self.assertEqual(var._string, "{}")
        self.assertIsInstance(var._args[0], parser.LazyFunction)
        self.assertEqual(var.to_value(variables_mapping), "abc123")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}DE",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}DE")
        self.assertIsInstance(var._args[0], parser.LazyFunction)
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123DE")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}$var_5",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var.to_value(variables_mapping), "ABCabc123True")

        var = parser.LazyString("ABC${func1($var_1, $var_3)}DE$var_4",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}DE{}")
        self.assertEqual(var.to_value(variables_mapping),
                         "ABCabc123DE{'a': 1}")

        var = parser.LazyString("ABC$var_5${func1($var_1, $var_3)}",
                                functions_mapping, check_variables_set)
        self.assertEqual(var._string, "ABC{}{}")
        self.assertEqual(var.to_value(variables_mapping), "ABCTrueabc123")