#!/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 * class UtilityTest(unittest.TestCase): 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),', ['('], '')) self.assertRaises(ValueError, getBalancedString, ts, '(', ',', forbidden =')') self.assertRaises(ValueError, getBalancedString, ts, ')', '(', ) self.assertRaises(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