diff --git a/tests/test_ytils_utils.py b/tests/test_ytils_utils.py new file mode 100644 index 000000000..c10f1dcd3 --- /dev/null +++ b/tests/test_ytils_utils.py @@ -0,0 +1,85 @@ +import pytest +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +from slic.utils.utils import * + + +# Test singleton() + +@singleton +class test_singleton_class: + def __init__(self): + self.value = 123 + + def get(self): + return self.value + +def test_singleton_instance(): + # To verify that it's not a class but an instance, and that the instance has a 'get' method + assert isinstance(test_singleton_class, object) + assert hasattr(test_singleton_class, "get") + + assert test_singleton_class.get() == 123 + assert test_singleton_class.value == 123 + +def test_singleton_identity(): + # Re-accessing the singleton returns same instance + ref = test_singleton_class + assert ref is test_singleton_class + +# Test: typename() + +@pytest.mark.parametrize("obj,expected", [ + (42, "int"), + ("hello", "str"), + (test_singleton_class, "test_singleton_class"), +]) +def test_typename(obj, expected): + assert typename(obj) == expected + +# Test: next_int() + +@pytest.mark.parametrize("nums,expected", [ + ([1, 2, 3], 4), + ([10, 20], 21), + ([], 0), +]) +def test_next_int(nums, expected): + assert next_int(nums) == expected + +# Test zero_pad() + +@pytest.mark.parametrize("i,n,expected", [ + (7, 3, "007"), + (123, 5, "00123"), + (0, 2, "00"), +]) +def test_zero_pad(i, n, expected): + assert zero_pad(i, n) == expected + +# Test iround() + +@pytest.mark.parametrize("val,expected", [ + (3.6, 4), + (2.1, 2), + (-1.5, -2), + (-1.4, -1), +]) +def test_iround(val, expected): + assert iround(val) == expected + +# Test: sorted_naturally + +@pytest.mark.parametrize("items,expected", [ + (["file1", "file10", "file2"], ["file1", "file2", "file10"]), + (["z9", "z10", "z2", "z1"], ["z1", "z2", "z9", "z10"]), +]) +def test_sorted_naturally(items, expected): + assert sorted_naturally(items) == expected + +@pytest.mark.parametrize("items,expected", [ + (["file1", "file10", "file2"], ["file10", "file2", "file1"]), +]) +def test_sorted_naturally_reverse(items, expected): + assert sorted_naturally(items, reverse=True) == expected