Skip to content

Instantly share code, notes, and snippets.

@Nassty
Created April 6, 2011 13:44
Show Gist options
  • Save Nassty/905660 to your computer and use it in GitHub Desktop.
Save Nassty/905660 to your computer and use it in GitHub Desktop.
import re
import sys
import string
class Parser:
def __init__(self, text):
self.elements = text.split("\n")
self.endings = {"Given":"Assumption", "When":"Action",\
"Then":"Assertion"}
self.class_template = """
@%s("%s")
public void %s(%s)
{
// %s
}"""
def underscore_to_case(self, element):
words = element.replace("_", " ")\
.replace("<", " ").replace(">", " ")\
.replace(",", " ").replace("'", " ")\
.split()
w = [words[0].lower()]
for word in words[1:]:
w.append(word.capitalize())
return "".join(w)
def get_elements(self, elements):
response = []
for element in elements:
words = element.split()
if words:
current_element = [words[0], " ".join(words[1:])]
if words[0] in ["Given", "When", "Then", "And"] \
and current_element not in response:
response.append(current_element)
return response
def generate_method_name(self, element):
name = self.underscore_to_case(element[1])[0:60]
ending = self.endings[element[0]]
return "%s%s" % (name, ending)
def run(self):
elements = self.get_elements(self.elements)
previous_method = "Given"
response = []
for element in elements:
if element[0] == "And":
element[0] = previous_method
else:
previous_method = element[0]
params_match = re.findall(r"<(.*?)>", element[1])
if params_match:
e = []
for param in params_match:
e.append("""@Named("%s") String %s""" %( param, \
self.underscore_to_case(param) ) )
params = ", ".join(e)
else:
params = ""
method_name = self.generate_method_name(element)
response.append(self.class_template %(element[0], \
element[1], method_name, params, element))
return "\n".join(response)
if __name__ == "__main__":
if sys.argv[1:]:
print Parser(open(sys.argv[1]).read()).run()
else:
print "Usage: python %s <story_file_path>" % sys.argv[0]
@marianoguerra
Copy link

te pinto BDD?

@Nassty
Copy link
Author

Nassty commented Apr 7, 2011

hace tiempo que hacemos bdd. me pintó automatizar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment