Ejemplo n.º 1
0
    def test_assign_word_print_complicated(self):
        test_string = \
            """color is a word
set color to "blue"
print color
c2 is a word
set c2 to "green"
set color to c2
"""
        correct_translation = \
            """color = None
color = "blue"
print_to_console(color)
c2 = None
c2 = "green"
color = c2
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 2
0
    def test_function_invocation_no_params(self):
        test_string = \
            """define moveBackwardFive
{
    drive backward 5
}
define moveForwardThenBackward
{
    drive forward 5
    moveBackwardFive
}
moveForwardThenBackward
"""
        correct_translation = \
            """def moveBackwardFive():
    translate_car(5, CarDirection.BACKWARDS)
def moveForwardThenBackward():
    translate_car(5, CarDirection.FORWARDS)
    moveBackwardFive()
moveForwardThenBackward()
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 3
0
    def test_function_invocation_with_two_parameters(self):
        test_string = \
            """define turnLeftThenDriveStraight using numStepsTurn \
            (number) and numStepsDrive (number)
{
turn left
drive forward numStepsTurn steps
turn right
drive forward numStepsDrive steps
}
turnLeftThenDriveStraight 5 10
"""
        correct_translation = \
            """def turnLeftThenDriveStraight(numStepsTurn, numStepsDrive):
    rotate_car(WheelDirection.LEFT)
    translate_car(numStepsTurn, CarDirection.FORWARDS)
    rotate_car(WheelDirection.RIGHT)
    translate_car(numStepsDrive, CarDirection.FORWARDS)
turnLeftThenDriveStraight(5, 10)
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 4
0
    def test_if_elseif_else_statement(self):
        test_string = \
            """if 1
{
    print "yay"
}
elseIf 2
{
    print "no"
}
else
{
    print "done"
}
"""
        correct_translation = \
            """if 1:
    print_to_console("yay")
elif 2:
    print_to_console("no")
else:
    print_to_console("done")
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 5
0
    def test_if_statement_nested(self):
        test_string = \
            """if 1
{
    print "yay"
    if 1
    {
        print "yahoo"
    }
}
"""
        correct_translation = \
            """if 1:
    print_to_console("yay")
    if 1:
        print_to_console("yahoo")
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 6
0
    def test_loop_for_nested(self):
        test_string = \
            """myCounter is a number
set myCounter to 10
myCounter2 is a number
set myCounter2 to 10
repeat myCounter times
{
    drive forward 1 step
    repeat myCounter2 times
    {
        drive forward 1 step
    }
}
"""
        correct_translation = \
            """myCounter = None
myCounter = 10
myCounter2 = None
myCounter2 = 10
for x in range(myCounter):
    translate_car(1, CarDirection.FORWARDS)
    for x in range(myCounter2):
        translate_car(1, CarDirection.FORWARDS)
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 7
0
    def test_loop_while(self):
        test_string = \
            """myCounter is a number
set myCounter to 1
repeat if myCounter is not 5
{
    drive forward 1 step
    set myCounter to myCounter + 1
}
"""
        correct_translation = \
            """myCounter = None
myCounter = 1
while myCounter != 5:
    translate_car(1, CarDirection.FORWARDS)
    myCounter = ((myCounter) + (1))
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 8
0
    def test_drive_backwards(self):
        test_string1 = \
            """drive backwards 10 steps
"""
        test_string2 = \
            """drive backward 10 steps
"""
        test_string3 = \
            """drive backwards 10 step
"""
        test_string4 = \
            """drive backward 10 step
"""
        correct_translation = \
            """translate_car(10, CarDirection.BACKWARDS)
"""

        ast1 = Parser.parseString(test_string1)
        ast2 = Parser.parseString(test_string1)
        ast3 = Parser.parseString(test_string1)
        ast4 = Parser.parseString(test_string1)

        self.assertEqual(len(ast1.errors), 0)
        self.assertEqual(len(ast2.errors), 0)
        self.assertEqual(len(ast3.errors), 0)
        self.assertEqual(len(ast4.errors), 0)

        result1 = Compiler.getPythonCode(test_string1)
        result2 = Compiler.getPythonCode(test_string2)
        result3 = Compiler.getPythonCode(test_string3)
        result4 = Compiler.getPythonCode(test_string4)

        saErrors1 = SemanticAnalyzer.analyzeStart(ast1)
        saErrors2 = SemanticAnalyzer.analyzeStart(ast2)
        saErrors3 = SemanticAnalyzer.analyzeStart(ast3)
        saErrors4 = SemanticAnalyzer.analyzeStart(ast4)

        self.assertEqual(len(saErrors1), 0)
        self.assertEqual(len(saErrors2), 0)
        self.assertEqual(len(saErrors3), 0)
        self.assertEqual(len(saErrors4), 0)

        self.assertEqual(result1[0], correct_translation)
        self.assertEqual(result2[0], correct_translation)
        self.assertEqual(result3[0], correct_translation)
        self.assertEqual(result4[0], correct_translation)
Ejemplo n.º 9
0
    def test_built_in_functions_params(self):
        test_string = \
            """drive forwards five steps
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 10
0
    def test_template(self):
        test_string = \
            """
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 11
0
    def test_print_undeclared_var(self):
        test_string = \
            """print myNum
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 12
0
    def test_basic(self):
        test_string = \
            """drive forward 5 steps
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 13
0
    def test_calling_nonexistant_function(self):
        test_string = \
            """fullTurn "left"
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 14
0
    def test_assignments_var_str_literal(self):
        test_string = \
            """myNum is a number
set myNum to "hello"
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 15
0
    def test_set_undeclared_var(self):
        test_string = \
            """set myNum to 10
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        #should have an error saying mySecondNum doesnt exist
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 16
0
    def test_var_uninitialized(self):
        test_string = \
            """myNum is a number
drive forward myNum steps
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 17
0
    def test_assignments_var_number(self):
        test_string = \
            """myWord is a word
set myWord to 10
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 18
0
    def test_template(self):
        test_string = \
            """
"""
        correct_translation = \
            """"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 19
0
    def test_get_car_position(self):
        test_string = \
            """print getCarPosition
"""
        correct_translation = \
            """print_to_console(getCurrentPosition())
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 20
0
    def test_print_var(self):
        test_string = \
            """myNum is a number
set myNum to 10
print myNum
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 21
0
    def test_compare_num_to_num(self):
        test_string = \
            """if 10 > 5
{
    print "good"
}
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 22
0
    def test_access_passed_in_var_in_func(self):
        test_string = \
            """define moveForward using numSteps (number)
{
    drive forward numSteps steps
}
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 23
0
    def test_compare_strL_to_strL_not(self):
        test_string = \
            """if "hello" is not "hi"
{
    print "good"
}
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 24
0
    def test_compare_num_str_is(self):
        test_string = \
            """if 10 is "hello"
{
    print "bad"
}
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 25
0
    def test_while_loop_bad(self):
        test_string = \
            """repeat five times
{
    print "hi"
}
"""

        ast = Parser.parseString(test_string)
        self.assertEqual(len(ast.errors), 0, "Test failed at parser.")

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 1)
Ejemplo n.º 26
0
    def test_empty_statement(self):
        test_string = \
            """
"""
        correct_translation = \
            """"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 27
0
    def test_var_declared_in_func(self):
        test_string = \
            """define moveForwardFive
{
    myNum is a number
    set myNum to 5
    drive forward myNum steps
}
"""

        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)
Ejemplo n.º 28
0
    def test_comment_singleline(self):
        test_string = \
            """:) this is a single line comment
drive forward 5 steps
"""
        correct_translation = \
            """"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 29
0
    def test_all_expression(self):
        test_string = \
            """print (1 + 2 * (3 + 4))
"""
        correct_translation = \
            """print_to_console(((1) + (((2) * (((3) + (4)))))))
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Ejemplo n.º 30
0
    def test_declare(self):
        test_string = \
            """myNum is a number
"""
        correct_translation = \
            """myNum = None
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        saErrors = SemanticAnalyzer.analyzeStart(ast)
        self.assertEqual(len(saErrors), 0)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)