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_writer.py @ 745

History | View | Annotate | Download (4.68 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 visitors.diadefs and extensions.diadefslib modules
18
"""
19

    
20

    
21
import os
22
import sys
23
import codecs
24
from os.path import join, dirname, abspath
25
from difflib import unified_diff
26
import unittest
27

    
28
from astroid import MANAGER
29

    
30
from pylint.pyreverse.inspector import Linker, project_from_files
31
from pylint.pyreverse.diadefslib import DefaultDiadefGenerator, DiadefsHandler
32
from pylint.pyreverse.writer import DotWriter
33
from pylint.pyreverse.utils import get_visibility
34

    
35

    
36
_DEFAULTS = {
37
    'all_ancestors': None, 'show_associated': None,
38
    'module_names': None,
39
    'output_format': 'dot', 'diadefs_file': None, 'quiet': 0,
40
    'show_ancestors': None, 'classes': (), 'all_associated': None,
41
    'mode': 'PUB_ONLY', 'show_builtin': False, 'only_classnames': False
42
    }
43

    
44
class Config(object):
45
    """config object for tests"""
46
    def __init__(self):
47
        for attr, value in _DEFAULTS.items():
48
            setattr(self, attr, value)
49

    
50

    
51
def _file_lines(path):
52
    # we don't care about the actual encoding, but python3 forces us to pick one
53
    with codecs.open(path, encoding='latin1') as stream:
54
        lines = [line.strip() for line in stream.readlines()
55
                 if (line.find('squeleton generated by ') == -1 and
56
                     not line.startswith('__revision__ = "$Id:'))]
57
    return [line for line in lines if line]
58

    
59
def get_project(module, name="No Name"):
60
    """return a astroid project representation"""
61
    def _astroid_wrapper(func, modname):
62
        return func(modname)
63
    return project_from_files([module], _astroid_wrapper,
64
                              project_name=name)
65

    
66
CONFIG = Config()
67

    
68
class DotWriterTC(unittest.TestCase):
69

    
70
    @classmethod
71
    def setUpClass(cls):
72
        project = get_project(os.path.join(os.path.dirname(__file__), 'data'))
73
        linker = Linker(project)
74
        handler = DiadefsHandler(CONFIG)
75
        dd = DefaultDiadefGenerator(linker, handler).visit(project)
76
        for diagram in dd:
77
            diagram.extract_relationships()
78
        writer = DotWriter(CONFIG)
79
        writer.write(dd)
80

    
81
    @classmethod
82
    def tearDownClass(cls):
83
        for fname in ('packages_No_Name.dot', 'classes_No_Name.dot',):
84
            try:
85
                os.remove(fname)
86
            except:
87
                continue
88

    
89
    def _test_same_file(self, generated_file):
90
        expected_file = os.path.join(os.path.dirname(__file__), 'data', generated_file)
91
        generated = _file_lines(generated_file)
92
        expected = _file_lines(expected_file)
93
        generated = '\n'.join(generated)
94
        expected = '\n'.join(expected)
95
        files = "\n *** expected : %s, generated : %s \n"  % (
96
            expected_file, generated_file)
97
        self.assertEqual(expected, generated, '%s%s' % (
98
            files, '\n'.join(line for line in unified_diff(
99
            expected.splitlines(), generated.splitlines() ))) )
100
        os.remove(generated_file)
101

    
102
    def test_package_diagram(self):
103
        self._test_same_file('packages_No_Name.dot')
104

    
105
    def test_class_diagram(self):
106
        self._test_same_file('classes_No_Name.dot')
107

    
108

    
109

    
110
class GetVisibilityTC(unittest.TestCase):
111

    
112
    def test_special(self):
113
        for name in ["__reduce_ex__",  "__setattr__"]:
114
            self.assertEqual(get_visibility(name), 'special')
115

    
116
    def test_private(self):
117
        for name in ["__g_", "____dsf", "__23_9"]:
118
            got = get_visibility(name)
119
            self.assertEqual(got, 'private',
120
                             'got %s instead of private for value %s' % (got, name))
121

    
122
    def test_public(self):
123
        self.assertEqual(get_visibility('simple'), 'public')
124

    
125
    def test_protected(self):
126
        for name in ["_","__", "___", "____", "_____", "___e__",
127
                     "_nextsimple", "_filter_it_"]:
128
            got = get_visibility(name)
129
            self.assertEqual(got, 'protected',
130
                             'got %s instead of protected for value %s' % (got, name))
131

    
132

    
133
if __name__ == '__main__':
134
    unittest.main()