mkhtml2: improve chunking to handle tocs
[gtk-doc.git] / tests / common.py
blob26dd16bc9f7dda560e0d7f6fec1a5d856a1a4443
1 # -*- python -*-
3 # gtk-doc - GTK DocBook documentation generator.
4 # Copyright (C) 2017 Stefan Sauer
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 try:
22 from unittest import mock
23 except ImportError:
24 import mock
26 import unittest
28 from six import PY2
29 from gtkdoc import common
32 if PY2:
33 openname = '__builtin__.open'
34 else:
35 openname = 'builtins.open'
38 class TestUpdateFileIfChanged(unittest.TestCase):
40 @mock.patch('os.path.exists')
41 @mock.patch('os.rename')
42 def test_NoOldFile(self, os_rename, os_path_exists):
43 os_path_exists.return_value = False
44 res = common.UpdateFileIfChanged('/old', '/new', False)
45 os_rename.assert_called_with('/new', '/old')
46 self.assertTrue(res)
48 @mock.patch('os.path.exists')
49 @mock.patch(openname, mock.mock_open(read_data='bar'))
50 @mock.patch('os.unlink')
51 def test_FilesAreTheSame(self, os_unlink, os_path_exists):
52 os_path_exists.return_value = True
53 res = common.UpdateFileIfChanged('/old', '/new', False)
54 os_unlink.assert_called_with('/new')
55 self.assertFalse(res)
58 class TestGetModuleDocDir(unittest.TestCase):
60 @mock.patch('subprocess.check_output')
61 def test_ReturnsPath(self, subprocess_check_output):
62 subprocess_check_output.return_value = '/usr'
63 self.assertEquals(common.GetModuleDocDir('glib-2.0'), '/usr/share/gtk-doc/html')
66 class TestCreateValidSGMLID(unittest.TestCase):
68 def test_AlreadyValid(self):
69 self.assertEquals(common.CreateValidSGMLID('x'), 'x')
71 def test_SpecialCharsBecomeDash(self):
72 self.assertEquals(common.CreateValidSGMLID('x_ y'), 'x--y')
74 def test_SpecialCharsGetRemoved(self):
75 self.assertEquals(common.CreateValidSGMLID('x,;y'), 'xy')
78 if __name__ == '__main__':
79 unittest.main()