def test_dict(self): from collections import OrderedDict self.assertEqual(_format({}, ()), '{}') x = OrderedDict([('hi', 3), ('yo', ('ouch',)), ('now', {'then': date(2014, 5, 24)})]) y = _format(x, ()) self.assertEqual(y, '{"hi":3,"yo":["ouch"],"now":{"then":2014-05-24}}') z = self.p(y) self.assertEqual(set(x.keys()), set(z.keys())) for k in x: self.assertEqual(x[k], z[k]) self.assertRaises(TypeError, _format, {4: 13}, ()) self.assertRaises(TypeError, _format, {(): 13}, ())
def test_list(self): self.assertEqual(_format([], ()), '[]') x = (3, 34.63, ('hello', date(2014, 10, 28)), '0') y = _format(x, ()) z = '[3,34.63,["hello",2014-10-28],"0"]' self.assertEqual(y, z) self.assertEqual(self.p(y), x) w = list(x) w[2] = list(w[2]) y = _format(w, ()) self.assertEqual(y, z) self.assertEqual(self.p(y), x)
def test_format_time(self): dt = datetime.now().time() x = _format(dt, ()) self.assertEqual(x, str(dt)) d = self.p(x) self.assertIsNot(d, dt) self.assertEqual(d, dt) dt = timezone.now().time() if dt.tzinfo is None: return x = _format(dt, ()) self.assertEqual(x, str(dt)) d = self.p(x) self.assertIsNot(d, dt) self.assertEqual(d, dt)
def test_format_date(self): dt = date.today() x = _format(dt, ()) self.assertEqual(x, str(dt)) d = self.p(x) self.assertIsNot(d, dt) self.assertEqual(d, dt)
def test_model_selector(self): d = [[]] x = Selector(('model', 'contenttypes.contenttype'), ['one', 'two', ('three', 'four')]) y = _format(x, d) self.assertIs(d[1], x) self.assertEqual(y, '\\0') self.assertEqual(d[0][0], 'model:contenttypes.contenttype.one;.two;.three:"four";') self.assertEqual(self.p(d[0][0]), x)
def test_extra_selector(self): d = [[]] x = Selector('extra', ['one', 'two', ('three', 'four')]) y = _format(x, d) self.assertIs(d[1], x) self.assertEqual(y, '\\0') self.assertEqual(d[0][0], 'extra.one;.two;.three:"four";') self.assertEqual(self.p(d[0][0]), x)
def test_const_selector(self): x = Selector(('const', 42), None) self.assertRaises(IndexError, _format, x, ()) self.assertRaises(AttributeError, _format, x, ((),)) self.assertRaises(AttributeError, _format, x, ([],)) d = [[]] y = _format(x, d) self.assertIs(d[1], x) self.assertEqual(y, '\\0') self.assertEqual(d[0][0], 'const:42') self.assertEqual(self.p(d[0][0]), x) x = Selector(('const', ('hey',)), None) y = _format(x, d) self.assertIs(d[2], x) self.assertEqual(y, '\\1') self.assertEqual(d[0][1], 'const:["hey"]') self.assertEqual(self.p(d[0][1]), x)
def test_float(self): x = _format(29386592.2345, ()) self.assertEqual(x, '29386592.2345') self.assertEqual(self.p(x), 29386592.2345) y = float('infinity') x = _format(y, ()) self.assertEqual(x, 'inf') self.assertEqual(self.p(x), y) y = float('-infinity') x = _format(y, ()) self.assertEqual(x, '-inf') self.assertEqual(self.p(x), y) y = float('nan') x = _format(y, ()) self.assertEqual(x, 'nan') z = self.p(x) self.assertNotEqual(z, z)
def test_object_selector(self): d = [[]] # Incorrect tuple lengths self.assertRaises(ValueError, _format, Selector(0, [(1, 2, 3)]), d) self.assertRaises(ValueError, _format, Selector(0, [(1,)]), d) x = Selector(5, ['one', 'two', ('three', 'four')]) y = _format(x, d) self.assertIs(d[1], x) self.assertEqual(y, '\\0') self.assertEqual(d[0][0], 'object:5.one;.two;.three:"four";') self.assertEqual(self.p(d[0][0]), x)
def test_function(self): x = Function('min', [3, 'hey']) self.assertRaises(IndexError, _format, x, ()) self.assertRaises(AttributeError, _format, x, ((),)) self.assertRaises(AttributeError, _format, x, ([],)) d = [[]] y = _format(x, d) self.assertIs(d[1], x) self.assertEqual(y, '\\0') self.assertEqual(d[0][0], 'min(3,"hey")') self.assertEqual(self.p(d[0][0]), x)
def test_deferred_selector(self): d = [[]] s1 = Selector(0, ['hey']) s2 = Selector(s1, ['ho']) y = _format(s2, d) self.assertIs(d[1], s1) self.assertIs(d[2], s2) self.assertEqual(y, '\\1') self.assertEqual(d[0][0], 'object:0.hey;') self.assertEqual(d[0][1], '\\0.ho;') self.deferred = [s1] self.assertEqual(self.p(d[0][1]), s2)
def test_decimal(self): x = _format(Decimal('29386592.2345'), ()) self.assertEqual(x, '29386592.2345') self.assertEqual(self.p(x), 29386592.2345)
def test_int(self): x = _format(29386592, ()) self.assertEqual(x, '29386592') self.assertEqual(self.p(x), 29386592)
def test_false(self): x = _format(False, ()) self.assertEqual(x, 'false') self.assertIs(self.p(x), False)
def test_true(self): x = _format(True, ()) self.assertEqual(x, 'true') self.assertIs(self.p(x), True)
def test_none(self): x = _format(None, ()) self.assertEqual(x, 'null') self.assertIs(self.p(x), None)
def test_format_string(self): x = "hello\"\\ you" y = _format(x, ()) self.assertEqual(y, r'"hello\"\\ you"') self.assertEqual(x, self.p(y))