| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import errno |
| 5 |
import gpiod |
| 6 |
import os |
| 7 |
|
| 8 |
from . import gpiosim |
| 9 |
from .helpers import LinkGuard |
| 10 |
from unittest import TestCase |
| 11 |
|
| 12 |
|
| 13 |
class ChipConstructor(TestCase): |
| 14 |
def test_open_existing_chip(self): |
| 15 |
sim = gpiosim.Chip() |
| 16 |
|
| 17 |
with gpiod.Chip(sim.dev_path): |
| 18 |
pass |
| 19 |
|
| 20 |
def test_open_existing_chip_with_keyword(self): |
| 21 |
sim = gpiosim.Chip() |
| 22 |
|
| 23 |
with gpiod.Chip(path=sim.dev_path): |
| 24 |
pass |
| 25 |
|
| 26 |
def test_open_chip_by_link(self): |
| 27 |
link = "/tmp/gpiod-py-test-link.{}".format(os.getpid()) |
| 28 |
sim = gpiosim.Chip() |
| 29 |
|
| 30 |
with LinkGuard(sim.dev_path, link): |
| 31 |
with gpiod.Chip(link): |
| 32 |
pass |
| 33 |
|
| 34 |
def test_open_nonexistent_chip(self): |
| 35 |
with self.assertRaises(OSError) as ex: |
| 36 |
gpiod.Chip("/dev/nonexistent") |
| 37 |
|
| 38 |
self.assertEqual(ex.exception.errno, errno.ENOENT) |
| 39 |
|
| 40 |
def test_open_not_a_character_device(self): |
| 41 |
with self.assertRaises(OSError) as ex: |
| 42 |
gpiod.Chip("/tmp") |
| 43 |
|
| 44 |
self.assertEqual(ex.exception.errno, errno.ENOTTY) |
| 45 |
|
| 46 |
def test_open_not_a_gpio_device(self): |
| 47 |
with self.assertRaises(OSError) as ex: |
| 48 |
gpiod.Chip("/dev/null") |
| 49 |
|
| 50 |
self.assertEqual(ex.exception.errno, errno.ENODEV) |
| 51 |
|
| 52 |
def test_missing_path(self): |
| 53 |
with self.assertRaises(TypeError): |
| 54 |
gpiod.Chip() |
| 55 |
|
| 56 |
def test_invalid_type_for_path(self): |
| 57 |
with self.assertRaises(TypeError): |
| 58 |
gpiod.Chip(4) |
| 59 |
|
| 60 |
|
| 61 |
class ChipBooleanConversion(TestCase): |
| 62 |
def test_chip_bool(self): |
| 63 |
sim = gpiosim.Chip() |
| 64 |
chip = gpiod.Chip(sim.dev_path) |
| 65 |
self.assertTrue(chip) |
| 66 |
chip.close() |
| 67 |
self.assertFalse(chip) |
| 68 |
|
| 69 |
|
| 70 |
class ChipProperties(TestCase): |
| 71 |
def setUp(self): |
| 72 |
self.sim = gpiosim.Chip() |
| 73 |
self.chip = gpiod.Chip(self.sim.dev_path) |
| 74 |
|
| 75 |
def tearDown(self): |
| 76 |
self.chip.close() |
| 77 |
self.sim = None |
| 78 |
|
| 79 |
def test_get_chip_path(self): |
| 80 |
self.assertEqual(self.sim.dev_path, self.chip.path) |
| 81 |
|
| 82 |
def test_get_fd(self): |
| 83 |
self.assertGreaterEqual(self.chip.fd, 0) |
| 84 |
|
| 85 |
def test_properties_are_immutable(self): |
| 86 |
with self.assertRaises(AttributeError): |
| 87 |
self.chip.path = "foobar" |
| 88 |
|
| 89 |
with self.assertRaises(AttributeError): |
| 90 |
self.chip.fd = 4 |
| 91 |
|
| 92 |
|
| 93 |
class ChipDevPathFromLink(TestCase): |
| 94 |
def test_dev_path_open_by_link(self): |
| 95 |
sim = gpiosim.Chip() |
| 96 |
link = "/tmp/gpiod-py-test-link.{}".format(os.getpid()) |
| 97 |
|
| 98 |
with LinkGuard(sim.dev_path, link): |
| 99 |
with gpiod.Chip(link) as chip: |
| 100 |
self.assertEqual(chip.path, link) |
| 101 |
|
| 102 |
|
| 103 |
class ChipMapLine(TestCase): |
| 104 |
def test_lookup_by_name_good(self): |
| 105 |
sim = gpiosim.Chip( |
| 106 |
num_lines=8, line_names={1: "foo", 2: "bar", 4: "baz", 5: "xyz"} |
| 107 |
) |
| 108 |
|
| 109 |
with gpiod.Chip(sim.dev_path) as chip: |
| 110 |
self.assertEqual(chip.line_offset_from_id("baz"), 4) |
| 111 |
|
| 112 |
def test_lookup_by_name_good_keyword_argument(self): |
| 113 |
sim = gpiosim.Chip( |
| 114 |
num_lines=8, line_names={1: "foo", 2: "bar", 4: "baz", 5: "xyz"} |
| 115 |
) |
| 116 |
|
| 117 |
with gpiod.Chip(sim.dev_path) as chip: |
| 118 |
self.assertEqual(chip.line_offset_from_id(id="baz"), 4) |
| 119 |
|
| 120 |
def test_lookup_bad_name(self): |
| 121 |
sim = gpiosim.Chip( |
| 122 |
num_lines=8, line_names={1: "foo", 2: "bar", 4: "baz", 5: "xyz"} |
| 123 |
) |
| 124 |
|
| 125 |
with gpiod.Chip(sim.dev_path) as chip: |
| 126 |
with self.assertRaises(FileNotFoundError): |
| 127 |
chip.line_offset_from_id("nonexistent") |
| 128 |
|
| 129 |
def test_lookup_bad_offset(self): |
| 130 |
sim = gpiosim.Chip() |
| 131 |
|
| 132 |
with gpiod.Chip(sim.dev_path) as chip: |
| 133 |
with self.assertRaises(ValueError): |
| 134 |
chip.line_offset_from_id(4) |
| 135 |
|
| 136 |
def test_lookup_bad_offset_as_string(self): |
| 137 |
sim = gpiosim.Chip() |
| 138 |
|
| 139 |
with gpiod.Chip(sim.dev_path) as chip: |
| 140 |
with self.assertRaises(ValueError): |
| 141 |
chip.line_offset_from_id("4") |
| 142 |
|
| 143 |
def test_duplicate_names(self): |
| 144 |
sim = gpiosim.Chip( |
| 145 |
num_lines=8, line_names={1: "foo", 2: "bar", 4: "baz", 5: "bar"} |
| 146 |
) |
| 147 |
|
| 148 |
with gpiod.Chip(sim.dev_path) as chip: |
| 149 |
self.assertEqual(chip.line_offset_from_id("bar"), 2) |
| 150 |
|
| 151 |
def test_integer_offsets(self): |
| 152 |
sim = gpiosim.Chip(num_lines=8, line_names={1: "foo", 2: "bar", 6: "baz"}) |
| 153 |
|
| 154 |
with gpiod.Chip(sim.dev_path) as chip: |
| 155 |
self.assertEqual(chip.line_offset_from_id(4), 4) |
| 156 |
self.assertEqual(chip.line_offset_from_id(1), 1) |
| 157 |
|
| 158 |
def test_offsets_as_string(self): |
| 159 |
sim = gpiosim.Chip(num_lines=8, line_names={1: "foo", 2: "bar", 7: "6"}) |
| 160 |
|
| 161 |
with gpiod.Chip(sim.dev_path) as chip: |
| 162 |
self.assertEqual(chip.line_offset_from_id("2"), 2) |
| 163 |
self.assertEqual(chip.line_offset_from_id("6"), 7) |
| 164 |
|
| 165 |
|
| 166 |
class ClosedChipCannotBeUsed(TestCase): |
| 167 |
def test_close_chip_and_try_to_use_it(self): |
| 168 |
sim = gpiosim.Chip(label="foobar") |
| 169 |
|
| 170 |
chip = gpiod.Chip(sim.dev_path) |
| 171 |
chip.close() |
| 172 |
|
| 173 |
with self.assertRaises(gpiod.ChipClosedError): |
| 174 |
chip.path |
| 175 |
|
| 176 |
def test_close_chip_and_try_controlled_execution(self): |
| 177 |
sim = gpiosim.Chip() |
| 178 |
|
| 179 |
chip = gpiod.Chip(sim.dev_path) |
| 180 |
chip.close() |
| 181 |
|
| 182 |
with self.assertRaises(gpiod.ChipClosedError): |
| 183 |
with chip: |
| 184 |
chip.fd |
| 185 |
|
| 186 |
def test_close_chip_twice(self): |
| 187 |
sim = gpiosim.Chip(label="foobar") |
| 188 |
chip = gpiod.Chip(sim.dev_path) |
| 189 |
chip.close() |
| 190 |
|
| 191 |
with self.assertRaises(gpiod.ChipClosedError): |
| 192 |
chip.close() |
| 193 |
|
| 194 |
|
| 195 |
class StringRepresentation(TestCase): |
| 196 |
def setUp(self): |
| 197 |
self.sim = gpiosim.Chip(num_lines=4, label="foobar") |
| 198 |
self.chip = gpiod.Chip(self.sim.dev_path) |
| 199 |
|
| 200 |
def tearDown(self): |
| 201 |
self.chip.close() |
| 202 |
self.sim = None |
| 203 |
|
| 204 |
def test_repr(self): |
| 205 |
self.assertEqual(repr(self.chip), 'gpiod.Chip("{}")'.format(self.sim.dev_path)) |
| 206 |
|
| 207 |
cmp = eval(repr(self.chip)) |
| 208 |
self.assertEqual(self.chip.path, cmp.path) |
| 209 |
|
| 210 |
def test_str(self): |
| 211 |
info = self.chip.get_info() |
| 212 |
self.assertEqual( |
| 213 |
str(self.chip), |
| 214 |
'<Chip path="{}" fd={} info=<ChipInfo name="{}" label="foobar" num_lines=4>>'.format( |
| 215 |
self.sim.dev_path, self.chip.fd, info.name |
| 216 |
), |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
class StringRepresentationClosed(TestCase): |
| 221 |
def setUp(self): |
| 222 |
self.sim = gpiosim.Chip(num_lines=4, label="foobar") |
| 223 |
self.chip = gpiod.Chip(self.sim.dev_path) |
| 224 |
|
| 225 |
def tearDown(self): |
| 226 |
self.sim = None |
| 227 |
|
| 228 |
def test_repr_closed(self): |
| 229 |
self.chip.close() |
| 230 |
self.assertEqual(repr(self.chip), "<Chip CLOSED>") |
| 231 |
|
| 232 |
def test_str_closed(self): |
| 233 |
self.chip.close() |
| 234 |
self.assertEqual(str(self.chip), "<Chip CLOSED>") |