Skip to content

Commit e3af0eb

Browse files
Increase export_eds() test coverage (#474)
* Test export_eds() to stdout * Use StringIO as a context manager iso. addCleanup * Expand the test to cover most paths * Expand test for unknown doctypes * Use subTest for better debugging
1 parent 028a57f commit e3af0eb

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

test/test_eds.py

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -183,37 +183,80 @@ def test_comments(self):
183183
|-------------|
184184
""".strip())
185185

186-
def test_export_eds(self):
186+
def test_export_eds_to_file(self):
187187
import tempfile
188-
from pathlib import Path
189-
with tempfile.TemporaryDirectory() as tempdir:
190-
for doctype in {"eds", "dcf"}:
191-
192-
# Test export_od with file object
193-
tempfile = str(Path(tempdir, "test." + doctype))
194-
with open(tempfile, "w+") as tempeds:
195-
print(f"exporting {doctype} to {tempeds.name}")
196-
canopen.export_od(self.od, tempeds, doc_type=doctype)
197-
self.verify_od(tempfile, doctype)
198-
199-
# Test export_od handling opening and closing the file
200-
tempfile = str(Path(tempdir, "test2." + doctype))
201-
canopen.export_od(self.od, tempfile, doc_type=doctype)
202-
self.verify_od(tempfile, doctype)
203-
204-
# Test for unknown doctype
205-
with self.assertRaisesRegex(ValueError, "'unknown'"):
206-
tempfile = str(Path(tempdir, "test.unknown"))
207-
canopen.export_od(self.od, tempfile, doc_type="unknown")
208-
209-
# Omit doctype
210-
tempfile = str(Path(tempdir, "test.eds"))
211-
canopen.export_od(self.od, tempfile)
212-
self.verify_od(tempfile, "eds")
213-
214-
215-
def verify_od(self, tempfile, doctype):
216-
exported_od = canopen.import_od(tempfile)
188+
for suffix in "eds", "dcf":
189+
for implicit in True, False:
190+
with tempfile.NamedTemporaryFile() as fn:
191+
dest = f"{fn.name}.{suffix}"
192+
doctype = None if implicit else suffix
193+
with self.subTest(dest=dest, doctype=doctype):
194+
canopen.export_od(self.od, dest, doctype)
195+
self.verify_od(dest, doctype)
196+
197+
def test_export_eds_to_file_unknown_extension(self):
198+
import io
199+
import tempfile
200+
for suffix in ".txt", "":
201+
with tempfile.NamedTemporaryFile() as fn:
202+
dest = f"{fn.name}{suffix}"
203+
with self.subTest(dest=dest, doctype=None):
204+
canopen.export_od(self.od, dest)
205+
206+
# The import_od() API has some shortcomings compared to the
207+
# export_od() API, namely that it does not take a doctype
208+
# parameter. This means it has to be able to deduce the
209+
# doctype from its 'source' parameter. In this case, this
210+
# is not possible, since we're using an unknown extension,
211+
# so we have to do a couple of tricks in order to make this
212+
# work.
213+
with open(dest, "r") as source:
214+
data = source.read()
215+
with io.StringIO() as buf:
216+
buf.write(data)
217+
buf.seek(io.SEEK_SET)
218+
buf.name = "mock.eds"
219+
self.verify_od(buf, "eds")
220+
221+
def test_export_eds_unknown_doctype(self):
222+
import io
223+
filelike_object = io.StringIO()
224+
self.addCleanup(filelike_object.close)
225+
for dest in "filename", None, filelike_object:
226+
with self.subTest(dest=dest):
227+
with self.assertRaisesRegex(ValueError, "'unknown'"):
228+
canopen.export_od(self.od, dest, doc_type="unknown")
229+
230+
def test_export_eds_to_filelike_object(self):
231+
import io
232+
for doctype in "eds", "dcf":
233+
with io.StringIO() as dest:
234+
with self.subTest(dest=dest, doctype=doctype):
235+
canopen.export_od(self.od, dest, doctype)
236+
237+
# The import_od() API assumes the file-like object has a
238+
# well-behaved 'name' member.
239+
dest.name = f"mock.{doctype}"
240+
dest.seek(io.SEEK_SET)
241+
self.verify_od(dest, doctype)
242+
243+
def test_export_eds_to_stdout(self):
244+
import contextlib
245+
import io
246+
with contextlib.redirect_stdout(io.StringIO()) as f:
247+
ret = canopen.export_od(self.od, None, "eds")
248+
self.assertIsNone(ret)
249+
250+
dump = f.getvalue()
251+
with io.StringIO(dump) as buf:
252+
# The import_od() API assumes the TextIO object has a well-behaved
253+
# 'name' member.
254+
buf.name = "mock.eds"
255+
self.verify_od(buf, "eds")
256+
257+
258+
def verify_od(self, source, doctype):
259+
exported_od = canopen.import_od(source)
217260

218261
for index in exported_od:
219262
self.assertIn(exported_od[index].name, self.od)

0 commit comments

Comments
 (0)