diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/rockstarpy/rockstar.py b/rockstarpy/rockstar.py index 2e71e87..dcb096b 100644 --- a/rockstarpy/rockstar.py +++ b/rockstarpy/rockstar.py @@ -155,7 +155,7 @@ def convert_code(rockstar_code, py_rockstar): py_line = find_proper_variables(py_line) py_line = find_common_variables(py_line) - py_line = re.sub(r'([A-Za-z]+(?: [A-Za-z]+)*) taking ([A-Za-z_]+(?:, [A-Za-z_]+)*)', r'\g<1>(\g<2>)', py_line) + py_line = re.sub(r'([A-Za-z]+(?: [A-Za-z]+)*) taking ([A-Za-z0-9_]+(?:, [A-Za-z_0-9]+)*)', r'\g<1>(\g<2>)', py_line) line_named = find_named(py_line) most_recently_named = line_named if line_named else most_recently_named @@ -177,4 +177,4 @@ def convert_code(rockstar_code, py_rockstar): py_rockstar.close() else: - print('Usage: python rockstar.py input.{rock|rockstar|lyrics} output.py') \ No newline at end of file + print('Usage: python rockstar.py input.{rock|rockstar|lyrics} output.py') diff --git a/tests/fizz.py b/tests/fizz.py new file mode 100644 index 0000000..908db19 --- /dev/null +++ b/tests/fizz.py @@ -0,0 +1,20 @@ +def Midnight(your_heart, your_soul): + while your_heart >= your_soul: #this is a comment + your_heart = your_heart - your_soul + return your_heart +Desire = 100 +my_world = False +Fire = 3 #i love comments +Hate = 5 +while not my_world == Desire: + my_world += 1 + if Midnight(my_world, Fire) == False and Midnight(my_world, Hate) == False: + print("FizzBuzz!") + continue + if Midnight(my_world, Fire) == False: + print("Fizz!") + continue + if Midnight(my_world, Hate) == False: + print("Buzz!") + continue + print(my_world) diff --git a/tests/fizz.rock b/tests/fizz.rock new file mode 100644 index 0000000..b8148bf --- /dev/null +++ b/tests/fizz.rock @@ -0,0 +1,26 @@ +Midnight takes your heart and your soul +While your heart is as high as your soul (this is a comment) +Put your heart without your soul into your heart + +Give back your heart + + +Desire is a lovestruck ladykiller +My world is nothing +Fire is ice (i love comments) +Hate is water +Until my world is Desire, +Build my world up +If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing +Shout "FizzBuzz!" +Take it to the top + +If Midnight taking my world, Fire is nothing +Shout "Fizz!" +Take it to the top + +If Midnight taking my world, Hate is nothing +Say "Buzz!" +Take it to the top + +Whisper my world diff --git a/tests/run_tests.py b/tests/run_tests.py new file mode 100644 index 0000000..8405e92 --- /dev/null +++ b/tests/run_tests.py @@ -0,0 +1,46 @@ +import os +from io import StringIO +import difflib + +from rockstarpy.rockstar import convert_code + + +def check_files_identical(expected, actual): + diff = difflib.unified_diff( + expected, + actual, + fromfile='expected', + tofile='actual', + ) + diff = list(diff) + if len(diff): + for d in diff: + print(''.join(diff)) + assert False, "There are differences" + +def main(): + files = os.listdir('.') + rock_files = filter(lambda f: f.split(".")[-1] in ['rock','rockstar','lyrics'] , files) + py_files = set(filter(lambda f: f.endswith('.py'), files)) + for rock_file in rock_files: + print("testing", rock_file) + file_name = os.path.splitext(rock_file)[0] # take off extension + py_file = file_name + ".py" + assert py_file in py_files, "Did not create a corrosponding expected output for " + rock_file + + converted_code = StringIO() + rockstar_code = "" + with open(rock_file, 'r') as rockstar_file: + rockstar_code = rockstar_file.readlines() + + convert_code(rockstar_code, converted_code ) + with open(file_name +".py", 'r') as expected: + expected_code = expected.read() + actual_code = converted_code.getvalue() + check_files_identical(expected_code, actual_code) + converted_code.close() + +if __name__ == '__main__': + main() + + diff --git a/tests/testfunccall.numbers.py b/tests/testfunccall.numbers.py new file mode 100644 index 0000000..6689f00 --- /dev/null +++ b/tests/testfunccall.numbers.py @@ -0,0 +1,3 @@ +def Multiply(Love, Life): + return Love * Life +Multiply(3, 444) diff --git a/tests/testfunccall.numbers.rock b/tests/testfunccall.numbers.rock new file mode 100644 index 0000000..9bbfdfb --- /dev/null +++ b/tests/testfunccall.numbers.rock @@ -0,0 +1,4 @@ +Multiply takes Love and Life +Give back Love of Life + +Multiply taking 3, 444 diff --git a/tests/testfunccall.rock b/tests/testfunccall.rock new file mode 100644 index 0000000..c55496e --- /dev/null +++ b/tests/testfunccall.rock @@ -0,0 +1,4 @@ +Multiply takes Love and Life +Give back Love of Life + +Multiply taking 3, 4 diff --git a/tests/testfunccall.variables.py b/tests/testfunccall.variables.py new file mode 100644 index 0000000..f4a7598 --- /dev/null +++ b/tests/testfunccall.variables.py @@ -0,0 +1,5 @@ +def Multiply(Love, Life): + return Love * Life +my_heart = 1 +the_devil = 666 +Multiply(my_heart, the_devil) diff --git a/tests/testfunccall.variables.rock b/tests/testfunccall.variables.rock new file mode 100644 index 0000000..7c366a6 --- /dev/null +++ b/tests/testfunccall.variables.rock @@ -0,0 +1,6 @@ +Multiply takes Love and Life +Give back Love of Life + +Put 1 into my heart +Put 666 into the devil +Multiply taking my heart, the devil