def test_empty_list(self): empty_list = Type.empty_list() self.assertEqual(Type.empty_list(), empty_list) self.assertEqual(EmptyList(), empty_list) self.assertEqual("list[]", empty_list.type_name) self.assertEqual([], empty_list.default_value) self.assertEqual(Type("all", 0), empty_list.elem_type) self.assertEqual("list[]", str(empty_list))
def visitList(self, ctx: xDroneParser.ListContext) -> Expression: exprs = [self.visit(expr) for expr in ctx.expr()] if len(exprs) == 0: return Expression(Type.empty_list(), []) if not all(e.type == exprs[0].type for e in exprs): raise CompileError( "Elements in list {} should have the same type".format( [str(e) for e in exprs])) return Expression(Type.list_of(exprs[0].type), [e.value for e in exprs])
def test_list_remove_from_temp_empty_list_should_give_error(self): with self.assertRaises(CompileError) as context: generate_commands(""" main () { [].remove(); } """) self.assertTrue( "List {} has length {}, but has been removed at out-of-range index {}" .format(Expression(Type.empty_list(), []), 0, -1) in str( context.exception))
def test_eq(self): types1 = [ None, Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int())), Type.list_of(Type.list_of(Type.decimal())) ] types2 = [ None, Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int())), Type.list_of(Type.list_of(Type.decimal())) ] for i in range(len(types1)): for j in range(len(types2)): if i == j: self.assertEqual(types1[i], types2[j]) else: self.assertNotEqual(types1[i], types2[j]) self.assertEqual(Type.empty_list(), Type.empty_list()) for type in types1: if isinstance(type, ListType): self.assertEqual(Type.empty_list(), type) self.assertEqual(type, Type.empty_list()) else: self.assertNotEqual(Type.empty_list(), type) self.assertNotEqual(type, Type.empty_list())