diff options
| author | Dave Hibberd <hibby@debian.org> | 2024-02-24 13:47:27 +0000 |
|---|---|---|
| committer | Dave Hibberd <hibby@debian.org> | 2024-02-24 13:47:27 +0000 |
| commit | fc438e6a84402e621877f6c30097442af46f122b (patch) | |
| tree | 7a6ca4af4159368f92d10c6c932c4b5e662715aa /tests/unit | |
| parent | 99bce70f3ca5cea0eb28f5158cfa00803cf6a19a (diff) | |
New upstream version 20240224upstream/20240224
Diffstat (limited to 'tests/unit')
| -rw-r--r-- | tests/unit/__pycache__/test_bitwise.cpython-310-pytest-7.1.3.pyc | bin | 13846 -> 15690 bytes | |||
| -rw-r--r-- | tests/unit/__pycache__/test_chirp_common.cpython-310-pytest-7.1.3.pyc | bin | 31324 -> 31358 bytes | |||
| -rw-r--r-- | tests/unit/__pycache__/test_csv.cpython-310-pytest-7.1.3.pyc | bin | 6717 -> 7624 bytes | |||
| -rw-r--r-- | tests/unit/__pycache__/test_logger.cpython-310-pytest-7.1.3.pyc | bin | 0 -> 1330 bytes | |||
| -rw-r--r-- | tests/unit/__pycache__/test_settings.cpython-310-pytest-7.1.3.pyc | bin | 7332 -> 7767 bytes | |||
| -rw-r--r-- | tests/unit/test_bitwise.py | 77 | ||||
| -rw-r--r-- | tests/unit/test_chirp_common.py | 4 | ||||
| -rw-r--r-- | tests/unit/test_csv.py | 18 | ||||
| -rw-r--r-- | tests/unit/test_logger.py | 38 | ||||
| -rw-r--r-- | tests/unit/test_settings.py | 16 |
10 files changed, 152 insertions, 1 deletions
diff --git a/tests/unit/__pycache__/test_bitwise.cpython-310-pytest-7.1.3.pyc b/tests/unit/__pycache__/test_bitwise.cpython-310-pytest-7.1.3.pyc Binary files differindex 398ad6b..3efe7a5 100644 --- a/tests/unit/__pycache__/test_bitwise.cpython-310-pytest-7.1.3.pyc +++ b/tests/unit/__pycache__/test_bitwise.cpython-310-pytest-7.1.3.pyc diff --git a/tests/unit/__pycache__/test_chirp_common.cpython-310-pytest-7.1.3.pyc b/tests/unit/__pycache__/test_chirp_common.cpython-310-pytest-7.1.3.pyc Binary files differindex 3334195..b47cb93 100644 --- a/tests/unit/__pycache__/test_chirp_common.cpython-310-pytest-7.1.3.pyc +++ b/tests/unit/__pycache__/test_chirp_common.cpython-310-pytest-7.1.3.pyc diff --git a/tests/unit/__pycache__/test_csv.cpython-310-pytest-7.1.3.pyc b/tests/unit/__pycache__/test_csv.cpython-310-pytest-7.1.3.pyc Binary files differindex b086c48..ef21a8e 100644 --- a/tests/unit/__pycache__/test_csv.cpython-310-pytest-7.1.3.pyc +++ b/tests/unit/__pycache__/test_csv.cpython-310-pytest-7.1.3.pyc diff --git a/tests/unit/__pycache__/test_logger.cpython-310-pytest-7.1.3.pyc b/tests/unit/__pycache__/test_logger.cpython-310-pytest-7.1.3.pyc Binary files differnew file mode 100644 index 0000000..b87c99b --- /dev/null +++ b/tests/unit/__pycache__/test_logger.cpython-310-pytest-7.1.3.pyc diff --git a/tests/unit/__pycache__/test_settings.cpython-310-pytest-7.1.3.pyc b/tests/unit/__pycache__/test_settings.cpython-310-pytest-7.1.3.pyc Binary files differindex fad2d83..09c95b4 100644 --- a/tests/unit/__pycache__/test_settings.cpython-310-pytest-7.1.3.pyc +++ b/tests/unit/__pycache__/test_settings.cpython-310-pytest-7.1.3.pyc diff --git a/tests/unit/test_bitwise.py b/tests/unit/test_bitwise.py index f150886..9f40dcc 100644 --- a/tests/unit/test_bitwise.py +++ b/tests/unit/test_bitwise.py @@ -16,6 +16,7 @@ from builtins import bytes import unittest +from unittest import mock import six @@ -97,6 +98,26 @@ class TestBitwiseBaseIntTypes(BaseTest): obj.foo[i] = i * 2 self.assertEqual(b'\x00\x02\x04\x06', data.get_packed()) + def test_int_array_set_raw(self): + data = memmap.MemoryMapBytes(bytes(b'\x00\x01\x02\x03')) + obj = bitwise.parse('u8 foo[4];', data) + obj.foo.set_raw(b'\x09\x08\x07\x06') + self.assertEqual(9, obj.foo[0]) + self.assertEqual(8, obj.foo[1]) + self.assertEqual(7, obj.foo[2]) + self.assertEqual(6, obj.foo[3]) + + obj = bitwise.parse('u16 foo[2];', data) + obj.foo.set_raw(b'\x00\x01\x00\x02') + self.assertEqual(1, obj.foo[0]) + self.assertEqual(2, obj.foo[1]) + + with self.assertRaises(AssertionError): + obj.foo.set_raw(b'123') + + with self.assertRaises(AssertionError): + obj.foo.set_raw(b'12345') + class TestBitfieldTypes(BaseTest): def test_bitfield_u8(self): @@ -302,6 +323,42 @@ class TestBitwiseStructTypes(BaseTest): obj.set_raw('2') self.assertEqual(b'2', data.get_packed()) + @mock.patch.object(bitwise.LOG, 'error') + def test_struct_duplicate(self, mock_log): + bitwise.parse('struct\n{ u8 foo; u8 foo1:2, foo:4, foo3:2;} bar;', + memmap.MemoryMapBytes(b'\x00' * 128)) + bitwise.parse('struct\n{ u8 foo; u8 foo;} bar;', + memmap.MemoryMapBytes(b'\x00' * 128)) + bitwise.parse('struct\n{ u8 foo; u8 foo[2];} bar;', + memmap.MemoryMapBytes(b'\x00' * 128)) + self.assertEqual(3, mock_log.call_count) + + def test_struct_fill_raw(self): + data = memmap.MemoryMapBytes(bytes(b"...")) + defn = "struct { u8 bar; u16 baz; } foo;" + obj = bitwise.parse(defn, data) + obj.fill_raw(b'\xAA') + self.assertEqual(0xAA, obj.foo.bar) + self.assertEqual(0xAAAA, obj.foo.baz) + obj.foo.fill_raw(b'\xBB') + self.assertEqual(0xBB, obj.foo.bar) + self.assertEqual(0xBBBB, obj.foo.baz) + obj.foo.bar.fill_raw(b'\xCC') + self.assertEqual(0xCC, obj.foo.bar) + self.assertEqual(0xBBBB, obj.foo.baz) + + self.assertRaises(AssertionError, obj.fill_raw, '1') + self.assertRaises(AssertionError, obj.fill_raw, b'AB') + self.assertRaises(AssertionError, obj.fill_raw, False) + + +class TestBitwisePrintoffset(BaseTest): + @mock.patch.object(bitwise.LOG, 'debug') + def test_printoffset(self, mock_log): + defn = 'u8 foo; u16 bar; #printoffset "bar";' + bitwise.parse(defn, b"abcdZ") + mock_log.assert_called_once_with('bar: 3 (0x00000003)') + class TestBitwiseSeek(BaseTest): def test_seekto(self): @@ -339,3 +396,23 @@ class TestBitwiseStringEncoders(BaseTest): def test_decode_bytes(self): self.assertEqual('foobar\x00', bitwise.string_straight_decode(b'foobar\x00')) + + +class TestPath(BaseTest): + def test_get_path(self): + fmt = ("u8 foo;" + "u8 bar[2];" + "struct {" + " u8 foo;" + " u8 bar[2];" + " u8 baz1:4," + " baz2:4;" + " struct {" + " u16 childitem;" + " } child;" + "} structure[2];") + obj = bitwise.parse(fmt, memmap.MemoryMapBytes(b'\x00' * 128)) + obj.structure[0].bar[1] = 123 + obj.structure[1].child.childitem = 456 + self.assertEqual(123, obj.get_path('.structure[0].bar[1]')) + self.assertEqual(456, obj.get_path('structure[1].child.childitem')) diff --git a/tests/unit/test_chirp_common.py b/tests/unit/test_chirp_common.py index fb880c9..d0f22f9 100644 --- a/tests/unit/test_chirp_common.py +++ b/tests/unit/test_chirp_common.py @@ -762,7 +762,9 @@ class TestOverrideRules(base.BaseTest): 'BTECH_GMRS-V2', 'BTECH_MURS-V2', 'Radioddity_DB25-G', - 'Retevis_RB17P' + 'Retevis_RB17P', + 'Baofeng_UV-17ProGPS', + 'Baofeng_5RM', ] def _test_radio_override_immutable_policy(self, rclass): diff --git a/tests/unit/test_csv.py b/tests/unit/test_csv.py index 68bbcb9..c8428b0 100644 --- a/tests/unit/test_csv.py +++ b/tests/unit/test_csv.py @@ -72,6 +72,24 @@ class TestCSV(unittest.TestCase): self.assertEqual('5.0W', str(mem.power)) self.assertIn('UHF calling', mem.comment) + def test_csv_with_comments(self): + lines = list(CHIRP_CSV_MODERN.strip().split('\n')) + lines.insert(0, '# This is a comment') + lines.insert(0, '# Test file with comments') + lines.insert(4, '# Test comment in the middle') + lines.append('# Comment at the end') + with open(self.testfn, 'w', newline='') as f: + f.write('\r\n'.join(lines)) + csv = generic_csv.CSVRadio(self.testfn) + mem = csv.get_memory(0) + self.assertEqual(146520000, mem.freq) + mem = csv.get_memory(1) + self.assertEqual(446000000, mem.freq) + csv.save(self.testfn) + with open(self.testfn, 'r') as f: + read_lines = [x.strip() for x in f.readlines()] + self.assertEqual(lines, read_lines) + def test_parse_modern_bom(self): self.test_parse_modern(output_encoding='utf-8-sig') diff --git a/tests/unit/test_logger.py b/tests/unit/test_logger.py new file mode 100644 index 0000000..e4c32a9 --- /dev/null +++ b/tests/unit/test_logger.py @@ -0,0 +1,38 @@ +import logging + +from chirp import logger +from tests.unit import base + + +class TestLogger(base.BaseTest): + def test_log_history(self): + drv_log = logging.getLogger('chirp.drivers.foo') + ui_log = logging.getLogger('chirp.wxui.foo') + root_log = logging.getLogger() + + root_log.setLevel(logging.DEBUG) + + def log_all(): + for log in (drv_log, ui_log, root_log): + log.debug('debug') + log.warning('warning') + log.error('error') + + # Log to everything + log_all() + + with logger.log_history(logging.WARNING, 'chirp.drivers') as h: + log_all() + history = h.get_history() + + # We should only have the error,warning messages from drivers + # since we started the capture, not before + self.assertEqual(2, len(history)) + + with logger.log_history(logging.WARNING, 'chirp.drivers') as h: + log_all() + history = h.get_history() + + # Make sure we only have the captured logs, not any leftover from + # the previous run + self.assertEqual(2, len(history)) diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py index 108fb16..3712847 100644 --- a/tests/unit/test_settings.py +++ b/tests/unit/test_settings.py @@ -30,24 +30,28 @@ class TestSettingValues(base.BaseTest): def test_radio_setting_value_integer(self): value = settings.RadioSettingValueInteger(0, 10, 5) + value.initialize() self.assertEqual(value.get_value(), 5) self._set_and_test(value, 1, 0, 10) self._set_and_catch(value, -1, 11) def test_radio_setting_value_float(self): value = settings.RadioSettingValueFloat(1.0, 10.5, 5.0) + value.initialize() self.assertEqual(value.get_value(), 5.0) self._set_and_test(value, 2.5, 1.0, 10.5) self._set_and_catch(value, 0.9, 10.6, -1.5) def test_radio_setting_value_boolean(self): value = settings.RadioSettingValueBoolean(True) + value.initialize() self.assertTrue(value.get_value()) self._set_and_test(value, True, False) def test_radio_setting_value_list(self): opts = ["Abc", "Def", "Ghi"] value = settings.RadioSettingValueList(opts, "Abc") + value.initialize() self.assertEqual(value.get_value(), "Abc") self.assertEqual(int(value), 0) self._set_and_test(value, "Def", "Ghi", "Abc") @@ -57,6 +61,7 @@ class TestSettingValues(base.BaseTest): def test_radio_setting_value_list_by_index(self): opts = ["Abc", "Def", "Ghi"] value = settings.RadioSettingValueList(opts, current_index=1) + value.initialize() self.assertEqual(value.get_value(), "Def") self.assertEqual(int(value), 1) self._set_and_test(value, "Def", "Ghi", "Abc") @@ -65,6 +70,7 @@ class TestSettingValues(base.BaseTest): # Make sure we int() the index, as we may pass a bitwise object value = settings.RadioSettingValueList(opts, current_index='1') + value.initialize() self.assertEqual(value.get_value(), "Def") value.set_index(0) @@ -78,6 +84,7 @@ class TestSettingValues(base.BaseTest): def test_radio_setting_value_string(self): value = settings.RadioSettingValueString(1, 5, "foo", autopad=False) + value.initialize() self.assertEqual(value.get_value(), "foo") self.assertEqual(str(value), "foo") self._set_and_test(value, "a", "abc", "abdef") @@ -88,6 +95,7 @@ class TestSettingValues(base.BaseTest): pass value = settings.RadioSettingValueString(0, 5, "foo", autopad=False) + value.initialize() def test_validate(val): if val == "bar": @@ -98,6 +106,7 @@ class TestSettingValues(base.BaseTest): def test_changed(self): value = settings.RadioSettingValueBoolean(False) + value.initialize() self.assertFalse(value.changed()) value.set_value(False) self.assertFalse(value.changed()) @@ -171,3 +180,10 @@ class TestSettingContainers(base.BaseTest): for c in settings.BANNED_NAME_CHARACTERS: self.assertRaises(settings.InvalidNameError, settings.RadioSetting, "foo%sbar" % c, "Foo") + + def test_setting_deferred(self): + val = settings.RadioSettingValueInteger(0, 10, 12) + rs = settings.RadioSetting('test', 'Test', val) + self.assertFalse(rs.value.initialized) + rs.value = 1 + self.assertTrue(rs.value.initialized) |
