Skip to content

Commit 25afc7d

Browse files
committed
tests: Add tests to improve coverage of objstr.c.
1 parent e2aa117 commit 25afc7d

13 files changed

Lines changed: 81 additions & 0 deletions

tests/basics/builtin_ord.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@
2020
ord(b'')
2121
except TypeError:
2222
print("TypeError")
23+
24+
# argument must be a string
25+
try:
26+
ord(1)
27+
except TypeError:
28+
print('TypeError')

tests/basics/bytes_construct.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
# long ints
1515
print(ord(bytes([14953042807679334000 & 0xff])))
1616

17+
# constructor value out of range
18+
try:
19+
bytes([-1])
20+
except ValueError:
21+
print('ValueError')
22+
23+
# constructor value out of range
24+
try:
25+
bytes([256])
26+
except ValueError:
27+
print('ValueError')
28+
1729
# error in construction
1830
try:
1931
a = bytes([1, 2, 3], 1)

tests/basics/setattr1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ def __init__(self):
1111
setattr(a, "var2", 56)
1212
print(a.var)
1313
print(a.var2)
14+
15+
try:
16+
setattr(a, b'var3', 1)
17+
except TypeError:
18+
print('TypeError')

tests/basics/string1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
'123' * '1'
2525
except TypeError:
2626
print('TypeError')
27+
try:
28+
'123' + 1
29+
except TypeError:
30+
print('TypeError')
2731

2832
# subscription
2933
print('abc'[1])

tests/basics/string_compare.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@
4848
print("1" <= "1/")
4949
print("10" <= "1")
5050
print("1/" <= "1")
51+
52+
# this tests an internal string that doesn't have a hash with a string
53+
# that does have a hash, but the lengths of the two strings are different
54+
import sys
55+
print(sys.version == 'a long string that has a hash')

tests/basics/string_format.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,9 @@ def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
207207
'{:*"1"}'.format('zz')
208208
except ValueError:
209209
print('ValueError')
210+
211+
# unknown format code for str arg
212+
try:
213+
'{:X}'.format('zz')
214+
except ValueError:
215+
print('ValueError')

tests/basics/struct1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@
5252

5353
# network byte order
5454
print(struct.pack('!i', 123))
55+
56+
# first arg must be a string
57+
try:
58+
struct.pack(1, 2)
59+
except TypeError:
60+
print('TypeError')

tests/extmod/ujson_dumps_extra.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# test uPy ujson behaviour that's not valid in CPy
2+
3+
import ujson
4+
5+
print(ujson.dumps(b'1234'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"1234"

tests/misc/non_compliant.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,26 @@
4040
except NotImplementedError:
4141
print('NotImplementedError')
4242

43+
# str(...) with keywords not implemented
44+
try:
45+
str(b'abc', encoding='utf8')
46+
except NotImplementedError:
47+
print('NotImplementedError')
48+
4349
# str.rsplit(None, n) not implemented
4450
try:
4551
'a a a'.rsplit(None, 1)
4652
except NotImplementedError:
4753
print('NotImplementedError')
54+
55+
# bytes(...) with keywords not implemented
56+
try:
57+
bytes('abc', encoding='utf8')
58+
except NotImplementedError:
59+
print('NotImplementedError')
60+
61+
# bytes subscr with step!=1 not implemented
62+
try:
63+
b'123'[0:3:2]
64+
except NotImplementedError:
65+
print('NotImplementedError')

0 commit comments

Comments
 (0)