mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Start using parameterized tests in test_utils.py
This commit is contained in:
parent
5980c48e26
commit
6aef1eee41
2 changed files with 117 additions and 150 deletions
|
|
@ -9,6 +9,7 @@ import time
|
|||
from sys import platform
|
||||
from tempfile import mkdtemp
|
||||
|
||||
from nose_parameterized import parameterized
|
||||
import pytz
|
||||
|
||||
from pelican import utils
|
||||
|
|
@ -106,114 +107,84 @@ class TestUtils(LoggedTestCase):
|
|||
for item in invalid_dates:
|
||||
self.assertRaises(ValueError, utils.get_date, item)
|
||||
|
||||
def test_slugify(self):
|
||||
|
||||
samples = (('this is a test', 'this-is-a-test'),
|
||||
('this is a test', 'this-is-a-test'),
|
||||
('this → is ← a ↑ test', 'this-is-a-test'),
|
||||
('this--is---a test', 'this-is-a-test'),
|
||||
('unicode測試許功蓋,你看到了嗎?',
|
||||
'unicodece-shi-xu-gong-gai-ni-kan-dao-liao-ma'),
|
||||
('大飯原発4号機、18日夜起動へ',
|
||||
'da-fan-yuan-fa-4hao-ji-18ri-ye-qi-dong-he'),)
|
||||
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.slugify(value), expected)
|
||||
|
||||
def test_slugify_substitute(self):
|
||||
|
||||
samples = (('C++ is based on C', 'cpp-is-based-on-c'),
|
||||
('C+++ test C+ test', 'cpp-test-c-test'),
|
||||
('c++, c#, C#, C++', 'cpp-c-sharp-c-sharp-cpp'),
|
||||
('c++-streams', 'cpp-streams'),)
|
||||
@parameterized.expand([
|
||||
('this is a test', 'this-is-a-test'),
|
||||
('this is a test', 'this-is-a-test'),
|
||||
('this → is ← a ↑ test', 'this-is-a-test'),
|
||||
('this--is---a test', 'this-is-a-test'),
|
||||
('unicode測試許功蓋,你看到了嗎?',
|
||||
'unicodece-shi-xu-gong-gai-ni-kan-dao-liao-ma'),
|
||||
('大飯原発4号機、18日夜起動へ',
|
||||
'da-fan-yuan-fa-4hao-ji-18ri-ye-qi-dong-he'),
|
||||
])
|
||||
def test_slugify(self, value, expected):
|
||||
self.assertEqual(utils.slugify(value), expected)
|
||||
|
||||
@parameterized.expand([
|
||||
('C++ is based on C', 'cpp-is-based-on-c'),
|
||||
('C+++ test C+ test', 'cpp-test-c-test'),
|
||||
('c++, c#, C#, C++', 'cpp-c-sharp-c-sharp-cpp'),
|
||||
('c++-streams', 'cpp-streams'),
|
||||
])
|
||||
def test_slugify_substitute(self, value, expected):
|
||||
subs = (('C++', 'CPP'), ('C#', 'C-SHARP'))
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.slugify(value, subs), expected)
|
||||
|
||||
def test_slugify_substitute_and_keeping_non_alphanum(self):
|
||||
|
||||
samples = (('Fedora QA', 'fedora.qa'),
|
||||
('C++ is used by Fedora QA', 'cpp is used by fedora.qa'),
|
||||
('C++ is based on C', 'cpp-is-based-on-c'),
|
||||
('C+++ test C+ test', 'cpp-test-c-test'),)
|
||||
self.assertEqual(utils.slugify(value, subs), expected)
|
||||
|
||||
@parameterized.expand([
|
||||
('Fedora QA', 'fedora.qa'),
|
||||
('C++ is used by Fedora QA', 'cpp is used by fedora.qa'),
|
||||
('C++ is based on C', 'cpp-is-based-on-c'),
|
||||
('C+++ test C+ test', 'cpp-test-c-test'),
|
||||
])
|
||||
def test_slugify_substitute_keeping_non_alphanum(self, value, expected):
|
||||
subs = (('Fedora QA', 'fedora.qa', True),
|
||||
('c++', 'cpp'),)
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.slugify(value, subs), expected)
|
||||
self.assertEqual(utils.slugify(value, subs), expected)
|
||||
|
||||
def test_get_relative_path(self):
|
||||
@parameterized.expand([
|
||||
(os.path.join('test', 'test.html'), os.pardir),
|
||||
(os.path.join('test', 'test', 'test.html'),
|
||||
os.path.join(os.pardir, os.pardir)),
|
||||
('test.html', os.curdir),
|
||||
(os.path.join('/test', 'test.html'), os.pardir),
|
||||
(os.path.join('/test', 'test', 'test.html'),
|
||||
os.path.join(os.pardir, os.pardir)),
|
||||
('/test.html', os.curdir),
|
||||
])
|
||||
def test_get_relative_path(self, value, expected):
|
||||
self.assertEqual(utils.get_relative_path(value), expected)
|
||||
|
||||
samples = ((os.path.join('test', 'test.html'), os.pardir),
|
||||
(os.path.join('test', 'test', 'test.html'),
|
||||
os.path.join(os.pardir, os.pardir)),
|
||||
('test.html', os.curdir),
|
||||
(os.path.join('/test', 'test.html'), os.pardir),
|
||||
(os.path.join('/test', 'test', 'test.html'),
|
||||
os.path.join(os.pardir, os.pardir)),
|
||||
('/test.html', os.curdir),)
|
||||
@parameterized.expand([
|
||||
# Plain text
|
||||
('short string', 'short string'),
|
||||
('word ' * 100, 'word ' * 20 + '…'),
|
||||
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.get_relative_path(value), expected)
|
||||
|
||||
def test_truncate_html_words(self):
|
||||
# Plain text.
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words('short string', 20),
|
||||
'short string')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words('word ' * 100, 20),
|
||||
'word ' * 20 + '…')
|
||||
|
||||
# Words enclosed or intervaled by HTML tags.
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words('<p>' + 'word ' * 100 + '</p>', 20),
|
||||
'<p>' + 'word ' * 20 + '…</p>')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words(
|
||||
'<span\nstyle="\n…\n">' + 'word ' * 100 + '</span>', 20),
|
||||
'<span\nstyle="\n…\n">' + 'word ' * 20 + '…</span>')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words('<br>' + 'word ' * 100, 20),
|
||||
'<br>' + 'word ' * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words('<!-- comment -->' + 'word ' * 100, 20),
|
||||
'<!-- comment -->' + 'word ' * 20 + '…')
|
||||
# Words enclosed or intervaled by HTML tags
|
||||
('<p>' + 'word ' * 100 + '</p>', '<p>' + 'word ' * 20 + '…</p>'),
|
||||
('<span\nstyle="\n…\n">' + 'word ' * 100 + '</span>',
|
||||
'<span\nstyle="\n…\n">' + 'word ' * 20 + '…</span>'),
|
||||
('<br>' + 'word ' * 100, '<br>' + 'word ' * 20 + '…'),
|
||||
('<!-- comment -->' + 'word ' * 100,
|
||||
'<!-- comment -->' + 'word ' * 20 + '…'),
|
||||
|
||||
# Words with hypens and apostrophes.
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("a-b " * 100, 20),
|
||||
"a-b " * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("it's " * 100, 20),
|
||||
"it's " * 20 + '…')
|
||||
("a-b " * 100, "a-b " * 20 + '…'),
|
||||
("it's " * 100, "it's " * 20 + '…'),
|
||||
|
||||
# Words with HTML entity references.
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("é " * 100, 20),
|
||||
"é " * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("café " * 100, 20),
|
||||
"café " * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("èlite " * 100, 20),
|
||||
"èlite " * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("cafetiére " * 100, 20),
|
||||
"cafetiére " * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("∫dx " * 100, 20),
|
||||
"∫dx " * 20 + '…')
|
||||
("é " * 100, "é " * 20 + '…'),
|
||||
("café " * 100, "café " * 20 + '…'),
|
||||
("èlite " * 100, "èlite " * 20 + '…'),
|
||||
("cafetiére " * 100, "cafetiére " * 20 + '…'),
|
||||
("∫dx " * 100, "∫dx " * 20 + '…'),
|
||||
|
||||
# Words with HTML character references inside and outside
|
||||
# the ASCII range.
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("é " * 100, 20),
|
||||
"é " * 20 + '…')
|
||||
self.assertEqual(
|
||||
utils.truncate_html_words("∫dx " * 100, 20),
|
||||
"∫dx " * 20 + '…')
|
||||
("é " * 100, "é " * 20 + '…'),
|
||||
("∫dx " * 100, "∫dx " * 20 + '…'),
|
||||
])
|
||||
def test_truncate_html_words(self, html_words, expected):
|
||||
self.assertEqual(utils.truncate_html_words(html_words, 20), expected)
|
||||
|
||||
def test_process_translations(self):
|
||||
# create a bunch of articles
|
||||
|
|
@ -327,80 +298,76 @@ class TestUtils(LoggedTestCase):
|
|||
utils.clean_output_dir(test_directory, retention)
|
||||
self.assertFalse(os.path.exists(test_directory))
|
||||
|
||||
def test_strftime(self):
|
||||
d = utils.SafeDatetime(2012, 8, 29)
|
||||
|
||||
@parameterized.expand([
|
||||
# simple formatting
|
||||
self.assertEqual(utils.strftime(d, '%d/%m/%y'), '29/08/12')
|
||||
self.assertEqual(utils.strftime(d, '%d/%m/%Y'), '29/08/2012')
|
||||
('%d/%m/%y', '29/08/12'),
|
||||
('%d/%m/%Y', '29/08/2012'),
|
||||
|
||||
# RFC 3339
|
||||
self.assertEqual(
|
||||
utils.strftime(d, '%Y-%m-%dT%H:%M:%SZ'),
|
||||
'2012-08-29T00:00:00Z')
|
||||
('%Y-%m-%dT%H:%M:%SZ', '2012-08-29T00:00:00Z'),
|
||||
|
||||
# % escaped
|
||||
self.assertEqual(utils.strftime(d, '%d%%%m%%%y'), '29%08%12')
|
||||
self.assertEqual(utils.strftime(d, '%d %% %m %% %y'), '29 % 08 % 12')
|
||||
('%d%%%m%%%y', '29%08%12'),
|
||||
('%d %% %m %% %y', '29 % 08 % 12'),
|
||||
|
||||
# not valid % formatter
|
||||
self.assertEqual(utils.strftime(d, '10% reduction in %Y'),
|
||||
'10% reduction in 2012')
|
||||
self.assertEqual(utils.strftime(d, '%10 reduction in %Y'),
|
||||
'%10 reduction in 2012')
|
||||
('10% reduction in %Y', '10% reduction in 2012'),
|
||||
('%10 reduction in %Y', '%10 reduction in 2012'),
|
||||
|
||||
# with text
|
||||
self.assertEqual(utils.strftime(d, 'Published in %d-%m-%Y'),
|
||||
'Published in 29-08-2012')
|
||||
('Published in %d-%m-%Y', 'Published in 29-08-2012'),
|
||||
|
||||
# with non-ascii text
|
||||
self.assertEqual(
|
||||
utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
|
||||
'29/08/2012 Øl trinken beim Besäufnis')
|
||||
('%d/%m/%Y Øl trinken beim Besäufnis',
|
||||
'29/08/2012 Øl trinken beim Besäufnis'),
|
||||
|
||||
# alternative formatting options
|
||||
self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '29/8/12')
|
||||
self.assertEqual(utils.strftime(d, '%-H:%-M:%-S'), '0:0:0')
|
||||
('%-d/%-m/%y', '29/8/12'),
|
||||
('%-H:%-M:%-S', '0:0:0'),
|
||||
])
|
||||
def test_strftime(self, fmt_string, expected):
|
||||
d = utils.SafeDatetime(2012, 8, 29)
|
||||
self.assertEqual(utils.strftime(d, fmt_string), expected)
|
||||
|
||||
d = utils.SafeDatetime(2012, 8, 9)
|
||||
self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '9/8/12')
|
||||
|
||||
# test the output of utils.strftime in a different locale
|
||||
# Turkish locale
|
||||
@unittest.skipUnless(locale_available('tr_TR.UTF-8') or
|
||||
locale_available('Turkish'),
|
||||
'Turkish locale needed')
|
||||
def test_strftime_locale_dependent_turkish(self):
|
||||
@parameterized.expand([
|
||||
# simple
|
||||
('%d %B %Y', '29 Ağustos 2012'),
|
||||
('%A, %d %B %Y', 'Çarşamba, 29 Ağustos 2012'),
|
||||
|
||||
# with text
|
||||
('Yayınlanma tarihi: %A, %d %B %Y',
|
||||
'Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012')
|
||||
|
||||
# non-ascii format candidate (someone might pass it… for
|
||||
# some reason)
|
||||
('%Y yılında %üretim artışı', '2012 yılında %üretim artışı'),
|
||||
])
|
||||
def test_strftime_locale_dependent_turkish(self, fmt_string, expected):
|
||||
'''
|
||||
Test the output of utils.strftime in a different locale (Turkish).
|
||||
'''
|
||||
if platform == 'win32':
|
||||
new_locale = 'Turkish'
|
||||
else:
|
||||
new_locale = 'tr_TR.UTF-8'
|
||||
|
||||
with utils.temporary_locale(new_locale):
|
||||
|
||||
d = utils.SafeDatetime(2012, 8, 29)
|
||||
self.assertEqual(utils.strftime(d, fmt_string), expected)
|
||||
|
||||
# simple
|
||||
self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 Ağustos 2012')
|
||||
self.assertEqual(utils.strftime(d, '%A, %d %B %Y'),
|
||||
'Çarşamba, 29 Ağustos 2012')
|
||||
|
||||
# with text
|
||||
self.assertEqual(
|
||||
utils.strftime(d, 'Yayınlanma tarihi: %A, %d %B %Y'),
|
||||
'Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012')
|
||||
|
||||
# non-ascii format candidate (someone might pass it… for
|
||||
# some reason)
|
||||
self.assertEqual(
|
||||
utils.strftime(d, '%Y yılında %üretim artışı'),
|
||||
'2012 yılında %üretim artışı')
|
||||
|
||||
# test the output of utils.strftime in a different locale
|
||||
# French locale
|
||||
@unittest.skipUnless(locale_available('fr_FR.UTF-8') or
|
||||
locale_available('French'),
|
||||
'French locale needed')
|
||||
def test_strftime_locale_dependent_french(self):
|
||||
'''
|
||||
Test the output of utils.strftime in a different locale (French).
|
||||
'''
|
||||
if platform == 'win32':
|
||||
new_locale = 'French'
|
||||
else:
|
||||
|
|
@ -427,16 +394,15 @@ class TestUtils(LoggedTestCase):
|
|||
utils.strftime(d, '%écrits en %Y'),
|
||||
'%écrits en 2012')
|
||||
|
||||
def test_maybe_pluralize(self):
|
||||
@parameterized.expand([
|
||||
(0, 'Article', 'Articles', '0 Articles'),
|
||||
(1, 'Article', 'Articles', '1 Article'),
|
||||
(2, 'Article', 'Articles', '2 Articles'),
|
||||
])
|
||||
def test_maybe_pluralize(self, quantity, singular, plural, expected):
|
||||
self.assertEqual(
|
||||
utils.maybe_pluralize(0, 'Article', 'Articles'),
|
||||
'0 Articles')
|
||||
self.assertEqual(
|
||||
utils.maybe_pluralize(1, 'Article', 'Articles'),
|
||||
'1 Article')
|
||||
self.assertEqual(
|
||||
utils.maybe_pluralize(2, 'Article', 'Articles'),
|
||||
'2 Articles')
|
||||
utils.maybe_pluralize(quantity, singular, plural),
|
||||
expected)
|
||||
|
||||
|
||||
class TestCopy(unittest.TestCase):
|
||||
|
|
|
|||
1
tox.ini
1
tox.ini
|
|
@ -13,6 +13,7 @@ deps =
|
|||
-rrequirements/developer.pip
|
||||
nose
|
||||
nose-cov
|
||||
nose-parameterized
|
||||
coveralls
|
||||
pygments==2.1.3
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue