1 | import globals # our source |
---|
2 | import re |
---|
3 | import sys |
---|
4 | import os, os.path |
---|
5 | |
---|
6 | |
---|
7 | class Comparison: |
---|
8 | def __init__(self, l1, l2): |
---|
9 | self.list2_file = '' # (last if there are many) file name with goal results (if they are stored in a file) |
---|
10 | self.list1 = self.prepare_fill_in_files(l1) |
---|
11 | self.list2 = self.prepare_fill_in_files(l2) |
---|
12 | self.p1 = 0 # updated position within list1 |
---|
13 | self.p2 = 0 # updated position within list2 |
---|
14 | self.equal = True # the final comparison result |
---|
15 | self.result = "" # details of comparing individual lines |
---|
16 | |
---|
17 | while not self.finished(): |
---|
18 | if not self.compare_element(): |
---|
19 | self.equal = False |
---|
20 | |
---|
21 | @staticmethod |
---|
22 | def load_platform_specific(file_name): |
---|
23 | name = file_name + "-" + globals.PLATFORM + ".goal" |
---|
24 | |
---|
25 | try: |
---|
26 | fin = open(os.path.join(globals.THISDIR, name)) # first we look for the file with the specialized name (platform-dependent)... |
---|
27 | except IOError: |
---|
28 | fin = open(os.path.join(globals.THISDIR, file_name + ".goal")) # ...if there is no such file, we try to find the default file |
---|
29 | list_of_lines = [] |
---|
30 | for line in fin: |
---|
31 | line = globals.stripEOL(line) |
---|
32 | list_of_lines.append(line) |
---|
33 | return list_of_lines |
---|
34 | |
---|
35 | def prepare_fill_in_files(self, list_of_lines): # when finds a line with SPEC_USEPLATFORMDEPENDENTFILE, replaces it with the contents of the appropriate file |
---|
36 | new_list = [] |
---|
37 | for l in list_of_lines: |
---|
38 | if l.startswith(globals.SPEC_INSERTPLATFORMDEPENDENTFILE): |
---|
39 | file_name = l[len(globals.SPEC_INSERTPLATFORMDEPENDENTFILE):] |
---|
40 | new_list += self.load_platform_specific(file_name) |
---|
41 | self.list2_file = file_name |
---|
42 | else: |
---|
43 | new_list.append(l) |
---|
44 | return new_list |
---|
45 | |
---|
46 | def finished(self): |
---|
47 | return self.p1 >= len(self.list1) and self.p2 >= len(self.list2) |
---|
48 | |
---|
49 | @staticmethod |
---|
50 | def compare_strings(s1, s2): |
---|
51 | if s1 == s2: |
---|
52 | return True |
---|
53 | if not globals.WIN_SLASH_SENSITIVE and os.path.sep == '\\': # give a chance for approximate comparison - does not distinguish /\ but only on the windows platform |
---|
54 | return s1.replace('\\', '/') == s2 # and matches only the local output, assuming that the goal uses linux-standard / |
---|
55 | else: |
---|
56 | return False |
---|
57 | |
---|
58 | def compare_element(self): |
---|
59 | e1 = "(missing)" if self.p1 >= len(self.list1) else self.list1[self.p1] |
---|
60 | e2 = "(missing)" if self.p2 >= len(self.list2) else self.list2[self.p2] |
---|
61 | e2disp = e2 |
---|
62 | if e2.startswith(globals.SPEC_SKIPTO): |
---|
63 | e2 = e2[len(globals.SPEC_SKIPTO):] |
---|
64 | waiting = True |
---|
65 | if self.p1 >= len(self.list1): # exception: skipto becomes a standard comparison when the input stream ends (because then we know we will not read anything more than we have now) |
---|
66 | waiting = False |
---|
67 | else: |
---|
68 | waiting = False |
---|
69 | if e2.startswith(globals.SPEC_REGEXP): |
---|
70 | equal = re.match(e2[len(globals.SPEC_REGEXP):], e1) |
---|
71 | else: |
---|
72 | equal = self.compare_strings(e1, e2) # ignoring the discrimination of /\ will work only when regexp's are not used |
---|
73 | self.result += repr(e1) + " " |
---|
74 | if equal: |
---|
75 | self.p2 += 1 |
---|
76 | else: |
---|
77 | if waiting: # handling skipto: |
---|
78 | if self.p1 >= len(self.list1): |
---|
79 | self.p2 += 1 |
---|
80 | else: |
---|
81 | equal = True |
---|
82 | else: |
---|
83 | self.p2 += 1 |
---|
84 | if equal: |
---|
85 | self.result += globals.ANSI_SETGREEN + "ok" + globals.ANSI_RESET + "\n" # if both are identical, we display only the left one |
---|
86 | else: |
---|
87 | self.result += globals.ANSI_SETRED + "<FAIL>" + globals.ANSI_RESET + " " |
---|
88 | self.result += repr(e2disp) + "\n" |
---|
89 | self.p1 += 1 |
---|
90 | return equal |
---|