mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-14 06:39:23 +01:00
134 lines
5.4 KiB
Python
134 lines
5.4 KiB
Python
#!/usr/bin/env python
|
|
|
|
###
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
# All rights reserved.
|
|
#
|
|
# Redistribution and use in source and binary forms, with or without
|
|
# modification, are permitted provided that the following conditions are met:
|
|
#
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
# this list of conditions, and the following disclaimer.
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
# this list of conditions, and the following disclaimer in the
|
|
# documentation and/or other materials provided with the distribution.
|
|
# * Neither the name of the author of this software nor the name of
|
|
# contributors to this software may be used to endorse or promote products
|
|
# derived from this software without specific prior written consent.
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
###
|
|
|
|
from test import *
|
|
|
|
import utils
|
|
|
|
|
|
class UtilsTest(unittest.TestCase):
|
|
def testTimeElapsed(self):
|
|
self.assertRaises(ValueError, utils.timeElapsed, 0, 0, seconds=False)
|
|
then = 0
|
|
now = 0
|
|
for now, expected in [(0, '0 seconds'),
|
|
(1, '1 second'),
|
|
(60, '1 minute and 0 seconds'),
|
|
(61, '1 minute and 1 second'),
|
|
(62, '1 minute and 2 seconds'),
|
|
(122, '2 minutes and 2 seconds'),
|
|
(3722, '1 hour, 2 minutes and 2 seconds'),
|
|
(7322, '2 hours, 2 minutes and 2 seconds')]:
|
|
self.assertEqual(utils.timeElapsed(now, then), expected)
|
|
|
|
def testEachSubstring(self):
|
|
s = 'foobar'
|
|
L = ['f', 'fo', 'foo', 'foob', 'fooba', 'foobar']
|
|
self.assertEqual(list(utils.eachSubstring(s)), L)
|
|
|
|
def testDistance(self):
|
|
self.assertEqual(utils.distance('', ''), 0)
|
|
self.assertEqual(utils.distance('a', 'b'), 1)
|
|
self.assertEqual(utils.distance('a', 'a'), 0)
|
|
self.assertEqual(utils.distance('foobar', 'jemfinch'), 8)
|
|
self.assertEqual(utils.distance('a', 'ab'), 1)
|
|
self.assertEqual(utils.distance('foo', ''), 3)
|
|
self.assertEqual(utils.distance('', 'foo'), 3)
|
|
self.assertEqual(utils.distance('appel', 'nappe'), 2)
|
|
self.assertEqual(utils.distance('nappe', 'appel'), 2)
|
|
|
|
def testAbbrev(self):
|
|
L = ['abc', 'bcd', 'bbe', 'foo', 'fool']
|
|
d = utils.abbrev(L)
|
|
def getItem(s):
|
|
return d[s]
|
|
self.assertRaises(KeyError, getItem, 'f')
|
|
self.assertRaises(KeyError, getItem, 'fo')
|
|
self.assertRaises(KeyError, getItem, 'b')
|
|
self.assertEqual(d['bb'], 'bbe')
|
|
self.assertEqual(d['bc'], 'bcd')
|
|
self.assertEqual(d['a'], 'abc')
|
|
self.assertEqual(d['ab'], 'abc')
|
|
self.assertEqual(d['fool'], 'fool')
|
|
self.assertEqual(d['foo'], 'foo')
|
|
|
|
def testSoundex(self):
|
|
L = [('Euler', 'E460'),
|
|
('Ellery', 'E460'),
|
|
('Gauss', 'G200'),
|
|
('Ghosh', 'G200'),
|
|
('Hilbert', 'H416'),
|
|
('Heilbronn', 'H416'),
|
|
('Knuth', 'K530'),
|
|
('Kant', 'K530'),
|
|
('Lloyd', 'L300'),
|
|
('Ladd', 'L300'),
|
|
('Lukasiewicz', 'L222'),
|
|
('Lissajous', 'L222')]
|
|
for (name, key) in L:
|
|
soundex = utils.soundex(name)
|
|
self.assertEqual(soundex, key,
|
|
'%s was %s, not %s' % (name, soundex, key))
|
|
|
|
def testDQRepr(self):
|
|
L = [('foo', '"foo"'),
|
|
('foo\'bar', '"foo\'bar"'),
|
|
('foo"bar', '"foo\\"bar"'),
|
|
('"', '"\\""'),
|
|
('', '""'),
|
|
('\x00', '"\\x00"')]
|
|
for (s, r) in L:
|
|
self.assertEqual(r, utils.dqrepr(s))
|
|
self.assertEqual(s, eval(utils.dqrepr(s)))
|
|
|
|
def testPerlReToPythonRe(self):
|
|
r = utils.perlReToPythonRe('m/foo/')
|
|
self.failUnless(r.search('foo'))
|
|
r = utils.perlReToPythonRe('/foo/')
|
|
self.failUnless(r.search('foo'))
|
|
r = utils.perlReToPythonRe('m/\\//')
|
|
self.failUnless(r.search('/'))
|
|
|
|
def testPerlReToReplacer(self):
|
|
f = utils.perlReToReplacer('s/foo/bar/')
|
|
self.assertEqual(f('foobarbaz'), 'barbarbaz')
|
|
f = utils.perlReToReplacer('s/fool/bar/')
|
|
self.assertEqual(f('foobarbaz'), 'foobarbaz')
|
|
f = utils.perlReToReplacer('s/foo//')
|
|
self.assertEqual(f('foobarbaz'), 'barbaz')
|
|
f = utils.perlReToReplacer('s/ba//')
|
|
self.assertEqual(f('foobarbaz'), 'foorbaz')
|
|
f = utils.perlReToReplacer('s/ba//g')
|
|
self.assertEqual(f('foobarbaz'), 'foorz')
|
|
f = utils.perlReToReplacer('s/ba\\///g')
|
|
self.assertEqual(f('fooba/rba/z'), 'foorz')
|
|
|