tests/misc.py

Sun, 23 Jun 2019 00:26:10 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 23 Jun 2019 00:26:10 +0300
changeset 72
62d0b5b9d797
parent 71
4e24867a0110
child 74
831d9f81a48c
permissions
-rw-r--r--

made problems be copied more nicely from the web front end

from testsuite import problem_type, report_problem
import linetypes

@problem_type('bad-colour',
    severity = 'hold',
    message = lambda colour_index, count: str.format(
        'invalid colour {} used {} time(s)',
        colour_index,
        count,
    ),
)
def colours_test(model):
    ''' Checks that all colours used in the part model are valid. '''
    from collections import defaultdict
    bad_colours = defaultdict(lambda: {'count': 0, 'first-occurrence': None})
    for element in model.body:
        if hasattr(element, 'colour') and not element.colour.is_valid:
            bad_colours[element.colour.index]['count'] += 1
            if not bad_colours[element.colour.index]['first-occurrence']:
                bad_colours[element.colour.index]['first-occurrence'] = element
    yield from [
        report_problem(
            'bad-colour',
            bad_object = bad_colour['first-occurrence'],
            colour_index = colour_index,
            count = bad_colour['count'],
        )
        for colour_index, bad_colour in bad_colours.items()
    ]

@problem_type('syntax-error',
    severity = 'hold',
    message = lambda reason: str.format('syntax error: {}', reason),
)
def syntax_errors(model):
    yield from (
        report_problem('syntax-error',
            bad_object = element,
            reason = element.reason
        )
        for element in model.body
        if isinstance(element, linetypes.Error)
    )

@problem_type('bad-header',
    severity = 'hold',
    message = lambda reason: str.format('bad header: {}', reason),
)
def bad_header(model):
    import header
    if isinstance(model.header, header.BadHeader):
        yield report_problem(
            'bad-header',
            bad_object = model.body[model.header.index],
            reason = model.header.reason,
        )

@problem_type('bfc-nocertify',
    severity = 'hold',
    message = 'all new parts must be BFC certified',
)
def nocertify_test(model):
    import header
    if model.header.valid and model.header.bfc == 'NOCERTIFY':
        yield report_problem(
            'bfc-nocertify',
            bad_object = model.find_first_header_object('bfc'),
        )

@problem_type('physical-colour-part',
    severity = 'hold',
    message = 'no new physical colour parts are accepted',
)
def physical_colours_test(model):
    if model.header.valid and 'Physical_Colour' in model.header.qualifiers:
        yield report_problem(
            'physical-colour-part',
            bad_object = model.find_first_header_object('part type'),
        )

@problem_type('unofficial-part',
    severity = 'hold',
    message = 'new parts must be unofficial',
)
def unofficiality_test(model):
    if model.header.valid and not model.header.filetype.startswith('Unofficial_'):
        yield report_problem(
            'unofficial-part',
            bad_object = model.find_first_header_object('part type')
        )

@problem_type('primitive-ccw',
    severity = 'hold',
    message = 'primitives must have CCW winding',
)
@problem_type('no-bfc-line',
    severity = 'hold',
    message = 'BFC declaration is missing',
)
def header_bfc_test(model):
    if model.header.valid and not model.header.bfc:
        yield report_problem(
            'no-bfc-line',
            bad_object = model.body[0],
        )
    elif model.header.valid \
    and model.header.filetype.endswith('Primitive') \
    and model.header.bfc != 'CERTIFY CCW':
        yield report_problem(
            'primitive-bfc-ccw',
            bad_object = model.find_first_header_object('bfc'),
        )

@problem_type('keywords-for-nonparts',
    severity = 'warning',
    message = lambda type: str.format(
        'keywords are not allowed for {type} files',
        type = type,
    ),
)
def keywords_tests(model):
    if model.header.valid:
        if model.header.keywords \
        and model.header.effective_filetype != 'Part':
            yield report_problem(
                'keywords-for-nonparts',
                bad_object = model.find_first_header_object('keywords'),
                type = model.header.effective_filetype,
            )

@problem_type('bad-colour-24-nonline',
    severity = 'hold',
    message = 'colour 24 used on non-lines',
)
@problem_type('bad-colour-24-line',
    severity = 'hold',
    message = 'line with colour other than 24',
)
def colour_24_test(model):
    for element in model.body:
        if hasattr(element, 'colour'):
            is_line = isinstance(element, linetypes.LineSegment)
            if not is_line and element.colour.index == 24:
                yield report_problem('bad-colour-24-nonline', bad_object = element)
            if is_line and element.colour.index != 24:
                yield report_problem('bad-colour-24-line', bad_object = element)

manifest = {
    'tests': [
        colours_test,
        syntax_errors,
        bad_header,
        nocertify_test,
        physical_colours_test,
        unofficiality_test,
        header_bfc_test,
        keywords_tests,
        colour_24_test,
    ],
}

mercurial