def test_unary(self): compiler = Compiler() result = compiler.compile("+1") self.assertIsInstance(result, UnaryOperator) self.assertEqual(result.operator, "+") self.assertEqual(result.operand, 1)
def test_binary(self): compiler = Compiler() result = compiler.compile("101 + 202") self.assertIsInstance(result, BinaryOperator) self.assertEqual(result.operator, "+") self.assertEqual(result.left, 101) self.assertEqual(result.right, 202)
def test_strings(self): compiler = Compiler() result = compiler.compile("''") self.assertEqual(result, "") result = compiler.compile("'a \\' quote'") self.assertEqual(result, "a ' quote")
def test_variable(self): compiler = Compiler() result = compiler.compile("foo") self.assertIsInstance(result, Variable) self.assertEqual(result.name, "foo") self.assertEqual(result.reference, ["foo"]) result = compiler.compile("foo.bar.baz") self.assertIsInstance(result, Variable) self.assertEqual(result.name, "foo.bar.baz") self.assertEqual(result.reference, ["foo", "bar", "baz"])
def test_basic(self): compiler = Compiler() result = compiler.compile("1") self.assertEqual(result, 1) self.assertIsInstance(result, int) result = compiler.compile("1.2") self.assertEqual(result, 1.2) self.assertIsInstance(result, float) result = compiler.compile("'a string'") self.assertEqual(result, "a string") self.assertIsInstance(result, str)
def test_operator(self): compiler = Compiler() result = compiler.compile("+1") self.assertIsInstance(result, UnaryOperator) self.assertEqual(result.operator, "+") self.assertEqual(result.operand, 1) result = compiler.compile("1 and 2") self.assertIsInstance(result, BinaryOperator) self.assertEqual(result.operator, "and") self.assertEqual(result.left, 1) self.assertEqual(result.right, 2)
def test_function(self): compiler = Compiler() result = compiler.compile("foo()") self.assertIsInstance(result, Function) self.assertEqual(result.name, "foo") self.assertEqual(result.reference, ["foo"]) self.assertEqual(result.args, []) result = compiler.compile("foo.bar.baz()") self.assertIsInstance(result, Function) self.assertEqual(result.name, "foo.bar.baz") self.assertEqual(result.reference, ["foo", "bar", "baz"]) self.assertEqual(result.args, []) result = compiler.compile("foo(10,20,30)") self.assertIsInstance(result, Function) self.assertEqual(result.args, [10, 20, 30])