source: tester/comparison.py @ 1244

Last change on this file since 1244 was 995, checked in by Maciej Komosinski, 4 years ago

Added support for utf-8 in tests

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1import globals  # our source
2import re
3import sys
4import os, os.path
5
6
7class 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), encoding='utf8')  # first we look for the file with the specialized name (platform-dependent)...
27                except IOError as e1:
28                        try:
29                                fin = open(os.path.join(globals.THISDIR, file_name + ".goal"), encoding='utf8')  # ...if there is no such file, we try to find the default file
30                        except IOError as e2:
31                                print()
32                                print(e1)
33                                print(e2)
34                                print('Proceeding as if the missing file were empty.')
35                                return []
36                list_of_lines = []
37                for line in fin:
38                        line = globals.stripEOL(line)
39                        list_of_lines.append(line)
40                return list_of_lines
41
42        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
43                new_list = []
44                for l in list_of_lines:
45                        if l.startswith(globals.SPEC_INSERTPLATFORMDEPENDENTFILE):
46                                file_name = l[len(globals.SPEC_INSERTPLATFORMDEPENDENTFILE):]
47                                new_list += self.load_platform_specific(file_name)
48                                self.list2_file = file_name
49                        else:
50                                new_list.append(l)
51                return new_list
52
53        def finished(self):
54                return self.p1 >= len(self.list1) and self.p2 >= len(self.list2)
55
56        @staticmethod
57        def compare_strings(s1, s2):
58                if s1 == s2:
59                        return True
60                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
61                        return s1.replace('\\', '/') == s2  # and matches only the local output, assuming that the goal uses linux-standard /
62                else:
63                        return False
64
65        def compare_element(self):
66                e1 = "(missing)" if self.p1 >= len(self.list1) else self.list1[self.p1]
67                e2 = "(missing)" if self.p2 >= len(self.list2) else self.list2[self.p2]
68                e2disp = e2
69                if e2.startswith(globals.SPEC_SKIPTO):
70                        e2 = e2[len(globals.SPEC_SKIPTO):]
71                        waiting = True
72                        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)
73                                waiting = False
74                else:
75                        waiting = False
76                if e2.startswith(globals.SPEC_REGEXP):
77                        equal = re.match(e2[len(globals.SPEC_REGEXP):], e1)
78                else:
79                        equal = self.compare_strings(e1, e2)  # ignoring the discrimination of /\ will work only when regexp's are not used
80                self.result += repr(e1) + " "
81                if equal:
82                        self.p2 += 1
83                else:
84                        if waiting:  # handling skipto:
85                                if self.p1 >= len(self.list1):
86                                        self.p2 += 1
87                                else:
88                                        equal = True
89                        else:
90                                self.p2 += 1
91                if equal:
92                        self.result += globals.ANSI_SETGREEN + "ok" + globals.ANSI_RESET + "\n"  # if both are identical, we display only the left one
93                else:
94                        self.result += globals.ANSI_SETRED + "<FAIL>" + globals.ANSI_RESET + " "
95                        self.result += repr(e2disp) + "\n"
96                self.p1 += 1
97                return equal
Note: See TracBrowser for help on using the repository browser.