def test_skip_whitespace(): s = ' hello ' dec = JSONDecoder('fake space', s) assert dec.pos == 0 assert dec.skip_whitespace(0) == 3 assert dec.skip_whitespace(3) == 3 assert dec.skip_whitespace(8) == len(s) dec.close()
def test_skip_whitespace(): s = " hello " dec = JSONDecoder("fake space", s) assert dec.pos == 0 assert dec.skip_whitespace(0) == 3 assert dec.skip_whitespace(3) == 3 assert dec.skip_whitespace(8) == len(s) dec.close()
def test_decode_string_caching(self): for s1 in ["abc", u"ä".encode("utf-8")]: s = '"%s" "%s" "%s"' % (s1, s1, s1) dec = JSONDecoder(self.space, s) dec.MIN_SIZE_FOR_STRING_CACHE = 0 assert dec.pos == 0 w_x = dec.decode_string(1) w_y = dec.decode_string(dec.skip_whitespace(dec.pos) + 1) assert w_x is not w_y # check caching w_z = dec.decode_string(dec.skip_whitespace(dec.pos) + 1) assert w_z is w_y 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()
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_skip_whitespace(): s = ' hello ' dec = JSONDecoder('fake space', s) assert dec.pos == 0 assert dec.skip_whitespace(0) == 3 assert dec.skip_whitespace(3) == 3 assert dec.skip_whitespace(8) == len(s) dec.close()
def test_decode_key(): s1 = "123" * 100 s = ' "%s" "%s" ' % (s1, s1) dec = JSONDecoder(FakeSpace(), s) assert dec.pos == 0 x = dec.decode_key(0) assert x == s1 # check caching y = dec.decode_key(dec.pos) assert y == s1 assert y is x dec.close()
def __init__(self, s): self.space = fakespace JSONDecoder.__init__(self, fakespace, s)
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 __init__(self, s): self.space = fakespace JSONDecoder.__init__(self, fakespace, s)