|
| 1 | +import pytest |
| 2 | +from selfie_lib.TypedPath import TypedPath |
| 3 | + |
| 4 | + |
| 5 | +def test_initialization(): |
| 6 | + path = TypedPath("/home/user/") |
| 7 | + assert path.absolute_path == "/home/user/" |
| 8 | + assert path.is_folder |
| 9 | + assert path.name == "user" |
| 10 | + |
| 11 | + |
| 12 | +def test_parent_folder(): |
| 13 | + path = TypedPath("/home/user/documents/") |
| 14 | + parent = path.parent_folder() |
| 15 | + assert isinstance(parent, TypedPath) |
| 16 | + assert parent.absolute_path == "/home/user/" |
| 17 | + |
| 18 | + |
| 19 | +def test_resolve_file(): |
| 20 | + folder = TypedPath("/home/user/") |
| 21 | + file = folder.resolve_file("document.txt") |
| 22 | + assert file.absolute_path == "/home/user/document.txt" |
| 23 | + assert not file.is_folder |
| 24 | + assert file.name == "document.txt" |
| 25 | + |
| 26 | + |
| 27 | +def test_resolve_folder(): |
| 28 | + folder = TypedPath("/home/user/") |
| 29 | + subfolder = folder.resolve_folder("documents") |
| 30 | + assert subfolder.absolute_path == "/home/user/documents/" |
| 31 | + assert subfolder.is_folder |
| 32 | + assert subfolder.name == "documents" |
| 33 | + |
| 34 | + |
| 35 | +def test_relativize(): |
| 36 | + folder = TypedPath("/home/user/") |
| 37 | + file = TypedPath("/home/user/document.txt") |
| 38 | + relative_path = folder.relativize(file) |
| 39 | + assert relative_path == "document.txt" |
| 40 | + |
| 41 | + |
| 42 | +def test_of_folder_class_method(): |
| 43 | + folder = TypedPath.of_folder("/home/user/documents") |
| 44 | + assert folder.absolute_path == "/home/user/documents/" |
| 45 | + assert folder.is_folder |
| 46 | + |
| 47 | + |
| 48 | +def test_of_file_class_method(): |
| 49 | + file = TypedPath.of_file("/home/user/document.txt") |
| 50 | + assert file.absolute_path == "/home/user/document.txt" |
| 51 | + assert not file.is_folder |
| 52 | + |
| 53 | + |
| 54 | +def test_assert_folder_failure(): |
| 55 | + with pytest.raises(AssertionError): |
| 56 | + file = TypedPath("/home/user/document.txt") |
| 57 | + file.assert_folder() |
| 58 | + |
| 59 | + |
| 60 | +def test_parent_folder_failure(): |
| 61 | + with pytest.raises(ValueError): |
| 62 | + path = TypedPath("/") |
| 63 | + path.parent_folder() |
| 64 | + |
| 65 | + |
| 66 | +def test_equality(): |
| 67 | + path1 = TypedPath("/home/user/") |
| 68 | + path2 = TypedPath("/home/user/") |
| 69 | + assert path1 == path2 |
| 70 | + |
| 71 | + |
| 72 | +def test_inequality(): |
| 73 | + path1 = TypedPath("/home/user/") |
| 74 | + path2 = TypedPath("/home/another_user/") |
| 75 | + assert path1 != path2 |
| 76 | + |
| 77 | + |
| 78 | +def test_ordering(): |
| 79 | + path1 = TypedPath("/home/a/") |
| 80 | + path2 = TypedPath("/home/b/") |
| 81 | + assert path1 < path2 |
| 82 | + assert path2 > path1 |
| 83 | + |
| 84 | + |
| 85 | +def test_relativize_error(): |
| 86 | + parent = TypedPath("/home/user/") |
| 87 | + child = TypedPath("/home/another_user/document.txt") |
| 88 | + with pytest.raises(ValueError): |
| 89 | + parent.relativize(child) |
0 commit comments