121 lines
2.8 KiB
Python
121 lines
2.8 KiB
Python
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()
|
|
|
|
import pytest
|
|
import types
|
|
import math
|
|
import sys
|
|
|
|
# Une classe un peu plus complexe
|
|
|
|
def sample_function(x):
|
|
def nested(y):
|
|
return y + x
|
|
return nested
|
|
|
|
@pytest.mark.parametrize("obj, expected", [
|
|
|
|
(None, "NoneType"),
|
|
(True, "bool"),
|
|
(42, "int"),
|
|
(3.14, "float"),
|
|
("text", "str"),
|
|
({1, 2, 3}, "set"),
|
|
([[1], [2, 3]], "list"),
|
|
([{"a": [1, 2]}, {"b": (3, 4)}], "list"),
|
|
({"key": [{"nested": 1}, (2, 3)]}, "dict"),
|
|
|
|
# Fonctions
|
|
(lambda x: x, "function"),
|
|
(sample_function, "function"),
|
|
(sample_function(10), "function"),
|
|
(len, "builtin_function_or_method"),
|
|
(sum, "builtin_function_or_method"),
|
|
|
|
# Modules
|
|
(math, "module"),
|
|
(sys, "module"),
|
|
(types.FunctionType, "type"),
|
|
((i for i in range(3)), "generator"),
|
|
(iter([1, 2, 3]), "list_iterator"),
|
|
])
|
|
|
|
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
|