-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestNameGenerator.py
More file actions
187 lines (147 loc) · 7.31 KB
/
TestNameGenerator.py
File metadata and controls
187 lines (147 loc) · 7.31 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import sublime, sublime_plugin, re
class ConvertTestNameCommand(sublime_plugin.TextCommand):
def isAllowedSyntax(self, syntax):
return ((syntax == "PHP") | (syntax == "JavaScript"))
# keep only alphanum and ",", ".", "(", ")"
def getCleanLineContents(self, lineContents):
pattern = TextHelper.patternCleanLine()
return re.sub(pattern, '', lineContents)
def getFallbackLineContent(self, i):
return "blank" + str(i)
# convert to CamelCase and keep only alphanumeric chars
def getMethodName(self, lineContents):
pattern = TextHelper.patternMethodName()
return re.sub(pattern, '', lineContents.title())
def getExistingMethod(self, lineContents, cursorLine):
pageContents = SublimeConnect.getPageContents(cursorLine)
lineContents = re.escape(lineContents)
pattern = TextHelper.patternExistingMethodPHP(lineContents)
searchExisting = re.search(pattern, pageContents)
if searchExisting != None:
if (searchExisting.group(7) is None) == False:
return searchExisting.group(7)
return False
# get a list of [test name with separate words, test name with no spaces in between and camel cased] from the current cursor(s) or selection(s)
def run(self, edit):
SublimeConnect.init(self)
currentSyntax = SublimeConnect.getSyntax()
if False == self.isAllowedSyntax(currentSyntax):
return
i = 0
for cursor in SublimeConnect.getCursors():
cursorLine = SublimeConnect.getLine(cursor)
lineContents = SublimeConnect.getLineContents(cursorLine)
phrase = self.getCleanLineContents(lineContents)
# if extracted line contents are empty, use "blank"
if not phrase:
phrase = self.getFallbackLineContent(i)
i += 1
if (currentSyntax == "PHP"):
methodName = self.getMethodName(phrase)
existingMethod = self.getExistingMethod(lineContents, cursorLine)
if existingMethod == False:
SublimeConnect.insertMethodName(edit, cursorLine, TextHelper.prepareTestBlockPHP(phrase, methodName))
else:
SublimeConnect.updateMethodNamePHP(edit, cursorLine, methodName, existingMethod)
else:
# JS will add a new test method because the text is inside the test method
SublimeConnect.insertMethodName(edit, cursorLine, TextHelper.prepareTestBlockJS(phrase))
SublimeConnect.close()
class TextHelper():
def patternExistingMethodPHP(lineContents):
# complete block: return r'\/\*([\*\n\s]+)' + lineContents + '([a-zA-Z0-9\n\s\*@]+)\*\/([\s]+)(public)?(\s)?function(\s)?test([a-zA-Z0-9_]+)(\s\n)?\('
return r'([\*\n\s]+)' + lineContents + '([a-zA-Z0-9\n\s\*@]+)\*\/([\s]+)(public)?(\s)?function(\s)?test([a-zA-Z0-9_]+)(\s\n)?\('
def patternCleanLine():
return r'[^a-zA-Z0-9 \(\)_\:\.\,\[\]]'
def patternMethodName():
return r'[^a-zA-Z0-9_]'
def prepareTestBlockPHP(phrase, methodName):
tab = SublimeConnect.getWhitespaceTab()
return tab + "/**\n" + tab + " * " + phrase + "\n" + tab + " */\n" + tab + "public function test" + methodName + "()\n" + tab + "{\n" + tab + tab + "\n" + tab + "}\n" + tab
# will generate Jasmine blocks
def prepareTestBlockJS(phrase):
tab = SublimeConnect.getWhitespaceTab()
examineNameTypeParts = phrase.split(" ")
if (examineNameTypeParts[0].lower() == "describe"):
phrase = " ".join(examineNameTypeParts.pop(0)) # remove the "describe" prefix
return TextHelper.getJasmineDescribeBlock(phrase, tab)
else:
return TextHelper.getJasmineItBlock(phrase, tab)
def getJasmineDescribeBlock(phrase, tab):
return tab + "describe('" + phrase + "', function () {\n\n" + tab + "});\n\n" + tab
def getJasmineItBlock(phrase, tab):
return tab + tab + "it('" + phrase + "', function () {\n" + tab + tab + tab + "\n" + tab + "" + tab + "});\n\n" + tab
# connector to the sublime api
class SublimeConnect():
context = False
@classmethod
def close(self):
view = self.context.view
queueMoveCursors = []
deltaCol = 1 if self.getWhitespaceTab() == "\t" else 4
for cursor in view.sel():
target = view.text_point(self.getCursorRow(cursor) - 2, self.getCursorCol(cursor) + deltaCol)
queueMoveCursors.append(sublime.Region(target))
pass
view.sel().clear()
for cursor in queueMoveCursors:
view.sel().add(cursor)
pass
@classmethod
def getCursorRow(self, cursor):
return self.context.view.rowcol(cursor.begin())[0]
@classmethod
def getCursorCol(self, cursor):
return self.context.view.rowcol(cursor.begin())[1]
@classmethod
def find_all(self, a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub) # use start += 1 to find overlapping matches
@classmethod
def getPageContents(self, cursorLine):
cursorPosition = cursorLine.begin()
return self.context.view.substr(sublime.Region(0, self.context.view.size()))
@classmethod
def updateMethodNamePHP(self, edit, cursorLine, methodName, existingMethodResults):
tab = SublimeConnect.getWhitespaceTab()
searchExisting = "test" + existingMethodResults # search for current test name
methodPosition = list(SublimeConnect.find_all(self.getPageContents(cursorLine), searchExisting))
for methodPositionCursor in methodPosition:
if (methodPositionCursor >= cursorLine.begin()):
print(methodPositionCursor, searchExisting, cursorLine.begin())
region = sublime.Region(methodPositionCursor, methodPositionCursor + len(searchExisting))
lineToUpdate = self.context.view.line(region)
lineContents = self.context.view.substr(lineToUpdate).strip().replace(searchExisting, "test" + methodName)
self.context.view.replace(edit, lineToUpdate, tab + lineContents)
return
@classmethod
def insertMethodName(self, edit, cursorLine, testBlock):
self.context.view.replace(edit, cursorLine, testBlock)
@classmethod
def getLine(self, region):
return self.context.view.line(region)
@classmethod
def getLineContents(self, cursorLine):
return self.context.view.substr(cursorLine).strip()
@classmethod
def getCursors(self):
return self.context.view.sel()
@classmethod
def init(self, mainContext):
self.context = mainContext
return self
# returns: PHP,JavaScript,...
@classmethod
def getSyntax(self):
return self.context.view.settings().get('syntax').replace('.tmLanguage', '').split("/")[1]
# returns tab character or spaces as an indent unit, based on the editor's settings
@classmethod
def getWhitespaceTab(self):
settings = self.context.view.settings()
if (settings.get('translate_tabs_to_spaces')):
return "".rjust(settings.get('tab_size'), ' ')
return "\t";