tests: move out of the main package.
[nmigen.git] / tests / test_build_plat.py
1 from nmigen import *
2 from nmigen.build.plat import *
3
4 from .utils import *
5
6
7 class MockPlatform(Platform):
8 resources = []
9 connectors = []
10
11 required_tools = []
12
13 def toolchain_prepare(self, fragment, name, **kwargs):
14 raise NotImplementedError
15
16
17 class PlatformTestCase(FHDLTestCase):
18 def setUp(self):
19 self.platform = MockPlatform()
20
21 def test_add_file_str(self):
22 self.platform.add_file("x.txt", "foo")
23 self.assertEqual(self.platform.extra_files["x.txt"], "foo")
24
25 def test_add_file_bytes(self):
26 self.platform.add_file("x.txt", b"foo")
27 self.assertEqual(self.platform.extra_files["x.txt"], b"foo")
28
29 def test_add_file_exact_duplicate(self):
30 self.platform.add_file("x.txt", b"foo")
31 self.platform.add_file("x.txt", b"foo")
32
33 def test_add_file_io(self):
34 with open(__file__) as f:
35 self.platform.add_file("x.txt", f)
36 with open(__file__) as f:
37 self.assertEqual(self.platform.extra_files["x.txt"], f.read())
38
39 def test_add_file_wrong_filename(self):
40 with self.assertRaisesRegex(TypeError,
41 r"^File name must be a string, not 1$"):
42 self.platform.add_file(1, "")
43
44 def test_add_file_wrong_contents(self):
45 with self.assertRaisesRegex(TypeError,
46 r"^File contents must be str, bytes, or a file-like object, not 1$"):
47 self.platform.add_file("foo", 1)
48
49 def test_add_file_wrong_duplicate(self):
50 self.platform.add_file("foo", "")
51 with self.assertRaisesRegex(ValueError,
52 r"^File 'foo' already exists$"):
53 self.platform.add_file("foo", "bar")