Skip to content

Commit fe400b5

Browse files
author
whitequark
committed
test: add tests for build.plat.Platform.add_file.
1 parent f8f7d83 commit fe400b5

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

nmigen/build/plat.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ def default_clk_frequency(self):
4848

4949
def add_file(self, filename, content):
5050
if not isinstance(filename, str):
51-
raise TypeError("File name must be a string")
51+
raise TypeError("File name must be a string, not {!r}"
52+
.format(filename))
5253
if filename in self.extra_files:
53-
raise ValueError("File {} already exists"
54+
raise ValueError("File {!r} already exists"
5455
.format(filename))
5556
if hasattr(content, "read"):
5657
content = content.read()
5758
elif not isinstance(content, (str, bytes)):
58-
raise TypeError("File contents must be str, bytes, or a file-like object")
59+
raise TypeError("File contents must be str, bytes, or a file-like object, not {!r}"
60+
.format(content))
5961
self.extra_files[filename] = content
6062

6163
@property

nmigen/test/test_build_plat.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from .. import *
2+
from ..build.plat import *
3+
from .utils import *
4+
5+
6+
class MockPlatform(Platform):
7+
resources = []
8+
connectors = []
9+
10+
required_tools = []
11+
12+
def toolchain_prepare(self, fragment, name, **kwargs):
13+
raise NotImplementedError
14+
15+
16+
class PlatformTestCase(FHDLTestCase):
17+
def setUp(self):
18+
self.platform = MockPlatform()
19+
20+
def test_add_file_str(self):
21+
self.platform.add_file("x.txt", "foo")
22+
self.assertEqual(self.platform.extra_files["x.txt"], "foo")
23+
24+
def test_add_file_bytes(self):
25+
self.platform.add_file("x.txt", b"foo")
26+
self.assertEqual(self.platform.extra_files["x.txt"], b"foo")
27+
28+
def test_add_file_io(self):
29+
with open(__file__) as f:
30+
self.platform.add_file("x.txt", f)
31+
with open(__file__) as f:
32+
self.assertEqual(self.platform.extra_files["x.txt"], f.read())
33+
34+
def test_add_file_wrong_filename(self):
35+
with self.assertRaises(TypeError,
36+
msg="File name must be a string, not 1"):
37+
self.platform.add_file(1, "")
38+
39+
def test_add_file_wrong_contents(self):
40+
with self.assertRaises(TypeError,
41+
msg="File contents must be str, bytes, or a file-like object, not 1"):
42+
self.platform.add_file("foo", 1)
43+
44+
def test_add_file_wrong_duplicate(self):
45+
self.platform.add_file("foo", "")
46+
with self.assertRaises(ValueError,
47+
msg="File 'foo' already exists"):
48+
self.platform.add_file("foo", "bar")

0 commit comments

Comments
 (0)