From 2828399ba5cbb14502b023d4de1ba02f13dd5055 Mon Sep 17 00:00:00 2001 From: Agustin Henze Date: Fri, 28 Feb 2014 08:49:38 -0300 Subject: Imported Upstream version 6.3.0 --- tests/README.rst | 38 +++++++--- tests/__init__.py | 25 ++++++ tests/base.py | 18 +++-- tests/context.py | 9 --- tests/data/translated_titles/conf.py | 18 ++++- tests/data/translated_titles/stories/1.txt.es | 4 - tests/data/translated_titles/stories/1.txt.pl | 4 + tests/test_command_import_wordpress.py | 8 +- tests/test_command_init.py | 9 ++- tests/test_compile_markdown.py | 29 ++----- tests/test_integration.py | 103 ++++++++++++++++++------- tests/test_locale.py | 6 ++ tests/test_plugin_importing.py | 8 +- tests/test_rss_feeds.py | 19 ++++- tests/test_rst_compiler.py | 10 ++- tests/test_scheduling.py | 19 +++++ tests/test_utils.py | 105 +++++++++++++++++++++++++- 17 files changed, 342 insertions(+), 90 deletions(-) delete mode 100644 tests/context.py delete mode 100644 tests/data/translated_titles/stories/1.txt.es create mode 100644 tests/data/translated_titles/stories/1.txt.pl (limited to 'tests') diff --git a/tests/README.rst b/tests/README.rst index 2b3afb8..8b2199a 100644 --- a/tests/README.rst +++ b/tests/README.rst @@ -38,33 +38,33 @@ You need: How to set the locale for Nikola tests? --------------------------------------- -For testing nikola needs to specify two languages, each one with a supported locale. By default, the test suite uses ``en`` and ``es`` as languages, and their respective default locale for them. +For testing nikola needs to specify two languages, each one with a supported locale. By default, the test suite uses ``en`` and ``pl`` as languages, and their respective default locale for them. -You can set the language - locale pairs by exporting two shell variables, like in:: +The choice of Polish is due to having one locale to generate instead of 20 (Spanish) and you can happily ignore it — just set the language–locale pairs by exporting two shell variables, for example:: export NIKOLA_LOCALE_DEFAULT=en,en_US.utf8 - export NIKOLA_LOCALE_OTHER=es,es_ES.utf8 + export NIKOLA_LOCALE_OTHER=pl,pl_PL.utf8 In Windows that would be:: - set NIKOLA_LOCALE_DEFAULT=en,English - set NIKOLA_LOCALE_OTHER=es,Spanish - + set NIKOLA_LOCALE_DEFAULT=en,English + set NIKOLA_LOCALE_OTHER=pl,Polish + Replace the part before the comma with a Nikola translation selector (see ``nikola/conf.py.in`` for details), and the part after the comma with an *installed* glibc locale. To check if the desired locale is supported in your host you can, in a python console:: - import locale - locale.setlocale(locale.LC_ALL, 'locale_name') - # by example, 'en_US.utf8' (posix) 'English' (windows) - # if it does not traceback, then python can use that locale + import locale + locale.setlocale(locale.LC_ALL, 'locale_name') + # for example, 'en_US.utf8' (posix) 'English' (windows) + # if it does not traceback, then python can use that locale Alternatively, if you have some disk space to spare, you can install the two default locales. Here is how to do that in Ubuntu:: - sudo apt-get install language-pack-en language-pack-es + sudo apt-get install language-pack-en language-pack-pl + - How to execute the tests ------------------------ @@ -90,3 +90,17 @@ Travis CI We also run our tests on `Travis CI `_. You can check the `current build status `_ there. + + +Writing tests +------------- + +* When adding new *.py files under tests/ , remember to include at the begining the lines:: + + # This code is so you can run the samples without installing the package, + # and should be before any import touching nikola, in any file under tests/ + import os + import sys + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + Those lines allow to run the tests without installing nikola. diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..6ad8bac 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2012-2014 Roberto Alsina and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/tests/base.py b/tests/base.py index 00f7486..f3e3545 100644 --- a/tests/base.py +++ b/tests/base.py @@ -8,10 +8,16 @@ __all__ = ["BaseTestCase", "cd", "LocaleSupportInTesting"] -from contextlib import contextmanager -import locale + +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ import os import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + +from contextlib import contextmanager +import locale import unittest import logbook @@ -106,18 +112,18 @@ class LocaleSupportInTesting(object): if hasattr(cls, 'langlocales'): return defaults = { - 'linux': { + 'posix': { # non-windows defaults, must be two locales suported by .travis.yml 'default': ("en", str("en_US.utf8")), - 'other': ("es", str("es_ES.utf8")), + 'other': ("pl", str("pl_PL.utf8")), }, 'windows': { # windows defaults 'default': ("en", str("English")), - 'other': ("es", str("Spanish")), + 'other': ("pl", str("Polish")), }, } - os_id = 'windows' if sys.platform == 'win32' else 'linux' + os_id = 'windows' if sys.platform == 'win32' else 'posix' langlocales = {} for suffix in ['other', 'default']: try: diff --git a/tests/context.py b/tests/context.py deleted file mode 100644 index 28eb872..0000000 --- a/tests/context.py +++ /dev/null @@ -1,9 +0,0 @@ -# -*- coding: utf-8 -*- - -# Path hack as shown by Kenneth Reitz at http://kennethreitz.com/repository-structure-and-python.html - -import os -import sys -sys.path.insert(0, os.path.abspath('..')) - -import nikola # NOQA diff --git a/tests/data/translated_titles/conf.py b/tests/data/translated_titles/conf.py index b445ba9..edfce3e 100644 --- a/tests/data/translated_titles/conf.py +++ b/tests/data/translated_titles/conf.py @@ -47,9 +47,23 @@ DEFAULT_LANG = "en" TRANSLATIONS = { "en": "", # Example for another language: - "es": "./es", + "pl": "./pl", } +# What will translated input files be named like? + +# If you have a page something.rst, then something.rst.pl will be considered +# its Polish translation. +# (in the above example: path == "something", lang == "pl", ext == "rst") +# this pattern is also used for metadata: +# something.meta -> something.meta.pl + +TRANSLATIONS_PATTERN = "{path}.{ext}.{lang}" + +# If you don't want your Polish files to be considered Perl code, use this: +# TRANSLATIONS_PATTERN = "{path}.{lang}.{ext}" +# Note that this pattern will become the default in v7.0.0. + # Links for the sidebar / navigation bar. # You should provide a key-value pair for each used language. SIDEBAR_LINKS = { @@ -57,7 +71,7 @@ SIDEBAR_LINKS = { ('/archive.html', 'Archives'), ('/categories/index.html', 'Tags'), ), - "es": () + "pl": () } diff --git a/tests/data/translated_titles/stories/1.txt.es b/tests/data/translated_titles/stories/1.txt.es deleted file mode 100644 index a888c1f..0000000 --- a/tests/data/translated_titles/stories/1.txt.es +++ /dev/null @@ -1,4 +0,0 @@ -.. title: Bar -.. slug: 1 - -Bar diff --git a/tests/data/translated_titles/stories/1.txt.pl b/tests/data/translated_titles/stories/1.txt.pl new file mode 100644 index 0000000..a888c1f --- /dev/null +++ b/tests/data/translated_titles/stories/1.txt.pl @@ -0,0 +1,4 @@ +.. title: Bar +.. slug: 1 + +Bar diff --git a/tests/test_command_import_wordpress.py b/tests/test_command_import_wordpress.py index f215705..d12c7f6 100644 --- a/tests/test_command_import_wordpress.py +++ b/tests/test_command_import_wordpress.py @@ -1,11 +1,17 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, absolute_import -from .context import nikola +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + import unittest import mock +import nikola import nikola.plugins.command.import_wordpress from .base import BaseTestCase diff --git a/tests/test_command_init.py b/tests/test_command_init.py index a9ec208..04e7d5f 100644 --- a/tests/test_command_init.py +++ b/tests/test_command_init.py @@ -1,10 +1,17 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, absolute_import -from .context import nikola +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + import unittest import mock +import nikola + class CommandInitCallTest(unittest.TestCase): def setUp(self): diff --git a/tests/test_compile_markdown.py b/tests/test_compile_markdown.py index a8252d8..d51d3aa 100644 --- a/tests/test_compile_markdown.py +++ b/tests/test_compile_markdown.py @@ -1,5 +1,13 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals + +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + import codecs import shutil import tempfile @@ -45,27 +53,6 @@ class CompileMarkdownTests(unittest.TestCase): actual_output = self.compile(input_str) self.assertEquals(actual_output, '') - def test_compile_html_heading_tags(self): - input_str = '''\ -# header 1 -## header 2 -### header 3 -#### header 4 -##### header 5 -###### header 6 -''' - expected_output = '''\ -

header 1

-

header 2

-

header 3

-
header 4
-
header 5
-header 6 -''' - - actual_output = self.compile(input_str) - self.assertEquals(actual_output.strip(), expected_output.strip()) - def test_compile_html_code_hilite(self): input_str = '''\ #!python diff --git a/tests/test_integration.py b/tests/test_integration.py index fdb2494..85d7892 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,19 +1,23 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function, absolute_import +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + import codecs import locale -import os import shutil -import subprocess -import sys import tempfile import unittest import lxml.html from nose.plugins.skip import SkipTest -from nikola import main +from nikola import __main__ import nikola import nikola.plugins.command import nikola.plugins.command.init @@ -60,7 +64,7 @@ class EmptyBuildTest(BaseTestCase): def build(self): """Build the site.""" with cd(self.target_dir): - main.main(["build"]) + __main__.main(["build"]) @classmethod def tearDownClass(self): @@ -163,7 +167,7 @@ class FuturePostTest(EmptyBuildTest): # Run deploy command to see if future post is deleted with cd(self.target_dir): - main.main(["deploy"]) + __main__.main(["deploy"]) self.assertTrue(os.path.isfile(index_path)) self.assertTrue(os.path.isfile(foo_path)) @@ -178,26 +182,62 @@ class TranslatedBuildTest(EmptyBuildTest): def __init__(self, *a, **kw): super(TranslatedBuildTest, self).__init__(*a, **kw) try: - locale.setlocale(locale.LC_ALL, ("es", "utf8")) + locale.setlocale(locale.LC_ALL, ("pl_PL", "utf8")) except: raise SkipTest def test_translated_titles(self): """Check that translated title is picked up.""" en_file = os.path.join(self.target_dir, "output", "stories", "1.html") - es_file = os.path.join(self.target_dir, "output", "es", "stories", "1.html") + pl_file = os.path.join(self.target_dir, "output", "pl", "stories", "1.html") # Files should be created self.assertTrue(os.path.isfile(en_file)) - self.assertTrue(os.path.isfile(es_file)) + self.assertTrue(os.path.isfile(pl_file)) # And now let's check the titles with codecs.open(en_file, 'r', 'utf8') as inf: doc = lxml.html.parse(inf) self.assertEqual(doc.find('//title').text, 'Foo | Demo Site') - with codecs.open(es_file, 'r', 'utf8') as inf: + with codecs.open(pl_file, 'r', 'utf8') as inf: doc = lxml.html.parse(inf) self.assertEqual(doc.find('//title').text, 'Bar | Demo Site') +class TranslationsPatternTest1(TranslatedBuildTest): + """Check that the path.lang.ext TRANSLATIONS_PATTERN works too""" + + @classmethod + def patch_site(self): + """Set the TRANSLATIONS_PATTERN to the new v7 default""" + os.rename(os.path.join(self.target_dir, "stories", "1.txt.pl"), + os.path.join(self.target_dir, "stories", "1.pl.txt") + ) + conf_path = os.path.join(self.target_dir, "conf.py") + with codecs.open(conf_path, "rb", "utf-8") as inf: + data = inf.read() + data = data.replace('TRANSLATIONS_PATTERN = "{path}.{ext}.{lang}"', + 'TRANSLATIONS_PATTERN = "{path}.{lang}.{ext}"') + with codecs.open(conf_path, "wb+", "utf8") as outf: + outf.write(data) + + +class TranslationsPatternTest2(TranslatedBuildTest): + """Check that the path_lang.ext TRANSLATIONS_PATTERN works too""" + + @classmethod + def patch_site(self): + """Set the TRANSLATIONS_PATTERN to the new v7 default""" + conf_path = os.path.join(self.target_dir, "conf.py") + os.rename(os.path.join(self.target_dir, "stories", "1.txt.pl"), + os.path.join(self.target_dir, "stories", "1_pl.txt") + ) + with codecs.open(conf_path, "rb", "utf-8") as inf: + data = inf.read() + data = data.replace('TRANSLATIONS_PATTERN = "{path}.{ext}.{lang}"', + 'TRANSLATIONS_PATTERN = "{path}_{lang}.{ext}"') + with codecs.open(conf_path, "wb+", "utf8") as outf: + outf.write(data) + + class RelativeLinkTest(DemoBuildTest): """Check that SITE_URL with a path doesn't break links.""" @@ -234,22 +274,22 @@ class RelativeLinkTest(DemoBuildTest): self.assertTrue('http://getnikola.com/foo/bar/index.html' in sitemap_data) -if sys.version_info[0] == 2 and sys.version_info[1] < 7: - check_output = subprocess.check_call -else: - check_output = subprocess.check_output - - class TestCheck(DemoBuildTest): """The demo build should pass 'nikola check'""" def test_check_links(self): with cd(self.target_dir): - check_output("nikola check -l", shell=True, stderr=subprocess.STDOUT) + try: + __main__.main(['check', '-l']) + except SystemExit as e: + self.assertEqual(e.code, 0) def test_check_files(self): with cd(self.target_dir): - check_output("nikola check -f", shell=True, stderr=subprocess.STDOUT) + try: + __main__.main(['check', '-f']) + except SystemExit as e: + self.assertEqual(e.code, 0) class TestCheckFailure(DemoBuildTest): @@ -258,19 +298,19 @@ class TestCheckFailure(DemoBuildTest): def test_check_links_fail(self): with cd(self.target_dir): os.unlink(os.path.join("output", "archive.html")) - self.assertRaises( - subprocess.CalledProcessError, - check_output, "nikola check -f", shell=True - ) + try: + __main__.main(['check', '-l']) + except SystemExit as e: + self.assertNotEqual(e.code, 0) def test_check_files_fail(self): with cd(self.target_dir): with codecs.open(os.path.join("output", "foobar"), "wb+", "utf8") as outf: outf.write("foo") - self.assertRaises( - subprocess.CalledProcessError, - check_output, "nikola check -f", shell=True - ) + try: + __main__.main(['check', '-f']) + except SystemExit as e: + self.assertNotEqual(e.code, 0) class RelativeLinkTest2(DemoBuildTest): @@ -334,5 +374,16 @@ class MonthlyArchiveTest(DemoBuildTest): self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'target', 'output', '2012', '03', 'index.html'))) +class SubdirRunningTest(DemoBuildTest): + """Check that running nikola from subdir works.""" + + def test_subdir_run(self): + """Check whether build works from posts/""" + + with cd(os.path.join(self.target_dir, 'posts')): + result = __main__.main(['build']) + self.assertEquals(result, 0) + + if __name__ == "__main__": unittest.main() diff --git a/tests/test_locale.py b/tests/test_locale.py index 93df2ae..90f9edb 100644 --- a/tests/test_locale.py +++ b/tests/test_locale.py @@ -1,6 +1,12 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals + +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + # needed if @unittest.expectedFailure is used try: diff --git a/tests/test_plugin_importing.py b/tests/test_plugin_importing.py index 5009f88..aaba113 100644 --- a/tests/test_plugin_importing.py +++ b/tests/test_plugin_importing.py @@ -1,7 +1,13 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, absolute_import -from .context import nikola # NOQA +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + import unittest diff --git a/tests/test_rss_feeds.py b/tests/test_rss_feeds.py index 169e1e7..d1404d8 100644 --- a/tests/test_rss_feeds.py +++ b/tests/test_rss_feeds.py @@ -1,6 +1,14 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, absolute_import + +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + from collections import defaultdict from io import StringIO import os @@ -9,18 +17,23 @@ import unittest import mock -from .context import nikola # NOQA from lxml import etree from .base import LocaleSupportInTesting +import nikola fake_conf = defaultdict(str) -fake_conf['TIMEZONE'] = None +fake_conf['TIMEZONE'] = 'UTC' fake_conf['DEFAULT_LANG'] = 'en' fake_conf['TRANSLATIONS'] = {'en': ''} fake_conf['BASE_URL'] = 'http://some.blog/' +class FakeCompiler(object): + demote_headers = False + compile_html = None + + class RSSFeedTest(unittest.TestCase): def setUp(self): LocaleSupportInTesting.initialize_locales_for_testing('unilingual') @@ -46,7 +59,7 @@ class RSSFeedTest(unittest.TestCase): True, {'en': ''}, 'post.tmpl', - lambda *a: None) + FakeCompiler()) opener_mock = mock.mock_open() diff --git a/tests/test_rst_compiler.py b/tests/test_rst_compiler.py index c5db8dd..ae4ac06 100644 --- a/tests/test_rst_compiler.py +++ b/tests/test_rst_compiler.py @@ -27,13 +27,18 @@ always unquoted. from __future__ import unicode_literals, absolute_import +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + import codecs try: from io import StringIO except ImportError: from StringIO import StringIO # NOQA -import os -import sys import tempfile import docutils @@ -113,6 +118,7 @@ class FakeSite(object): FakePost(title='Fake post', slug='fake-post') ] + self.debug = True def render_template(self, name, _, context): return('') diff --git a/tests/test_scheduling.py b/tests/test_scheduling.py index 264d1c5..c9cda42 100644 --- a/tests/test_scheduling.py +++ b/tests/test_scheduling.py @@ -1,5 +1,13 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, absolute_import + +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + from .base import BaseTestCase import datetime from nose.plugins.skip import SkipTest @@ -24,6 +32,17 @@ class TestScheduling(BaseTestCase): if not _freeze_time: raise SkipTest('freezegun not installed') + d = [name for name in sys.modules if name.startswith("six.moves.")] + self.deleted = {} + for name in d: + self.deleted[name] = sys.modules[name] + del sys.modules[name] + + @classmethod + def tearDown(self): + for name, mod in self.deleted.items(): + sys.modules[name] = mod + @freeze_time(NOW) def test_get_date(self): from nikola.plugins.command.new_post import get_date diff --git a/tests/test_utils.py b/tests/test_utils.py index 4f3fd72..3e66157 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,18 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +# This code is so you can run the samples without installing the package, +# and should be before any import touching nikola, in any file under tests/ +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + + import unittest import mock +import lxml.html from nikola.post import get_meta +from nikola.utils import demote_headers class dummy(object): @@ -117,8 +126,9 @@ class GetMetaTest(unittest.TestCase): post.source_path = '2013-01-23-the_slug-dubdubtitle.md' post.metadata_path = '2013-01-23-the_slug-dubdubtitle.meta' with mock.patch('nikola.post.codecs.open', create=True): - meta = get_meta(post, - '(?P\d{4}-\d{2}-\d{2})-(?P.*)-(?P.*)\.md') + meta = get_meta( + post, + '(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md') self.assertEqual('dubdubtitle', meta['title']) self.assertEqual('the_slug', meta['slug']) @@ -133,5 +143,96 @@ class GetMetaTest(unittest.TestCase): self.assertEqual('the_slug', meta['slug']) + +class HeaderDemotionTest(unittest.TestCase): + def demote_by_zero(self): + input_str = '''\ +<h1>header 1</h1> +<h2>header 2</h2> +<h3>header 3</h3> +<h4>header 4</h4> +<h5>header 5</h5> +<h6>header 6</h6> +''' + expected_output = '''\ +<h1>header 1</h1> +<h2>header 2</h2> +<h3>header 3</h3> +<h4>header 4</h4> +<h5>header 5</h5> +<h6>header 6</h6> +''' + doc = lxml.html.fromstring(input_str) + outdoc = lxml.html.fromstring(expected_output) + demote_headers(doc, 0) + self.assertEquals(lxml.html.tostring(outdoc), lxml.html.tostring(doc)) + + def demote_by_one(self): + input_str = '''\ +<h1>header 1</h1> +<h2>header 2</h2> +<h3>header 3</h3> +<h4>header 4</h4> +<h5>header 5</h5> +<h6>header 6</h6> +''' + expected_output = '''\ +<h2>header 1</h2> +<h3>header 2</h3> +<h4>header 3</h4> +<h5>header 4</h5> +<h6>header 5</h6> +<h6>header 6</h6> +''' + doc = lxml.html.fromstring(input_str) + outdoc = lxml.html.fromstring(expected_output) + demote_headers(doc, 1) + self.assertEquals(lxml.html.tostring(outdoc), lxml.html.tostring(doc)) + + def demote_by_two(self): + input_str = '''\ +<h1>header 1</h1> +<h2>header 2</h2> +<h3>header 3</h3> +<h4>header 4</h4> +<h5>header 5</h5> +<h6>header 6</h6> +''' + expected_output = '''\ +<h3>header 1</h3> +<h4>header 2</h4> +<h5>header 3</h5> +<h6>header 4</h6> +<h6>header 5</h6> +<h6>header 6</h6> +''' + doc = lxml.html.fromstring(input_str) + outdoc = lxml.html.fromstring(expected_output) + demote_headers(doc, 2) + self.assertEquals(lxml.html.tostring(outdoc), lxml.html.tostring(doc)) + + def demote_by_minus_one(self): + input_str = '''\ +<h1>header 1</h1> +<h2>header 2</h2> +<h3>header 3</h3> +<h4>header 4</h4> +<h5>header 5</h5> +<h6>header 6</h6> +''' + expected_output = '''\ +<h1>header 1</h1> +<h1>header 2</h1> +<h2>header 3</h2> +<h3>header 4</h3> +<h4>header 5</h4> +<h5>header 6</h5> +''' + doc = lxml.html.fromstring(input_str) + outdoc = lxml.html.fromstring(expected_output) + demote_headers(doc, -1) + self.assertEquals(lxml.html.tostring(outdoc), lxml.html.tostring(doc)) + + if __name__ == '__main__': unittest.main() -- cgit v1.2.3