def test_creation(self):
        valid_guids = [
            uuid.UUID('71720bcd-feed-499c-8305-8cf68c0fa95f'),
            uuid.UUID('cc89478d-99a3-4481-8618-d9d0ea497235'),
            uuid.UUID('502a625c-6b4e-483d-8572-ac01d300b2b8'),
            uuid.UUID('954d9866-83ea-456c-8c6e-f995e31994ed'),
        ]

        invalid_guids = [
            1,
            '954d9866-83ea-456c-8c6e-f995e31994ed',
            -23,
            5 + 6,
            'c',
            'My name',
            False,
            True,
            10j,
            '',
        ]

        for guid in valid_guids:
            c = Component(guid, "my component")
            self.assertEqual(guid, c.guid)
            self.assertEqual(guid, c.get_guid())

        for invalid in invalid_guids:
            self.assertRaises(AssertionError, Component, invalid,
                              'my compnonent')

        valid_names = [
            'My component',
            'c',
            'front light'
            'l',
            '12345',
            '##$@@#%helfgstnha',
        ]

        invalid_names = [
            '',
            '    ',
            '\n',
            '    \n',
            12,
            -10,
        ]

        for name in valid_names:
            c = Component(uuid.UUID('71720bcd-feed-499c-8305-8cf68c0fa95f'),
                          name)
            self.assertEqual(c.name, name)
            self.assertEqual(c.get_name(), name)

        for name in invalid_names:
            self.assertRaises(
                AssertionError, Component,
                uuid.UUID('71720bcd-feed-499c-8305-8cf68c0fa95f'), name)
Пример #2
0
    def load(self, filename, host="localhost", port=6499):
        print "Tuner.load " + str(port)
        basename = os.path.basename(str(filename))
        fileroot, ext = os.path.splitext(basename)

        if filename == "":
            error = QtGui.QErrorMessage(self)
            error.showMessage( "Error, you must specify an autopilot.xml config file name" )
            return
        elif not os.path.exists(filename):
            error = QtGui.QErrorMessage(self)
            error.showMessage( filename + ": does not exist" )
            return

        try:
            self.xml = ET.parse(filename)
        except:
            error = QtGui.QErrorMessage(self)
            error.showMessage( filename + ": xml parse error:\n"
                               + str(sys.exc_info()[1]) )
            return

        self.filename = str(filename)
        self.fileroot, ext = os.path.splitext(self.filename)

        root = self.xml.getroot()

        # Route follow parameters
        self.L1 = L1Controller(changefunc=self.onChange, host=host, port=port)
        self.L1.parse_xml( root.find('L1_controller') )
        self.tabs.addTab( self.L1.get_widget(), "L1" )

        # PID controller parameters
        for i,pid_node in enumerate(root.findall('component')):
            e = pid_node.find('module')
            if e != None and e.text != None:
                comp_type = e.text
            else:
                comp_type = 'unknown'
            print "component found:", comp_type
            if comp_type == 'pid_component':
                pid = Component(index=i, changefunc=self.onChange, host=host,
                                port=port, type="pid")
            elif comp_type == 'pid_vel_component':
                pid = Component(index=i, changefunc=self.onChange, host=host,
                                port=port, type="vel")
            else:
                print "unknown ..."
                next
            pid.parse_xml(pid_node)
            self.components.append(pid)
            self.tabs.addTab( pid.get_widget(), pid.get_name() )
Пример #3
0
    def load(self, filename, host="localhost", port=6499):
        print "Tuner.load " + str(port)
        basename = os.path.basename(str(filename))
        fileroot, ext = os.path.splitext(basename)

        if filename == "":
            error = QtGui.QErrorMessage(self)
            error.showMessage(
                "Error, you must specify an autopilot config file name")
            return
        elif not os.path.exists(filename):
            error = QtGui.QErrorMessage(self)
            error.showMessage(filename + ": does not exist")
            return

        try:
            props_json.load(filename, root)
        except:
            error = QtGui.QErrorMessage(self)
            error.showMessage(filename + ": parse error:\n" +
                              str(sys.exc_info()[1]))
            return

        self.filename = str(filename)
        self.fileroot, ext = os.path.splitext(self.filename)

        # Route follow parameters
        self.L1 = L1Controller(changefunc=self.onChange, host=host, port=port)
        L1_node = getNode('/L1_controller', create=True)
        self.L1.parse(L1_node)
        self.tabs.addTab(self.L1.get_widget(), "L1")

        # PID controller parameters
        print root.getChild('component')
        len = root.getLen('component')
        #comp_node = getNode('/component', create=True)
        for i in range(len):
            node_name = 'component[%d]' % i
            print node_name
            node = root.getChild(node_name)
            comp_type = node.getString('module')
            if comp_type == 'pid_component':
                pid = Component(index=i,
                                changefunc=self.onChange,
                                host=host,
                                port=port,
                                type="pid")
            elif comp_type == 'pid_vel_component':
                pid = Component(index=i,
                                changefunc=self.onChange,
                                host=host,
                                port=port,
                                type="vel")
            elif comp_type == 'summer':
                pid = Component(index=i,
                                changefunc=self.onChange,
                                host=host,
                                port=port,
                                type="sum")
            else:
                print "unknown ..."
                next
            pid.parse(node)
            self.components.append(pid)
            self.tabs.addTab(pid.get_widget(), pid.get_name())
Пример #4
0
class UCVTests(TestCase):
    """Tests for VisualGenerator.py, ChartGenerator, DiagramGenerator"""

    def setUp(self):
        if path.exists("output"):
            rmtree("output")
        self.test_file = "./tests/test_class_5.py"
        self.vg = VisualGenerator(self.test_file)
        self.components = list(self.vg.comp_dict.values())
        self.comp = Component()
        self.comp.set_name("Herbivore")
        att_dict = {'teeth': {'str': 'sharp'}, 'eyes': {'int': '2'}, 'genus': {'dict': '{}'}, 'skin': {'str': 'furry'}}
        self.comp.set_attributes(att_dict)

        self.components.append(self.comp)

    def tearDown(self):
        if path.exists("output"):
            rmtree("output")
        if path.exists(".\\temp"):
            rename(".\\temp", ".\\graphviz")
        if path.exists(".\\temp2"):
            rmtree(".\\graphviz")
            rename(".\\temp2", ".\\graphviz")

    def test_file_path(self):
        self.assertEqual(self.test_file, self.vg.input_path)

    def test_extractor(self):
        self.assertIsInstance(self.vg.e, Extractor)

    def test_components_not_none(self):
        self.assertIsNotNone(self.components)

    def test_components_not_empty(self):
        self.assertIsNot(len(self.components), 0)

    def test_components_is_list(self):
        self.assertIsInstance(self.components, list)

    def test_components_initialised(self):
        empty_comp = VisualGenerator(output_path='output/')
        self.assertEqual(len(empty_comp.comp_dict.values()), 0)

    def test_components_list_components(self):
        comps = self.components
        test_item = comps[0]
        self.assertIsInstance(test_item, Component)

    def test_get_attribute_types(self):
        expected_types = {'dict': 1, 'int': 1, 'list': 0, 'object': 0, 'str': 2, 'tuple': 0}
        att_types = self.vg.pie_chart_gen._get_attribute_types(self.comp)
        self.assertEqual(expected_types, att_types)

    def test_get_attribute_types_without_component(self):
        expected_types = {'dict': 0, 'int': 0, 'list': 0, 'object': 0, 'str': 0, 'tuple': 0}
        att_types = self.vg.pie_chart_gen._get_attribute_types(component=None)
        self.assertEqual(expected_types, att_types)

    def test_get_attribute_types_without_attributes(self):
        expected_types = {'dict': 0, 'int': 0, 'list': 0, 'object': 0, 'str': 0, 'tuple': 0}
        self.comp.set_attributes(None)
        att_types = self.vg.pie_chart_gen._get_attribute_types(self.comp)
        self.assertEqual(expected_types, att_types)

    def test_get_attribute_types_with_empty_attributes(self):
        expected_types = {'dict': 0, 'int': 0, 'list': 0, 'object': 0, 'str': 0, 'tuple': 0}
        att_dict = {'teeth': {'': 'sharp'}}
        self.comp.set_attributes(att_dict)
        att_types = self.vg.pie_chart_gen._get_attribute_types(self.comp)
        self.assertEqual(expected_types, att_types)

    def test_generate_pie_chart(self):
        self.vg.e.component_dict[self.comp.get_name()] = self.comp
        self.vg.pie_chart_gen._get_attribute_types(self.comp)
        self.vg.generate_pie_chart("Herbivore", '')
        fname = "output\\Herbivore-pie-chart.png"
        self.assertTrue(path.isfile(fname))

    def test_generate_pie_chart_without_component(self):
        self.vg.generate_pie_chart("Hello", '')
        fname = "output\\Hello-pie-chart.png"
        self.assertFalse(path.isfile(fname))

    def test_generate_pie_chart_with_output_file_name(self):
        self.vg.e.component_dict[self.comp.get_name()] = self.comp
        self.vg.pie_chart_gen._get_attribute_types(self.comp)
        self.vg.generate_pie_chart("Herbivore", output_file_name='Herbivore-pie-chart')
        fname = "output\\Herbivore-pie-chart.png"
        print(path.dirname(fname))
        self.assertTrue(path.isfile(fname))

    def test_generate_class_diagram(self):
        self.vg.generate_diagram()
        fname = 'output\\class-diagram.png'
        self.assertTrue(path.isfile(fname))

    def test_generate_class_diagram_without_components(self):
        self.vg.components = []
        self.vg.diagram_gen.generate_class_diagram(output_file_name='class-diagram.png')
        fname = 'output\\class-diagram.png'
        self.assertFalse(path.isfile(fname))

    def test_generate_pie_charts(self):
        self.vg.e.component_dict[self.comp.get_name()] = self.comp
        self.vg.pie_chart_gen._get_attribute_types(self.comp)
        fname = "output\\Herbivore-pie-chart.png"
        self.vg.generate_pie_charts()
        self.assertTrue(path.isfile(fname))

    def test_generate_bar_chart(self):
        fname = "output\\bar-chart.png"
        self.vg.generate_bar_chart(None)
        self.assertTrue(path.isfile(fname))

    def test_generate_bar_chart_with_output_path_created(self):
        fname = "output\\bar-chart.png"
        makedirs("output")
        self.vg.generate_bar_chart(None)
        self.assertTrue(path.isfile(fname))

    def test_remove_temp_files(self):
        fname = 'output\\class-diagram'
        self.vg.diagram_gen._remove_temp_files(fname)
        self.assertFalse(path.isfile(fname))

    def test_generate_bar_chart_without_object(self):
        self.vg.comp_dict = {}
        expected = "unable to generate bar chart, no components found\n"

        # Act
        with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
            self.vg.generate_bar_chart(output_file_name='bar-chart')
        # Assert
        self.assertEqual(expected, fake_stdout.getvalue())