Exemple #1
0
def simplify(case):  # TODO this is a hack
    if isinstance(case, SON) and "$ref" not in case:
        simplified = SON(case)  # make a copy!
        if random.choice([True, False]):
            # delete
            if not len(simplified.keys()):
                return (False, case)
            del simplified[random.choice(simplified.keys())]
            return (True, simplified)
        else:
            # simplify a value
            if not len(simplified.items()):
                return (False, case)
            (key, value) = random.choice(simplified.items())
            (success, value) = simplify(value)
            simplified[key] = value
            return (success, success and simplified or case)
    if isinstance(case, list):
        simplified = list(case)
        if random.choice([True, False]):
            # delete
            if not len(simplified):
                return (False, case)
            simplified.pop(random.randrange(len(simplified)))
            return (True, simplified)
        else:
            # simplify an item
            if not len(simplified):
                return (False, case)
            index = random.randrange(len(simplified))
            (success, value) = simplify(simplified[index])
            simplified[index] = value
            return (success, success and simplified or case)
    return (False, case)
    def test_ordered_dict(self):
        a1 = SON()
        a1["hello"] = "world"
        a1["mike"] = "awesome"
        a1["hello_"] = "mike"
        self.assertEqual(a1.items(), [("hello", "world"), ("mike", "awesome"), ("hello_", "mike")])

        b2 = SON({"hello": "world"})
        self.assertEqual(b2["hello"], "world")
        self.assertRaises(KeyError, lambda: b2["goodbye"])
    def test_ordered_dict(self):
        a = SON()
        a["hello"] = "world"
        a["mike"] = "awesome"
        a["hello_"] = "mike"
        self.assertEqual(a.items(), [("hello", "world"), ("mike", "awesome"),
                                     ("hello_", "mike")])

        b = SON({"hello": "world"})
        self.assertEqual(b["hello"], "world")
        self.assertRaises(KeyError, lambda: b["goodbye"])
Exemple #4
0
    def test_ordered_dict(self):
        a1 = SON()
        a1["hello"] = "world"
        a1["mike"] = "awesome"
        a1["hello_"] = "mike"
        self.assertEqual(list(a1.items()), [("hello", "world"),
                                            ("mike", "awesome"),
                                            ("hello_", "mike")])

        b2 = SON({"hello": "world"})
        self.assertEqual(b2["hello"], "world")
        self.assertRaises(KeyError, lambda: b2["goodbye"])
    def test_ordered_dict(self):
        a = SON()
        a["hello"] = "world"
        a["mike"] = "awesome"
        a["hello_"] = "mike"
        self.assertEqual(list(a.items()), [("hello", "world"),
                                     ("mike", "awesome"),
                                     ("hello_", "mike")])

        b = SON({"hello": "world"})
        self.assertEqual(b["hello"], "world")
        self.assertRaises(KeyError, lambda: b["goodbye"])