51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import pytest
|
|
import os
|
|
import sys
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from functions.string_utils import *
|
|
import warnings
|
|
|
|
def test_uppercase_normal():
|
|
# Normal string uppercase conversion
|
|
assert uppercase("hello") == "HELLO"
|
|
|
|
def test_uppercase_type_error():
|
|
# Raises TypeError when input is None (invalid input)
|
|
with pytest.raises(TypeError):
|
|
uppercase(None)
|
|
|
|
def test_reverse_string():
|
|
# Tests string reversal correctness
|
|
assert reverse("abc") == "cba"
|
|
|
|
def test_warning_emit():
|
|
# Emits a Python UserWarning, checks warning capture
|
|
warnings.warn("Test warning", UserWarning)
|
|
|
|
def test_unicode_encode_error():
|
|
# UnicodeEncodeError due to decoding malformed surrogate byte
|
|
with pytest.raises(UnicodeEncodeError):
|
|
b'\udc80'.decode('utf-8')
|
|
|
|
def test_unicode_decode_error():
|
|
# UnicodeDecodeError when decoding invalid byte sequence
|
|
with pytest.raises(UnicodeDecodeError):
|
|
b'\xff'.decode('utf-8')
|
|
|
|
def test_unicode_decode_surrogateescape():
|
|
# UnicodeDecodeError with strict error handler on invalid byte
|
|
with pytest.raises(UnicodeDecodeError):
|
|
b"\x80".decode("utf-8", errors="strict")
|
|
|
|
def test_import_warning():
|
|
# Capture and check ImportWarning emitted
|
|
with warnings.catch_warnings(record=True) as w:
|
|
warnings.simplefilter("always")
|
|
warnings.warn("Import warning", ImportWarning)
|
|
assert any(item.category == ImportWarning for item in w)
|
|
|
|
@pytest.mark.xfail(reason="Expected failure: uppercase does not handle digits")
|
|
def test_xfail_uppercase_digits():
|
|
# Expected fail test because uppercase won't change digits
|
|
assert uppercase("abc123") == "ABC1234"
|