source: tester/comparison.py @ 954

Last change on this file since 954 was 798, checked in by sz, 6 years ago

unit testing infrastructure + few example tests for the Framsticks SDK

  • Property svn:eol-style set to native
File size: 3.1 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.list1 = self.prepare_fill_in_files(l1)
10                self.list2 = self.prepare_fill_in_files(l2)
11                self.p1 = 0  # updated position within list1
12                self.p2 = 0  # updated position within list2
13                self.ok = True  # the final comparison result
14                self.result = ""  # details of comparing individual lines
15
16                while not self.finished():
17                        if not self.compare_element():
18                                self.ok = False
19
20        @staticmethod
21        def load_platform_specific(file_name):
22                name = file_name + "-" + globals.PLATFORM + ".goal"
23
24                try:
25                        fin = open(os.path.join(globals.THISDIR, name))  # first we look for the file with the specialized name (platform-dependent)...
26                except IOError:
27                        fin = open(os.path.join(globals.THISDIR, file_name + ".goal"))  # ...if there is no such file, we try to find the default file
28                list_of_lines = []
29                for line in fin:
30                        line = globals.stripEOL(line)
31                        list_of_lines.append(line)
32                return list_of_lines
33
34        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
35                new_list = []
36                for l in list_of_lines:
37                        if l.startswith(globals.SPEC_INSERTPLATFORMDEPENDENTFILE):
38                                file_name = l[len(globals.SPEC_INSERTPLATFORMDEPENDENTFILE):]
39                                new_list += self.load_platform_specific(file_name)
40                        else:
41                                new_list.append(l)
42                return new_list
43
44        def finished(self):
45                return self.p1 >= len(self.list1) and self.p2 >= len(self.list2)
46
47        @staticmethod
48        def compare_strings(s1, s2):
49                if s1 == s2:
50                        return True
51                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
52                        return s1.replace('\\', '/') == s2  # and matches only the local output, assuming that the goal uses linux-standard /
53                else:
54                        return False
55
56        def compare_element(self):
57                e1 = "(missing)" if self.p1 >= len(self.list1) else self.list1[self.p1]
58                e2 = "(missing)" if self.p2 >= len(self.list2) else self.list2[self.p2]
59                e2disp = e2
60                if e2.startswith(globals.SPEC_SKIPTO):
61                        e2 = e2[len(globals.SPEC_SKIPTO):]
62                        waiting = True
63                        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)
64                                waiting = False
65                else:
66                        waiting = False
67                if e2.startswith(globals.SPEC_REGEXP):
68                        ok = re.match(e2[len(globals.SPEC_REGEXP):], e1)
69                else:
70                        ok = self.compare_strings(e1, e2)  # ignoring the discrimination of /\ will work only when regexp's are not used
71                self.result += repr(e1) + " "
72                if ok:
73                        self.p2 += 1
74                else:
75                        if waiting:  # handling skipto:
76                                if self.p1 >= len(self.list1):
77                                        self.p2 += 1
78                                else:
79                                        ok = True
80                        else:
81                                self.p2 += 1
82                if ok:
83                        self.result += globals.ANSI_SETGREEN + "ok" + globals.ANSI_RESET + "\n"  # if both are identical, we display only the left one
84                else:
85                        self.result += globals.ANSI_SETRED + "<FAIL>" + globals.ANSI_RESET + " "
86                        self.result += repr(e2disp) + "\n"
87                self.p1 += 1
88                return ok
Note: See TracBrowser for help on using the repository browser.