tests/category.py

Fri, 18 Sep 2020 20:22:22 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 18 Sep 2020 20:22:22 +0300
changeset 117
178d6e54694f
parent 93
ffe05d369412
permissions
-rw-r--r--

added unit testing

from testsuite import problem_type, report_problem
import linetypes
from librarystandards import library_standards

@problem_type('bad-category',
    severity = 'hold',
    message = lambda category: str.format(
        '"{category}" is not an official category',
        category = category,
    )
)
@problem_type('bad-category-in-description',
    severity = 'hold',
    message = lambda category: str.format(
        'the category "{category}" must be set using !CATEGORY '
        'and not by description',
        category = category,
    )
)
def category_test(model):
    # Only test the category if the description does not start with a '~'
    if model.header.valid and not model.header.description.startswith('~'):
        categories = library_standards['categories']
        illegal_categories_in_description = [
            category_name.lower()
            for category_name in categories.keys()
            if ' ' in category_name
        ]
        has_bad_category = False
        if model.header.effective_category not in categories.keys():
            try:
                bad_object = model.find_first_header_object('category')
            except KeyError:
                # category was not specified using !CATEGORY, blame
                # the description instead
                bad_object = model.body[0]
            has_bad_category = True
            yield report_problem(
                'bad-category',
                bad_object = bad_object,
                category = model.header.effective_category,
            )
        # Check if the description sets a multi-word category
        if not has_bad_category and model.header.category is None:
            for category_name in illegal_categories_in_description:
                if model.header.description.lower().startswith(category_name):
                    yield report_problem(
                        'bad-category-in-description',
                        bad_object = model.body[0],
                        category = category_name.title(),
                    )
                    break

manifest = {'tests': [category_test]}

mercurial