def test_build_partially_typed_dict(self):
        value = build_value("a:1;b:2", typing.Mapping[typing.Any, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value('{"a": "1", "b": 2, 0: 3}',
                            typing.Mapping[typing.Any, int], True)
        self.assertEqual(value, {"a": 1, "b": 2, 0: 3})
    def test_build_typed_dict_with_list(self):
        value = build_value("a=1,2,3;b=2", typing.Mapping[str, str], False)
        self.assertEqual(value, {"a": "1,2,3", "b": "2"})

        value = build_value("a=1,2,3;b=2",
                            typing.Mapping[str, typing.List[int]], False)
        self.assertEqual(value, {"a": [1, 2, 3], "b": [2]})
    def test_build_typed_dict(self):
        value = build_value("a:1;b:2", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value('{"a": "1", "b": 2, "c": 3.2}',
                            typing.Mapping[str, int], True)
        self.assertEqual(value, {"a": 1, "b": 2, "c": 3})
    def test_build_tuple(self):
        value = build_value("foo bar,1,0.5", typing.Tuple[str, int, float],
                            False)
        self.assertEqual(value, ("foo bar", 1, 0.5))

        value = build_value('("foo bar",1,0.5)', typing.Tuple[str, int, float],
                            True)
        self.assertEqual(value, ("foo bar", 1, 0.5))
    def test_build_tuple_partially_typed(self):
        value = build_value("foo bar,1,0.5", typing.Tuple[str, typing.Any,
                                                          float], False)
        self.assertEqual(value, ("foo bar", "1", 0.5))

        value = build_value('("foo bar",1,0.5)', typing.Tuple[str, typing.Any,
                                                              float], True)
        self.assertEqual(value, (str("foo bar"), 1, 0.5))
    def test_build_with_casting(self):
        value = build_value("a:1;b:2;c:3", typing.Mapping[str, float])
        self.assertEqual(value, {"a": 1.0, "b": 2.0, "c": 3.0})

        value = build_value("a:1;b:2;c:3", typing.Mapping[str, str])
        self.assertEqual(value, {"a": "1", "b": "2", "c": "3"})

        self.assertRaises(ValueError, build_value, "a:1;b:2;c:3",
                          typing.Mapping[int, int])
    def test_build_custom_type(self):
        def parser(string):
            return string.split("#")

        value = build_value("special#string", parser, False)
        self.assertEqual(value, ["special", "string"])

        value = build_value('"special#string"', parser, True)
        self.assertEqual(value, ["special", "string"])
    def test_build_typed_list(self):
        value = build_value("1,2,3", typing.List[int], False)
        self.assertEqual(value, [1, 2, 3])

        value = build_value("hello,world,test", typing.List[str], False)
        self.assertEqual(value, ["hello", "world", "test"])

        value = build_value("hello", typing.List[str], False)
        self.assertEqual(value, ["hello"])

        value = build_value('["1",2,3.2]', typing.List[int], True)
        self.assertEqual(value, [1, 2, 3])
    def test_build_nested_structures(self):
        inpt = """{
            "a": 1,
            "b": {
                "c": [2, 3, 4, [5, 6]]
            }
        }"""
        expected = {"a": 1, "b": {"c": [2, 3, 4, [5, 6]]}}
        expected_type = typing.Any
        self.assertEqual(build_value(inpt, expected_type, True), expected)

        inpt = """{
            "a": [ [1, 2], [3, 4] ],
            "b": [ [10, 20, 30], [40] ]
        }"""
        expected = {"a": [[1, 2], [3, 4]], "b": [[10, 20, 30], [40]]}
        # dict of str => list of list of ints
        expected_type = typing.Mapping[str, typing.List[typing.List[int]]]
        self.assertEqual(build_value(inpt, expected_type, True), expected)
Beispiel #10
0
    def test_build_whitespaces(self):
        value = build_value(" a : 1 ; b : 2 ", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value('{ "a" : 1 , "b" : 2 }', typing.Mapping[str, int], True)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value(" 1 , 2 , 3 ", typing.List[int], False)
        self.assertEqual(value, [1, 2, 3])

        value = build_value("[ 1 , 2 , 3 ]", typing.List[int], True)
        self.assertEqual(value, [1, 2, 3])

        value = build_value(" 1 , 2 , 3 ", typing.Tuple[int, int, int], False)
        self.assertEqual(value, (1, 2, 3))

        value = build_value("( 1 , 2 , 3 )", typing.Tuple[int, int, int], True)
        self.assertEqual(value, (1, 2, 3))
Beispiel #11
0
 def parse_dict_(value):
     return build_value(value, target_type, python_syntax=False)
    def test_build_tuple_untyped(self):
        value = build_value("foo bar,1,0.5", typing.Tuple, False)
        self.assertEqual(value, ("foo bar", "1", "0.5"))

        value = build_value('("foo bar",1,0.5)', typing.Tuple, True)
        self.assertEqual(value, (str("foo bar"), 1, 0.5))
    def test_build_tuple_single_element(self):
        value = build_value("foo bar", typing.Tuple[str], False)
        self.assertEqual(value, ("foo bar", ))

        value = build_value('("foo bar",)', typing.Tuple[str], True)
        self.assertEqual(value, (str("foo bar"), ))
    def test_build_typed_dict_mixed(self):
        value = build_value("a=1;b=2", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value("a:1;b=2", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})
    def test_build_untyped_dict(self):
        value = build_value("a:1;b:2", typing.Mapping, False)
        self.assertEqual(value, {"a": "1", "b": "2"})

        value = build_value('{"a": 1, "b": 2.5}', typing.Mapping, True)
        self.assertEqual(value, {"a": 1, "b": 2.5})
    def test_build_any_typed_list(self):
        value = build_value("1,2,3", typing.List[typing.Any], False)
        self.assertEqual(value, ["1", "2", "3"])

        value = build_value('["1",2,3.5]', typing.List[typing.Any], True)
        self.assertEqual(value, ["1", 2, 3.5])
    def test_build_int(self):
        value = build_value("1", int, False)
        self.assertEqual(value, 1)

        value = build_value("1", int, True)
        self.assertEqual(value, 1)
    def test_build_string(self):
        value = build_value("some string", str, False)
        self.assertEqual(value, "some string")

        value = build_value('"some string"', str, True)
        self.assertEqual(value, "some string")