def testHeaderParsed(self): parser = cpp_to_graphml.StateMachineParser( cpp_file_path='./testdata/oregonPlayer.cpp') sm = parser.Parse() self.assertTrue(sm.raw_h_code) self.assertTrue(sm.raw_h_code.startswith('#define')) self.assertTrue(sm.raw_h_code.endswith('(DEFAULT_HP/3)\n')) self.assertEqual(sm.event_fields, 'unsigned int value;') self.assertEqual( sm.state_fields, '''unsigned int CharHP; QStateHandler StartState; unsigned int TimerAgony;''') self.assertEqual( sm.constructor_fields, '''unsigned int HP; unsigned int State; unsigned int TimerAgony;''') self.assertTrue(sm.constructor_code) self.assertTrue(sm.constructor_code.startswith('me->CharHP = HP;'), sm.constructor_code) self.assertTrue(sm.constructor_code.endswith('}')) self.assertEqual(sm.initial_code, 'return Q_TRAN(me->StartState);') self.assertEqual(sm.initial_state, 'healthy')
def testFullCycle(self): parser = cpp_to_graphml.StateMachineParser( cpp_file_path='./testdata/oregonPlayer.cpp') sm1 = parser.Parse() cpp_to_graphml.StateMachineWriter(sm1).WriteToFile( './testdata/test_output/oregonPlayer.graphml', './testdata/oregonPlayer.graphml') graphmltoqm.main('./testdata/test_output/oregonPlayer.graphml') shutil.copy('./testdata/qhsm.h', './testdata/test_output') shutil.copy('./testdata/eventHandlers.h', './testdata/test_output') subprocess.run(getQmWithArgs() + ['./testdata/test_output/oregonPlayer.qm', '-c'], check=True, timeout=10) parser2 = cpp_to_graphml.StateMachineParser( cpp_file_path='./testdata/test_output/oregonPlayer.cpp') sm2 = parser2.Parse() self.assertEqual(len(sm1.states), len(sm2.states)) self.assertEqual(sm1.state_fields, sm2.state_fields) self.assertEqual(sm1.event_fields, sm2.event_fields) self.assertEqual(sm1.constructor_fields, sm2.constructor_fields) self.assertEqual(sm1.constructor_code, sm2.constructor_code) self.assertEqual(sm1.raw_h_code, sm2.raw_h_code) self.assertEqual(sm1.initial_state, sm2.initial_state) self.assertEqual(sm1.initial_code, sm2.initial_code) for state_name in sorted(sm1.states.keys()): s1 = sm1.states[state_name] self.assertIn(state_name, sm2.states) s2 = sm2.states[state_name] self.assertEqual(s1.state_name, s2.state_name) self.assertEqual(s1.parent_state_name, s2.parent_state_name, s1.state_name) def names(state_list): return [s.state_name for s in state_list] self.assertEqual(names(s1.child_states), names(s2.child_states)) self.assertCountEqual(s1.event_handlers, s2.event_handlers) # This one is quite fragile as literally any change in the output will break it. Not sure if it's actually needed. with open('./testdata/oregonPlayer.cpp', 'r') as f: sm1_cpp_content = f.read() with open('./testdata/test_output/oregonPlayer.cpp', 'r') as f: sm2_cpp_content = f.read() self.assertEqual(sm1_cpp_content, sm2_cpp_content)
def testValidGraphml(self): parser = cpp_to_graphml.StateMachineParser( cpp_file_path='./testdata/oregonPlayer.cpp') cpp_to_graphml.StateMachineWriter(parser.Parse()).WriteToFile( self.OUTPUT_FILE) output_file = Path(self.OUTPUT_FILE) # Test that output file is present ... self.assertTrue(output_file.is_file()) # ... but in addition test that it's a valid XML containing something graphml-looking. with open(self.OUTPUT_FILE, 'r') as f: xml_dict = xmltodict.parse(f.read()) self.assertTrue(xml_dict) self.assertIsInstance(xml_dict['graphml'], OrderedDict)
def testNumberOfStates(self): parser = cpp_to_graphml.StateMachineParser( cpp_file_path='./testdata/oregonPlayer.cpp') sm = parser.Parse() self.assertEqual(len(sm.states), 15)