2004-08-16 19:20:06 +00:00
|
|
|
|
# -*- coding: iso-8859-1 -*-
|
2014-01-08 21:33:04 +00:00
|
|
|
|
# Copyright (C) 2004-2014 Bastian Kleineidam
|
2004-08-16 19:20:06 +00:00
|
|
|
|
#
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
|
# 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.
|
2005-01-19 15:08:02 +00:00
|
|
|
|
"""
|
|
|
|
|
|
Test string formatting operations.
|
|
|
|
|
|
"""
|
2004-08-16 19:20:06 +00:00
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
import os
|
2013-12-06 06:13:44 +00:00
|
|
|
|
import time
|
2004-08-16 19:20:06 +00:00
|
|
|
|
import linkcheck.strformat
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
class TestStrFormat(unittest.TestCase):
|
2005-01-19 14:38:01 +00:00
|
|
|
|
"""
|
|
|
|
|
|
Test string formatting routines.
|
|
|
|
|
|
"""
|
2004-08-16 19:20:06 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_unquote(self):
|
2010-03-09 07:42:46 +00:00
|
|
|
|
# Test quote stripping.
|
2005-03-08 19:29:16 +00:00
|
|
|
|
u = linkcheck.strformat.unquote
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(u(""), "")
|
|
|
|
|
|
self.assertEqual(u(None), None)
|
|
|
|
|
|
self.assertEqual(u("'"), "'")
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(u('"'), '"')
|
|
|
|
|
|
self.assertEqual(u('""'), "")
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(u("''"), "")
|
|
|
|
|
|
self.assertEqual(u("'a'"), "a")
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(u("'a\"'"), 'a"')
|
|
|
|
|
|
self.assertEqual(u("'\"a'"), '"a')
|
|
|
|
|
|
self.assertEqual(u('"a\'"'), "a'")
|
|
|
|
|
|
self.assertEqual(u('"\'a"'), "'a")
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(u("'a'", matching=True), "a")
|
|
|
|
|
|
self.assertEqual(u('"a"', matching=True), "a")
|
2004-08-16 19:20:06 +00:00
|
|
|
|
# even mis-matching quotes should be removed...
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(u("'a\""), "a")
|
|
|
|
|
|
self.assertEqual(u("\"a'"), "a")
|
2005-03-08 19:29:16 +00:00
|
|
|
|
# ...but not when matching is True
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(u("'a\"", matching=True), "'a\"")
|
|
|
|
|
|
self.assertEqual(u("\"a'", matching=True), "\"a'")
|
2004-08-16 19:20:06 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_wrap(self):
|
2010-03-09 07:42:46 +00:00
|
|
|
|
# Test line wrapping.
|
2006-02-02 22:58:11 +00:00
|
|
|
|
wrap = linkcheck.strformat.wrap
|
2020-05-28 19:29:13 +00:00
|
|
|
|
s = "11%(sep)s22%(sep)s33%(sep)s44%(sep)s55" % {"sep": os.linesep}
|
2004-08-16 19:20:06 +00:00
|
|
|
|
# testing width <= 0
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(wrap(s, -1), s)
|
|
|
|
|
|
self.assertEqual(wrap(s, 0), s)
|
2020-05-28 19:29:13 +00:00
|
|
|
|
n = len(os.linesep)
|
2014-03-02 06:45:04 +00:00
|
|
|
|
gap = " "
|
2020-05-28 19:29:13 +00:00
|
|
|
|
s2 = "11%(gap)s22%(sep)s33%(gap)s44%(sep)s55" % {"sep": os.linesep, "gap": gap}
|
2004-08-16 19:20:06 +00:00
|
|
|
|
# splitting lines
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(wrap(s2, 2), s)
|
2004-08-16 19:20:06 +00:00
|
|
|
|
# combining lines
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(wrap(s, 4 + n), s2)
|
2006-02-02 22:58:11 +00:00
|
|
|
|
# misc
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(wrap(s, -1), s)
|
|
|
|
|
|
self.assertEqual(wrap(s, 0), s)
|
|
|
|
|
|
self.assertEqual(wrap(None, 10), None)
|
2006-02-02 22:58:11 +00:00
|
|
|
|
self.assertFalse(linkcheck.strformat.get_paragraphs(None))
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_strsize(self):
|
2010-03-09 07:42:46 +00:00
|
|
|
|
# Test byte size strings.
|
2004-08-16 19:20:06 +00:00
|
|
|
|
self.assertRaises(ValueError, linkcheck.strformat.strsize, -1)
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(0), "0B")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1), "1B")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(2), "2B")
|
2013-02-27 18:36:03 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1023, grouping=False), "1023B")
|
2010-03-05 09:00:34 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1024), "1KB")
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1024 * 25), "25.00KB")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1024 * 1024), "1.00MB")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1024 * 1024 * 11), "11.0MB")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1024 * 1024 * 1024), "1.00GB")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.strsize(1024 * 1024 * 1024 * 14), "14.0GB")
|
2004-08-16 19:20:06 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_indent(self):
|
2005-08-16 13:42:46 +00:00
|
|
|
|
s = "bla"
|
2006-02-02 22:58:11 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.indent(s, ""), s)
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.indent(s, " "), " " + s)
|
2005-08-16 13:42:46 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_stripurl(self):
|
2020-04-30 19:11:59 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.stripurl("a\tb"), "a\tb")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.stripurl(" a\t b"), "a\t b")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.stripurl(" ab\t\ra\nb"), "ab")
|
2011-12-10 10:49:44 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.stripurl(None), None)
|
2020-04-30 19:11:59 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.stripurl(""), "")
|
2005-10-10 21:14:05 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_limit(self):
|
2006-02-02 22:58:11 +00:00
|
|
|
|
self.assertEqual(linkcheck.strformat.limit("", 0), "")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.limit("a", 0), "")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.limit("1", 1), "1")
|
|
|
|
|
|
self.assertEqual(linkcheck.strformat.limit("11", 1), "1...")
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_strtime(self):
|
2006-02-02 22:58:11 +00:00
|
|
|
|
zone = linkcheck.strformat.strtimezone()
|
2013-12-06 06:13:44 +00:00
|
|
|
|
t = linkcheck.strformat.strtime(0, func=time.gmtime)
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(t, "1970-01-01 00:00:00" + zone)
|
2006-02-02 22:58:11 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_duration_long(self):
|
2020-05-28 19:29:13 +00:00
|
|
|
|
def duration(s):
|
|
|
|
|
|
return linkcheck.strformat.strduration_long(s, do_translate=False)
|
2006-05-03 16:35:37 +00:00
|
|
|
|
self.assertEqual(duration(-0.5), "-0.50 seconds")
|
|
|
|
|
|
self.assertEqual(duration(0), "0.00 seconds")
|
|
|
|
|
|
self.assertEqual(duration(0.9), "0.90 seconds")
|
|
|
|
|
|
self.assertEqual(duration(1), "1 second")
|
|
|
|
|
|
self.assertEqual(duration(2), "2 seconds")
|
|
|
|
|
|
self.assertEqual(duration(60), "1 minute")
|
|
|
|
|
|
self.assertEqual(duration(120), "2 minutes")
|
2020-05-28 19:29:13 +00:00
|
|
|
|
self.assertEqual(duration(60 * 60), "1 hour")
|
|
|
|
|
|
self.assertEqual(duration(60 * 60 * 24), "1 day")
|
|
|
|
|
|
self.assertEqual(duration(60 * 60 * 24 * 365), "1 year")
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
duration(60 * 60 * 24 * 365 + 60 * 60 * 24 + 2), "1 year, 1 day"
|
|
|
|
|
|
)
|
2006-02-02 22:58:11 +00:00
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
|
def test_ascii_safe(self):
|
2006-02-02 22:58:11 +00:00
|
|
|
|
ascii_safe = linkcheck.strformat.ascii_safe
|
2020-04-30 19:11:59 +00:00
|
|
|
|
self.assertEqual(ascii_safe("a"), "a")
|
|
|
|
|
|
self.assertEqual(ascii_safe("<EFBFBD>"), "")
|
2012-10-10 08:54:30 +00:00
|
|
|
|
|
|
|
|
|
|
def test_strip_control_chars(self):
|
|
|
|
|
|
strip = linkcheck.strformat.strip_control_chars
|
|
|
|
|
|
self.assertEqual(strip(""), "")
|
2020-04-30 19:11:59 +00:00
|
|
|
|
self.assertEqual(strip("a"), "a")
|
|
|
|
|
|
self.assertEqual(strip("<EFBFBD>"), "<EFBFBD>")
|
|
|
|
|
|
self.assertEqual(strip("\x01"), "")
|