-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Add tests for chr(), ord(), encode() and decode() #10914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
1610611
2b178f7
2028e63
6f3dc2e
9a3a6bd
eae807e
dc2559c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Test cases for strings (compile and run) | ||
|
||
[case testStr] | ||
[case testStrBasics] | ||
from typing import Tuple | ||
def f() -> str: | ||
return 'some string' | ||
|
@@ -511,3 +511,69 @@ def test_format_method_python_doc() -> None: | |
' 9 9 11 1001',\ | ||
' 10 A 12 1010',\ | ||
' 11 B 13 1011'] | ||
|
||
[case testUnicodeEncodeDecode] | ||
# Some test cases are from https://docs.python.org/3/howto/unicode.html | ||
|
||
def test_chr() -> None: | ||
assert chr(57344) == '\ue000' | ||
assert chr(0) == '\x00' | ||
assert chr(65) == 'A' | ||
assert chr(150) == '\x96' | ||
try: | ||
chr(-1) | ||
assert False | ||
except ValueError: | ||
pass | ||
try: | ||
chr(1114112) | ||
assert False | ||
except ValueError: | ||
pass | ||
assert chr(1114111) == '\U0010ffff' | ||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x = 100 | ||
assert chr(x + int()) == 'd' | ||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_ord() -> None: | ||
assert ord('\ue000') == 57344 | ||
s = "a\xac\u1234\u20ac\U00008000" | ||
# ^^^^ two-digit hex escape | ||
# ^^^^^^ four-digit Unicode escape | ||
# ^^^^^^^^^^ eight-digit Unicode escape | ||
l1 = [ord(c) for c in s] | ||
assert l1 == [97, 172, 4660, 8364, 32768] | ||
u = 'abcdé' | ||
assert ord(u[-1]) == 233 | ||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert ord(b'a') == 97 | ||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert ord(b'a' + bytes()) == 97 | ||
u2 = '\U0010ffff' | ||
assert ord(u2) == 1114111 | ||
try: | ||
ord('aa') | ||
assert False | ||
except TypeError: | ||
pass | ||
|
||
def test_decode() -> None: | ||
assert "\N{GREEK CAPITAL LETTER DELTA}" == '\u0394' | ||
assert "\u0394" == "\u0394" | ||
assert "\U00000394" == '\u0394' | ||
assert b'\x80abc'.decode("utf-8", "replace") == '\ufffdabc' | ||
assert b'\x80abc'.decode("utf-8", "backslashreplace") == '\\x80abc' | ||
assert b'\x80abc'.decode("utf-8", "ignore") == 'abc' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few more ideas: Test decode exception, test |
||
|
||
def test_encode() -> None: | ||
u = chr(40960) + 'abcd' + chr(1972) | ||
assert u.encode() == b'\xea\x80\x80abcd\xde\xb4' | ||
assert u.encode('utf-8') == b'\xea\x80\x80abcd\xde\xb4' | ||
try: | ||
u.encode('ascii') | ||
assert False | ||
except UnicodeEncodeError: | ||
pass | ||
assert u.encode('ascii', 'ignore') == b'abcd' | ||
assert u.encode('ascii', 'replace') == b'?abcd?' | ||
assert u.encode('ascii', 'xmlcharrefreplace') == b'ꀀabcd޴' | ||
assert u.encode('ascii', 'backslashreplace') == b'\\ua000abcd\\u07b4' | ||
assert u.encode('ascii', 'namereplace') == b'\\N{YI SYLLABLE IT}abcd\\u07b4' | ||
assert 'pythön!'.encode() == b'pyth\xc3\xb6n!' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test |
Uh oh!
There was an error while loading. Please reload this page.