148 lines
4.7 KiB
Python
148 lines
4.7 KiB
Python
import pytest
|
|
import os
|
|
import sys
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from slic.functions.math_utils import *
|
|
import math
|
|
import importlib
|
|
from unittest import mock
|
|
|
|
class CustomError(Exception):
|
|
pass
|
|
|
|
def test_broken():
|
|
# simulating a broken or faulty test implementation that will cause the test to error
|
|
want_the_test_to_fail
|
|
|
|
def test_call_missing_function():
|
|
# Accessing a missing function attribute raises AttributeError
|
|
with pytest.raises(AttributeError):
|
|
getattr(math_utils, "non_existent_function")()
|
|
|
|
def test_addition_pass():
|
|
# Test passes: correct addition
|
|
assert math_utils.addition(2, 2) == 4
|
|
|
|
def test_addition_fail():
|
|
# Assertion failure: expected incorrect result
|
|
assert math_utils.addition(2, 2) == 5
|
|
|
|
def test_division_zero():
|
|
# Expect ZeroDivisionError when dividing by zero
|
|
with pytest.raises(ZeroDivisionError):
|
|
math_utils.division(1, 0)
|
|
|
|
@pytest.mark.xfail(reason="Expected failure")
|
|
def test_multiply_xfail():
|
|
# Expected fail test (xfail): incorrect expected multiply result
|
|
assert math_utils.multiply(2, 2) == 5
|
|
|
|
def test_runtime_error():
|
|
# Test raises an uncaught RuntimeError
|
|
raise RuntimeError("Forced runtime error")
|
|
|
|
def test_memory_error():
|
|
# Manually raise MemoryError to simulate out-of-memory condition
|
|
with pytest.raises(MemoryError):
|
|
raise MemoryError("Simulated memory error")
|
|
|
|
def test_timeout_error():
|
|
# Manually raise TimeoutError simulating timeout conditions
|
|
with pytest.raises(TimeoutError):
|
|
raise TimeoutError("Simulated timeout error")
|
|
|
|
def test_recursion_error():
|
|
# Infinite recursion triggers RecursionError
|
|
def recursive():
|
|
return recursive()
|
|
with pytest.raises(RecursionError):
|
|
recursive()
|
|
|
|
def test_floating_point_error():
|
|
# Manually raise FloatingPointError (rare in practice)
|
|
with pytest.raises(FloatingPointError):
|
|
raise FloatingPointError("Simulated floating point error")
|
|
|
|
def test_floating_point_overflow():
|
|
# Exponential overflow triggers OverflowError
|
|
with pytest.raises(OverflowError):
|
|
math.exp(1000)
|
|
|
|
def test_value_error():
|
|
# ValueError on invalid integer conversion
|
|
with pytest.raises(ValueError):
|
|
int("invalid")
|
|
|
|
def test_type_error():
|
|
# TypeError when passing wrong argument type to sum
|
|
with pytest.raises(TypeError):
|
|
sum(5)
|
|
|
|
def test_unhandled_exception():
|
|
# Raises generic unhandled Exception
|
|
raise Exception("Generic unhandled exception")
|
|
|
|
def test_custom_error():
|
|
# Raises user-defined CustomError exception
|
|
with pytest.raises(CustomError):
|
|
raise CustomError("Custom error simulation")
|
|
|
|
def test_stop_iteration_direct():
|
|
# Directly raise StopIteration exception
|
|
raise StopIteration()
|
|
|
|
def test_generator_exit_direct():
|
|
# Directly raise GeneratorExit exception
|
|
raise GeneratorExit()
|
|
|
|
def test_keyboard_interrupt_direct():
|
|
# Directly raise KeyboardInterrupt exception
|
|
raise KeyboardInterrupt()
|
|
|
|
def test_recursion_limit(monkeypatch):
|
|
# Lower recursion limit to force RecursionError on deep recursion
|
|
original_limit = sys.getrecursionlimit()
|
|
sys.setrecursionlimit(50)
|
|
def recurse():
|
|
return recurse()
|
|
with pytest.raises(RecursionError):
|
|
recurse()
|
|
sys.setrecursionlimit(original_limit)
|
|
|
|
def test_malformed_code():
|
|
# SyntaxError when executing malformed Python code
|
|
with pytest.raises(SyntaxError):
|
|
exec("def bad(:\n pass")
|
|
|
|
def test_sys_exit(monkeypatch):
|
|
# Simulate SystemExit via patched sys.exit
|
|
def fake_exit(code=0):
|
|
raise SystemExit(f"Exit with code {code}")
|
|
monkeypatch.setattr(sys, "exit", fake_exit)
|
|
with pytest.raises(SystemExit):
|
|
sys.exit(1)
|
|
|
|
def test_broken_function(monkeypatch):
|
|
# Simulate broken function raising TypeError
|
|
def broken_func(*args, **kwargs):
|
|
raise TypeError("Broken function")
|
|
monkeypatch.setattr(__name__, "test_broken_function", broken_func)
|
|
with pytest.raises(TypeError):
|
|
broken_func()
|
|
|
|
def test_import_error_patch(monkeypatch):
|
|
# Patch import to simulate ImportError on specific module
|
|
original_import = __import__
|
|
def fake_import(name, *args, **kwargs):
|
|
if name == "fake_module":
|
|
raise ImportError("Simulated ImportError")
|
|
return original_import(name, *args, **kwargs)
|
|
monkeypatch.setattr("builtins.__import__", fake_import)
|
|
with pytest.raises(ImportError):
|
|
__import__("fake_module")
|
|
|
|
def test_module_not_found_error():
|
|
# Raises ModuleNotFoundError (subclass of ImportError) for missing module
|
|
with pytest.raises(ModuleNotFoundError):
|
|
importlib.import_module("non_existent_module_xyz")
|