def load(args): if verbose: print "foo: loading sources into model" model = api.create_model() for source in args.sources: if args.verbose: print " -", source.name api.load(source.read(), model) source.seek(0) return model
def test_source(self): """ Make sure that the given source file is parsed and emitted correctly. To do this, we parse & emit two times. The first and second emission, should be exactly the same. """ input = open(file).read() output1 = api.load(input).accept(Dumper()) output2 = api.load(output1).accept(Dumper()) if output1 != output2: for line in unified_diff(output1.split("\n"), output2.split("\n")): print_stderr(line) assert False, "Roundtripping of " + file + "failed."
def test_property_as_obj_for_method_call(self): src = """module test function t1(node) { node.queue.remove() }""" model = api.load(src) self.assertEqual(model.accept(Dumper()), src)
def test_with_nodes(self): src = """ module test const interval = 1000 @every(interval) with nodes do function(node) {} """ model = api.load(src) self.infer(model, 4) # after (assert successes) m = model.modules["test"] f = m.functions self.assertIsInstance(f.objects.values()[0].type, VoidType)
def test_default_environment(test_self): model = api.load("module test function test() {}") class TestVisitor(SemanticVisitor): def __init__(self): super(TestVisitor, self).__init__() self.prefix = "visit_" def visit_Module(self, module): test_self.assertTrue(self.env.is_empty()) def visit_FunctionDecl(self, module): test_self.assertIsInstance(self.env["test"], FunctionDecl) test_self.assertIsInstance(self.env["nodes"], Domain) model.accept(TestVisitor())
def test_simple_function_and_call(self): src = """ module test function abc() {} function def() { abc() } """ model = api.load(src) self.infer(model, 3) # after (assert successes) f = model.modules["test"].functions self.assertIsInstance(f["abc"].type, VoidType) self.assertIsInstance(f["def"].type, VoidType) self.assertIsInstance(f["def"].body.statements[0].function.type, VoidType)
def test_unvisited_tupletypes(self): source = """ module test extend nodes with { queue : [timestamp, byte*]* = [] } after nodes transmit do function(from, to, hop, payload) { hop.queue.push( [ 1000, payload ] ) } """ sm = api.load(source) api.infer(sm, silent=True) api.check(sm, silent=True) generator = Generator(Bunch({"language":"c", "platform":"moose"})) cm = generator.construct_code_model(sm) cm.accept(C.Transformer()) # Module > Section > StructuredType > Property queue_prop = cm.select("node_t", "def") \ .children[3] \ .children[4] self.assertIsInstance(queue_prop.type, ManyType) self.assertIsInstance(queue_prop.type.type, NamedType) # Module > Section > Function > FunctionCall .arguments[0] hop_queue = cm.select("nodes-test", "dec") \ .children[1] \ .children[0] \ .arguments[0] \ .variable # AddressOf in between for push !! # TupleType -> NamedType self.assertIsInstance(hop_queue.type, ManyType) self.assertIsInstance(hop_queue.type.type, NamedType) # NamedTypes must match self.assertEqual(queue_prop.type.type.name, hop_queue.type.type.name)
def test_simple_function_and_call_with_parameters(self): src = """ module test function abc(a, b, c) {} function def() { abc(1, true, 1.0) } """ model = api.load(src) self.infer(model, 6) # after (assert successes) f = model.modules["test"].functions self.assertIsInstance(f["abc"].type, VoidType) self.assertIsInstance(f["def"].type, VoidType) self.assertIsInstance(f["def"].body.statements[0].function.type, VoidType) self.assertIsInstance(f["abc"].parameters[0].type, IntegerType) self.assertIsInstance(f["abc"].parameters[1].type, BooleanType) self.assertIsInstance(f["abc"].parameters[2].type, FloatType)
def test_nodes_receive_handler(self): src = """ module test function abc(from, hop, to, payload) {} after nodes receive do abc """ model = api.load(src) self.infer(model, 7) # after (assert successes) m = model.modules["test"] f = m.functions self.assertIsInstance(f["abc"].type, VoidType) self.assertIsInstance(f["abc"].parameters[0].type, ObjectType) # from self.assertIsInstance(f["abc"].parameters[1].type, ObjectType) # hop self.assertIsInstance(f["abc"].parameters[2].type, ObjectType) # to self.assertIsInstance(f["abc"].parameters[3].type, ObjectType) # payload self.assertIsInstance(m.executions[0].event.type, VoidType) self.assertIsInstance(m.executions[0].executed.type, VoidType)
def test_case(self): src = """ module test after nodes receive do function(me, sender, from, hop, to, payload) { case payload { contains( [ #heartbeat, time, sequence, signature ] ) {} } } """ model = api.load(src) self.infer(model, 17) # after (assert successes) m = model.modules["test"] f = m.functions self.assertIsInstance(f.objects.values()[0].type, VoidType) contains = f.objects.values()[0].body.statements[0].cases[0] self.assertIsInstance(contains.arguments[0].type.subtype, ByteType) arguments = contains.arguments[0].expressions self.assertIsInstance(arguments[0].type, AtomType) # heartbeat self.assertIsInstance(arguments[1].type, ByteType) # time self.assertIsInstance(arguments[2].type, ByteType) # sequence self.assertIsInstance(arguments[3].type, ByteType) # signature