-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtests.py
163 lines (146 loc) · 6.19 KB
/
tests.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import unittest
import difflib
import os, pprint
import format
#from difflib_data import *
import sys
o = []
#unittest.TestCase.assertEqual(testCases[0],testCases[1])
class TestFormatting(unittest.TestCase):
def stringsAreEqual(self,a,b,fileName):
try:
self.assertEqual(a,b)
print("TEST OK for " + fileName)
except AssertionError:
d = difflib.Differ()
lineNum = 0
diffs = difflib.context_diff(a, b,n=14)
print("TEST failed for " + fileName)
#print '\n'.join(diffs)
for line in diffs:
line = line.strip(" ")
sys.stdout.write(line)
print(" ")
#for line in diffs:
# split off the code
#code = line[:2]
#if the line is in both files or just b, increment the line number.
# if code in (" ", "+ "):
# lineNum += 1
# if this line is only in b, print the line number and the text on the line
# if code == "+ ":
# print "%d: %s" % (lineNum, line[2:].strip())
#for char in result:
# char = char + " "
# if "+" in char:
# print("added " + char)
# elif "-" in char:
# print("removed " + char)
print("####### Tried to format the code from " + fileName + " #########")
print(a.replace(" ","_"))
print("####### It did not match what the file said was correct formatting: #########")
print(b.replace(" ","_"))
print("#######END")
lineNum = 0
def stringsAreNotEqual(self,a,b,fileName):
try:
self.assertNotEqual(a,b)
print("should not be equal-test OK for " + fileName)
except AssertionError:
d = difflib.Differ()
result = list(d.compare(a, b))
for char in result:
char = char + " "
if "+" in char:
print("added " + char)
elif "-" in char:
print("removed " + char)
print("####### Tried to format the code from " + fileName + " #########")
print(a.replace(" ","_"))
print("####### It should not match: #########")
print(b.replace(" ","_"))
print("#######END")
#print('{} => {}'.format(a,b))
#for i,s in enumerate(difflib.ndiff(a, b)):
# if s[0]==' ': continue
# elif s[0]=='-':
# print(u'Delete "{}" from position {}'.format(s[-1],i))
# elif s[0]=='+':
# print(u'Add "{}" to position {}'.format(s[-1],i))
#print()
def pathForInTestCaseSubFolder(self,foldername):
directoryname = os.path.dirname(os.path.abspath(__file__))
testCaseDirectoryName = directoryname + "/testCases"
subfolderName = testCaseDirectoryName + "/" + foldername
fileAbsolutePaths = []
for root, dirs, files in os.walk(subfolderName):
for fileName in files:
if ".test" in fileName:
absoluteFilePath = subfolderName + "/" + fileName
fileAbsolutePaths.append(absoluteFilePath)
return fileAbsolutePaths
def testIndentation(self):
for fileName in self.pathForInTestCaseSubFolder("indentation"):
file = open(os.path.abspath(fileName), "r")
fileReader = format.FileReader(file)
testCases = fileReader.compareTestCasesInReadFile()
self.stringsAreEqual(testCases[0],testCases[1],fileName)
def testSpaces(self):
for fileName in self.pathForInTestCaseSubFolder("spaces"):
file = open(os.path.abspath(fileName), "r")
fileReader = format.FileReader(file)
testCases = fileReader.compareTestCasesInReadFile()
self.stringsAreEqual(testCases[0],testCases[1],fileName)
def testFailingTests(self):
for fileName in self.pathForInTestCaseSubFolder("shouldFail"):
file = open(os.path.abspath(fileName), "r")
fileReader = format.FileReader(file)
testCases = fileReader.compareTestCasesInReadFile()
self.stringsAreNotEqual(testCases[0],testCases[1],fileName)
def testClosures(self):
for fileName in self.pathForInTestCaseSubFolder("closures"):
file = open(os.path.abspath(fileName), "r")
fileReader = format.FileReader(file)
testCases = fileReader.compareTestCasesInReadFile()
self.stringsAreEqual(testCases[0],testCases[1],fileName)
#def testIndentation2(self):
# file = open("SwiftFormatter/indentTest.test", "r")
# fileReader = reader.FileReader(file)
# testCases = fileReader.parseTestCaseAtIndex(1)
# self.stringsAreEqual(testCases[0],testCases[1])
# fileReader.close()
#
#def testIndentation3(self):
# file = open("SwiftFormatter/indentTest.test", "r")
# fileReader = reader.FileReader(file)
# testCases = fileReader.parseTestCaseAtIndex(2)
# self.stringsAreEqual(testCases[0],testCases[1])
# fileReader.close()
#
#def testIndentation4(self):
# file = open("SwiftFormatter/indentTest.test", "r")
# fileReader = reader.FileReader(file)
# testCases = fileReader.parseTestCaseAtIndex(3)
# self.stringsAreEqual(testCases[0],testCases[1])
# fileReader.close()
#
#def testIndentation5(self):
# file = open("SwiftFormatter/indentTest.test", "r")
# fileReader = reader.FileReader(file)
# testCases = fileReader.parseTestCaseAtIndex(4)
# self.stringsAreEqual(testCases[0],testCases[1])
# fileReader.close()
#
#def testIndentation6(self):
# file = open("SwiftFormatter/indentTest.test", "r")
# fileReader = reader.FileReader(file)
# testCases = fileReader.parseTestCaseAtIndex(4)
# self.stringsAreEqual(testCases[0],testCases[1])
# fileReader.close()
#
#def testSpaces1(self):
# d = ""
if __name__ == '__main__':
unittest.main()
#testFormatter = TestFormatting()
#testFormatter.test()