Beispiel #1
0
    def test_is_poetic(self):
        transpiler = Transpiler()
        py_line = transpiler.transpile_line("My life is a mess\n")
        self.assertEqual(py_line, "my_life = 14\n")

        py_line = transpiler.transpile_line("Life is a one day after another\n")
        self.assertEqual(py_line, "Life = 13357\n")
Beispiel #2
0
    def test_common_variables(self):
        transpiler = Transpiler()
        py_line = transpiler.transpile_line("My variable is 5\n")
        self.assertEqual(py_line, 'my_variable = 5\n')

        py_line = transpiler.transpile_line("Shout the total\n")
        self.assertEqual(py_line, 'print(the_total)\n')
Beispiel #3
0
    def test_is_poetic_with_hyphen(self):
        transpiler = Transpiler()
        py_line = transpiler.transpile_line("Greed is all-consuming\n")
        self.assertEqual(py_line, "Greed = 3\n")

        py_line = transpiler.transpile_line("God is power-hungry\n")
        self.assertEqual(py_line, "God = 2\n")
Beispiel #4
0
    def test_simple_variables(self):
        transpiler = Transpiler()
        py_line = transpiler.transpile_line("X is 2\n")
        self.assertEqual(py_line, 'X = 2\n')

        py_line = transpiler.transpile_line("Tommy is a rockstar\n")
        self.assertEqual(py_line, 'Tommy = 18\n')
def command_line():

    # connnect input
    if args.stdin:
        lyrics = sys.stdin
    else:
        lyrics = open(args.input, 'r')

    # connect output
    if args.stdout:
        enc = False
        output = sys.stdout
    else:
        enc = True
        output = open(args.output, 'wb', 0)

    # Read, Convert, Write, loop
    transpiler = Transpiler()
    for line in lyrics:
        output.write(encode(transpiler.transpile_line(line), enc))

    # close input
    if not args.stdin:
        lyrics.close()

    # close output
    if not args.stdout:
        output.close()
Beispiel #6
0
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

        transpiler = Transpiler()

        converted_code = ''
        with open(rock_file, 'r') as rockstar_file:
            for line in rockstar_file:
                converted_code += transpiler.transpile_line(line)

        with open(file_name + ".py", 'r') as expected:
            expected_code = expected.read()

        check_files_identical(expected_code, converted_code)

    suite = unittest.defaultTestLoader.discover(".", pattern='test_*.py')
    runner = unittest.TextTestRunner()
    runner.run(suite)
Beispiel #7
0
def command_line():
    sys.tracebacklimit = 0
    lyrics = args.stdin or open(args.input, "rb")
    output = BytesIO() if args.exec else args.stdout or open(args.output, "wb")

    transpiler = Transpiler()
    line_number = 1
    for line in lyrics:
        line = line.decode("utf8")
        try:
            output.write(transpiler.transpile_line(line).encode("utf8"))
        except SyntaxError as err:
            raise SyntaxError(err.msg + f":\n{line_number}.\t{line}")
        line_number += 1

    if args.exec:
        exec(output.getvalue())
    if args.stdin is None:
        lyrics.close()
    if args.stdout is None:
        output.close()
Beispiel #8
0
def main():
    identical_test_files = [(file, file.with_suffix(".py"))
                            for file in test_path.iterdir()
                            if file.suffix == ".rock"]
    for rock_path, py_path in identical_test_files:
        print("testing", rock_path.name)

        assert py_path.is_file(), f"{py_path} does not exist"

        transpiler = Transpiler()
        converted_code = ""
        with rock_path.open() as rockstar_file:
            for line in rockstar_file:
                converted_code += transpiler.transpile_line(line)

        with py_path.open() as expected_file:
            expected_code = expected_file.read()

        check_files_identical(expected_code, converted_code)

    suite = unittest.defaultTestLoader.discover(test_path, pattern="test_*.py")
    runner = unittest.TextTestRunner()
    runner.run(suite)
Beispiel #9
0
 def test_until_greater(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line(
         'Until Tommy is stronger than Superman\n')
     self.assertEqual(py_line, 'while not Tommy > Superman:\n')
Beispiel #10
0
 def test_less_equals(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line(
         'If my mind is as low as my soul\n')
     self.assertEqual(py_line, 'if my_mind <= my_soul:\n')
Beispiel #11
0
 def test_while_equals(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('While Tommy is nobody\n')
     self.assertEqual(py_line, 'while Tommy == False:\n')
Beispiel #12
0
 def test_greater_equals(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('If Love is as high as mountain\n')
     self.assertEqual(py_line, 'if Love >= mountain:\n')
Beispiel #13
0
 def test_less(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('If Tommy is weaker than a worm\n')
     self.assertEqual(py_line, 'if Tommy < a_worm:\n')
Beispiel #14
0
 def test_put_into_bool(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Put nothing into my hand\n")
     self.assertEqual(py_line, "my_hand = False\n")
Beispiel #15
0
 def test_let_be_string(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('Let the letter be "R"\n')
     self.assertEqual(py_line, 'the_letter = "R"\n')
Beispiel #16
0
 def test_not(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('If not number is 28\n')
     self.assertEqual(py_line, 'if not number == 28:\n')
Beispiel #17
0
 def test_aint(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('If Tommy ain\'t nobody\n')
     self.assertEqual(py_line, 'if Tommy != False:\n')
Beispiel #18
0
 def __run_block_test(self, rock, py):
     transpiler = Transpiler()
     transpiled = [transpiler.transpile_line(line) for line in rock]
     self.assertEqual(len(py), len(transpiled))
     for py_line, transpiled_line in zip(py, transpiled):
         self.assertEqual(py_line, transpiled_line)
Beispiel #19
0
 def test_put_into_string(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line('Put "letter" into the envelope\n')
     self.assertEqual(py_line, 'the_envelope = "letter"\n')
Beispiel #20
0
 def test_put_into_numeric(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Put 3.14 into PI\n")
     self.assertEqual(py_line, "Pi = 3.14\n")
Beispiel #21
0
 def test_is_bool(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Life is ok\n")
     self.assertEqual(py_line, "Life = True\n")
Beispiel #22
0
 def test_put_into_poetic_assigns_variable(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Put a flower into the vase\n")
     self.assertEqual(py_line, "the_vase = a_flower\n")
Beispiel #23
0
 def test_and(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line(
         'If Tommy is nobody and Billy is nobody\n')
     self.assertEqual(py_line, 'if Tommy == False and Billy == False:\n')
Beispiel #24
0
 def test_let_be_poetic(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Let Stuart be a yellow Minion\n")
     self.assertEqual(py_line, "Stuart = 166\n")
Beispiel #25
0
 def test_nor(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line(
         'If Tommy is nobody nor Billy is nobody\n')
     self.assertEqual(py_line, 'if not Tommy == False or Billy == False:\n')
Beispiel #26
0
 def test_let_be_bool(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Let my beer be empty\n")
     self.assertEqual(py_line, "my_beer = False\n")
Beispiel #27
0
 def test_greater(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line(
         'If Tommy is stronger than Superman\n')
     self.assertEqual(py_line, 'if Tommy > Superman:\n')
Beispiel #28
0
 def test_let_be_numeric(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line("Let One be 0\n")
     self.assertEqual(py_line, "One = 0\n")
Beispiel #29
0
 def test_proper_variables(self):
     transpiler = Transpiler()
     py_line = transpiler.transpile_line(
         "Master Of The Universe is nothing\n")
     self.assertEqual(py_line, 'Master_Of_The_Universe = False\n')
Beispiel #30
0
 def __run_block_test(self, rock, py):
     transpiler = Transpiler()
     transpiled = [transpiler.transpile_line(line) for line in rock]
     self.assertEqual(len(py), len(transpiled))
     for i in range(len(py)):
         self.assertEqual(py[i], transpiled[i])