-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_methods.py
75 lines (58 loc) · 2.48 KB
/
test_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import re
import glob
from functools import partial
import pytest
import methods
SOLUTION_REG = re.compile('% ([a-z ]+)\s*×\s*([a-z ]+)\s*')
def solutions_from_file(filename:str) -> iter:
"""Yield solutions found in given file as pairs of frozenset"""
with open(filename) as fd:
for line in fd:
if line.startswith('% '):
match = SOLUTION_REG.fullmatch(line)
if match:
objs, atts = match.groups()
objs = frozenset(objs.strip().split(' '))
atts = frozenset(atts.strip().split(' '))
yield objs, atts
def solutions_from_method(method, filename:str) -> iter:
"""Solve the given context using the given method, and extract the concepts
in each answer (obj/1 and att/1 atoms).
Yield the {obj}×{att} as pairs of frozenset.
"""
for answer in method(filename):
obj, att = set(), set()
for atom in answer:
if atom.predicate == 'obj':
assert len(atom.args()) == 1
obj.add(atom.args()[0])
elif atom.predicate == 'att':
assert len(atom.args()) == 1
att.add(atom.args()[0])
yield frozenset(obj), frozenset(att)
def pprint_concept(concept:(frozenset, frozenset)) -> str:
obj, att = concept
return '{{{}}}×{{{}}}'.format(','.join(obj), ','.join(att))
def run_test_routine(method, context_file, should_fail=False):
"""Total run of the testing routine for given method and context.
method -- method function to test
context_file -- filename of the context used to test the method
should_fail -- if true, expect the method to fail.
"""
print('context_file =', context_file, '\t method =', method.__name__)
expected = frozenset(solutions_from_file(context_file))
found = frozenset(solutions_from_method(method, context_file))
print('expected =', tuple(map(pprint_concept, expected)))
print(' found =', tuple(map(pprint_concept, found)))
if should_fail:
assert expected != found
else:
assert expected == found
print()
# Create and add in global scope the tests for pytest.
for context_file in glob.glob('test_cases/*.lp'):
filename = os.path.basename(context_file)
for method, name in methods.METHODS_ANS.items():
func = partial(run_test_routine, method, context_file, should_fail=name == 'false')
globals()['test_method_' + name + '_on_' + filename] = func