Fri, 18 Sep 2020 21:53:08 +0300
added category unit tests
89
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | from testsuite import problem_type, report_problem |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | import linetypes |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
3 | from librarystandards import library_standards |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | @problem_type('bad-category', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | severity = 'hold', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | message = lambda category: str.format( |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | '"{category}" is not an official category', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
9 | category = category, |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | ) |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | ) |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | @problem_type('bad-category-in-description', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
13 | severity = 'hold', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | message = lambda category: str.format( |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
15 | 'the category "{category}" must be set using !CATEGORY ' |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | 'and not by description', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | category = category, |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | ) |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | ) |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | def category_test(model): |
93
ffe05d369412
don't check the category of '~'-files
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
21 | # Only test the category if the description does not start with a '~' |
ffe05d369412
don't check the category of '~'-files
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
22 | if model.header.valid and not model.header.description.startswith('~'): |
89
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | categories = library_standards['categories'] |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | illegal_categories_in_description = [ |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | category_name.lower() |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | for category_name in categories.keys() |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | if ' ' in category_name |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | ] |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | has_bad_category = False |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | if model.header.effective_category not in categories.keys(): |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | try: |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | bad_object = model.find_first_header_object('category') |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | except KeyError: |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | # category was not specified using !CATEGORY, blame |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | # the description instead |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | bad_object = model.body[0] |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | has_bad_category = True |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | yield report_problem( |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | 'bad-category', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | bad_object = bad_object, |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | category = model.header.effective_category, |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | ) |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | # Check if the description sets a multi-word category |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | if not has_bad_category and model.header.category is None: |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | for category_name in illegal_categories_in_description: |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | if model.header.description.lower().startswith(category_name): |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | yield report_problem( |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | 'bad-category-in-description', |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | bad_object = model.body[0], |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | category = category_name.title(), |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | ) |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | break |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | |
d2b277cb948e
split more tests into new files
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | manifest = {'tests': [category_test]} |