import unittest import sys import os from utils import execute, kill, run_file, run_barebone def flatten(lst): new_lst = [] for f in lst: if isinstance(f, (list, tuple)): new_lst.extend(flatten(f)) else: new_lst.append(f) return new_lst bottom = [ '"model"', '"instantiate_bottom"', 1, '"add_node"', 1, '"Class"', '"add_node"', 1, '"Type"', '"add_node"', 1, '"Any"', '"add_node"', 1, '"String"', '"add_value"', 1, '"inheritance"', '"inheritance"', '"add_value"', 1, '"link"', '"link"', '"add_value"', 1, '"name"', '"name"', '"add_edge"', 1, '"l1"', '"Class"', '"Any"', '"add_edge"', 1, '"l2"', '"Type"', '"Any"', '"add_edge"', 1, '"l3"', '"Any"', '"Any"', '"add_edge"', 1, '"l4"', '"l3"', '"inheritance"', '"add_edge"', 1, '"l5"', '"Any"', '"Any"', '"add_edge"', 1, '"l6"', '"l5"', '"Any"', '"add_edge"', 1, '"l7"', '"l5"', '"link"', '"add_edge"', 1, '"l8"', '"l5"', '"String"', '"add_edge"', 1, '"l9"', '"l8"', '"name"', '"exit"', ] retype = [ '"model"', '"retype_model"', 1, 1, '"define_inheritance"', 1, '"l3"', '"retype"', 1, '"Class"', '"Class"', '"retype"', 1, '"Type"', '"Class"', '"retype"', 1, '"Any"', '"Class"', '"retype"', 1, '"String"', '"Type"', '"retype"', 1, '"inheritance"', '"String"', '"retype"', 1, '"link"', '"String"', '"retype"', 1, '"name"', '"String"', '"retype"', 1, '"l1"', '"l3"', '"retype"', 1, '"l2"', '"l3"', '"retype"', 1, '"l3"', '"l5"', '"retype"', 1, '"l4"', '"l8"', '"retype"', 1, '"l5"', '"l5"', '"retype"', 1, '"l6"', '"l3"', '"retype"', 1, '"l7"', '"l8"', '"retype"', 1, '"l8"', '"l5"', '"retype"', 1, '"l9"', '"l8"', '"exit"', ] instantiate_scd = [ '"model"', '"instantiate_model"', 1, 2, '"define_inheritance"', 2, '"l3"', '"instantiate_node"', 2, '"Class"', '"Place"', '"instantiate_node"', 2, '"Class"', '"Transition"', '"instantiate_node"', 2, '"Type"', '"Integer"', '"instantiate_link"', 2, '"l5"', '"P2T"', '"Place"', '"Transition"', '"instantiate_link"', 2, '"l5"', '"T2P"', '"Transition"', '"Place"', '"instantiate_named"', 2, '"l5"', '"tokens"', '"Place"', '"Integer"', '"instantiate_named"', 2, '"l5"', '"weight"', '"P2T"', '"Integer"', '"instantiate_named"', 2, '"l5"', '"weight"', '"T2P"', '"Integer"', '"exit"', ] instantiate_pn = [ '"model"', '"instantiate_model"', 2, 3, '"instantiate_node"', 3, '"Place"', '"p1"', '"instantiate_node"', 3, '"Place"', '"p2"', '"instantiate_node"', 3, '"Transition"', '"t1"', '"instantiate_node"', 3, '"Transition"', '"t2"', '"instantiate_link"', 3, '"P2T"', '"p1_t1"', '"p1"', '"t1"', '"instantiate_link"', 3, '"T2P"', '"t1_p2"', '"t1"', '"p2"', '"instantiate_link"', 3, '"P2T"', '"p2_t2"', '"p2"', '"t2"', '"instantiate_link"', 3, '"T2P"', '"t2_p1"', '"t2"', '"p1"', '"instantiate_attribute"', 3, '"p1_t1"', '"weight"', '1', '"instantiate_attribute"', 3, '"t1_p2"', '"weight"', '2', '"instantiate_attribute"', 3, '"p2_t2"', '"weight"', '3', '"instantiate_attribute"', 3, '"t2_p1"', '"weight"', '4', '"instantiate_attribute"', 3, '"p1"', '"tokens"', '5', '"instantiate_attribute"', 3, '"p2"', '"tokens"', '6', '"exit"', ] instantiate_example = [ '"model"', '"instantiate_model"', 1, 2, '"define_inheritance"', 2, '"l3"', '"instantiate_node"', 2, '"Class"', '"A"', '"instantiate_node"', 2, '"Class"', '"B"', '"instantiate_node"', 2, '"Class"', '"C"', '"instantiate_link"', 2, '"l3"', '"b_inherits_a"', '"B"', '"A"', '"exit"', '"model"', '"instantiate_model"', 2, 3, '"instantiate_node"', 3, '"A"', '"a"', '"instantiate_node"', 3, '"B"', '"b"', '"instantiate_node"', 3, '"C"', '"c"', '"exit"', ] def is_direct_instance_call(model, metamodel): return [ '"output"', '"call"', '"access"', '"resolve"', '"is_direct_instance"', '3', '"const"', 3, '"call"', '"access"', '"resolve"', '"dict_read"', '2', '"const"', 3, '"const"', '"%s"' % model, 'false', '"call"', '"access"', '"resolve"', '"dict_read"', '2', '"const"', 2, '"const"', '"%s"' % metamodel, 'false', 'false', 'true', ] is_direct_instance = \ is_direct_instance_call("a", "A") + \ is_direct_instance_call("b", "A") + \ is_direct_instance_call("c", "A") + \ is_direct_instance_call("a", "B") + \ is_direct_instance_call("b", "B") + \ is_direct_instance_call("c", "B") + \ is_direct_instance_call("a", "C") + \ is_direct_instance_call("b", "C") + \ is_direct_instance_call("c", "C") def conformance_check(node): return [ '"output"', '"call"', '"access"', '"resolve"', '"conformance_scd"', '1', '"const"', node, 'false', 'true', ] class TestConstructorsModels(unittest.TestCase): def test_constructors_instantiate_bottom(self): commands = bottom + retype + conformance_check(1) + ['"return"', 'false'] self.assertTrue(run_barebone(commands, ["OK"], 1)) def test_constructors_instantiate_scd(self): commands = bottom + retype + instantiate_scd + conformance_check(2) + ['"return"', 'false'] self.assertTrue(run_barebone(commands, ["OK"], 1)) def test_constructors_instantiate_pn(self): commands = bottom + retype + instantiate_scd + instantiate_pn + conformance_check(3) + ['"return"', 'false'] self.assertTrue(run_barebone(commands, ["OK"], 1)) def test_constructors_instantiate_example(self): commands = bottom + retype + instantiate_example + conformance_check(2) + conformance_check(3) + ['"return"', 'false'] self.assertTrue(run_barebone(commands, ["OK", "OK"], 1)) def test_constructors_is_direct_instance(self): commands = bottom + retype + instantiate_example + is_direct_instance + ['"return"', 'false'] expected = ['True', 'False', 'False', 'False', 'True', 'False', 'False', 'False', 'True'] self.assertTrue(run_barebone(commands, expected, 1))