Skip to content

Commit 3406616

Browse files
committed
test_eds: Cover pathlib and StringIO as example inputs.
1 parent e0da0a8 commit 3406616

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

test/test_eds.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import io
12
import os
3+
import pathlib
24
import unittest
35

46
import canopen
@@ -54,10 +56,18 @@ def setUp(self):
5456
def test_load_nonexisting_file(self):
5557
with self.assertRaises(IOError):
5658
canopen.import_od('/path/to/wrong_file.eds')
59+
with self.assertRaises(IOError):
60+
canopen.import_od(pathlib.Path('/path/to/wrong_file.eds'))
5761

5862
def test_load_unsupported_format(self):
5963
with self.assertRaisesRegex(ValueError, "'py'"):
6064
canopen.import_od(__file__)
65+
with self.assertRaisesRegex(ValueError, "''"):
66+
canopen.import_od('')
67+
with self.assertRaisesRegex(ValueError, "''"):
68+
filelike_object = io.StringIO() # no .name attribute
69+
self.addCleanup(filelike_object.close)
70+
canopen.import_od(filelike_object)
6171

6272
def test_load_file_object(self):
6373
with open(SAMPLE_EDS) as fp:
@@ -70,8 +80,6 @@ def test_load_implicit_nodeid(self):
7080
self.assertEqual(od.node_id, 16)
7181

7282
def test_load_implicit_nodeid_fallback(self):
73-
import io
74-
7583
# First, remove the NodeID option from DeviceComissioning.
7684
with open(SAMPLE_EDS) as f:
7785
lines = [L for L in f.readlines() if not L.startswith("NodeID=")]
@@ -93,8 +101,6 @@ def test_load_baudrate(self):
93101
self.assertEqual(od.bitrate, 500_000)
94102

95103
def test_load_baudrate_fallback(self):
96-
import io
97-
98104
# Remove the Baudrate option.
99105
with open(SAMPLE_EDS) as f:
100106
lines = [L for L in f.readlines() if not L.startswith("Baudrate=")]
@@ -240,7 +246,6 @@ def test_read_domain_subobject(self):
240246

241247
def test_roundtrip_domain_objects(self):
242248
# ObjectType==DOMAIN survive an EDS export/import round-trip
243-
import io
244249
with io.StringIO() as dest:
245250
canopen.export_od(self.od, dest, 'eds')
246251
dest.name = 'mock.eds'
@@ -271,7 +276,6 @@ def test_export_eds_to_file(self):
271276
self.verify_od(dest, doctype)
272277

273278
def test_export_eds_to_file_unknown_extension(self):
274-
import io
275279
for suffix in ".txt", "":
276280
with tmp_file(suffix=suffix) as tmp:
277281
dest = tmp.name
@@ -294,7 +298,6 @@ def test_export_eds_to_file_unknown_extension(self):
294298
self.verify_od(buf, "eds")
295299

296300
def test_export_eds_unknown_doctype(self):
297-
import io
298301
filelike_object = io.StringIO()
299302
self.addCleanup(filelike_object.close)
300303
for dest in "filename", None, filelike_object:
@@ -307,7 +310,6 @@ def test_export_eds_unknown_doctype(self):
307310
os.stat(dest)
308311

309312
def test_export_eds_to_filelike_object(self):
310-
import io
311313
for doctype in "eds", "dcf":
312314
with io.StringIO() as dest:
313315
with self.subTest(dest=dest, doctype=doctype):
@@ -321,7 +323,6 @@ def test_export_eds_to_filelike_object(self):
321323

322324
def test_export_eds_to_stdout(self):
323325
import contextlib
324-
import io
325326
with contextlib.redirect_stdout(io.StringIO()) as f:
326327
ret = canopen.export_od(self.od, None, "eds")
327328
self.assertIsNone(ret)

0 commit comments

Comments
 (0)