[gtk-doc] tests: test cleanup and more tests



commit e591975f402a9f2688d8d7ea208d0ac8bad52243
Author: Stefan Sauer <ensonic users sf net>
Date:   Fri May 26 20:06:33 2017 +0200

    tests: test cleanup and more tests
    
    Add tests for the markdown parser. Rename the other test files (drop the
    gtkdoc prefix). Add copyright headers.

 tests/Makefile.am      |    7 +-
 tests/check.py         |   52 ++++++++++
 tests/common.py        |   70 +++++++++++++
 tests/gtkdoc-check.py  |   33 ------
 tests/gtkdoc-common.py |   50 ---------
 tests/mk_to_db.py      |  261 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 385 insertions(+), 88 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 873e738..794db94 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -5,7 +5,7 @@ SUBDIRS = gobject bugs annotations fail empty program .
 if BUILD_TESTS
 
 TESTS = \
-  gtkdoc-check.py gtkdoc-common.py \
+  check.py common.py mk_to_db.py \
   tools.sh gobject.sh bugs.sh annotations.sh fail.sh empty.sh sanity.sh \
   program.sh
 TESTS_ENVIRONMENT = \
@@ -20,10 +20,7 @@ TESTS_ENVIRONMENT = \
 
 endif
 
-EXTRA_DIST = gtkdoctest.sh \
-  gtkdoc-check.py gtkdoc-common.py \
-  tools.sh gobject.sh bugs.sh annotations.sh fail.sh empty.sh sanity.sh \
-       program.sh
+EXTRA_DIST = gtkdoctest.sh $(TESTS)
 
 # run any given test by running make <test>.check
 %.check: %
diff --git a/tests/check.py b/tests/check.py
new file mode 100755
index 0000000..e6f12b5
--- /dev/null
+++ b/tests/check.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+# -*- python -*-
+#
+# gtk-doc - GTK DocBook documentation generator.
+# Copyright (C) 2017  Stefan Sauer
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+
+import unittest
+
+from gtkdoc import check
+
+
+class TestCheck(unittest.TestCase):
+
+    def test_grep_finds_token_in_one_line(self):
+        result = check.grep(r'^(foo)', ['foo'], 'foo')
+        self.assertEqual('foo', result)
+
+    def test_grep_does_not_find_token(self):
+        with self.assertRaises(check.FileFormatError) as ctx:
+            check.grep(r'^(foo)', ['bar'], 'foo')
+        self.assertEqual(str(ctx.exception), 'foo')
+
+    def test_get_variable_prefers_env(self):
+        result = check.get_variable({'foo': 'bar'}, ['foo=baz'], 'foo')
+        self.assertEqual('bar', result)
+
+    def test_get_variable_finds_in_file(self):
+        result = check.get_variable({}, ['foo=bar'], 'foo')
+        self.assertEqual('bar', result)
+
+    def test_get_variable_finds_in_file_with_whitespce(self):
+        result = check.get_variable({}, ['foo = bar'], 'foo')
+        self.assertEqual('bar', result)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/common.py b/tests/common.py
new file mode 100755
index 0000000..3f78220
--- /dev/null
+++ b/tests/common.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+#!/usr/bin/env python
+# -*- python -*-
+#
+# gtk-doc - GTK DocBook documentation generator.
+# Copyright (C) 2017  Stefan Sauer
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+
+import mock
+import unittest
+
+from gtkdoc import common
+
+
+class TestUpdateFileIfChanged(unittest.TestCase):
+
+    @mock.patch('os.path.exists')
+    @mock.patch('os.rename')
+    def test_NoOldFile(self, os_rename, os_path_exists):
+        os_path_exists.return_value = False
+        res = common.UpdateFileIfChanged('/old', '/new', False)
+        os_rename.assert_called_with('/new', '/old')
+        self.assertTrue(res)
+
+    @mock.patch('os.path.exists')
+    @mock.patch('__builtin__.open', mock.mock_open(read_data='bar'))
+    @mock.patch('os.unlink')
+    def test_FilesAreTheSame(self, os_unlink, os_path_exists):
+        os_path_exists.return_value = True
+        res = common.UpdateFileIfChanged('/old', '/new', False)
+        os_unlink.assert_called_with('/new')
+        self.assertFalse(res)
+
+
+class TestGetModuleDocDir(unittest.TestCase):
+
+    @mock.patch('subprocess.check_output')
+    def test_ReturnsPath(self, subprocess_check_output):
+        subprocess_check_output.return_value = '/usr'
+        self.assertEquals(common.GetModuleDocDir('glib-2.0'), '/usr/share/gtk-doc/html')
+
+
+class TestCreateValidSGMLID(unittest.TestCase):
+
+    def test_AlreadyValid(self):
+        self.assertEquals(common.CreateValidSGMLID('x'), 'x')
+
+    def test_SpecialCharsBecomeDash(self):
+        self.assertEquals(common.CreateValidSGMLID('x_ y'), 'x--y')
+
+    def test_SpecialCharsGetRemoved(self):
+        self.assertEquals(common.CreateValidSGMLID('x,;y'), 'xy')
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/mk_to_db.py b/tests/mk_to_db.py
new file mode 100755
index 0000000..646ad39
--- /dev/null
+++ b/tests/mk_to_db.py
@@ -0,0 +1,261 @@
+#!/usr/bin/env python
+# -*- python; coding: utf-8 -*-
+#
+# gtk-doc - GTK DocBook documentation generator.
+# Copyright (C) 2015 Christoph Reiter
+#               2017  Stefan Sauer
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+
+import unittest
+
+from gtkdoc import md_to_db
+
+
+class TestConverter(unittest.TestCase):
+
+    def setUp(self):
+        md_to_db.Init()
+
+    def test_main(self):
+        input_ = """\
+SUPPORTED MARKDOWN
+==================
+
+Atx-style Headers
+-----------------
+
+# Header 1
+
+## Header 2 ##
+
+Setext-style Headers
+--------------------
+
+Header 1
+========
+
+Header 2
+--------
+
+Ordered (unnested) Lists
+------------------------
+
+1. item 1
+
+1. item 2 with loooong *foo*
+   description
+
+3. item 3
+
+Note: we require a blank line above the list items
+"""
+
+        expexted = """\
+<para>SUPPORTED MARKDOWN</para>
+<para>Atx-style Headers</para>
+<refsect2><title>Header 1</title><refsect3><title>Header 2</title></refsect3>
+<refsect3><title>Setext-style Headers</title></refsect3>
+</refsect2>
+<refsect2><title>Header 1</title><para>Header 2</para>
+<para>Ordered (unnested) Lists</para>
+<orderedlist>
+<listitem>
+<para>item 1</para>
+</listitem>
+<listitem>
+<para>item 2 with loooong *foo*
+description</para>
+</listitem>
+<listitem>
+<para>item 3</para>
+</listitem>
+</orderedlist>
+<para>Note: we require a blank line above the list items</para>
+</refsect2>
+"""
+
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expexted, output)
+
+    def test_docbook(self):
+        input_ = """\
+<itemizedlist>
+  <listitem><para>foo</para></listitem>
+</itemizedlist>
+"""
+
+        # docbook should stay the same
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(input_, output)
+
+    def test_header(self):
+        input_ = """
+widget lifecycle, states and style.
+
+# Height-for-width Geometry Management # {#geometry-management}
+
+GTK+ uses a height-for-width (and wid
+"""
+
+        expected = """\
+<para>widget lifecycle, states and style.</para>
+<refsect2 id="geometry-management"><title>Height-for-width Geometry Management</title><para>GTK+ uses a 
height-for-width (and wid</para>
+</refsect2>
+"""
+
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_lists(self):
+        input_ = """\
+bla bla
+bla:
+
+- The channel was just created, and has not been written to or read from yet.
+  bla
+
+- The channel is write-only.
+
+foo
+"""
+        expected = """\
+<para>bla bla
+bla:</para>
+<itemizedlist>
+<listitem>
+<para>The channel was just created, and has not been written to or read from yet.
+bla</para>
+</listitem>
+<listitem>
+<para>The channel is write-only.</para>
+</listitem>
+</itemizedlist>
+<para>foo</para>
+"""
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_paragraphs(self):
+        input_ = """\
+foo,
+bar.
+
+foo,
+bar.
+
+foo,
+bar.
+"""
+        expected = """\
+<para>foo,
+bar.</para>
+<para>foo,
+bar.</para>
+<para>foo,
+bar.</para>
+"""
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_reference(self):
+        input_ = """\
+The #GData struct is an opaque data structure to represent a
+[Keyed Data List][glib-Keyed-Data-Lists]. It should only be
+accessed via the following functions."""
+
+        expected = """\
+<para>The <link linkend="GData"><type>GData</type></link> struct is an opaque data structure to represent a
+<link linkend="glib-Keyed-Data-Lists">Keyed Data List</link>. It should only be
+accessed via the following functions.</para>
+"""
+
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_reference2(self):
+        input_ = "a [foo][bar] b [quux][baz]"
+        expected = '<para>a <link linkend="bar">foo</link> b <link linkend="baz">quux</link></para>\n'
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_reference_empty(self):
+        input_ = "[][]"
+        # expected = '<para><ulink url=""></ulink></para>\n'
+        expected = '<para><link linkend=""></link></para>\n'
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_inline_code(self):
+        input_ = "a `abc`"
+        expected = '<para>a <literal>abc</literal></para>\n'
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_inline_code2(self):
+        input_ = "a `[][]`"
+        expected = '<para>a <literal>[][]</literal></para>\n'
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_code(self):
+        input_ = """\
+|[<!-- language="C" -->
+    GdkEvent *event;
+    GdkEventType type;
+
+    type = event->type;
+]|
+"""
+
+        expected = '''\
+<informalexample><programlisting language="C"><![CDATA[
+    GdkEvent *event;
+    GdkEventType type;
+
+    type = event->type;
+]]></programlisting></informalexample>
+<para></para>
+'''
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+    def test_plain(self):
+        input_ = u"""\
+|[<!-- language="plain" -->
+frame
+├── border
+├── <label widget>
+╰── <child>
+]|
+"""
+
+        expected = u"""\
+<informalexample><screen><![CDATA[
+frame
+├── border
+├── <label widget>
+╰── <child>
+]]></screen></informalexample>
+<para></para>
+"""
+
+        output = md_to_db.MarkDownParse(input_, "")
+        self.assertEqual(expected, output)
+
+
+if __name__ == '__main__':
+    unittest.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]