test_unit.py 3.04 KB
Newer Older
1 2
import re
import logging
3
from textwrap import dedent
4 5
from .test_common import add_sources, dict_merge, TestsBase, get_common_modelsim_init_files
from pprint import pprint
6 7 8

log = logging.getLogger(__name__)

9

10 11 12
class UnitTests(TestsBase):
    def add_sources(self):
        add_sources(self.lib, ['unit/**/*.vhd'])
13
        self._create_wrapper(self.build / "tb_wrappers.vhd")
14 15

    def configure(self):
16
        lib, config, build = self.lib, self.config, self.build
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        default = config['default']
        unit_tests = lib.get_test_benches('*_unit_test')
        for name, cfg in config['tests'].items():
            dict_merge(cfg, default)
            tb = lib.get_test_benches('*tb_{}_unit_test'.format(name), allow_empty=True)
            if not len(tb):
                pprint([x.name for x in unit_tests])
                raise RuntimeError('Testbench {}_unit_test does not exist (but specified in config).'.format(name))
            assert len(tb) == 1
            tb = tb[0]
            tb.set_generic('timeout', cfg['timeout'])
            tb.set_generic('iterations', cfg['iterations'])
            tb.set_generic('log_level', cfg['log_level'] + '_l')
            tb.set_generic('error_tol', cfg['error_tolerance'])

            # generate & set per-test modelsim tcl file
            tcl = build / 'modelsim_init_{}.tcl'.format(name)
            with tcl.open('wt', encoding='utf-8') as f:
                print(dedent('''\
                    global TCOMP
                    set TCOMP tb_{}_unit_test/tb/i_test
                    '''.format(name)), file=f)
            init_files = get_common_modelsim_init_files()
            init_files += [str(tcl)]
            tb.set_sim_option("modelsim.init_files.after_load", init_files)
42
            self.add_modelsim_gui_file(tb, cfg, name)
43 44 45 46 47 48 49 50 51 52 53 54
            self._check_for_unconfigured()

    def _check_for_unconfigured(self):
        lib, config = self.lib, self.config
        # check for unconfigured unit tests
        unit_tests = lib.get_test_benches('*tb_*_unit_test')
        configured = ['tb_{}_unit_test'.format(name) for name in config['tests'].keys()]
        log.debug('Configured unit tests: {}'.format(', '.join(configured)))
        unconfigured = [tb for tb in unit_tests if tb.name not in configured]
        if len(unconfigured):
            log.warn("Unit tests with no configuration found (defaults will be used): {}".format(', '.join(tb.name for tb in unconfigured)))

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    def _create_wrapper(self, fname):
        fname = str(fname)
        files = self.lib.get_source_files()
        tests = []
        r = re.compile(r'^architecture\s+(\S+)\s+of\s+CAN_test\s+is$')
        for file in files:
            with open(file.name, 'rt', encoding='utf-8') as f:
                for l in f:
                    m = r.match(l)
                    if m:
                        tests.append(m.group(1))
        configs = []
        tbs = []
        c = self.jinja_env.get_template('unit_wrappers.vhd').render(tests=tests)
        with open(fname, "wt", encoding='utf-8') as f:
70 71
            f.write(c)

72
        self.lib.add_source_file(fname)