File: test_pyment.py

package info (click to toggle)
pyment 0.3.4%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 500 kB
  • sloc: python: 2,738; makefile: 16; sh: 9
file content (109 lines) | stat: -rwxr-xr-x 3,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python

import unittest
import shutil
import os
import pyment.pyment as pym

myelem = '    def my_method(self, first, second=None, third="value"):'
mydocs = '''        """This is a description of a method.
        It is on several lines.
        Several styles exists:
            -javadoc,
            -reST,
            -cstyle.
        It uses the javadoc style.

        @param first: the 1st argument.
        with multiple lines
        @type first: str
        @param second: the 2nd argument.
        @return: the result value
        @rtype: int
        @raise KeyError: raises exception

        """'''

current_dir = os.path.dirname(__file__)
absdir = lambda f: os.path.join(current_dir, f)

inifile = absdir('origin_test.py')
jvdfile = absdir('javadoc_test.py')
rstfile = absdir('rest_test.py')
foo = absdir("foo")


class DocStringTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # prepare test file
        txt = ""
        shutil.copyfile(inifile, jvdfile)
        with open(jvdfile, 'r') as fs:
            txt = fs.read()
        txt = txt.replace("@return", ":returns")
        txt = txt.replace("@raise", ":raises")
        txt = txt.replace("@", ":")
        with open(rstfile, 'w') as ft:
            ft.write(txt)
        with open(foo, "w") as fooo:
            fooo.write("foo")
        print("setup")

    @classmethod
    def tearDownClass(cls):
        os.remove(jvdfile)
        os.remove(rstfile)
        os.remove(foo)
        print("end")

    def testParsedJavadoc(self):
        p = pym.PyComment(inifile)
        p._parse()
        self.assertTrue(p.parsed)

    def testSameOutJavadocReST(self):
        pj = pym.PyComment(jvdfile)
        pr = pym.PyComment(rstfile)
        pj._parse()
        pr._parse()
        self.assertEqual(pj.get_output_docs(), pr.get_output_docs())

    def testMultiLinesElements(self):
        p = pym.PyComment(inifile)
        p._parse()
        self.assertTrue('first' in p.get_output_docs()[1])
        self.assertTrue('second' in p.get_output_docs()[1])
        self.assertTrue('third' in p.get_output_docs()[1])
        self.assertTrue('multiline' in p.get_output_docs()[1])

    def testMultiLinesShiftElements(self):
        p = pym.PyComment(inifile)
        p._parse()
        #TODO: improve this test
        self.assertEqual((len(p.get_output_docs()[13])-len(p.get_output_docs()[13].lstrip())), 8)
        self.assertTrue('first' in p.get_output_docs()[13])
        self.assertTrue('second' in p.get_output_docs()[13])
        self.assertTrue('third' in p.get_output_docs()[13])
        self.assertTrue('multiline' in p.get_output_docs()[13])

    def testWindowsRename(self):
        bar = absdir("bar")
        with open(bar, "w") as fbar:
            fbar.write("bar")
        p = pym.PyComment(foo)
        p._windows_rename(bar)
        self.assertFalse(os.path.isfile(bar))
        self.assertTrue(os.path.isfile(foo))
        with open(foo, "r") as fooo:
            foo_txt = fooo.read()
        self.assertTrue(foo_txt == "bar")


def main():
    unittest.main()


if __name__ == '__main__':
    main()