diff -r 02b52bf9f5f8 -r 0865e1e90674 test/unittest_utils.py --- a/test/unittest_utils.py Wed Mar 24 10:23:31 2010 +0100 +++ b/test/unittest_utils.py Wed Apr 28 11:54:13 2010 +0200 @@ -1,9 +1,22 @@ +# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of CubicWeb. +# +# CubicWeb 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. +# +# logilab-common 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 CubicWeb. If not, see . """unit tests for module cubicweb.utils -:organization: Logilab -:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. -:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr -:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses """ import re @@ -11,13 +24,12 @@ import datetime from logilab.common.testlib import TestCase, unittest_main -from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList +from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList try: - import simplejson - from cubicweb.utils import CubicWebJsonEncoder + from cubicweb.utils import CubicWebJsonEncoder, json except ImportError: - simplejson = None + json = None class MakeUidTC(TestCase): def test_1(self): @@ -41,6 +53,52 @@ self.assert_(UStringIO()) +class RepeatListTC(TestCase): + + def test_base(self): + l = RepeatList(3, (1, 3)) + self.assertEquals(l[0], (1, 3)) + self.assertEquals(l[2], (1, 3)) + self.assertEquals(l[-1], (1, 3)) + self.assertEquals(len(l), 3) + # XXX + self.assertEquals(l[4], (1, 3)) + + self.failIf(RepeatList(0, None)) + + def test_slice(self): + l = RepeatList(3, (1, 3)) + self.assertEquals(l[0:1], [(1, 3)]) + self.assertEquals(l[0:4], [(1, 3)]*3) + self.assertEquals(l[:], [(1, 3)]*3) + + def test_iter(self): + self.assertEquals(list(RepeatList(3, (1, 3))), + [(1, 3)]*3) + + def test_add(self): + l = RepeatList(3, (1, 3)) + self.assertEquals(l + [(1, 4)], [(1, 3)]*3 + [(1, 4)]) + self.assertEquals([(1, 4)] + l, [(1, 4)] + [(1, 3)]*3) + self.assertEquals(l + RepeatList(2, (2, 3)), [(1, 3)]*3 + [(2, 3)]*2) + + x = l + RepeatList(2, (1, 3)) + self.assertIsInstance(x, RepeatList) + self.assertEquals(len(x), 5) + self.assertEquals(x[0], (1, 3)) + + x = l + [(1, 3)] * 2 + self.assertEquals(x, [(1, 3)] * 5) + + def test_eq(self): + self.assertEquals(RepeatList(3, (1, 3)), + [(1, 3)]*3) + + def test_pop(self): + l = RepeatList(3, (1, 3)) + l.pop(2) + self.assertEquals(l, [(1, 3)]*2) + class SizeConstrainedListTC(TestCase): def test_append(self): @@ -61,11 +119,11 @@ class JSONEncoderTC(TestCase): def setUp(self): - if simplejson is None: - self.skip('simplejson not available') + if json is None: + self.skip('json not available') def encode(self, value): - return simplejson.dumps(value, cls=CubicWebJsonEncoder) + return json.dumps(value, cls=CubicWebJsonEncoder) def test_encoding_dates(self): self.assertEquals(self.encode(datetime.datetime(2009, 9, 9, 20, 30)),