root/branches/mk/tests/unit/test_code_parser.py

Revision 66, 5.5 kB (checked in by mk, 6 years ago)

Moved tests data directory.

Line 
1 import os
2
3 import _path_cheesecake
4 from _helper_cheesecake import set, DATA_PATH
5
6 from cheesecake.codeparser import CodeParser, use_format
7
8
9 class TestCodeParser(object):
10     def setUp(self):
11         self.code1 = CodeParser(os.path.join(DATA_PATH, "module1.py"))
12
13     def test_modules(self):
14         assert self.code1.modules == ["module1"]
15
16     def test_classes(self):
17         assert self.code1.classes == [
18             "module1.Class1",
19             "module1.Class2",
20             "module1.Class3",
21         ]
22
23     def test_methods(self):
24         assert self.code1.methods == [
25             "module1.Class1.__init__",
26             "module1.Class1.__another_method__",
27             "module1.Class1.method1",
28             "module1.Class1.method2",
29             "module1.Class1.method3",
30             "module1.Class1.method4",
31             "module1.Class1.method5",
32         ]
33
34     def test_functions(self):
35         assert self.code1.functions == [
36             "module1.func1",
37             "module1.func2",
38             "module1.func3",
39             "module1.func4",
40             "module1.__func5__",
41             "module1.func6",
42             "module1.func7",
43             "module1.func8",
44             "module1.outer_function",
45             "module1.outer_function.inner_function",
46         ]
47
48     def test_count(self):
49         assert self.code1.object_count() == 21
50         assert self.code1.docstring_count() == 17
51         assert self.code1.docstring_count_by_type('reST') == 2
52         assert self.code1.docstring_count_by_type('epytext') == 3
53         assert self.code1.docstring_count_by_type('javadoc') == 2
54
55     def test_docstrings(self):
56         objects_with_docstrings = [
57             "module1",
58             "module1.Class1",
59             "module1.Class2",
60             "module1.Class3",
61             "module1.Class1.__init__",
62             "module1.Class1.__another_method__",
63             "module1.Class1.method1",
64             "module1.Class1.method2",
65             "module1.Class1.method3",
66             "module1.Class1.method5",
67             "module1.func1",
68             "module1.func2",
69             "module1.func3",
70             "module1.__func5__",
71             "module1.func7",
72             "module1.func8",
73             "module1.outer_function.inner_function",
74         ]
75         objects_with_rest_docstrings = [
76             "module1.Class1.method5",
77             "module1.func7",
78         ]
79         objects_with_epytext_docstrings = [
80             "module1",
81             "module1.Class3",
82             "module1.func8",
83         ]
84         objects_with_javadoc_docstrings = [
85             "module1.Class1",
86             "module1.func8", # intentional overlap with epytext
87         ]
88
89         print self.code1.docstrings
90
91         assert set(objects_with_docstrings) == set(self.code1.docstrings)
92         assert set(objects_with_rest_docstrings) == set(self.code1.docstrings_by_format['reST'])
93         assert set(objects_with_epytext_docstrings) == set(self.code1.docstrings_by_format['epytext'])
94         assert set(objects_with_javadoc_docstrings) == set(self.code1.docstrings_by_format['javadoc'])
95
96
97 class TestDocumentationFormats(object):
98     def _do_it(self, format, valid, invalid):
99         for test in valid:
100             print "Trying '%s'" % test
101             assert use_format(test, format) is True
102
103         for test in invalid:
104             print "Trying '%s'" % test
105             assert use_format(test, format) is False
106
107     def test_reST(self):
108         valid_test_strings = [
109             "String with *emphasis*.",
110             "*Multi-word emphasis.*",
111             "How about testing **strong string**?",
112             "Some *noisy!* punctuation",
113             "**characters?**, in the way.",
114             "Don't forget ``inline literals``.",
115             "This is reST (hyperlink_).",
116             "This is (`quite long hyperlink`_).",
117             "* Bullet\n* List\n",
118             "+ Another\n+ Bullet\n+ List\n",
119             "1. Ordered\n2. List\n",
120             "  a) Another\n  b) ordered\n  c) list\n",
121             " (a) one\n (b) more",
122             ":Field: list\n:indeed: it is\n",
123         ]
124         invalid_test_strings = [
125             "Plain string.",
126             "Do some math: 2 * 2a* 2 = 8a",
127             "Not*really*strong.",
128             "Interpreted `text` is widely used as quotes, so exclude it.",
129             "Not a :field:.",
130         ]
131
132         self._do_it('reST', valid_test_strings, invalid_test_strings)
133
134     def test_epytext(self):
135         valid_test_strings = [
136             "- Bullet\n- List\n",
137             "1. Ordered\n2. List\n",
138             "1.1 Few points\n1.2 To remember\n",
139             "Some I{italics} here.",
140             "And a small bit of C{code}.",
141             "@param self: You know what it means.",
142             "@return: Return a long\ndescription.",
143         ]
144         invalid_test_strings = [
145             "Aha - This is not an unordered list.",
146             "email@example.com",
147             "Short Python dictionary: {0: 'zero', 1: 'one'}.",
148             "@ not a field: at all",
149         ]
150
151         self._do_it('epytext', valid_test_strings, invalid_test_strings)
152
153     def test_javadoc(self):
154         valid_test_strings = [
155             'Inline <a href="{@docRoot}/html/documents/">are ugly</a>!',
156             "Call {@link #test_javadoc(object) test_javadoc} method.",
157             '@see Why#java(sucks)',
158         ]
159         invalid_test_strings = [
160             "Normal text.",
161             "mail.address@example.com",
162             "Mathematical: a < b < c while x > y.",
163             "@it: is not javadoc, but epytext!",
164         ]
165
166         self._do_it('javadoc', valid_test_strings, invalid_test_strings)
Note: See TracBrowser for help on using the browser.