def test_update_in():
    assert update_in({"a": 0}, ["a"], inc) == {"a": 1}
    assert update_in({"a": 0, "b": 1}, ["b"], str) == {"a": 0, "b": "1"}
    assert update_in({"t": 1, "v": {"a": 0}}, ["v", "a"], inc) == {"t": 1, "v": {"a": 1}}
    # Handle one missing key.
    assert update_in({}, ["z"], str, None) == {"z": "None"}
    assert update_in({}, ["z"], inc, 0) == {"z": 1}
    assert update_in({}, ["z"], lambda x: x + "ar", default="b") == {"z": "bar"}
    # Same semantics as Clojure for multiple missing keys, ie. recursively
    # create nested empty dictionaries to the depth specified by the
    # keys with the innermost value set to f(default).
    assert update_in({}, [0, 1], inc, default=-1) == {0: {1: 0}}
    assert update_in({}, [0, 1], str, default=100) == {0: {1: "100"}}
    assert update_in({"foo": "bar", 1: 50}, ["d", 1, 0], str, 20) == {"foo": "bar", 1: 50, "d": {1: {0: "20"}}}
    # Verify immutability:
    d = {"x": 1}
    oldd = d
    update_in(d, ["x"], inc)
    assert d is oldd
Example #2
0
 def test_update_in(self):
     D, kw = self.D, self.kw
     assert update_in(D({"a": 0}), ["a"], inc, **kw) == D({"a": 1})
     assert update_in(D({"a": 0, "b": 1}), ["b"], str, **kw) == D({"a": 0, "b": "1"})
     assert (update_in(D({"t": 1, "v": D({"a": 0})}), ["v", "a"], inc, **kw) ==
             D({"t": 1, "v": D({"a": 1})}))
     # Handle one missing key.
     assert update_in(D({}), ["z"], str, None, **kw) == D({"z": "None"})
     assert update_in(D({}), ["z"], inc, 0, **kw) == D({"z": 1})
     assert update_in(D({}), ["z"], lambda x: x+"ar", default="b", **kw) == D({"z": "bar"})
     # Same semantics as Clojure for multiple missing keys, ie. recursively
     # create nested empty dictionaries to the depth specified by the
     # keys with the innermost value set to f(default).
     assert update_in(D({}), [0, 1], inc, default=-1, **kw) == D({0: D({1: 0})})
     assert update_in(D({}), [0, 1], str, default=100, **kw) == D({0: D({1: "100"})})
     assert (update_in(D({"foo": "bar", 1: 50}), ["d", 1, 0], str, 20, **kw) ==
             D({"foo": "bar", 1: 50, "d": D({1: D({0: "20"})})}))
     # Verify immutability:
     d = D({'x': 1})
     oldd = d
     update_in(d, ['x'], inc, **kw)
     assert d is oldd
Example #3
0
def test_update_in():
    assert update_in({"a": 0}, ["a"], inc) == {"a": 1}
    assert update_in({"a": 0, "b": 1}, ["b"], str) == {"a": 0, "b": "1"}
    assert (update_in({
        "t": 1,
        "v": {
            "a": 0
        }
    }, ["v", "a"], inc) == {
        "t": 1,
        "v": {
            "a": 1
        }
    })
    # Handle one missing key.
    assert update_in({}, ["z"], str, None) == {"z": "None"}
    assert update_in({}, ["z"], inc, 0) == {"z": 1}
    assert update_in({}, ["z"], lambda x: x + "ar", default="b") == {
        "z": "bar"
    }
    # Same semantics as Clojure for multiple missing keys, ie. recursively
    # create nested empty dictionaries to the depth specified by the
    # keys with the innermost value set to f(default).
    assert update_in({}, [0, 1], inc, default=-1) == {0: {1: 0}}
    assert update_in({}, [0, 1], str, default=100) == {0: {1: "100"}}
    assert (update_in({
        "foo": "bar",
        1: 50
    }, ["d", 1, 0], str, 20) == {
        "foo": "bar",
        1: 50,
        "d": {
            1: {
                0: "20"
            }
        }
    })
    # Verify immutability:
    d = {'x': 1}
    oldd = d
    update_in(d, ['x'], inc)
    assert d is oldd