def test_json_map(self): m = Terminator(self.space) w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_c = self.space.newutf8("c", 1) m1 = m.get_next(w_a, '"a"', 0, 3, m) assert m1.w_key == w_a assert m1.nextmap_first is None assert m1.key_repr == '"a"' assert m1.key_repr_cmp('"a": 123', 0) assert not m1.key_repr_cmp('b": 123', 0) assert m.nextmap_first.w_key == w_a m2 = m.get_next(w_a, '"a"', 0, 3, m) assert m2 is m1 m3 = m.get_next(w_b, '"b"', 0, 3, m) assert m3.w_key == w_b assert m3.nextmap_first is None assert m3.key_repr == '"b"' assert m.nextmap_first is m1 m4 = m3.get_next(w_c, '"c"', 0, 3, m) assert m4.w_key == w_c assert m4.nextmap_first is None assert m4.key_repr == '"c"' assert m3.nextmap_first is m4
def test_repeated_key_get_next(self): m = Terminator(self.space) w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_c = self.space.newutf8("c", 1) m1 = m.get_next(w_a, '"a"', 0, 3, m) m1 = m1.get_next(w_b, '"b"', 0, 3, m) m1 = m1.get_next(w_c, '"c"', 0, 3, m) m2 = m1.get_next(w_a, '"a"', 0, 3, m) assert m2 is None
def test_jsonmap_fill_dict(self): from collections import OrderedDict m = Terminator(self.space) space = self.space w_a = space.newutf8("a", 1) w_b = space.newutf8("b", 1) w_c = space.newutf8("c", 1) m1 = m.get_next(w_a, 'a"', 0, 2, m) m2 = m1.get_next(w_b, 'b"', 0, 2, m) m3 = m2.get_next(w_c, 'c"', 0, 2, m) d = OrderedDict() m3.fill_dict(d, [space.w_None, space.w_None, space.w_None]) assert list(d) == [w_a, w_b, w_c]
def test_deal_with_blocked_number_of_leaves(self): w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_x = self.space.newutf8("x", 1) w_u = self.space.newutf8("u", 1) space = self.space base = Terminator(space) m1 = base.get_next(w_a, 'a"', 0, 2, base) m2 = m1.get_next(w_b, 'b"', 0, 2, base) m2.get_next(w_x, 'x"', 0, 2, base) m2.get_next(w_u, 'u"', 0, 2, base) assert base.number_of_leaves == 2 m2.mark_blocked(base) assert base.number_of_leaves == 1
def test_cleanup_fringe(self): w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_c = self.space.newutf8("c", 1) w_d = self.space.newutf8("d", 1) base = Terminator(self.space) base.instantiation_count = 6 m1 = base.get_next(w_a, 'a"', 0, 2, base) m2 = base.get_next(w_b, 'b"', 0, 2, base) m3 = base.get_next(w_c, 'c"', 0, 2, base) m4 = base.get_next(w_d, 'd"', 0, 2, base) m5 = m4.get_next(w_a, 'a"', 0, 2, base) base.instantiation_count = 7 m1.instantiation_count = 2 m2.instantiation_count = 2 m3.instantiation_count = 2 m4.instantiation_count = 1 m5.instantiation_count = 1 assert base.current_fringe == dict.fromkeys([m1, m2, m3, m4]) base.cleanup_fringe() assert base.current_fringe == dict.fromkeys([m1, m2, m3]) assert m4.state == MapBase.BLOCKED assert m4.nextmap_first is None assert m4.nextmap_all is None assert m5.state == MapBase.BLOCKED assert m5.nextmap_first is None assert m5.nextmap_all is None
def _make_some_maps(self): # base -> m1 -> m2 -> m3 # \-> m4 w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_c = self.space.newutf8("c", 1) w_d = self.space.newutf8("d", 1) base = Terminator(self.space) base.instantiation_count = 6 m1 = base.get_next(w_a, 'a"', 0, 2, base) m2 = m1.get_next(w_b, 'b"', 0, 2, base) m3 = m2.get_next(w_c, 'c"', 0, 2, base) m4 = m2.get_next(w_d, 'd"', 0, 2, base) return base, m1, m2, m3, m4
def test_json_map_get_index(self): m = Terminator(self.space) w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_c = self.space.newutf8("c", 1) m1 = m.get_next(w_a, 'a"', 0, 2, m) assert m1.get_index(w_a) == 0 assert m1.get_index(w_b) == -1 m2 = m.get_next(w_b, 'b"', 0, 2, m) assert m2.get_index(w_b) == 0 assert m2.get_index(w_a) == -1 m3 = m2.get_next(w_c, 'c"', 0, 2, m) assert m3.get_index(w_b) == 0 assert m3.get_index(w_c) == 1 assert m3.get_index(w_a) == -1
def test_decode_key_map(self): m = Terminator(self.space) m_diff = Terminator(self.space) for s1 in ["abc", "1001" * 10, u"ä".encode("utf-8")]: s = ' "%s" "%s" "%s"' % (s1, s1, s1) dec = JSONDecoder(self.space, s) assert dec.pos == 0 m1 = dec.decode_key_map(dec.skip_whitespace(0), m) assert m1.w_key._utf8 == s1 assert m1.key_repr == '"%s"' % s1 # check caching on w_key level m2 = dec.decode_key_map(dec.skip_whitespace(dec.pos), m_diff) assert m1.w_key is m2.w_key # check caching on map level m3 = dec.decode_key_map(dec.skip_whitespace(dec.pos), m_diff) assert m3 is m2 dec.close()
def test_instatiation_count(self): m = Terminator(self.space) dec = JSONDecoder(self.space, '"abc" "def"') m1 = dec.decode_key_map(dec.skip_whitespace(0), m) m2 = dec.decode_key_map(dec.skip_whitespace(6), m1) m1 = dec.decode_key_map(dec.skip_whitespace(0), m) m2 = dec.decode_key_map(dec.skip_whitespace(6), m1) m1 = dec.decode_key_map(dec.skip_whitespace(0), m) assert m1.instantiation_count == 3 assert m2.instantiation_count == 2 dec.close()
def test_deal_with_blocked(self): w_a = self.space.newutf8("a", 1) w_b = self.space.newutf8("b", 1) w_c = self.space.newutf8("c", 1) space = self.space s = '{"a": 1, "b": 2, "c": 3}' dec = JSONDecoder(space, s) dec.startmap = base = Terminator(space) m1 = base.get_next(w_a, 'a"', 0, 2, base) m2 = m1.get_next(w_b, 'b"', 0, 2, base) m2.mark_blocked(base) w_res = dec.decode_object(1) assert space.int_w(space.len(w_res)) == 3 assert space.int_w(space.getitem(w_res, w_a)) == 1 assert space.int_w(space.getitem(w_res, w_b)) == 2 assert space.int_w(space.getitem(w_res, w_c)) == 3 dec.close()