#!/usr/bin/python # Copyright (c) 2005-2006 by Mark T. Holder # Florida State University. (see end of file) # New file (post April, 2006, expected to be clean under pylint) '''Tests PIPRes.utility''' import unittest from cStringIO import StringIO from PIPRes.utility import * from PIPRes.testing import silentAssertRaises class UtilityTest(unittest.TestCase): def testGet_key(self): d = { 1:'a', 2: 'b', 3:'c', 4:'d', 5:'e'} self.assertEqual(5, get_key(d, 'e')) self.assertEqual(None, get_key(d, 4)) self.assertEqual(2, get_key(d, 'b')) def testPaddingInsert(self): b = [] b.insert(10, 'a') self.assertEqual(['a'], b) b = [] self.assertEqual(10, paddingInsert(b, 10, 'a')) self.assertEqual([None, None, None, None, None, None, None, None, None, None, 'a'], b) self.assertEqual('a', b[10]) self.assertEqual(0, paddingInsert(b, 10, 'b')) self.assertEqual([None, None, None, None, None, None, None, None, None, None, 'b', 'a'], b) self.assertEqual('b', b[10]) def testChompIter(self): a = '\na\nab\n' o = StringIO(a) self.assertEqual([i for i in o], ['\n', 'a\n', 'ab\n']) o.reset() self.assertEqual([i for i in chompIter(o)], ['', 'a', 'ab']) def testRemoveAll(self): a = list('a abaaiaaataa') self.assertEqual(removeAll(a, 'a'), list(' bit')) a = ['a ', 'ab', 'aa', 'ia', 'aa', 'ta', 'a'] self.assertEqual(removeAll(a, 'aa'), ['a ', 'ab', 'ia', 'ta', 'a']) def testGetBalancedString(self): ts = '(a),' self.assertEqual(getBalancedString(ts, '(', ')'), ('(a)', [], ',')) self.assertEqual(getBalancedString(ts, '(', ','), ('(a),', [], '')) self.assertEqual(getBalancedString(ts, 'a', ','), ('a),', ['('], '')) silentAssertRaises(self, ValueError, getBalancedString, ts, '(', ',', forbidden =')') silentAssertRaises(self, ValueError, getBalancedString, ts, ')', '(', ) silentAssertRaises(self, StopIteration, getBalancedString, ts, ')', 'z', ) ts = ' ((b, c),a);' self.assertEqual(getBalancedString(ts, '(', ')'), ('((b, c),a)', [' '], ';')) ts = ' ((b\n, c),a);' self.assertEqual(getBalancedString(ts, '(', ')'), ('((b\n, c),a)', [' '], ';')) ts = ' [[b\n, c},a};' self.assertEqual(getBalancedString(ts, '[', '}'), ('[[b\n, c},a}', [' '], ';')) ts = ' ((b' o = StringIO(',c ),\na);') self.assertEqual(getBalancedString(ts, '(', ')', sourceIter = o), ('((b,c ),\na)', [' '], ';')) o.reset() self.assertEqual(getBalancedString(ts, '(', ')', sourceIter = chompIter(o)), ('((b,c ),a)', [' '], ';')) if __name__ == '__main__': unittest.main() # This file is part of the PIPRes library # # The PIPRes library is free software; you can redistribute it # and/or modify it under the terms of the GNU Lesser General # Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later # version. # # This library 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 Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA