Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / test / unittest_pyreverse_diadefs.py @ 745

History | View | Annotate | Download (7.25 KB)

1
# Copyright (c) 2000-2013 LOGILAB S.A. (Paris, FRANCE).
2
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
3
#
4
# This program is free software; you can redistribute it and/or modify it under
5
# the terms of the GNU General Public License as published by the Free Software
6
# Foundation; either version 2 of the License, or (at your option) any later
7
# version.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License along with
14
# this program; if not, write to the Free Software Foundation, Inc.,
15
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
"""
17
unittest for the extensions.diadefslib modules
18
"""
19

    
20
import unittest
21
import sys
22

    
23
import six
24

    
25
import astroid
26
from astroid import MANAGER
27

    
28
from pylint.pyreverse.inspector import Linker
29
from pylint.pyreverse.diadefslib import *
30

    
31
from unittest_pyreverse_writer import Config, get_project
32

    
33
PROJECT = get_project('data')
34
HANDLER = DiadefsHandler(Config())
35

    
36
def _process_classes(classes):
37
    """extract class names of a list"""
38
    return sorted([(isinstance(c.node, astroid.ClassDef), c.title) for c in classes])
39

    
40
def _process_relations(relations):
41
    """extract relation indices from a relation list"""
42
    result = []
43
    for rel_type, rels in six.iteritems(relations):
44
        for rel in rels:
45
            result.append( (rel_type, rel.from_object.title,
46
                            rel.to_object.title) )
47
    result.sort()
48
    return result
49

    
50

    
51
class DiaDefGeneratorTC(unittest.TestCase):
52
    def test_option_values(self):
53
        """test for ancestor, associated and module options"""
54
        handler = DiadefsHandler(Config())
55
        df_h = DiaDefGenerator(Linker(PROJECT), handler)
56
        cl_config = Config()
57
        cl_config.classes = ['Specialization']
58
        cl_h = DiaDefGenerator(Linker(PROJECT), DiadefsHandler(cl_config) )
59
        self.assertEqual( (0, 0), df_h._get_levels())
60
        self.assertEqual( False, df_h.module_names)
61
        self.assertEqual( (-1, -1), cl_h._get_levels())
62
        self.assertEqual( True, cl_h.module_names)
63
        for hndl in [df_h, cl_h]:
64
            hndl.config.all_ancestors = True
65
            hndl.config.all_associated = True
66
            hndl.config.module_names = True
67
            hndl._set_default_options()
68
            self.assertEqual( (-1, -1), hndl._get_levels())
69
            self.assertEqual( True, hndl.module_names)
70
        handler = DiadefsHandler( Config())
71
        df_h = DiaDefGenerator(Linker(PROJECT), handler)
72
        cl_config = Config()
73
        cl_config.classes = ['Specialization']
74
        cl_h = DiaDefGenerator(Linker(PROJECT), DiadefsHandler(cl_config) )
75
        for hndl in [df_h, cl_h]:
76
            hndl.config.show_ancestors = 2
77
            hndl.config.show_associated = 1
78
            hndl.config.module_names = False
79
            hndl._set_default_options()
80
            self.assertEqual( (2, 1), hndl._get_levels())
81
            self.assertEqual( False, hndl.module_names)
82
    #def test_default_values(self):
83
        """test efault values for package or class diagrams"""
84
        # TODO : should test difference between default values for package
85
        # or class diagrams
86

    
87
class DefaultDiadefGeneratorTC(unittest.TestCase):
88
    def test_known_values1(self):
89
        dd = DefaultDiadefGenerator(Linker(PROJECT), HANDLER).visit(PROJECT)
90
        self.assertEqual(len(dd), 2)
91
        keys = [d.TYPE for d in dd]
92
        self.assertEqual(keys, ['package', 'class'])
93
        pd = dd[0]
94
        self.assertEqual(pd.title, 'packages No Name')
95
        modules = sorted([(isinstance(m.node, astroid.Module), m.title)
96
                         for m in pd.objects])
97
        self.assertEqual(modules, [(True, 'data'),
98
                                   (True, 'data.clientmodule_test'),
99
                                   (True, 'data.suppliermodule_test')])
100
        cd = dd[1]
101
        self.assertEqual(cd.title, 'classes No Name')
102
        classes = _process_classes(cd.objects)
103
        self.assertEqual(classes, [(True, 'Ancestor'),
104
                                   (True, 'DoNothing'),
105
                                   (True, 'Interface'),
106
                                   (True, 'Specialization')]
107
                          )
108

    
109
    _should_rels = [('association', 'DoNothing', 'Ancestor'),
110
                    ('association', 'DoNothing', 'Specialization'),
111
                    ('implements', 'Ancestor', 'Interface'),
112
                    ('specialization', 'Specialization', 'Ancestor')]
113
    def test_exctract_relations(self):
114
        """test extract_relations between classes"""
115
        cd = DefaultDiadefGenerator(Linker(PROJECT), HANDLER).visit(PROJECT)[1]
116
        cd.extract_relationships()
117
        relations = _process_relations(cd.relationships)
118
        self.assertEqual(relations, self._should_rels)
119

    
120
    def test_functional_relation_extraction(self):
121
        """functional test of relations extraction;
122
        different classes possibly in different modules"""
123
        # XXX should be catching pyreverse environnement problem but doesn't
124
        # pyreverse doesn't extracts the relations but this test ok
125
        project = get_project('data')
126
        handler = DiadefsHandler(Config())
127
        diadefs = handler.get_diadefs(project, Linker(project, tag=True) )
128
        cd = diadefs[1]
129
        relations = _process_relations(cd.relationships)
130
        self.assertEqual(relations, self._should_rels)
131

    
132
    def test_known_values2(self):
133
        project = get_project('data.clientmodule_test')
134
        dd = DefaultDiadefGenerator(Linker(project), HANDLER).visit(project)
135
        self.assertEqual(len(dd), 1)
136
        keys = [d.TYPE for d in dd]
137
        self.assertEqual(keys, ['class'])
138
        cd = dd[0]
139
        self.assertEqual(cd.title, 'classes No Name')
140
        classes = _process_classes(cd.objects)
141
        self.assertEqual(classes, [(True, 'Ancestor'),
142
                                   (True, 'DoNothing'),
143
                                   (True, 'Specialization')]
144
                          )
145

    
146
class ClassDiadefGeneratorTC(unittest.TestCase):
147
    def test_known_values1(self):
148
        HANDLER.config.classes = ['Specialization']
149
        cdg = ClassDiadefGenerator(Linker(PROJECT), HANDLER)
150
        special = 'data.clientmodule_test.Specialization'
151
        cd = cdg.class_diagram(PROJECT, special)
152
        self.assertEqual(cd.title, special)
153
        classes = _process_classes(cd.objects)
154
        self.assertEqual(classes, [(True, 'data.clientmodule_test.Ancestor'),
155
                                   (True, special),
156
                                   (True, 'data.suppliermodule_test.DoNothing'),
157
                                  ])
158

    
159
    def test_known_values2(self):
160
        HANDLER.config.module_names = False
161
        cd = ClassDiadefGenerator(Linker(PROJECT), HANDLER).class_diagram(PROJECT, 'data.clientmodule_test.Specialization')
162
        self.assertEqual(cd.title, 'data.clientmodule_test.Specialization')
163
        classes = _process_classes(cd.objects)
164
        self.assertEqual(classes, [(True, 'Ancestor'),
165
                                   (True, 'DoNothing'),
166
                                   (True, 'Specialization')
167
                                  ])
168

    
169

    
170
if __name__ == '__main__':
171
    unittest.main()