provoj/provoj/__init__.py
provoj/__init__.pyRaw
1#!/usr/bin/env python
2import math
3import datetime
4
5class NewTest(object):
6 def __init__(self, title=None):
7 # define the scores dictionary, and initialize
8 self.scores = {}
9 self.scores["success"] = 0
10 self.scores["error"] = 0
11 self.time = datetime.datetime.now()
12 if title != None:
13 self.title = title
14 print("")
15 pc.blue("- " + title)
16 else:
17 self.title = ""
18 print("")
19 pc.blue("- unnamed test")
20 def equal(self, action, a, b):
21 if a != b:
22 pc.error("{} - Error, {} should be equal to {}".format(action, a, b))
23 self.scores["error"] += 1
24 else:
25 pc.checked(action)
26 self.scores["success"] += 1
27 def notequal(self, action, a, b):
28 if a == b:
29 pc.error("{} - Error, {} should be not equal to {}".format(action, a, b))
30 self.scores["error"] += 1
31 else:
32 pc.checked(action)
33 self.scores["success"] += 1
34 def bigger(self, action, a, b):
35 if a < b:
36 pc.error("{} - Error, {} should be bigger than {}".format(action, a, b))
37 self.scores["error"] += 1
38 else:
39 pc.checked(action)
40 self.scores["success"] += 1
41 def smaller(self, action, a, b):
42 if a > b:
43 pc.error("{} - Error, {} should be smaller than {}".format(action, a, b))
44 self.scores["error"] += 1
45 else:
46 pc.checked(action)
47 self.scores["success"] += 1
48
49 def length(self, action, a, l):
50 if len(a) != l:
51 pc.error("{} - Error, actual length is {}, expected to be {}".format(action, a, l))
52 self.scores["error"] += 1
53 else:
54 pc.checked(action)
55 self.scores["success"] += 1
56
57 def rStatus(self, action, r, verbose=False):
58 if r.status_code < 200 or r.status_code > 299:
59 if verbose:
60 pc.error("{} - Error {}: {}".format(action, r.status_code, r.text))
61 else:
62 pc.error("{} - Error {}".format(action, r.status_code))
63
64 self.scores["error"] += 1
65 else:
66 pc.checked(action)
67 self.scores["success"] += 1
68
69 def printScores(self):
70 pc.purple(" Test title: " + self.title)
71 success = self.scores["success"]
72 error = self.scores["error"]
73 score = math.ceil((success/(success+error))*100)
74 elapsed = datetime.datetime.now() - self.time
75 pc.purple(" Score: {} % ({}/{}) Time benchmark: {}".format(score, success, success+error, elapsed))
76
77
78'''
79code to print the colors in the terminal
80'''
81checkIcon = "✓ "
82errorIcon = "❌ "
83
84class pc:
85 RED = '\033[31m'
86 GREEN = '\033[32m'
87 BLUE = '\033[94m'
88 PURPLE = '\033[95m'
89 END = '\033[0m'
90
91 @classmethod
92 def red(cls, s, **kwargs):
93 print(cls.RED + s + cls.END, **kwargs)
94
95 @classmethod
96 def green(cls, s, **kwargs):
97 print(cls.GREEN + s + cls.END, **kwargs)
98
99 @classmethod
100 def blue(cls, s, **kwargs):
101 print(cls.BLUE + s + cls.END, **kwargs)
102
103 @classmethod
104 def purple(cls, s, **kwargs):
105 print(cls.PURPLE + s + cls.END, **kwargs)
106
107 @classmethod
108 def checked(cls, s, **kwargs):
109 print(" " + cls.GREEN + checkIcon + s + cls.END, **kwargs)
110
111 @classmethod
112 def error(cls, s, **kwargs):
113 print(" " + cls.RED + errorIcon + s + cls.END, **kwargs)