-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestHasher.py
114 lines (99 loc) · 3.53 KB
/
testHasher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from pyHasher import hash,normalisePy
import sys;
def assertTrueHash(f1,f2, test_dir=""):
""" hashes two python files and checks if they are equivalent
Input:
test_dir is a prefix folder path to the file if they are both
in the same folder
Output:
returns 1 if they are the same
"""
f1 = test_dir + "/" + f1;
f2 = test_dir + "/" + f2;
if hash(f1) == hash(f2):
print('assertTrue PASS: ', f1, " == " , f2)
return 1
else:
dumpAssertFail(f1,f2)
print('assertTrue FAIL: ', f1, " != ", f2)
return 0
def dumpAssertFail(f1,f2):
""" Normalises two files and outputs to file
Input:
f1,f2- files to be normalised
Output:
Creates two files: f1-dump and f2-dump local directory of f1 and f2.
return - None
"""
minifier = normalisePy(f1)
f1=open(f1+"-dump","wb")
if sys.version_info.major == 3:
f1.write(bytes(minifier,'UTF-8'))
else:
f1.write(minifier)
f1.close()
minifier = normalisePy(f2)
f2=open(f2+"-dump","wb")
if sys.version_info.major == 3:
f2.write(bytes(minifier,'UTF-8'))
else:
f2.write(minifier)
f2.close()
def assertFalseHash(f1,f2, test_dir=""):
""" hashes two python files and checks if they are equivalent
Input:
test_dir is a prefix folder path to the file if they are both
in the same folder
Output:
returns 1 if they are not the same
"""
if not test_dir == "":
f1 = test_dir + "/" + f1;
f2 = test_dir + "/" + f2;
if hash(f1) != hash(f2):
print('assertFalse PASS: ', f1, " != " , f2)
return 1
else:
dumpAssertFail(f1,f2)
print('assertFalse FAIL: ', f1, " == ", f2,)
return 0
def main():
#Determine version number:
#python3: Minimum 3.3 is required.
#python2.x: Needs to be run from Python 2.7
testDir = 'test_cases'
if sys.version_info.major == 3:
if sys.version_info.minor >= 3:
testDir = testDir + "/" + "2.x tests"
else:
print ("Python version found: ", sys.version_info.major,".", sys.version_info.minor)
print("Python version >= 3.3 or Python 2.7 required to run, input files do not need to be under this requirement")
return -1;
elif sys.version_info.major == 2:
if sys.version_info.minor >= 7:
testDir = testDir + "/" + "2.x tests"
else:
print("Python version >= 3.3 or Python 2.7 required to run, input files do not need to be under this requirement")
return -1;
### TESTS ###
success = 0
total = 15
success += assertTrueHash('emptyFile.py','emptyFile2.py',testDir);
success += assertTrueHash('helloworld.py','helloworld.py',testDir);
success += assertTrueHash('helloworld.py','helloworld2.py',testDir);
success += assertTrueHash('helloworld.py','helloworld3.py',testDir);
success += assertTrueHash('helloworld.py','helloworld4.py',testDir);
success += assertTrueHash('helloworld3.py','helloworld4.py',testDir);
success += assertTrueHash('classesAndImports.py','classesAndImports2.py',testDir);
success += assertTrueHash('dictionary.py','dictionary2.py',testDir);
success += assertFalseHash('indent.py','indent2.py',testDir);
success += assertFalseHash('helloworld.py', 'emptyFile2.py', testDir);
success += assertFalseHash('helloworld4.py','helloworld5.py',testDir);
success += assertFalseHash('emptyFile.py', 'classesAndImports.py',testDir);
success += assertFalseHash('docstrings.py', 'docstrings2.py',testDir);
#Tests indiscriminate removal of docstrings
success += assertTrueHash('docstrings.py', 'docstrings3.py',testDir);
success += assertTrueHash('docstrings.py', 'docstrings4.py',testDir);
print("Simple whitebox testing: ",success," out of ",total, " passed.", total-success," failed") ;
if __name__ == '__main__':
main()