test: add tests for build.plat.Platform.add_file.
authorwhitequark <cz@m-labs.hk>
Fri, 15 Nov 2019 23:35:55 +0000 (23:35 +0000)
committerwhitequark <cz@m-labs.hk>
Fri, 15 Nov 2019 23:39:13 +0000 (23:39 +0000)
nmigen/build/plat.py
nmigen/test/test_build_plat.py [new file with mode: 0644]

index 7c7bd0f7e052832bc98d5855008a5cc1f99fdd3d..00c147f3060ec03225e36516024aee00cb79e76d 100644 (file)
@@ -48,14 +48,16 @@ class Platform(ResourceManager, metaclass=ABCMeta):
 
     def add_file(self, filename, content):
         if not isinstance(filename, str):
-            raise TypeError("File name must be a string")
+            raise TypeError("File name must be a string, not {!r}"
+                            .format(filename))
         if filename in self.extra_files:
-            raise ValueError("File {} already exists"
+            raise ValueError("File {!r} already exists"
                              .format(filename))
         if hasattr(content, "read"):
             content = content.read()
         elif not isinstance(content, (str, bytes)):
-            raise TypeError("File contents must be str, bytes, or a file-like object")
+            raise TypeError("File contents must be str, bytes, or a file-like object, not {!r}"
+                            .format(content))
         self.extra_files[filename] = content
 
     @property
diff --git a/nmigen/test/test_build_plat.py b/nmigen/test/test_build_plat.py
new file mode 100644 (file)
index 0000000..a974a81
--- /dev/null
@@ -0,0 +1,48 @@
+from .. import *
+from ..build.plat import *
+from .utils import *
+
+
+class MockPlatform(Platform):
+    resources  = []
+    connectors = []
+
+    required_tools = []
+
+    def toolchain_prepare(self, fragment, name, **kwargs):
+        raise NotImplementedError
+
+
+class PlatformTestCase(FHDLTestCase):
+    def setUp(self):
+        self.platform = MockPlatform()
+
+    def test_add_file_str(self):
+        self.platform.add_file("x.txt", "foo")
+        self.assertEqual(self.platform.extra_files["x.txt"], "foo")
+
+    def test_add_file_bytes(self):
+        self.platform.add_file("x.txt", b"foo")
+        self.assertEqual(self.platform.extra_files["x.txt"], b"foo")
+
+    def test_add_file_io(self):
+        with open(__file__) as f:
+            self.platform.add_file("x.txt", f)
+        with open(__file__) as f:
+            self.assertEqual(self.platform.extra_files["x.txt"], f.read())
+
+    def test_add_file_wrong_filename(self):
+        with self.assertRaises(TypeError,
+                msg="File name must be a string, not 1"):
+            self.platform.add_file(1, "")
+
+    def test_add_file_wrong_contents(self):
+        with self.assertRaises(TypeError,
+                msg="File contents must be str, bytes, or a file-like object, not 1"):
+            self.platform.add_file("foo", 1)
+
+    def test_add_file_wrong_duplicate(self):
+        self.platform.add_file("foo", "")
+        with self.assertRaises(ValueError,
+                msg="File 'foo' already exists"):
+            self.platform.add_file("foo", "bar")