tests: move out of the main package.
Compared to tests in the repository root, tests in the package have
many downsides:
* Unless explicitly excluded in find_packages(), tests and their
support code effectively become a part of public API.
This, unfortunately, happened with FHDLTestCase, which was never
intended for downstream use.
* Even if explicitly excluded from the setuptools package, using
an editable install, or setting PYTHONPATH still allows accessing
the tests.
* Having a sub-package that is present in the source tree but not
exported (or, worse, exported only sometimes) is confusing.
* The name `nmigen.test` cannot be used for anything else, such as
testing utilities that *are* intended for downstream use.
2020-08-26 18:33:31 -06:00
|
|
|
from nmigen import *
|
|
|
|
from nmigen.build.plat import *
|
|
|
|
|
2019-11-15 16:35:55 -07:00
|
|
|
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")
|
|
|
|
|
2019-11-15 16:40:44 -07:00
|
|
|
def test_add_file_exact_duplicate(self):
|
|
|
|
self.platform.add_file("x.txt", b"foo")
|
|
|
|
self.platform.add_file("x.txt", b"foo")
|
|
|
|
|
2019-11-15 16:35:55 -07:00
|
|
|
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):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^File name must be a string, not 1$"):
|
2019-11-15 16:35:55 -07:00
|
|
|
self.platform.add_file(1, "")
|
|
|
|
|
|
|
|
def test_add_file_wrong_contents(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^File contents must be str, bytes, or a file-like object, not 1$"):
|
2019-11-15 16:35:55 -07:00
|
|
|
self.platform.add_file("foo", 1)
|
|
|
|
|
|
|
|
def test_add_file_wrong_duplicate(self):
|
|
|
|
self.platform.add_file("foo", "")
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^File 'foo' already exists$"):
|
2019-11-15 16:35:55 -07:00
|
|
|
self.platform.add_file("foo", "bar")
|
2020-11-09 22:30:21 -07:00
|
|
|
|
|
|
|
def test_iter_files(self):
|
|
|
|
self.platform.add_file("foo.v", "")
|
|
|
|
self.platform.add_file("bar.v", "")
|
|
|
|
self.platform.add_file("baz.vhd", "")
|
|
|
|
self.assertEqual(list(self.platform.iter_files(".v")),
|
|
|
|
["foo.v", "bar.v"])
|
|
|
|
self.assertEqual(list(self.platform.iter_files(".vhd")),
|
|
|
|
["baz.vhd"])
|
|
|
|
self.assertEqual(list(self.platform.iter_files(".v", ".vhd")),
|
|
|
|
["foo.v", "bar.v", "baz.vhd"])
|