PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/Tests/test_strformat.py

http://github.com/IronLanguages/main
Python | 1219 lines | 1140 code | 52 blank | 27 comment | 31 complexity | 994f4ffd19b46da1d1bbb75f071c3a63 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from iptest.assert_util import *
  16. import sys
  17. allChars = ''
  18. for y in [chr(x) for x in xrange(256) if chr(x) != '[' and chr(x) != '.']:
  19. allChars += y
  20. class TestException(Exception): pass
  21. class bad_str(object):
  22. def __str__(self):
  23. raise TestException('booh')
  24. def test_formatter_parser_errors():
  25. errors = [ ("{0!", "unmatched '{' in format"),
  26. ("{0!}}", "end of format while looking for conversion specifier"),
  27. ("}a{", "Single '}' encountered in format string"),
  28. ("{0:0.1a", "unmatched '{' in format"),
  29. ("{0:{}", "unmatched '{' in format"),
  30. ("{0:aa{ab}", "unmatched '{' in format"),
  31. ("{0!}}", "end of format while looking for conversion specifier"),
  32. ("{0!}", "end of format while looking for conversion specifier"),
  33. ("{0!", "unmatched '{' in format"),
  34. ("{0!aa}", "expected ':' after format specifier"),
  35. ("{0.{!:{.}.}}", "expected ':' after format specifier"),
  36. ("{", "Single '{' encountered in format string"),
  37. ]
  38. for format, errorMsg in errors:
  39. AssertErrorWithMessage(ValueError, errorMsg, list, format._formatter_parser())
  40. def test_formatter_parser():
  41. tests = [ ('{0.}', [('', '0.', '', None)]),
  42. ('{0:.{abc}}', [('', '0', '.{abc}', None)]),
  43. ('{0[]}', [('', '0[]', '', None)]),
  44. ('{0.{:{.}.}}', [('', '0.{', '{.}.}', None)]),
  45. ('{0.{:.}.}', [('', '0.{', '.}.', None)]),
  46. ('{0.{!.:{.}.}}', [('', '0.{', '{.}.}', '.')]),
  47. ('{0.{!!:{.}.}}', [('', '0.{', '{.}.}', '!')]),
  48. ('{0!!}', [('', '0', '', '!')]),
  49. ('{0[}', [('', '0[', '', None)]),
  50. ('{0:::!!::!::}', [('', '0', '::!!::!::', None)]),
  51. ('{0.]}', [('', '0.]', '', None)]),
  52. ('{0.[}', [('', '0.[', '', None)]),
  53. ('{0..}', [('', '0..', '', None)]),
  54. ('{{', [('{', None, None, None)]),
  55. ('}}', [('}', None, None, None)]),
  56. ('{{}}', [('{', None, None, None), ('}', None, None, None)]),
  57. ]
  58. for format, expected in tests:
  59. AreEqual(list(format._formatter_parser()), expected)
  60. def test_format_field_name_split_errors():
  61. if is_cpython: #http://ironpython.codeplex.com/workitem/28224
  62. temp = ''._formatter_field_name_split() #Just ensure it doesn't throw
  63. else:
  64. AssertErrorWithMessage(ValueError, "empty field name", ''._formatter_field_name_split)
  65. AssertErrorWithMessage(ValueError, "empty field name", '['._formatter_field_name_split)
  66. AssertErrorWithMessage(ValueError, "empty field name", '.'._formatter_field_name_split)
  67. AssertErrorWithMessage(ValueError, "empty field name", '[.abc'._formatter_field_name_split)
  68. AssertErrorWithMessage(ValueError, "empty field name", '.abc'._formatter_field_name_split)
  69. errors = [ ("0[", "Missing ']' in format string"),
  70. ("abc.", "Empty attribute in format string"),
  71. ("abc[]", "Empty attribute in format string"),
  72. ]
  73. for format, errorMsg in errors:
  74. AssertErrorWithMessage(ValueError, errorMsg, list, format._formatter_field_name_split()[1])
  75. def test_format_field_name_split():
  76. tests = [ ('0', [0L, []]),
  77. ('abc.foo', ['abc', [(True, 'foo')]]),
  78. ('abc[2]', ['abc', [(False, 2L)]]),
  79. ('1[2]', [1L, [(False, 2L)]]),
  80. ('1.abc', [1L, [(True, 'abc')]]),
  81. ('abc 2.abc', ['abc 2', [(True, 'abc')]]),
  82. ('abc!2.abc', ['abc!2', [(True, 'abc')]]),
  83. ('].abc', [']', [(True, 'abc')]]),
  84. ("abc[[]", ['abc', [(False, '[')]] ),
  85. ("abc[[[]", ['abc', [(False, '[[')]] ),
  86. ]
  87. if not is_cpython: #http://ironpython.codeplex.com/workitem/28331
  88. tests.append(("abc[2]#x", ['abc', [(False, 2L)]] ))
  89. tests.append([allChars, [allChars, []]])
  90. tests.append([allChars + '.foo', [allChars, [(True, 'foo')]]])
  91. tests.append([allChars + '[2]', [allChars, [(False, 2L)]]])
  92. for format, expected in tests:
  93. res = list(format._formatter_field_name_split())
  94. res[1] = list(res[1])
  95. AreEqual(res, expected)
  96. def test_format_arg_errors():
  97. AssertError(IndexError, '{0}'.format)
  98. AssertErrorWithMessage(ValueError, "Empty attribute in format string", '{0.}'.format, 42)
  99. AssertErrorWithMessage(ValueError, "Empty attribute in format string", '{0[]}'.format, 42)
  100. AssertErrorWithMessage(ValueError, "Missing ']' in format string", '{0[}'.format, 42)
  101. AssertError(IndexError, '{0[}'.format)
  102. @skip("win32")
  103. def test_format_cli_interop():
  104. # test classes implementing IFormattable where we pass
  105. # the format spec through
  106. import System
  107. dt = System.DateTime(2008, 10, 26)
  108. class x(object):
  109. abc = dt
  110. AreEqual(format(dt, 'MM-dd'), '10-26')
  111. AreEqual('{0:MM-dd}'.format(dt), '10-26')
  112. AreEqual('{abc:MM-dd}'.format(abc=dt), '10-26')
  113. AreEqual('{0.abc:MM-dd}'.format(x), '10-26')
  114. # test accessing a .NET attribute
  115. AreEqual('{0.Year}'.format(dt), '2008')
  116. # indexing into .NET dictionaries
  117. strDict = System.Collections.Generic.Dictionary[str, object]()
  118. strDict['abc'] = dt
  119. AreEqual('{0[abc]:MM-dd}'.format(strDict), '10-26')
  120. intDict = System.Collections.Generic.Dictionary[int, object]()
  121. intDict[42] = dt
  122. AreEqual('{0[42]:MM-dd}'.format(intDict), '10-26')
  123. objDict = System.Collections.Generic.Dictionary[object, object]()
  124. objDict[42], objDict['42'] = 'abc', 'def'
  125. AreEqual('{0[42]}'.format(objDict), 'abc')
  126. # import clr doesn't flow through
  127. AssertError(AttributeError, '{0.MaxValue}'.format, int)
  128. def test_format_object_access():
  129. class y(object):
  130. bar = 23
  131. class x(object):
  132. def __getitem__(self, index):
  133. return type(index).__name__ + ' ' + str(index)
  134. abc = 42
  135. baz = y
  136. def __str__(self): return 'foo'
  137. def __repr__(self): return 'bar'
  138. AreEqual('{0.abc}'.format(x), '42')
  139. AreEqual('{0.abc}xyz'.format(x), '42xyz')
  140. AreEqual('{0[42]}'.format(x()), 'int 42')
  141. AreEqual('{0[abc]}'.format(x()), 'str abc')
  142. AreEqual('{0.baz.bar}'.format(x()), '23')
  143. AreEqual('{0[abc]!r}'.format(x()), "'str abc'")
  144. AreEqual('{0[abc]!s}'.format(x()), 'str abc')
  145. AreEqual('{0!r}'.format(x()), 'bar')
  146. AreEqual('{0!s}'.format(x()), 'foo')
  147. AreEqual('{abc!s}'.format(abc = x()), 'foo')
  148. AreEqual('{abc!r}'.format(abc = x()), 'bar')
  149. def test_format():
  150. class x(object):
  151. def __format__(self, formatSpec):
  152. return formatSpec
  153. # computed format specs
  154. AreEqual('{0:{1}}'.format(x(), 'abc'), 'abc')
  155. AssertErrorWithMessage(ValueError, "Max string recursion exceeded", '{0:{1:{2}}}'.format, x(), x(), 'abc')
  156. # built-in format method
  157. AreEqual(format(x()), '')
  158. AreEqual(format(x(), 'abc'), 'abc')
  159. class x:
  160. def __format__(self, *args):
  161. return 'abc'
  162. AreEqual(format(x(), ''), 'abc')
  163. AreEqual('{0}'.format(x()), 'abc')
  164. # format for old instances isn't done by an old-instance __format__ method,
  165. # it would seem to be done by a special case.
  166. Assert('__format__' not in type(x()).__dict__)
  167. def test_format_errors():
  168. class bad(object):
  169. def __format__(self, *args):
  170. return None
  171. class bad2(object):
  172. def __format__(self, *args):
  173. return 42
  174. AssertErrorWithMessage(TypeError, "bad.__format__ must return string or unicode, not NoneType", '{0}'.format, bad())
  175. AssertErrorWithMessage(TypeError, "bad2.__format__ must return string or unicode, not int", '{0}'.format, bad2())
  176. AssertErrorWithMessage(ValueError, "Unknown conversion specifier x", '{0!x}'.format, 'abc')
  177. AssertErrorWithMessage(TypeError, "bad.__format__ must return string or unicode, not NoneType", format, bad())
  178. AssertErrorWithMessage(TypeError, "bad2.__format__ must return string or unicode, not int", format, bad2())
  179. def test_object___format__():
  180. class x(object):
  181. def __str__(self):
  182. return 'abc'
  183. tests = [ ('', 'abc'),
  184. ('6', 'abc '),
  185. ('6s', 'abc '),
  186. ('<6', 'abc '),
  187. ('>6', ' abc'),
  188. ('^6', ' abc '),
  189. ('x^6', 'xabcxx'),
  190. ('x<6', 'abcxxx'),
  191. ('<<6', 'abc<<<'),
  192. ('x>6', 'xxxabc'),
  193. ('}^6', '}abc}}'),
  194. ('\0<6', 'abc '),
  195. ('.0', ''),
  196. ('.1', 'a'),
  197. ('5.2', 'ab '),
  198. ('5.2s', 'ab '),
  199. ]
  200. for spec, result in tests:
  201. AreEqual(object.__format__(x(), spec), result)
  202. def test_object___format___errors():
  203. errors = [ ("+", "Sign not allowed in string format specifier"),
  204. ("=+", "Sign not allowed in string format specifier"),
  205. ('=10', "'=' alignment not allowed in string format specifier"),
  206. ("10r", "Unknown format code 'r' for object of type 'str'"),
  207. ("=+r", "Unknown format code 'r' for object of type 'str'"),
  208. (".", "Format specifier missing precision"),
  209. (".a", "Format specifier missing precision"),
  210. ]
  211. # ensure only the s format type is recognized
  212. for char in allChars:
  213. if char != 's' and (char < '0' or char > '9'):
  214. if char==',':
  215. errors.append(('10' + char, "Cannot specify ',' with 's'."))
  216. else:
  217. errors.append(('10' + char, "Unknown format code '%s' for object of type 'str'" % char))
  218. for errorFmt, errorMsg in errors:
  219. AssertErrorWithMessage(ValueError, errorMsg, object().__format__, errorFmt)
  220. # __str__ is called before processing spec
  221. AssertErrorWithMessage(TestException, 'booh', bad_str().__format__, '+')
  222. AssertErrorWithMessage(TestException, 'booh', bad_str().__format__, '=10')
  223. AssertErrorWithMessage(TestException, 'booh', bad_str().__format__, '.')
  224. def test_float___format__():
  225. tests = []
  226. if is_cpython: #In large part due to http://ironpython.codeplex.com/workitem/28206
  227. tests+= [ (2.0, '6.1', ' 2e+00'),
  228. (2.5, '6.1', ' 2e+00'),
  229. (2.25, '6.1', ' 2e+00'),
  230. (2.25, '6.2', ' 2.2'),
  231. (23.0, '.1', '2e+01'),
  232. (23.0, '.2', '2.3e+01'),
  233. (230.5, '.3', '2.3e+02'),
  234. (11230.54, '.5', '1.1231e+04'),
  235. (111230.54, '.1', '1e+05'),
  236. (100000.0, '.5', '1e+05'),
  237. (230.5, '.3g', '230'),
  238. (230.5, '.3n', '230'),
  239. (0.0, '1.1', '0e+00'),
  240. (0.0, '1.0', '0e+00'),
  241. (1.0, '.0', '1e+00'),
  242. (1.1, '.0', '1e+00'),
  243. (1.1, '.1', '1e+00'),
  244. (10.0, '.1', '1e+01'),
  245. (10.0, '.0', '1e+01'),
  246. (100000000000.0, '', '1e+11'),
  247. (1000000000.12, '1.10', '1e+09'),
  248. (1000000000.12, '1.3', '1e+09'),
  249. (999999999999.9, '1.0', '1e+12'),
  250. (999999999999.9, '1.2', '1e+12'),
  251. (999999999999.0, '', '9.99999999999e+11'),
  252. (-999999999999.0, '', '-9.99999999999e+11'),
  253. (10e667, '+', '+inf'),
  254. (-10e667, '+', '-inf'),
  255. (10e667/10e667, '+', '+nan'),
  256. (10e667, '-', 'inf'),
  257. (-10e667, '-', '-inf'),
  258. (10e667/10e667, '-', 'nan'),
  259. (10e667, ' ', ' inf'),
  260. (-10e667, ' ', '-inf'),
  261. (10e667/10e667, ' ', ' nan'),
  262. ]
  263. else:
  264. tests+= [ (2.0, '6.1', ' 2.0'),
  265. (2.5, '6.1', ' 3.0'),
  266. (2.25, '6.1', ' 2.0'),
  267. (2.25, '6.2', ' 2.3'),
  268. (23.0, '.1', '2.0e+01'),
  269. (23.0, '.2', '23.0'),
  270. (230.5, '.3', '231.0'),
  271. (11230.54, '.5', '11231.0'),
  272. (111230.54, '.1', '1.0e+05'),
  273. (100000.0, '.5', '1.0e+05'),
  274. (230.5, '.3g', '231'),
  275. (230.5, '.3n', '231'),
  276. (0.0, '1.1', '0.0'),
  277. (0.0, '1.0', '0.0'),
  278. (1.0, '.0', '1.0'),
  279. (1.1, '.0', '1.0'),
  280. (1.1, '.1', '1.0'),
  281. (10.0, '.1', '1.0e+01'),
  282. (10.0, '.0', '1.0e+01'),
  283. (100000000000.0, '', '100000000000.0'),
  284. (1000000000.12, '1.10', '1000000000.0'),
  285. (1000000000.12, '1.3', '1.0e+09'),
  286. (999999999999.9, '1.0', '1.0e+12'),
  287. (999999999999.9, '1.2', '1.0e+12'),
  288. (999999999999.0, '', '999999999999.0'),
  289. (-999999999999.0, '', '-999999999999.0'),
  290. (10e667, '+', '+inf'),
  291. (-10e667, '+', '-inf'),
  292. (10e667/10e667, '+', '+nan'),
  293. (10e667, '-', 'inf'),
  294. (-10e667, '-', '-inf'),
  295. (10e667/10e667, '-', 'nan'),
  296. (10e667, ' ', ' inf'),
  297. (-10e667, ' ', '-inf'),
  298. (10e667/10e667, ' ', ' nan'),
  299. ]
  300. tests+= [ (2.0, '', '2.0'),
  301. (2.0, 'g', '2'),
  302. (2.0, 'f', '2.000000'),
  303. (2.5, '', '2.5'),
  304. (2.5, 'g', '2.5'),
  305. (2.0, '+', '+2.0'),
  306. (2.0, '-', '2.0'),
  307. (2.0, ' ', ' 2.0'),
  308. (2.0, '<5', '2.0 '),
  309. (2.0, '>5', ' 2.0'),
  310. (2.0, '=5', ' 2.0'),
  311. (2.0, '^6', ' 2.0 '),
  312. (2.0, '6', ' 2.0'),
  313. (2.0, 'x< 10.10', ' 2.0xxxxxx'),
  314. (2.01, 'x< 10.10', ' 2.01xxxxx'),
  315. (2.0, 'x> 10.10', 'xxxxxx 2.0'),
  316. (2.0, 'x= 10.10', ' xxxxxx2.0'),
  317. (2.0, 'x^ 10.10', 'xxx 2.0xxx'),
  318. (2.0, 'x^ 9.10', 'xx 2.0xxx'),
  319. (2.0, '\0^ 9.10', ' 2.0 '),
  320. (2.23, '6.2', ' 2.2'),
  321. (2.25, '6.3', ' 2.25'),
  322. (2.123456789, '2.10', '2.123456789'),
  323. (230.0, '.2', '2.3e+02'),
  324. (230.1, '.2', '2.3e+02'),
  325. (230.5, '.4', '230.5'),
  326. (230.54, '.4', '230.5'),
  327. (230.54, '.5', '230.54'),
  328. (1230.54, '.5', '1230.5'),
  329. (111230.54, '.5', '1.1123e+05'),
  330. (111230.54, '.4', '1.112e+05'),
  331. (111230.54, '.3', '1.11e+05'),
  332. (111230.54, '.2', '1.1e+05'),
  333. (23.0, 'e', '2.300000e+01'),
  334. (23.0, '.6e', '2.300000e+01'),
  335. (23.0, '.0e', '2e+01'),
  336. (23.0, '.1e', '2.3e+01'),
  337. (23.0, '.2e', '2.30e+01'),
  338. (23.0, '.3e', '2.300e+01'),
  339. (23.0, '.4e', '2.3000e+01'),
  340. (230.0, '.2e', '2.30e+02'),
  341. (230.1, '.2e', '2.30e+02'),
  342. (230.5, '.3e', '2.305e+02'),
  343. (230.5, '.4e', '2.3050e+02'),
  344. (230.54, '.4e', '2.3054e+02'),
  345. (230.54, '.5e', '2.30540e+02'),
  346. (1230.54, '.5e', '1.23054e+03'),
  347. (11230.54, '.5e', '1.12305e+04'),
  348. (111230.54, '.5e', '1.11231e+05'),
  349. (111230.54, '.4e', '1.1123e+05'),
  350. (111230.54, '.3e', '1.112e+05'),
  351. (111230.54, '.2e', '1.11e+05'),
  352. (111230.54, '.1e', '1.1e+05'),
  353. (23.0, 'E', '2.300000E+01'),
  354. (23.0, '.6E', '2.300000E+01'),
  355. (23.0, '.0E', '2E+01'),
  356. (23.0, '.1E', '2.3E+01'),
  357. (23.0, '.2E', '2.30E+01'),
  358. (23.0, '.3E', '2.300E+01'),
  359. (23.0, '.4E', '2.3000E+01'),
  360. (230.0, '.2E', '2.30E+02'),
  361. (230.1, '.2E', '2.30E+02'),
  362. (230.5, '.3E', '2.305E+02'),
  363. (230.5, '.4E', '2.3050E+02'),
  364. (230.54, '.4E', '2.3054E+02'),
  365. (230.54, '.5E', '2.30540E+02'),
  366. (1230.54, '.5E', '1.23054E+03'),
  367. (11230.54, '.5E', '1.12305E+04'),
  368. (111230.54, '.5E', '1.11231E+05'),
  369. (111230.54, '.4E', '1.1123E+05'),
  370. (111230.54, '.3E', '1.112E+05'),
  371. (111230.54, '.2E', '1.11E+05'),
  372. (111230.54, '.1E', '1.1E+05'),
  373. (23.0, 'F', '23.000000'),
  374. (23.0, '.6F', '23.000000'),
  375. (23.0, '.0F', '23'),
  376. (23.0, '.1F', '23.0'),
  377. (23.0, '.2F', '23.00'),
  378. (23.0, '.3F', '23.000'),
  379. (23.0, '.4F', '23.0000'),
  380. (230.0, '.2F', '230.00'),
  381. (230.1, '.2F', '230.10'),
  382. (230.5, '.3F', '230.500'),
  383. (230.5, '.4F', '230.5000'),
  384. (230.54, '.4F', '230.5400'),
  385. (230.54, '.5F', '230.54000'),
  386. (1230.54, '.5F', '1230.54000'),
  387. (11230.54, '.5F', '11230.54000'),
  388. (111230.54, '.5F', '111230.54000'),
  389. (111230.54, '.4F', '111230.5400'),
  390. (111230.54, '.3F', '111230.540'),
  391. (111230.54, '.2F', '111230.54'),
  392. (111230.54, '.1F', '111230.5'),
  393. (111230.55, '.1F', '111230.6'),
  394. (-111230.55, '.1F', '-111230.6'),
  395. (111230.55, '.1f', '111230.6'),
  396. (-111230.55, '.1f', '-111230.6'),
  397. (23.0, '%', '2300.000000%'),
  398. (23.0, '.6%', '2300.000000%'),
  399. (23.0, '.0%', '2300%'),
  400. (23.0, '.1%', '2300.0%'),
  401. (23.0, '.2%', '2300.00%'),
  402. (23.0, '.3%', '2300.000%'),
  403. (23.0, '.4%', '2300.0000%'),
  404. (230.0, '.2%', '23000.00%'),
  405. (230.1, '.2%', '23010.00%'),
  406. (230.5, '.3%', '23050.000%'),
  407. (230.5, '.4%', '23050.0000%'),
  408. (230.54, '.4%', '23054.0000%'),
  409. (230.54, '.5%', '23054.00000%'),
  410. (1230.54, '.5%', '123054.00000%'),
  411. (11230.54, '.5%', '1123054.00000%'),
  412. (111230.54, '.5%', '11123054.00000%'),
  413. (111230.54, '.4%', '11123054.0000%'),
  414. (111230.54, '.3%', '11123054.000%'),
  415. (111230.54, '.2%', '11123054.00%'),
  416. (111230.54, '.1%', '11123054.0%'),
  417. (111230.55, '.1%', '11123055.0%'),
  418. (-111230.55, '.1%', '-11123055.0%'),
  419. (23.0, '.1g', '2e+01'),
  420. (23.0, '.0g', '2e+01'),
  421. (230.0, '.1g', '2e+02'),
  422. (23.0, '.2g', '23'),
  423. (23.5, '.2g', '24'),
  424. (23.4, '.2g', '23'),
  425. (23.45, '.2g', '23'),
  426. (230.0, '.2g', '2.3e+02'),
  427. (230.1, '.2g', '2.3e+02'),
  428. (230.5, '.4g', '230.5'),
  429. (230.54, '.4g', '230.5'),
  430. (230.54, '.5g', '230.54'),
  431. (1230.54, '.5g', '1230.5'),
  432. (11230.54, '.5g', '11231'),
  433. (111230.54, '.5g', '1.1123e+05'),
  434. (111230.54, '.4g', '1.112e+05'),
  435. (111230.54, '.3g', '1.11e+05'),
  436. (111230.54, '.2g', '1.1e+05'),
  437. (111230.54, '.1g', '1e+05'),
  438. (23.0, '.1n', '2e+01'),
  439. (23.0, '.2n', '23'),
  440. (230.0, '.2n', '2.3e+02'),
  441. (230.1, '.2n', '2.3e+02'),
  442. (230.5, '.4n', '230.5'),
  443. (230.54, '.4n', '230.5'),
  444. (230.54, '.5n', '230.54'),
  445. (1230.54, '.5n', '1230.5'),
  446. (11230.54, '.5n', '11231'),
  447. (111230.54, '.5n', '1.1123e+05'),
  448. (111230.54, '.4n', '1.112e+05'),
  449. (111230.54, '.3n', '1.11e+05'),
  450. (111230.54, '.2n', '1.1e+05'),
  451. (111230.54, '.1n', '1e+05'),
  452. (11231.54, 'n', '11231.5'),
  453. (111230.54, 'n', '111231'),
  454. (111230.54, 'g', '111231'),
  455. (0.0, '', '0.0'),
  456. (0.0, '1', '0.0'),
  457. (1.1, '.2', '1.1'),
  458. (1000000.0, '', '1000000.0'),
  459. (10000000.0, '', '10000000.0'),
  460. (100000000.0, '', '100000000.0'),
  461. (1000000000.0, '', '1000000000.0'),
  462. (10000000000.0, '', '10000000000.0'),
  463. (1000000000000.0, '', '1e+12'),
  464. (-1000000000000.0, '', '-1e+12'),
  465. (-1000000000000.0, 'g', '-1e+12'),
  466. (-1000000000000.0, 'G', '-1E+12'),
  467. (-1000000000000.0, '.1g', '-1e+12'),
  468. (-1000000000000.0, '.1G', '-1E+12'),
  469. (10e667, '', 'inf'),
  470. (-10e667, '', '-inf'),
  471. (10e667/10e667, '', 'nan'),
  472. ]
  473. for value, spec, result in tests:
  474. AreEqual(value.__format__(spec), result)
  475. # check locale specific formatting
  476. import _locale
  477. try:
  478. if is_cli:
  479. _locale.setlocale(_locale.LC_ALL, 'en_US')
  480. else:
  481. _locale.setlocale(_locale.LC_ALL, 'English_United States.1252')
  482. tests = [ (1000.0, 'n', '1,000'),
  483. (1000.12345, 'n', '1,000.12'),
  484. (1000.5, 'n', '1,000.5'),
  485. (100000.0, 'n', '100,000'),
  486. (100000.0, '.5n', '1e+05'),
  487. (100000.5, '.5n', '1e+05'),
  488. (100000.5, '.7n', '100,000.5'),
  489. ]
  490. if is_cpython: #http://ironpython.codeplex.com/workitem/28206
  491. tests+= [
  492. (100000.5, 'n', '100,000'),
  493. (100000.5, '.6n', '100,000'),
  494. ]
  495. else:
  496. tests+= [
  497. (100000.5, 'n', '100,001'),
  498. (100000.5, '.6n', '100,001'),
  499. ]
  500. for value, spec, result in tests:
  501. AreEqual(value.__format__(spec), result)
  502. finally:
  503. # and restore it back...
  504. _locale.setlocale(_locale.LC_ALL, 'C')
  505. AreEqual(100000.0.__format__('n'), '100000')
  506. def test_float___format___errors():
  507. errors = []
  508. okChars = set(['\0', '%', 'E', 'F', 'G', 'e', 'f', 'g', 'n', ','])
  509. # verify the okChars are actually ok
  510. for char in okChars:
  511. 2.0.__format__('10' + char)
  512. for char in allChars:
  513. if char not in okChars and (char < '0' or char > '9'):
  514. errors.append((2.0, '10' + char, "Unknown format code '%s' for object of type 'float'" % char))
  515. for value, errorFmt, errorMsg in errors:
  516. AssertErrorWithMessage(ValueError, errorMsg, value.__format__, errorFmt)
  517. def test_int___format__():
  518. tests = [
  519. (0, '+', '+0'),
  520. (0, ' ', ' 0'),
  521. (0, '-', '0'),
  522. (2, '', '2'),
  523. (2, '+', '+2'),
  524. (2, '-', '2'),
  525. (2, ' ', ' 2'),
  526. (2, '<5', '2 '),
  527. (2, '05', '00002'),
  528. (20000,'+4', '+20000'),
  529. (2, '>5', ' 2'),
  530. (2, '=5', ' 2'),
  531. (2, '^6', ' 2 '),
  532. (2, '6', ' 2'),
  533. (2, 'x< 10', ' 2xxxxxxxx'),
  534. (2, 'x> 10', 'xxxxxxxx 2'),
  535. (2, 'x= 10', ' xxxxxxxx2'),
  536. (2, 'x^ 10', 'xxxx 2xxxx'),
  537. (2, 'x^ 9', 'xxx 2xxxx'),
  538. (2, 'x<+10', '+2xxxxxxxx'),
  539. (2, 'x>+10', 'xxxxxxxx+2'),
  540. (2, 'x=+10', '+xxxxxxxx2'),
  541. (2, 'x^+10', 'xxxx+2xxxx'),
  542. (2, 'x^+9', 'xxx+2xxxx'),
  543. (2, 'x<-10', '2xxxxxxxxx'),
  544. (2, 'x>-10', 'xxxxxxxxx2'),
  545. (2, 'x=-10', 'xxxxxxxxx2'),
  546. (2, 'x^-10', 'xxxx2xxxxx'),
  547. (2, 'x^-9', 'xxxx2xxxx'),
  548. (-2, 'x<-10', '-2xxxxxxxx'),
  549. (-2, 'x>-10', 'xxxxxxxx-2'),
  550. (-2, 'x=-10', '-xxxxxxxx2'),
  551. (-2, 'x^-10', 'xxxx-2xxxx'),
  552. (-2, 'x^-9', 'xxx-2xxxx'),
  553. (-2, 'x<+10', '-2xxxxxxxx'),
  554. (-2, 'x>+10', 'xxxxxxxx-2'),
  555. (-2, 'x=+10', '-xxxxxxxx2'),
  556. (-2, 'x^+10', 'xxxx-2xxxx'),
  557. (-2, 'x^+9', 'xxx-2xxxx'),
  558. (-2, 'x< 10', '-2xxxxxxxx'),
  559. (-2, 'x> 10', 'xxxxxxxx-2'),
  560. (-2, 'x= 10', '-xxxxxxxx2'),
  561. (-2, 'x^ 10', 'xxxx-2xxxx'),
  562. (-2, 'x^ 9', 'xxx-2xxxx'),
  563. (2, '\0^ 9', ' 2 '),
  564. (2, 'c', '\x02'),
  565. (2, '<5c', '\x02 '),
  566. (2, '>5c', ' \x02'),
  567. (2, '=5c', ' \x02'),
  568. (2, '^6c', ' \x02 '),
  569. (2, '6c', ' \x02'),
  570. (3, 'b', '11'),
  571. (3, '+b', '+11'),
  572. (3, '-b', '11'),
  573. (3, ' b', ' 11'),
  574. (3, '<5b', '11 '),
  575. (3, '>5b', ' 11'),
  576. (3, '=5b', ' 11'),
  577. (3, '^6b', ' 11 '),
  578. (3, '6b', ' 11'),
  579. (3, 'x< 010b', ' 11xxxxxxx'),
  580. (3, '< 010b', ' 110000000'),
  581. (3, 'x< 010b', ' 11xxxxxxx'),
  582. (3, 'x< 10b', ' 11xxxxxxx'),
  583. (3, 'x< 10b', ' 11xxxxxxx'),
  584. (3, 'x> 10b', 'xxxxxxx 11'),
  585. (3, 'x= 10b', ' xxxxxxx11'),
  586. (3, 'x^ 10b', 'xxx 11xxxx'),
  587. (3, 'x^ 9b', 'xxx 11xxx'),
  588. (3, 'x<+10b', '+11xxxxxxx'),
  589. (3, 'x>+10b', 'xxxxxxx+11'),
  590. (3, 'x=+10b', '+xxxxxxx11'),
  591. (3, 'x^+10b', 'xxx+11xxxx'),
  592. (3, 'x^+9b', 'xxx+11xxx'),
  593. (3, 'x<-10b', '11xxxxxxxx'),
  594. (3, 'x>-10b', 'xxxxxxxx11'),
  595. (3, 'x=-10b', 'xxxxxxxx11'),
  596. (3, 'x^-10b', 'xxxx11xxxx'),
  597. (3, 'x^-9b', 'xxx11xxxx'),
  598. (-3, 'x<-10b', '-11xxxxxxx'),
  599. (-3, 'x>-10b', 'xxxxxxx-11'),
  600. (-3, 'x=-10b', '-xxxxxxx11'),
  601. (-3, 'x^-10b', 'xxx-11xxxx'),
  602. (-3, 'x^-9b', 'xxx-11xxx'),
  603. (-3, 'x<+10b', '-11xxxxxxx'),
  604. (-3, 'x>+10b', 'xxxxxxx-11'),
  605. (-3, 'x=+10b', '-xxxxxxx11'),
  606. (-3, 'x^+10b', 'xxx-11xxxx'),
  607. (-3, 'x^+9b', 'xxx-11xxx'),
  608. (-3, 'x< 10b', '-11xxxxxxx'),
  609. (-3, 'x> 10b', 'xxxxxxx-11'),
  610. (-3, 'x= 10b', '-xxxxxxx11'),
  611. (-3, 'x^ 10b', 'xxx-11xxxx'),
  612. (-3, 'x^ #10b', 'xx-0b11xxx'),
  613. (-3, 'x^ 9b', 'xxx-11xxx'),
  614. (3, '\0^ 9b', ' 11 '),
  615. (-2147483648, 'b', '-10000000000000000000000000000000'),
  616. (0, 'b', '0'),
  617. (9, 'o', '11'),
  618. (9, '+o', '+11'),
  619. (9, '-o', '11'),
  620. (9, ' o', ' 11'),
  621. (9, '<5o', '11 '),
  622. (9, '>5o', ' 11'),
  623. (9, '=5o', ' 11'),
  624. (9, '^6o', ' 11 '),
  625. (9, '6o', ' 11'),
  626. (9, 'x< 10o', ' 11xxxxxxx'),
  627. (9, 'x> 10o', 'xxxxxxx 11'),
  628. (9, 'x= 10o', ' xxxxxxx11'),
  629. (9, 'x^ 10o', 'xxx 11xxxx'),
  630. (9, 'x^ 9o', 'xxx 11xxx'),
  631. (9, 'x<+10o', '+11xxxxxxx'),
  632. (9, 'x>+10o', 'xxxxxxx+11'),
  633. (9, 'x=+10o', '+xxxxxxx11'),
  634. (9, 'x^+10o', 'xxx+11xxxx'),
  635. (9, 'x^+9o', 'xxx+11xxx'),
  636. (9, 'x<-10o', '11xxxxxxxx'),
  637. (9, 'x>-10o', 'xxxxxxxx11'),
  638. (9, 'x=-10o', 'xxxxxxxx11'),
  639. (9, 'x^-10o', 'xxxx11xxxx'),
  640. (9, 'x^-9o', 'xxx11xxxx'),
  641. (-9, 'x<-10o', '-11xxxxxxx'),
  642. (-9, 'x>-10o', 'xxxxxxx-11'),
  643. (-9, 'x=-10o', '-xxxxxxx11'),
  644. (-9, 'x^-10o', 'xxx-11xxxx'),
  645. (-9, 'x^-9o', 'xxx-11xxx'),
  646. (-9, 'x<+10o', '-11xxxxxxx'),
  647. (-9, 'x>+10o', 'xxxxxxx-11'),
  648. (-9, 'x=+10o', '-xxxxxxx11'),
  649. (-9, 'x^+10o', 'xxx-11xxxx'),
  650. (-9, 'x^+9o', 'xxx-11xxx'),
  651. (-9, 'x< 10o', '-11xxxxxxx'),
  652. (-9, 'x< #10o', '-0o11xxxxx'),
  653. (-9, 'x> 10o', 'xxxxxxx-11'),
  654. (-9, 'x= 10o', '-xxxxxxx11'),
  655. (-9, 'x^ 10o', 'xxx-11xxxx'),
  656. (-9, 'x^ 9o', 'xxx-11xxx'),
  657. (9, '\0^ 9o', ' 11 '),
  658. (-9, 'x^ 9o', 'xxx-11xxx'),
  659. (-2147483648, 'o', '-20000000000'),
  660. (-42, 'o', '-52'),
  661. (42, 'o', '52'),
  662. (0, 'o', '0'),
  663. (-2147483648, 'X', '-80000000'),
  664. (-2147483648, 'x', '-80000000'),
  665. (-42, 'X', '-2A'),
  666. (-42, 'x', '-2a'),
  667. (42, 'X', '2A'),
  668. (42, 'x', '2a'),
  669. (2147483647, 'X', '7FFFFFFF'),
  670. (0, 'x', '0'),
  671. (2147483647, 'x', '7fffffff'),
  672. (2147483647, '#x', '0x7fffffff'),
  673. (2147483647, '#X', '0X7FFFFFFF'),
  674. (2147483647, 'f', '2147483647.000000'),
  675. (2147483647, '%', '214748364700.000000%'),
  676. (999999, '-g', '999999'),
  677. (999999, '+g', '+999999'),
  678. (999999, ' g', ' 999999'),
  679. (999999, 'g', '999999'),
  680. (999999 , 'G', '999999'),
  681. (-999999, 'g', '-999999'),
  682. (-999999 , 'G', '-999999'),
  683. (100000, 'g', '100000'),
  684. (100000, 'G', '100000'),
  685. (-1000000, 'g', '-1e+06'),
  686. (-1000000, 'G', '-1E+06'),
  687. (1000000, 'g', '1e+06'),
  688. (1000000, 'G', '1E+06'),
  689. (10000000, 'g', '1e+07'),
  690. (10000000, 'G', '1E+07'),
  691. (100000000, 'g', '1e+08'),
  692. (100000000, 'G', '1E+08'),
  693. (1000000, '10g', ' 1e+06'),
  694. (1000000, '10G', ' 1E+06'),
  695. (10000000, '10g', ' 1e+07'),
  696. (10000000, '10G', ' 1E+07'),
  697. (100000000, '10g', ' 1e+08'),
  698. (100000000, '10G', ' 1E+08'),
  699. (110000000, 'g', '1.1e+08'),
  700. (110000000, 'G', '1.1E+08'),
  701. (112000000, 'g', '1.12e+08'),
  702. (112000000, 'G', '1.12E+08'),
  703. (112300000, 'g', '1.123e+08'),
  704. (112300000, 'G', '1.123E+08'),
  705. (112340000, 'g', '1.1234e+08'),
  706. (112340000, 'G', '1.1234E+08'),
  707. (112345000, 'g', '1.12345e+08'),
  708. (112345000, 'G', '1.12345E+08'),
  709. (112345600, 'g', '1.12346e+08'),
  710. (112345600, 'G', '1.12346E+08'),
  711. (112345500, 'g', '1.12346e+08'),
  712. (112345500, 'G', '1.12346E+08'),
  713. (112345510, 'g', '1.12346e+08'),
  714. (112345510, 'G', '1.12346E+08'),
  715. (112345400, 'g', '1.12345e+08'),
  716. (112345400, 'G', '1.12345E+08'),
  717. (112345401, 'g', '1.12345e+08'),
  718. (112345401, 'G', '1.12345E+08'),
  719. (-112345000, 'g', '-1.12345e+08'),
  720. (-112345000, 'G', '-1.12345E+08'),
  721. (-112345600, 'g', '-1.12346e+08'),
  722. (-112345600, 'G', '-1.12346E+08'),
  723. (-112345500, 'g', '-1.12346e+08'),
  724. (-112345500, 'G', '-1.12346E+08'),
  725. (-112345510, 'g', '-1.12346e+08'),
  726. (-112345510, 'G', '-1.12346E+08'),
  727. (-112345400, 'g', '-1.12345e+08'),
  728. (-112345400, 'G', '-1.12345E+08'),
  729. (-112345401, 'g', '-1.12345e+08'),
  730. (-112345401, 'G', '-1.12345E+08'),
  731. (2147483647, 'g', '2.14748e+09'),
  732. (2147483647, 'G', '2.14748E+09'),
  733. (-2147483647, 'g', '-2.14748e+09'),
  734. (-2147483647, 'G', '-2.14748E+09'),
  735. (2147483647, 'e', '2.147484e+09'),
  736. (100000, 'e', '1.000000e+05'),
  737. (100000, 'E', '1.000000E+05'),
  738. (10000000, 'E', '1.000000E+07'),
  739. (100000000, 'e', '1.000000e+08'),
  740. (100000000, 'E', '1.000000E+08'),
  741. (110000000, 'e', '1.100000e+08'),
  742. (110000000, 'E', '1.100000E+08'),
  743. (112000000, 'e', '1.120000e+08'),
  744. (112000000, 'E', '1.120000E+08'),
  745. (112300000, 'e', '1.123000e+08'),
  746. (112300000, 'E', '1.123000E+08'),
  747. (112340000, 'e', '1.123400e+08'),
  748. (112340000, 'E', '1.123400E+08'),
  749. (112345000, 'e', '1.123450e+08'),
  750. (112345000, 'E', '1.123450E+08'),
  751. (1112345600, 'e', '1.112346e+09'),
  752. (1112345600, 'E', '1.112346E+09'),
  753. (1112345500, 'e', '1.112346e+09'),
  754. (1112345500, 'E', '1.112346E+09'),
  755. (1112345510, 'e', '1.112346e+09'),
  756. (1112345510, 'E', '1.112346E+09'),
  757. (1112345400, 'e', '1.112345e+09'),
  758. (1112345400, 'E', '1.112345E+09'),
  759. (1112345401, 'e', '1.112345e+09'),
  760. (1112345401, 'E', '1.112345E+09'),
  761. (100000, 'n', '100000'),
  762. ]
  763. for value, spec, result in tests:
  764. AreEqual(value.__format__(spec), result)
  765. # check locale specific formatting
  766. import _locale
  767. try:
  768. if is_cli:
  769. _locale.setlocale(_locale.LC_ALL, 'en_US')
  770. else:
  771. _locale.setlocale(_locale.LC_ALL, 'English_United States.1252')
  772. x = 100000
  773. AreEqual(x.__format__('n'), '100,000')
  774. finally:
  775. # and restore it back...
  776. _locale.setlocale(_locale.LC_ALL, 'C')
  777. AreEqual(x.__format__('n'), '100000')
  778. def test_int___format___errors():
  779. errors = [
  780. (ValueError, 2, '6.1', "Precision not allowed in integer format specifier"),
  781. (ValueError, 2, '+c', "Sign not allowed with integer format specifier 'c'"),
  782. (ValueError, 2, '-c', "Sign not allowed with integer format specifier 'c'"),
  783. (ValueError, 2, ' c', "Sign not allowed with integer format specifier 'c'"),
  784. (OverflowError, -2, 'c', "%c arg not in range(0x10000)"),
  785. #(-2, 'c', ),
  786. #(-2, '%', "Sign not allowed with integer format specifier 'c'"),
  787. ]
  788. okChars = set(['%', 'E', 'F', 'G', 'X', 'x', 'b', 'c', 'd', 'o', 'e', 'f', 'g', 'n', ','])
  789. # verify the okChars are actually ok
  790. for char in okChars:
  791. (2).__format__('10' + char)
  792. for char in allChars:
  793. if char not in okChars and (char < '0' or char > '9'):
  794. errors.append((ValueError, 2, '10' + char, "Unknown format code '%s'" % char))
  795. for error, value, errorFmt, errorMsg in errors:
  796. AssertErrorWithPartialMessage(error, errorMsg, value.__format__, errorFmt)
  797. @skip("posix") # mono doesn't support some of the formatting specifiers https://github.com/IronLanguages/main/issues/1598
  798. def test_long___format__():
  799. tests = [
  800. (0L, '+', '+0'),
  801. (0L, ' ', ' 0'),
  802. (0L, '-', '0'),
  803. (2L, '', '2'),
  804. (2L, '+', '+2'),
  805. (2L, '-', '2'),
  806. (2L, ' ', ' 2'),
  807. (2L, '<5', '2 '),
  808. (2L, '>5', ' 2'),
  809. (2L, '=5', ' 2'),
  810. (2L, '^6', ' 2 '),
  811. (2L, '6', ' 2'),
  812. (2L, 'x< 10', ' 2xxxxxxxx'),
  813. (2L, 'x> 10', 'xxxxxxxx 2'),
  814. (2L, 'x= 10', ' xxxxxxxx2'),
  815. (2L, 'x^ 10', 'xxxx 2xxxx'),
  816. (2L, 'x^ 9', 'xxx 2xxxx'),
  817. (2L, 'x<+10', '+2xxxxxxxx'),
  818. (2L, 'x>+10', 'xxxxxxxx+2'),
  819. (2L, 'x=+10', '+xxxxxxxx2'),
  820. (2L, 'x^+10', 'xxxx+2xxxx'),
  821. (2L, 'x^+9', 'xxx+2xxxx'),
  822. (2L, 'x<-10', '2xxxxxxxxx'),
  823. (2L, 'x>-10', 'xxxxxxxxx2'),
  824. (2L, 'x=-10', 'xxxxxxxxx2'),
  825. (2L, 'x^-10', 'xxxx2xxxxx'),
  826. (2L, 'x^-9', 'xxxx2xxxx'),
  827. (-2L, 'x<-10', '-2xxxxxxxx'),
  828. (-2L, 'x>-10', 'xxxxxxxx-2'),
  829. (-2L, 'x=-10', '-xxxxxxxx2'),
  830. (-2L, 'x^-10', 'xxxx-2xxxx'),
  831. (-2L, 'x^-9', 'xxx-2xxxx'),
  832. (-2L, 'x<+10', '-2xxxxxxxx'),
  833. (-2L, 'x>+10', 'xxxxxxxx-2'),
  834. (-2L, 'x=+10', '-xxxxxxxx2'),
  835. (-2L, 'x^+10', 'xxxx-2xxxx'),
  836. (-2L, 'x^+9', 'xxx-2xxxx'),
  837. (-2L, 'x< 10', '-2xxxxxxxx'),
  838. (-2L, 'x> 10', 'xxxxxxxx-2'),
  839. (-2L, 'x= 10', '-xxxxxxxx2'),
  840. (-2L, 'x^ 10', 'xxxx-2xxxx'),
  841. (-2L, 'x^ 9', 'xxx-2xxxx'),
  842. (2L, '\0^ 9', ' 2 '),
  843. (2L, 'c', '\x02'),
  844. (2L, '<5c', '\x02 '),
  845. (2L, '>5c', ' \x02'),
  846. (2L, '=5c', ' \x02'),
  847. (2L, '^6c', ' \x02 '),
  848. (2L, '6c', ' \x02'),
  849. (3L, 'b', '11'),
  850. (3L, '+b', '+11'),
  851. (3L, '-b', '11'),
  852. (3L, ' b', ' 11'),
  853. (3L, '<5b', '11 '),
  854. (3L, '>5b', ' 11'),
  855. (3L, '=5b', ' 11'),
  856. (3L, '^6b', ' 11 '),
  857. (3L, '6b', ' 11'),
  858. (3L, 'x< 010b', ' 11xxxxxxx'),
  859. (3L, '< 010b', ' 110000000'),
  860. (3L, 'x< 010b', ' 11xxxxxxx'),
  861. (3L, 'x< 10b', ' 11xxxxxxx'),
  862. (3L, 'x< 10b', ' 11xxxxxxx'),
  863. (3L, 'x> 10b', 'xxxxxxx 11'),
  864. (3L, 'x= 10b', ' xxxxxxx11'),
  865. (3L, 'x^ 10b', 'xxx 11xxxx'),
  866. (3L, 'x^ 9b', 'xxx 11xxx'),
  867. (3L, 'x<+10b', '+11xxxxxxx'),
  868. (3L, 'x>+10b', 'xxxxxxx+11'),
  869. (3L, 'x=+10b', '+xxxxxxx11'),
  870. (3L, 'x^+10b', 'xxx+11xxxx'),
  871. (3L, 'x^+9b', 'xxx+11xxx'),
  872. (3L, 'x<-10b', '11xxxxxxxx'),
  873. (3L, 'x>-10b', 'xxxxxxxx11'),
  874. (3L, 'x=-10b', 'xxxxxxxx11'),
  875. (3L, 'x^-10b', 'xxxx11xxxx'),
  876. (3L, 'x^-9b', 'xxx11xxxx'),
  877. (-3L, 'x<-10b', '-11xxxxxxx'),
  878. (-3L, 'x>-10b', 'xxxxxxx-11'),
  879. (-3L, 'x=-10b', '-xxxxxxx11'),
  880. (-3L, 'x^-10b', 'xxx-11xxxx'),
  881. (-3L, 'x^-9b', 'xxx-11xxx'),
  882. (-3L, 'x<+10b', '-11xxxxxxx'),
  883. (-3L, 'x>+10b', 'xxxxxxx-11'),
  884. (-3L, 'x=+10b', '-xxxxxxx11'),
  885. (-3L, 'x^+10b', 'xxx-11xxxx'),
  886. (-3L, 'x^+9b', 'xxx-11xxx'),
  887. (-3L, 'x< 10b', '-11xxxxxxx'),
  888. (-3L, 'x> 10b', 'xxxxxxx-11'),
  889. (-3L, 'x= 10b', '-xxxxxxx11'),
  890. (-3L, 'x^ 10b', 'xxx-11xxxx'),
  891. (-3L, 'x^ #10b', 'xx-0b11xxx'),
  892. (-3L, 'x^ 9b', 'xxx-11xxx'),
  893. (3L, '\0^ 9b', ' 11 '),
  894. (-2147483648L, 'b', '-10000000000000000000000000000000'),
  895. (0L, 'b', '0'),
  896. (9L, 'o', '11'),
  897. (9L, '+o', '+11'),
  898. (9L, '-o', '11'),
  899. (9L, ' o', ' 11'),
  900. (9L, '<5o', '11 '),
  901. (9L, '>5o', ' 11'),
  902. (9L, '=5o', ' 11'),
  903. (9L, '^6o', ' 11 '),
  904. (9L, '6o', ' 11'),
  905. (9L, 'x< 10o', ' 11xxxxxxx'),
  906. (9L, 'x> 10o', 'xxxxxxx 11'),
  907. (9L, 'x= 10o', ' xxxxxxx11'),
  908. (9L, 'x^ 10o', 'xxx 11xxxx'),
  909. (9L, 'x^ 9o', 'xxx 11xxx'),
  910. (9L, 'x<+10o', '+11xxxxxxx'),
  911. (9L, 'x>+10o', 'xxxxxxx+11'),
  912. (9L, 'x=+10o', '+xxxxxxx11'),
  913. (9L, 'x^+10o', 'xxx+11xxxx'),
  914. (9L, 'x^+9o', 'xxx+11xxx'),
  915. (9L, 'x<-10o', '11xxxxxxxx'),
  916. (9L, 'x>-10o', 'xxxxxxxx11'),
  917. (9L, 'x=-10o', 'xxxxxxxx11'),
  918. (9L, 'x^-10o', 'xxxx11xxxx'),
  919. (9L, 'x^-9o', 'xxx11xxxx'),
  920. (-9L, 'x<-10o', '-11xxxxxxx'),
  921. (-9L, 'x>-10o', 'xxxxxxx-11'),
  922. (-9L, 'x=-10o', '-xxxxxxx11'),
  923. (-9L, 'x^-10o', 'xxx-11xxxx'),
  924. (-9L, 'x^-9o', 'xxx-11xxx'),
  925. (-9L, 'x<+10o', '-11xxxxxxx'),
  926. (-9L, 'x>+10o', 'xxxxxxx-11'),
  927. (-9L, 'x=+10o', '-xxxxxxx11'),
  928. (-9L, 'x^+10o', 'xxx-11xxxx'),
  929. (-9L, 'x^+9o', 'xxx-11xxx'),
  930. (-9L, 'x< 10o', '-11xxxxxxx'),
  931. (-9L, 'x< #10o', '-0o11xxxxx'),
  932. (-9L, 'x> 10o', 'xxxxxxx-11'),
  933. (-9L, 'x= 10o', '-xxxxxxx11'),
  934. (-9L, 'x^ 10o', 'xxx-11xxxx'),
  935. (-9L, 'x^ 9o', 'xxx-11xxx'),
  936. (9L, '\0^ 9o', ' 11 '),
  937. (-9L, 'x^ 9o', 'xxx-11xxx'),
  938. (-2147483648L, 'o', '-20000000000'),
  939. (-42L, 'o', '-52'),
  940. (0, 'o', '0'),
  941. (42L, 'o', '52'),
  942. (0, 'x', '0'),
  943. (-2147483648L, 'X', '-80000000'),
  944. (-2147483648L, 'x', '-80000000'),
  945. (-42L, 'X', '-2A'),
  946. (-42L, 'x', '-2a'),
  947. (42L, 'X', '2A'),
  948. (42L, 'x', '2a'),
  949. (2147483647L, 'X', '7FFFFFFF'),
  950. (2147483647L, 'x', '7fffffff'),
  951. (2147483647L, '#x', '0x7fffffff'),
  952. (2147483647L, '#X', '0X7FFFFFFF'),
  953. (2147483647L, 'f', '2147483647.000000'),
  954. (2147483647L, '%', '214748364700.000000%'),
  955. (999999L, '-g', '999999'),
  956. (999999L, '+g', '+999999'),
  957. (999999L, ' g', ' 999999'),
  958. (999999L, 'g', '999999'),
  959. (999999L, 'G', '999999'),
  960. (-999999L, 'g', '-999999'),
  961. (-999999L, 'G', '-999999'),
  962. (100000L, 'g', '100000'),
  963. (100000L, 'G', '100000'),
  964. (-1000000L, 'g', '-1e+06'),
  965. (-1000000L, 'G', '-1E+06'),
  966. (1000000L, 'g', '1e+06'),
  967. (1000000L, 'G', '1E+06'),
  968. (10000000L, 'g', '1e+07'),
  969. (10000000L, 'G', '1E+07'),
  970. (100000000L, 'g', '1e+08'),
  971. (100000000L, 'G', '1E+08'),
  972. (1000000L, '10g', ' 1e+06'),
  973. (1000000L, '10G', ' 1E+06'),
  974. (10000000L, '10g', ' 1e+07'),
  975. (10000000L, '10G', ' 1E+07'),
  976. (10200000L, '10G', ' 1.02E+07'),
  977. (100000000L, '10g', ' 1e+08'),
  978. (100000000L, '10G', ' 1E+08'),
  979. (110000000L, 'g', '1.1e+08'),
  980. (110000000L, 'G', '1.1E+08'),
  981. (112000000L, 'g', '1.12e+08'),
  982. (112000000L, 'G', '1.12E+08'),
  983. (112300000L, 'g', '1.123e+08'),
  984. (112300000L, 'G', '1.123E+08'),
  985. (112340000L, 'g', '1.1234e+08'),
  986. (112340000L, 'G', '1.1234E+08'),
  987. (112345000L, 'g', '1.12345e+08'),
  988. (112345000L, 'G', '1.12345E+08'),
  989. (112345600L, 'g', '1.12346e+08'),
  990. (112345600L, 'G', '1.12346E+08'),
  991. (112345500L, 'g', '1.12346e+08'),
  992. (112345500L, 'G', '1.12346E+08'),
  993. (112345510L, 'g', '1.12346e+08'),
  994. (112345510L, 'G', '1.12346E+08'),
  995. (112345400L, 'g', '1.12345e+08'),
  996. (112345400L, 'G', '1.12345E+08'),
  997. (112345401L, 'g', '1.12345e+08'),
  998. (112345401L, 'G', '1.12345E+08'),
  999. (-112345000L, 'g', '-1.12345e+08'),
  1000. (-112345000L, 'G', '-1.12345E+08'),
  1001. (-112345600L, 'g', '-1.12346e+08'),
  1002. (-112345600L, 'G', '-1.12346E+08'),
  1003. (-112345500L, 'g', '-1.12346e+08'),
  1004. (-112345500L, 'G', '-1.12346E+08'),
  1005. (-112345510L, 'g', '-1.12346e+08'),
  1006. (-112345510L, 'G', '-1.12346E+08'),
  1007. (-112345400L, 'g', '-1.12345e+08'),
  1008. (-112345400L, 'G', '-1.12345E+08'),
  1009. (-112345401L, 'g', '-1.12345e+08'),
  1010. (-112345401L, 'G', '-1.12345E+08'),
  1011. (2147483647L, 'g', '2.14748e+09'),
  1012. (2147483647L, 'G', '2.14748E+09'),
  1013. (-2147483647L, 'g', '-2.14748e+09'),
  1014. (-2147483647L, 'G', '-2.14748E+09'),
  1015. (2147483647L, 'e', '2.147484e+09'),
  1016. (100000L, 'e', '1.000000e+05'),
  1017. (100000L, 'E', '1.000000E+05'),
  1018. (10000000L, 'E', '1.000000E+07'),
  1019. (100000000L, 'e', '1.000000e+08'),
  1020. (100000000L, 'E', '1.000000E+08'),
  1021. (110000000L, 'e', '1.100000e+08'),
  1022. (110000000L, 'E', '1.100000E+08'),
  1023. (112000000L, 'e', '1.120000e+08'),
  1024. (112000000L, 'E', '1.120000E+08'),
  1025. (112300000L, 'e', '1.123000e+08'),
  1026. (112300000L, 'E', '1.123000E+08'),
  1027. (112340000L, 'e', '1.123400e+08'),
  1028. (112340000L, 'E', '1.123400E+08'),
  1029. (112345000L, 'e', '1.123450e+08'),
  1030. (112345000L, 'E', '1.123450E+08'),
  1031. (1112345600L, 'e', '1.112346e+09'),
  1032. (1112345600L, 'E', '1.112346E+09'),
  1033. (1112345500L, 'e', '1.112346e+09'),
  1034. (1112345500L, 'E', '1.112346E+09'),
  1035. (1112345510L, 'e', '1.112346e+09'),
  1036. (1112345510L, 'E', '1.112346E+09'),
  1037. (1112345400L, 'e', '1.112345e+09'),
  1038. (1112345400L, 'E', '1.112345E+09'),
  1039. (1112345401L, 'e', '1.112345e+09'),
  1040. (1112345401L, 'E', '1.112345E+09'),
  1041. (111234540100L, 'E', '1.112345E+11'),
  1042. (100000L, 'n', '100000'),
  1043. ]
  1044. for value, spec, result in tests:
  1045. AreEqual(value.__format__(spec), result)
  1046. # check locale specific formatting
  1047. import _locale
  1048. try:
  1049. if is_cli:
  1050. _locale.setlocale(_locale.LC_ALL, 'en_US')
  1051. else:
  1052. _locale.setlocale(_locale.LC_ALL, 'English_United States.1252')
  1053. AreEqual(100000L.__format__('n'), '100,000')
  1054. AreEqual(100000000L.__format__('n'), '100,000,000')
  1055. finally:
  1056. # and restore it back...
  1057. _locale.setlocale(_locale.LC_ALL, 'C')
  1058. AreEqual(100000L.__format__('n'), '100000')
  1059. AreEqual(100000000L.__format__('n'), '100000000')
  1060. @skip("posix") # mono doesn't support some of the formatting specifiers https://github.com/IronLanguages/main/issues/1598
  1061. def test_long___format___errors():
  1062. errors = [
  1063. (ValueError, 2L, '6.1', "Precision not allowed in integer format specifier"),
  1064. (ValueError, 2L, '+c', "Sign not allowed with integer format specifier 'c'"),
  1065. (ValueError, 2L, '-c', "Sign not allowed with integer format specifier 'c'"),
  1066. (ValueError, 2L, ' c', "Sign not allowed with integer format specifier 'c'"),
  1067. (OverflowError, -2L, 'c', "%c arg not in range(0x10000)"),
  1068. (OverflowError, 1000000L, 'c', "%c arg not in range(0x10000)"),
  1069. ]
  1070. if not is_cpython: #http://ironpython.codeplex.com/workitem/28373
  1071. errors.append((OverflowError, sys.maxint + 1, 'c', "long int too large to convert to int"))
  1072. else:
  1073. errors.append((OverflowError, sys.maxint + 1, 'c', "Python int too large to convert to C long"))
  1074. okChars = set(['%', 'E', 'F', 'G', 'X', 'x', 'b', 'c', 'd', 'o', 'e', 'f', 'g', 'n', ','])
  1075. # verify the okChars are actually ok
  1076. for char in okChars:
  1077. (2L).__format__('10' + char)
  1078. for char in allChars:
  1079. if char not in okChars and (char < '0' or char > '9'):
  1080. errors.append((ValueError, 2L, '10' + char, "Unknown format code '%s'" % char))
  1081. for exceptionType, value, errorFmt, errorMsg in errors:
  1082. AssertErrorWithPartialMessage(exceptionType, errorMsg, value.__format__, errorFmt)
  1083. def test_builtin_types_that_implement_format():
  1084. import __builtin__
  1085. types = [getattr(__builtin__, typeName) for typeName in dir(__builtin__) if type(getattr(__builtin__, typeName)) is type]
  1086. formatTypes = list(set([builtinType.__name__ for builtinType in types if '__format__' in builtinType.__dict__]))
  1087. formatTypes.sort()
  1088. if is_cli:
  1089. # no unicode
  1090. # why does bool have __format__ in ipy?
  1091. AreEqual(formatTypes, ['bool', 'complex', 'float', 'int', 'long', 'object', 'str'])
  1092. else:
  1093. AreEqual(formatTypes, ['complex', 'float', 'int', 'long', 'object', 'str', 'unicode'])
  1094. def test_computed_format():
  1095. AreEqual("|{0:10}|".format("a"), "|a |")
  1096. AreEqual("|{0:*^10}|".format("a"), "|****a*****|")
  1097. AreEqual("|{0:*^{1}}|".format("a", 10), "|****a*****|")
  1098. AreEqual("{0:*{2}10}".format("a", "*", "^", "10"), "****a*****")
  1099. AreEqual("{0:{1}^{3}}".format("a", "*", "^", "10"), "****a*****")
  1100. AreEqual("{0:{1}{2}{3}}".format("a", "*", "^", "10"), "****a*****")
  1101. AreEqual("{0:{1}*^{2}}".format("a", "", "10"), "****a*****")
  1102. def test_none_format():
  1103. AreEqual("{0} {1}".format(None, 10), "None 10")
  1104. AreEqual("{0}".format(None), 'None')
  1105. run_test(__name__)