summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-01 10:48:58 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-01 10:48:58 +0100
commit46330bd9b52bdd8865be62c1f6c534ef7709b842 (patch)
treedc531b159f6e5d3c3940e5fad752df9692e4ec60 /tests
parente44d26ae0c1b5d248fa4db112cdeabe404944f3c (diff)
parentbaf6f14deb567ab626c1b05213af346108f41700 (diff)
downloadmicropython-46330bd9b52bdd8865be62c1f6c534ef7709b842.tar.gz
micropython-46330bd9b52bdd8865be62c1f6c534ef7709b842.zip
Merge pull request #407 from dhylands/str-format
Enhance str.format support
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/string-format.py146
1 files changed, 138 insertions, 8 deletions
diff --git a/tests/basics/string-format.py b/tests/basics/string-format.py
index ba51e0890b..0a9764bf25 100644
--- a/tests/basics/string-format.py
+++ b/tests/basics/string-format.py
@@ -1,8 +1,138 @@
-print("{}-{}".format(1, [4, 5]))
-print("{0}-{1}".format(1, [4, 5]))
-print("{:x}".format(1))
-print("{!r}".format(2))
-# TODO
-#print("{1}-{0}".format(1, [4, 5]))
-#print("{:x}".format(0x10))
-#print("{!r}".format("foo"))
+def test(fmt, *args):
+ print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<')
+
+test("{}-{}", 1, [4, 5])
+test("{0}-{1}", 1, [4, 5])
+test("{1}-{0}", 1, [4, 5])
+test("{:x}", 1)
+test("{!r}", 2)
+test("{1}-{0}", 1, [4, 5])
+test("{:x}", 0x10)
+test("{!r}", "foo")
+test("{!s}", "foo")
+
+def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
+ fmt = '{'
+ if conv:
+ fmt += '!'
+ fmt += conv
+ fmt += ':'
+ if alignment:
+ fmt += fill
+ fmt += alignment
+ fmt += sign
+ fmt += prefix
+ fmt += width
+ if precision:
+ fmt += '.'
+ fmt += precision
+ fmt += type
+ fmt += '}'
+ test(fmt, arg)
+ if fill == '0' and alignment == '=':
+ fmt = '{:'
+ fmt += sign
+ fmt += prefix
+ fmt += width
+ if precision:
+ fmt += '.'
+ fmt += precision
+ fmt += type
+ fmt += '}'
+ test(fmt, arg)
+
+int_nums = (-1234, -123, -12, -1, 0, 1, 12, 123, 1234, True, False)
+int_nums2 = (-12, -1, 0, 1, 12, True, False)
+
+if True:
+ for type in ('', 'b', 'd', 'o', 'x', 'X'):
+ for width in ('', '1', '3', '5', '7'):
+ for alignment in ('', '<', '>', '=', '^'):
+ for fill in ('', ' ', '0', '@'):
+ for sign in ('', '+', '-', ' '):
+ for prefix in ('', '#'):
+ for num in int_nums:
+ test_fmt('', fill, alignment, sign, prefix, width, '', type, num)
+
+if True:
+ for width in ('', '1', '2'):
+ for alignment in ('', '<', '>', '^'):
+ for fill in ('', ' ', '0', '@'):
+ test_fmt('', fill, alignment, '', '', width, '', 'c', 48)
+
+if True:
+ for conv in ('', 'r', 's'):
+ for width in ('', '1', '4', '10'):
+ for alignment in ('', '<', '>', '^'):
+ for fill in ('', ' ', '0', '@'):
+ for str in ('', 'a', 'bcd', 'This is a test with a longer string'):
+ test_fmt(conv, fill, alignment, '', '', width, '', 's', str)
+
+eg_nums = (0.0, -0.0, 0.1, 1.234, 12.3459, 1.23456789, 123456789.0, -0.0,
+ -0.1, -1.234, -12.3459, 1e4, 1e-4, 1e5, 1e-5, 1e6, 1e-6, 1e10,
+ 1e37, -1e37, 1e-37, -1e-37,
+ 1.23456e8, 1.23456e7, 1.23456e6, 1.23456e5, 1.23456e4, 1.23456e3, 1.23456e2, 1.23456e1, 1.23456e0,
+ 1.23456e-1, 1.23456e-2, 1.23456e-3, 1.23456e-4, 1.23456e-5, 1.23456e-6, 1.23456e-7, 1.23456e-8,
+ -1.23456e8, -1.23456e7, -1.23456e6, -1.23456e5, -1.23456e4, -1.23456e3, -1.23456e2, -1.23456e1, -1.23456e0,
+ -1.23456e-1, -1.23456e-2, -1.23456e-3, -1.23456e-4, -1.23456e-5, -1.23456e-6, -1.23456e-7, -1.23456e-8)
+
+if True:
+ for type in ('e', 'E', 'g', 'G', 'n'):
+ for width in ('', '4', '6', '8', '10'):
+ for alignment in ('', '<', '>', '=', '^'):
+ for fill in ('', '@', '0', ' '):
+ for sign in ('', '+', '-', ' '):
+ for prec in ('', '1', '3', '6'):
+ for num in eg_nums:
+ test_fmt('', fill, alignment, sign, '', width, prec, type, num)
+
+# Note: We use 1.23459 rather than 1.2345 because '{:3f}'.format(1.2345)
+# rounds differently than print("%.3f", 1.2345);
+
+f_nums = (0.0, -0.0, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0,
+ 0.0012, 0.0123, 0.1234, 1.23459, 12.3456,
+ -0.0001, -0.001, -0.01, -0.1, -1.0, -10.0,
+ -0.0012, -0.0123, -0.1234, -1.23459, -12.3456)
+
+if True:
+ for type in ('f', 'F'):
+ for width in ('', '4', '6', '8', '10'):
+ for alignment in ('', '<', '>', '=', '^'):
+ for fill in ('', ' ', '0', '@'):
+ for sign in ('', '+', '-', ' '):
+ # An empty precision defaults to 6, but when uPy is
+ # configured to use a float, we can only use a
+ # precision of 6 with numbers less than 10 and still
+ # get results that compare to CPython (which uses
+ # long doubles).
+ for prec in ('1', '2', '3'):
+ for num in f_nums:
+ test_fmt('', fill, alignment, sign, '', width, prec, type, num)
+ for num in int_nums2:
+ test_fmt('', fill, alignment, sign, '', width, '', type, num)
+
+pct_nums1 = (0.1, 0.58, 0.99, -0.1, -0.58, -0.99)
+pct_nums2 = (True, False, 1, 0, -1)
+
+if True:
+ type = '%'
+ for width in ('', '4', '6', '8', '10'):
+ for alignment in ('', '<', '>', '=', '^'):
+ for fill in ('', ' ', '0', '@'):
+ for sign in ('', '+', '-', ' '):
+ # An empty precision defaults to 6, but when uPy is
+ # configured to use a float, we can only use a
+ # precision of 6 with numbers less than 10 and still
+ # get results that compare to CPython (which uses
+ # long doubles).
+ for prec in ('1', '2', '3'):
+ for num in pct_nums1:
+ test_fmt('', fill, alignment, sign, '', width, prec, type, num)
+ for num in pct_nums2:
+ test_fmt('', fill, alignment, sign, '', width, '', type, num)
+
+# We don't currently test a type of '' with floats (see the detailed comment
+# in objstr.c)
+
+# TODO Add tests for erroneous format strings.
+