tests/misc.py

Sat, 22 Jun 2019 23:14:40 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 22 Jun 2019 23:14:40 +0300
changeset 66
e28b249b4422
parent 64
1c0884f5506e
child 68
c19dd0d3248e
permissions
-rw-r--r--

Fix unofficial part test

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.body[model.header.first_occurrence['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.body[model.header.first_occurrence['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.body[model.header.first_occurrence['part type']])

@problem_type('primitive-ccw',
    severity = 'hold',
    message = 'primitives must have CCW winding',
)
def primitive_ccw_test(model):
    if model.header.valid \
    and model.header.filetype.endswith('Primitive') \
    and model.header.bfc != 'CERTIFY CCW':
        yield report_problem(
            'primitive-bfc-ccw',
            bad_object = model.body[model.header.first_occurrence['bfc']],
        )

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

mercurial