test_strformat.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. __author__ = "gerbal"
  2. import unittest
  3. class string_format(unittest.TestCase):
  4. def test_simple_position(self):
  5. self.assertEqual('a, b, c', '{0}, {1}, {2}'.format('a', 'b', 'c'))
  6. self.assertEqual('a, b, c', '{}, {}, {}'.format('a', 'b', 'c'))
  7. self.assertEqual('c, b, a', '{2}, {1}, {0}'.format('a', 'b', 'c'))
  8. self.assertEqual('c, b, a', '{2}, {1}, {0}'.format(*'abc'))
  9. self.assertEqual('abracadabra', '{0}{1}{0}'.format('abra', 'cad'))
  10. #Kwargs don't work
  11. def test_arg_names(self):
  12. self.assertEqual('Coordinates: 37.24N, -115.81W', 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W'))
  13. ## **kwargs does not work properly in Skulpt
  14. # coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
  15. # self.assertEqual('Coordinates: 37.24N, -115.81W', 'Coordinates: {latitude}, {longitude}'.format(**coord))
  16. ## Complex Numbers Currently unsupported
  17. # def test_arg_attr(self):
  18. # c = 3-5j
  19. # self.assertEqual('The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.', ('The complex number {0} is formed from the real part {0.real} and the imaginary part {0.imag}.').format(c))
  20. # class Point(object):
  21. # def __init__(self, x, y):
  22. # self.x, self.y = x, y
  23. # def __str__(self):
  24. # return 'Point({self.x}, {self.y})'.format(self=self)
  25. # self.assertEqual('Point(4, 2)', str(Point(4, 2)))
  26. def test_arg_items(self):
  27. coord = (3, 5)
  28. self.assertEqual('X: 3; Y: 5','X: {0[0]}; Y: {0[1]}'.format(coord))
  29. # self.assertEqual('My name is Fred',"My name is {0[name]}".format({'name':'Fred'}))
  30. # TODO: make these pass
  31. # def test_width(self):
  32. # self.assertEqual(' 2,2',"{0:10},{0}".format(2))
  33. # self.assertEqual('foo bar baz ',"{0:4}{1:4}{2:4}".format("foo","bar","baz"))
  34. def test_conversion(self):
  35. self.assertEqual("repr() shows quotes: 'test1'; str() doesn't: test2", "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2'))
  36. def test_expansion(self):
  37. self.assertEqual('left aligned ', '{:<30}'.format('left aligned'))
  38. self.assertEqual(' right aligned', '{:>30}'.format('right aligned'))
  39. self.assertEqual(' centered ', '{:^30}'.format('centered'))
  40. self.assertEqual('***********centered***********', '{:*^30}'.format('centered'))
  41. def test_fixed_point(self):
  42. self.assertEqual('+3.140000; -3.140000', '{:+f}; {:+f}'.format(3.14, -3.14))
  43. self.assertEqual(' 3.140000; -3.140000', '{: f}; {: f}'.format(3.14, -3.14))
  44. self.assertEqual('3.140000; -3.140000', '{:-f}; {:-f}'.format(3.14, -3.14))
  45. def test_hex_oct(self):
  46. self.assertEqual('int: 42; hex: 2a; oct: 52; bin: 101010', "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42))
  47. self.assertEqual('int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010', "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42))
  48. def test_comma_sep(self):
  49. self.assertEqual('1,234,567,890', '{:,}'.format(1234567890))
  50. def test_percentage(self):
  51. points = 19.5
  52. total = 22
  53. self.assertEqual('Correct answers: 88.64%', 'Correct answers: {:.2%}'.format(points/total))
  54. ## Datetime requires more work.
  55. # def test_datetome(self):
  56. # import datetime
  57. # d = datetime.datetime(2010, 7, 4, 12, 15, 58)
  58. # self.assertEqual('2010-07-04 12:15:58', '{:%Y-%m-%d %H:%M:%S}'.format(d))
  59. if __name__ == '__main__':
  60. unittest.main()