#!/usr/bin/python """ Copyright (c) 2004 Colin Stewart (http://www.owlfish.com/) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. 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. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. If you make any bug fixes or feature enhancements please let me know! Unit test cases. """ import unittest, os import StringIO import logging, logging.config from simpletal import simpleTAL, simpleTALES if (os.path.exists ("logging.ini")): logging.config.fileConfig ("logging.ini") else: logging.basicConfig() class TALRepeatTestCases (unittest.TestCase): def setUp (self): self.context = simpleTALES.Context() self.context.addGlobal ('test', 'testing') self.context.addGlobal ('one', [1]) self.context.addGlobal ('two', ["one", "two"]) self.context.addGlobal ('three', [1,"Two",3]) self.context.addGlobal ('emptyList', []) self.context.addGlobal ('bigList', range (1,100)) self.context.addGlobal ('fourList', ["zero", "one", "two", "three"]) self.context.addGlobal ('nested', [{'title': 'Image 1', 'catList': [1,2,3]} ,{'title': 'Image 2', 'catList': [5,2,3], 'selected': simpleTALES.DEFAULTVALUE} ,{'title': 'Image 3', 'catList': [8,9,1]} ]) self.context.addGlobal ('defList', ["Hello", simpleTALES.DEFAULTVALUE, "World"]) def _runTest_ (self, txt, result, errMsg="Error"): template = simpleTAL.compileHTMLTemplate (txt) file = StringIO.StringIO () try: template.expand (self.context, file) except Exception, e: print "Error, template compiled to: " + str (template) raise e realResult = file.getvalue() self.failUnless (realResult == result, "%s - \npassed in: %s \ngot back %s \nexpected %s\n\nTemplate: %s" % (errMsg, txt, realResult, result, template)) def testInvalidPath (self): self._runTest_ ('
Hello
', "", "Repeat of non-existant element failed") def testDefaultValue (self): self._runTest_ ('Default Only
', 'Default Only
', 'Default did not keep existing structure intact') def testStringRepeat (self): self._runTest_ ('', 'testing', 'Itteration over string failed.') def testEmptyList (self): self._runTest_ ('
Empty
' ,'' ,'Empty list repeat failed.' ) def testListRepeat (self): self._runTest_ ('', '
one
two
', 'Itteration over list failed.') def testTwoCmndsOneTagListRepeat (self): self._runTest_ ('' ,'one
two
' ,'Itteration over list with both content and repeat on same element failed.') def testAttributesInRepeat (self): self._runTest_ ('' ,'Image 1
Image 2
Image 3
' ,'Accessing attributes in repeat loop failed.') def testDefaultInContentInRepeat (self): self._runTest_ ('Default Word
' ,'Hello
Default Word
World
' ,'Using default content in repeat loop failed.') def testDefaultInReplaceInRepeat (self): self._runTest_ ('Default Word
' ,'HelloDefault Word
World' ,'Using default content in repeat loop failed.') def testNestedRepeat (self): self._runTest_ ('' ,'Index
' ,expectedResult ,"Repeat variable index failed." ) def testRepeatVarNumber (self): self._runTest_ ('Index
' ,'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899' ,'Repeat variable number failed.' ) def testRepeatVarEvenOdd (self): self._runTest_ ('- OddEven
' ,'zero - Even
one - Odd
two - Even
three - Odd
' ,'Repeat variables odd and even failed.' ) def testRepeatVarStartEnd (self): self._runTest_ ('StartEnd
' ,'Startzero
one
two
threeEnd
' ,'Repeat variables start and end failed.' ) def testRepeatVarLength (self): self._runTest_ ('Len: lengthEntry:
' ,'Len: 4Entry: zero
Entry: one
Entry: two
Entry: three
' ,'Repeat variable length failed.' ) def testRepeatVarLowerLetter (self): self._runTest_ ('a,b,c,etc:
' ,'a: zero
b: one
c: two
d: three
' ,'Repeat variable letter failed.' ) def testRepeatVarLowerLetterLarge (self): self._runTest_ ('a,b,c,etc:
' ,'a: 1
b: 2
c: 3
d: 4
e: 5
f: 6
g: 7
h: 8
i: 9
j: 10
k: 11
l: 12
m: 13
n: 14
o: 15
p: 16
q: 17
r: 18
s: 19
t: 20
u: 21
v: 22
w: 23
x: 24
y: 25
z: 26
ba: 27
bb: 28
bc: 29
bd: 30
be: 31
bf: 32
bg: 33
bh: 34
bi: 35
bj: 36
bk: 37
bl: 38
bm: 39
bn: 40
bo: 41
bp: 42
bq: 43
br: 44
bs: 45
bt: 46
bu: 47
bv: 48
bw: 49
bx: 50
by: 51
bz: 52
ca: 53
cb: 54
cc: 55
cd: 56
ce: 57
cf: 58
cg: 59
ch: 60
ci: 61
cj: 62
ck: 63
cl: 64
cm: 65
cn: 66
co: 67
cp: 68
cq: 69
cr: 70
cs: 71
ct: 72
cu: 73
cv: 74
cw: 75
cx: 76
cy: 77
cz: 78
da: 79
db: 80
dc: 81
dd: 82
de: 83
df: 84
dg: 85
dh: 86
di: 87
dj: 88
dk: 89
dl: 90
dm: 91
dn: 92
do: 93
dp: 94
dq: 95
dr: 96
ds: 97
dt: 98
du: 99
' ,'Repeat variable letter failed on a large list.' ) def testRepeatVarUpperLetter (self): self._runTest_ ('A,B,C,etc:
' ,'A: zero
B: one
C: two
D: three
' ,'Repeat variable Letter failed.' ) def testRepeatVarLowerRoman (self): self._runTest_ ('i,ii,iii,etc:
' ,'i: 1
ii: 2
iii: 3
iv: 4
v: 5
vi: 6
vii: 7
viii: 8
ix: 9
x: 10
xi: 11
xii: 12
xiii: 13
xiv: 14
xv: 15
xvi: 16
xvii: 17
xviii: 18
xix: 19
xx: 20
xxi: 21
xxii: 22
xxiii: 23
xxiv: 24
xxv: 25
xxvi: 26
xxvii: 27
xxviii: 28
xxix: 29
xxx: 30
xxxi: 31
xxxii: 32
xxxiii: 33
xxxiv: 34
xxxv: 35
xxxvi: 36
xxxvii: 37
xxxviii: 38
xxxix: 39
xl: 40
xli: 41
xlii: 42
xliii: 43
xliv: 44
xlv: 45
xlvi: 46
xlvii: 47
xlviii: 48
xlix: 49
l: 50
li: 51
lii: 52
liii: 53
liv: 54
lv: 55
lvi: 56
lvii: 57
lviii: 58
lix: 59
lx: 60
lxi: 61
lxii: 62
lxiii: 63
lxiv: 64
lxv: 65
lxvi: 66
lxvii: 67
lxviii: 68
lxix: 69
lxx: 70
lxxi: 71
lxxii: 72
lxxiii: 73
lxxiv: 74
lxxv: 75
lxxvi: 76
lxxvii: 77
lxxviii: 78
lxxix: 79
lxxx: 80
lxxxi: 81
lxxxii: 82
lxxxiii: 83
lxxxiv: 84
lxxxv: 85
lxxxvi: 86
lxxxvii: 87
lxxxviii: 88
lxxxix: 89
xc: 90
xci: 91
xcii: 92
xciii: 93
xciv: 94
xcv: 95
xcvi: 96
xcvii: 97
xcviii: 98
xcix: 99
' ,'Repeat variable roman failed.' ) def testRepeatVarUpperRoman (self): self._runTest_ ('I,II,III,etc:
' ,'I: 1
II: 2
III: 3
IV: 4
V: 5
VI: 6
VII: 7
VIII: 8
IX: 9
X: 10
XI: 11
XII: 12
XIII: 13
XIV: 14
XV: 15
XVI: 16
XVII: 17
XVIII: 18
XIX: 19
XX: 20
XXI: 21
XXII: 22
XXIII: 23
XXIV: 24
XXV: 25
XXVI: 26
XXVII: 27
XXVIII: 28
XXIX: 29
XXX: 30
XXXI: 31
XXXII: 32
XXXIII: 33
XXXIV: 34
XXXV: 35
XXXVI: 36
XXXVII: 37
XXXVIII: 38
XXXIX: 39
XL: 40
XLI: 41
XLII: 42
XLIII: 43
XLIV: 44
XLV: 45
XLVI: 46
XLVII: 47
XLVIII: 48
XLIX: 49
L: 50
LI: 51
LII: 52
LIII: 53
LIV: 54
LV: 55
LVI: 56
LVII: 57
LVIII: 58
LIX: 59
LX: 60
LXI: 61
LXII: 62
LXIII: 63
LXIV: 64
LXV: 65
LXVI: 66
LXVII: 67
LXVIII: 68
LXIX: 69
LXX: 70
LXXI: 71
LXXII: 72
LXXIII: 73
LXXIV: 74
LXXV: 75
LXXVI: 76
LXXVII: 77
LXXVIII: 78
LXXIX: 79
LXXX: 80
LXXXI: 81
LXXXII: 82
LXXXIII: 83
LXXXIV: 84
LXXXV: 85
LXXXVI: 86
LXXXVII: 87
LXXXVIII: 88
LXXXIX: 89
XC: 90
XCI: 91
XCII: 92
XCIII: 93
XCIV: 94
XCV: 95
XCVI: 96
XCVII: 97
XCVIII: 98
XCIX: 99
' ,'Repeat variable Roman failed.' ) def testLocalVarScope (self): self._runTest_ ('bold
VAR EXISTS' ,'zero
one
two
three
' ,'Local repeat variable remained accessible out of scope!' ) if __name__ == '__main__': unittest.main()