2b2ec62ee72cbfba95332686eb39041e45ffa5c0
[nmigen.git] / nmigen / test / test_build_plat.py
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_exact_duplicate(self):
29 self.platform.add_file("x.txt", b"foo")
30 self.platform.add_file("x.txt", b"foo")
31
32 def test_add_file_io(self):
33 with open(__file__) as f:
34 self.platform.add_file("x.txt", f)
35 with open(__file__) as f:
36 self.assertEqual(self.platform.extra_files["x.txt"], f.read())
37
38 def test_add_file_wrong_filename(self):
39 with self.assertRaises(TypeError,
40 msg="File name must be a string, not 1"):
41 self.platform.add_file(1, "")
42
43 def test_add_file_wrong_contents(self):
44 with self.assertRaises(TypeError,
45 msg="File contents must be str, bytes, or a file-like object, not 1"):
46 self.platform.add_file("foo", 1)
47
48 def test_add_file_wrong_duplicate(self):
49 self.platform.add_file("foo", "")
50 with self.assertRaises(ValueError,
51 msg="File 'foo' already exists"):
52 self.platform.add_file("foo", "bar")