|
1 |
| -"""Tests for the main Cobhan module""" |
| 1 | +# pylint: disable=missing-function-docstring,missing-class-docstring,missing-module-docstring |
2 | 2 |
|
3 |
| -from unittest import TestCase |
| 3 | +from pathlib import Path |
| 4 | +from unittest import mock, TestCase |
4 | 5 |
|
| 6 | +from cobhan import Cobhan |
5 | 7 |
|
6 |
| -class CobhanTest(TestCase): |
7 |
| - """Tests for the main Cobhan class""" |
8 | 8 |
|
9 |
| - def test_pass(self): |
10 |
| - """Fake test to start off""" |
11 |
| - self.assertEqual(True, True) |
| 9 | +class LoadLibraryTests(TestCase): |
| 10 | + """Tests for Cobhan.load_library""" |
| 11 | + |
| 12 | + def setUp(self) -> None: |
| 13 | + self.ffi_patcher = mock.patch("cobhan.cobhan.FFI") |
| 14 | + self.platform_patcher = mock.patch("cobhan.cobhan.platform") |
| 15 | + |
| 16 | + self.mock_ffi = self.ffi_patcher.start() |
| 17 | + self.mock_dlopen = self.mock_ffi.return_value.dlopen |
| 18 | + self.mock_platform = self.platform_patcher.start() |
| 19 | + |
| 20 | + self.addCleanup(self.ffi_patcher.stop) |
| 21 | + self.addCleanup(self.platform_patcher.stop) |
| 22 | + |
| 23 | + self.cobhan = Cobhan() |
| 24 | + return super().setUp() |
| 25 | + |
| 26 | + def test_load_linux_x64(self): |
| 27 | + self.mock_platform.system.return_value = "Linux" |
| 28 | + self.mock_platform.machine.return_value = "x86_64" |
| 29 | + |
| 30 | + self.cobhan.load_library("libfoo", "libbar", "") |
| 31 | + self.mock_dlopen.assert_called_once_with( |
| 32 | + str(Path("libfoo/libbar-x64.so").resolve()) |
| 33 | + ) |
| 34 | + |
| 35 | + def test_load_linux_arm64(self): |
| 36 | + self.mock_platform.system.return_value = "Linux" |
| 37 | + self.mock_platform.machine.return_value = "arm64" |
| 38 | + |
| 39 | + self.cobhan.load_library("libfoo", "libbar", "") |
| 40 | + self.mock_dlopen.assert_called_once_with( |
| 41 | + str(Path("libfoo/libbar-arm64.so").resolve()) |
| 42 | + ) |
| 43 | + |
| 44 | + def test_load_macos_x64(self): |
| 45 | + self.mock_platform.system.return_value = "Darwin" |
| 46 | + self.mock_platform.machine.return_value = "x86_64" |
| 47 | + |
| 48 | + self.cobhan.load_library("libfoo", "libbar", "") |
| 49 | + self.mock_dlopen.assert_called_once_with( |
| 50 | + str(Path("libfoo/libbar-x64.dylib").resolve()) |
| 51 | + ) |
| 52 | + |
| 53 | + def test_load_macos_arm64(self): |
| 54 | + self.mock_platform.system.return_value = "Darwin" |
| 55 | + self.mock_platform.machine.return_value = "arm64" |
| 56 | + |
| 57 | + self.cobhan.load_library("libfoo", "libbar", "") |
| 58 | + self.mock_dlopen.assert_called_once_with( |
| 59 | + str(Path("libfoo/libbar-arm64.dylib").resolve()) |
| 60 | + ) |
| 61 | + |
| 62 | + def test_load_windows_x64(self): |
| 63 | + self.mock_platform.system.return_value = "Windows" |
| 64 | + self.mock_platform.machine.return_value = "x86_64" |
| 65 | + |
| 66 | + self.cobhan.load_library("libfoo", "libbar", "") |
| 67 | + self.mock_dlopen.assert_called_once_with( |
| 68 | + str(Path("libfoo/libbar-x64.dll").resolve()) |
| 69 | + ) |
| 70 | + |
| 71 | + def test_load_windows_arm64(self): |
| 72 | + self.mock_platform.system.return_value = "Windows" |
| 73 | + self.mock_platform.machine.return_value = "arm64" |
| 74 | + |
| 75 | + self.cobhan.load_library("libfoo", "libbar", "") |
| 76 | + self.mock_dlopen.assert_called_once_with( |
| 77 | + str(Path("libfoo/libbar-arm64.dll").resolve()) |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +class StringTests(TestCase): |
| 82 | + def setUp(self) -> None: |
| 83 | + |
| 84 | + self.cobhan = Cobhan() |
| 85 | + return super().setUp() |
| 86 | + |
| 87 | + def test_minimum_allocation_is_enforced(self): |
| 88 | + buf = self.cobhan.str_to_buf("foo") |
| 89 | + self.assertEqual( |
| 90 | + len(buf), (self.cobhan.minimum_allocation + self.cobhan.header_size) |
| 91 | + ) |
| 92 | + |
| 93 | + def test_can_allocate_beyond_minimum(self): |
| 94 | + long_str = "foobar" * 1000 # This will be 6k characters in length |
| 95 | + buf = self.cobhan.str_to_buf(long_str) |
| 96 | + self.assertEqual(len(buf), (len(long_str) + self.cobhan.header_size)) |
| 97 | + |
| 98 | + def test_two_way_conversion_maintains_string(self): |
| 99 | + buf = self.cobhan.str_to_buf("foobar") |
| 100 | + result = self.cobhan.buf_to_str(buf) |
| 101 | + self.assertEqual(result, "foobar") |
| 102 | + |
| 103 | + def test_empty_string_returns_empty_buffer(self): |
| 104 | + buf = self.cobhan.str_to_buf("") |
| 105 | + self.assertEqual(len(buf), self.cobhan.header_size) |
| 106 | + |
| 107 | + def test_input_of_none_returns_empty_buffer(self): |
| 108 | + buf = self.cobhan.str_to_buf(None) |
| 109 | + self.assertEqual(len(buf), self.cobhan.header_size) |
0 commit comments