Salome HOME
import, formule, param et etc
[tools/eficas.git] / Tests / compare.py
1 # -*- coding: iso-8859-1 -*-
2 import re
3
4 BLANKLINE_MARKER = '<BLANKLINE>'
5 ELLIPSIS_MARKER = '...'
6 True=1
7 False=0
8
9 def check(want,got):
10     if got == want: return True
11
12     # Replace <BLANKLINE> in want with a blank line.
13     want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
14                   '', want)
15     # If a line in got contains only spaces, then remove the
16     # spaces.
17     got = re.sub('(?m)^\s*?$', '', got)
18     if got == want:
19        return True
20
21     if _ellipsis_match(want, got): 
22        return True
23
24     return False
25
26 def _ellipsis_match(want, got):
27     if ELLIPSIS_MARKER not in want:
28         return want == got
29
30     # Find "the real" strings.
31     ws = want.split(ELLIPSIS_MARKER)
32     assert len(ws) >= 2
33
34     # Deal with exact matches possibly needed at one or both ends.
35     startpos, endpos = 0, len(got)
36     w = ws[0]
37     if w:   # starts with exact match
38         if got.startswith(w):
39             startpos = len(w)
40             del ws[0]
41         else:
42             return False
43     w = ws[-1]
44     if w:   # ends with exact match
45         if got.endswith(w):
46             endpos -= len(w)
47             del ws[-1]
48         else:
49             return False
50
51     if startpos > endpos:
52         # Exact end matches required more characters than we have, as in
53         # _ellipsis_match('aa...aa', 'aaa')
54         return False
55
56     # For the rest, we only need to find the leftmost non-overlapping
57     # match for each piece.  If there's no overall match that way alone,
58     # there's no overall match period.
59     for w in ws:
60         # w may be '' at times, if there are consecutive ellipses, or
61         # due to an ellipsis at the start or end of `want`.  That's OK.
62         # Search for an empty string succeeds, and doesn't change startpos.
63         startp = got.find(w, startpos, endpos)
64         if startp < 0:
65             return False
66         startpos = startp + len(w)
67
68     return True