66import time
77import random
88import itertools
9+ import copy
10+ import pickle
911import gc
1012
1113sys .path .insert (0 , '..' )
1214import tdl
1315
14- ERROR_RANGE = 100 # a number to test out of bound errors
16+ #ERROR_RANGE = 100 # a number to test out of bound errors
17+ WIDTH , HEIGHT = 30 , 20
18+ WINWIDTH , WINHEIGHT = 10 , 10
19+
20+ DEFAULT_CHAR = (0x20 , (0 , 0 , 0 ), (0 , 0 , 0 ))
1521
1622class TDLTemplate (unittest .TestCase ):
1723 "Nearly all tests need tdl.init to be called"
1824
1925 @classmethod
2026 def setUpClass (cls ):
2127 tdl .setFont ('../fonts/libtcod/terminal8x8_gs_ro.png' )
22- cls .console = tdl .init (30 , 20 , 'TDL UnitTest' , False , renderer = 'SDL' )
28+ cls .console = tdl .init (WIDTH , HEIGHT , 'TDL UnitTest' , False , renderer = 'SDL' )
29+ # make a small window in the corner
30+ cls .window = tdl .Window (cls .console , 0 , 0 , WINWIDTH , WINHEIGHT )
2331
2432 def setUp (self ):
33+ tdl .setFont ('../fonts/libtcod/terminal8x8_gs_ro.png' )
34+ tdl .event .get ()
2535 self .console .clear ()
2636
2737 @classmethod
2838 def tearDownClass (cls ):
2939 del cls .console
3040 gc .collect () # make sure console.__del__ is called quickly
3141
42+ def inWindow (self , x , y ):
43+ "returns True if this point is in the Window"
44+ return 0 <= x < WINWIDTH and 0 <= y < WINHEIGHT
45+
46+ def randomizeConsole (self ):
47+ "Randomize the console returning the random data"
48+ noise = [((x , y ), self .getRandomCharacter ()) for x ,y in self .getDrawables ()]
49+ for (x , y ), graphic in noise :
50+ self .console .drawChar (x , y , * graphic )
51+ return noise # [((x, y), (cg, fg, bg)), ...]
52+
53+ def flush (self ):
54+ 'Pump events and refresh screen so show progress'
55+ tdl .event .get ()
56+ tdl .flush ()
57+
3258 def getRandomCharacter (self ):
3359 "returns a tuple with a random character and colors (ch, fg, bg)"
3460 return (random .getrandbits (8 ), self .getRandomColor (), self .getRandomColor ())
@@ -75,16 +101,34 @@ def getUndrawables(self, console=None):
75101class BasicTests (TDLTemplate ):
76102
77103 def test_clearConsole (self ):
104+ self .randomizeConsole ()
78105 _ , fg , bg = self .getRandomCharacter ()
79106 ch = 0x20 # space
80107 self .console .clear (fg , bg )
108+ self .flush ()
109+ for x ,y in self .getDrawables ():
110+ self .assertEqual ((ch , fg , bg ), self .console .getChar (x , y ), 'color should be changed with clear' )
111+ _ , fg2 , bg2 = self .getRandomCharacter ()
112+ self .window .clear (fg2 , bg2 )
113+ self .flush ()
114+ for x ,y in self .getDrawables ():
115+ if self .inWindow (x , y ):
116+ self .assertEqual ((ch , fg2 , bg2 ), self .console .getChar (x , y ), 'color in window should be changed' )
117+ else :
118+ self .assertEqual ((ch , fg , bg ), self .console .getChar (x , y ), 'color outside of window should persist' )
119+
120+ def test_cloneConsole (self ):
121+ noiseData = self .randomizeConsole ()
122+ clone = copy .copy (self .console )
123+ for x ,y in self .getDrawables ():
124+ self .assertEqual (self .console .getChar (x , y ), clone .getChar (x , y ), 'console clone should match root console' )
125+
126+ def test_pickleConsole (self ):
127+ noiseData = self .randomizeConsole ()
128+ pickled = pickle .dumps (self .console )
129+ clone = pickle .loads (pickled )
81130 for x ,y in self .getDrawables ():
82- self .assertEqual ((ch , fg , bg ), self .console .getChar (x , y ), 'color should be changeable with clear' )
83- #fg = (255, 255, 255)
84- #bg = (0, 0, 0)
85- #self.console.clear()
86- #for x,y in self.getDrawables():
87- # self.assertEqual((ch, fg, bg), self.console.getChar(x, y), 'clear should default to white on black')
131+ self .assertEqual (self .console .getChar (x , y ), clone .getChar (x , y ), 'pickled console should match root console' )
88132
89133 def test_changeFonts (self ):
90134 "Fonts are changable on the fly... kind of"
@@ -97,8 +141,8 @@ def test_changeFonts(self):
97141 for x ,y in self .getDrawables ():
98142 self .console .drawChar (x , y , * self .getRandomCharacter ())
99143 tdl .setTitle (font )
100- tdl .flush ()
101- time .sleep (.25 )
144+ self .flush ()
145+ time .sleep (.05 )
102146
103147
104148class DrawingTests (TDLTemplate ):
@@ -111,6 +155,8 @@ def test_drawCharTuples(self):
111155 record [x ,y ] = (ch , fg , bg )
112156 self .console .drawChar (x , y , ch , fg , bg )
113157 self .assertEqual (record [x ,y ], self .console .getChar (x , y ), 'console data should be overwritten' )
158+ self .flush () # show progress
159+
114160 for (x ,y ), data in record .items ():
115161 self .assertEqual (data , self .console .getChar (x , y ), 'drawChar should not overwrite any other tiles' )
116162
@@ -125,6 +171,7 @@ def test_drawCharWebcolor(self):
125171 bg = bg [0 ] << 16 | bg [1 ] << 8 | bg [2 ]
126172 self .console .drawChar (x , y , ch , fg , bg )
127173 self .assertEqual (record [x ,y ], self .console .getChar (x , y ), 'console data should be overwritten' )
174+ self .flush () # show progress
128175 for (x ,y ), data in record .items ():
129176 self .assertEqual (data , self .console .getChar (x , y ), 'drawChar should not overwrite any other tiles' )
130177
@@ -155,6 +202,7 @@ def test_drawStrArray(self):
155202 y += 1
156203 if y == height :
157204 break # end of console
205+ self .flush () # show progress
158206
159207 #@unittest.skipIf(not __debug__, 'python run with optimized flag, skipping an AssertionError test')
160208 #def test_drawStrErrors(self):
@@ -171,9 +219,10 @@ def test_drawRect(self):
171219 width , height = self .console .getSize ()
172220 width , height = random .randint (1 , width - x ), random .randint (1 , height - y )
173221 self .console .drawRect (x , y , width , height , ch , fg , bg )
222+ self .flush () # show progress
174223 for testX ,testY in self .getDrawables ():
175224 if x <= testX < x + width and y <= testY < y + height :
176- self .assertEqual (self .console .getChar (testX , testY ), (ch , fg , bg ), 'rectangle are should be overwritten' )
225+ self .assertEqual (self .console .getChar (testX , testY ), (ch , fg , bg ), 'rectangle area should be overwritten' )
177226 else :
178227 self .assertEqual (self .console .getChar (testX , testY ), consoleCopy .getChar (testX , testY ), 'this area should remain untouched' )
179228
@@ -185,6 +234,7 @@ def test_drawFrame(self):
185234 width , height = self .console .getSize ()
186235 width , height = random .randint (1 , width - x ), random .randint (1 , height - y )
187236 self .console .drawFrame (x , y , width , height , ch , fg , bg )
237+ self .flush () # show progress
188238 for testX ,testY in self .getDrawables ():
189239 if x + 1 <= testX < x + width - 1 and y + 1 <= testY < y + height - 1 :
190240 self .assertEqual (self .console .getChar (testX , testY ), consoleCopy .getChar (testX , testY ), 'inner frame should remain untouched' )
@@ -204,27 +254,29 @@ def test_drawFrame(self):
204254 # with self.assertRaises(AssertionError):
205255 # self.console.drawFrame(x, y, width, height, ch, fg, bg)
206256
207- @unittest .skip ("Need this to be faster before unskipping" )
257+ # @unittest.skip("Need this to be faster before unskipping")
208258 def test_scrolling (self ):
209259 """marks a spot and then scrolls the console, checks to make sure no
210260 other spots are marked, test also knows if it's out of bounds.
211261
212262 This test is a bit slow, it could be made more efficent by marking
213263 several areas and not clearing the console every loop.
214264 """
215- for sx , sy in itertools .product (range (- 30 , 30 , 5 ), range (- 20 , 20 , 5 )):
216- self .console .clear ()
217- char = self .getRandomCharacter ()
218- dx , dy = random .choice (list (self .getDrawables ()))
219- self .console .drawChar (dx , dy , * char )
265+ scrollTests = set ([(0 , 0 ), (WIDTH , HEIGHT )]) # include zero and out of bounds
266+ while len (scrollTests ) < 5 : # add 3 more randoms
267+ scrollTests .add ((random .randint (- WIDTH , WIDTH ),
268+ random .randint (- HEIGHT , HEIGHT )))
269+ for sx , sy in scrollTests :
270+ noiseData = dict (self .randomizeConsole ())
220271 self .console .scroll (sx , sy )
221- dx += sx # if these go out of bounds then the check will make sure everything is cleared
222- dy += sy
272+ self .flush () # show progress
223273 for x , y in self .getDrawables ():
224- if x == dx and y == dy :
225- self .assertEqual (self .console .getChar (x , y ), char , 'marked position should have scrolled here' )
274+ nX = x - sx
275+ nY = y - sy
276+ if (nX , nY ) in noiseData :
277+ self .assertEqual (self .console .getChar (x , y ), noiseData [nX , nY ], 'random noise should be scrolled' )
226278 else :
227- self .assertEqual (self .console .getChar (x , y ), ( 0x20 , ( 255 , 255 , 255 ), ( 0 , 0 , 0 )), 'every other place should be clear' )
279+ self .assertEqual (self .console .getChar (x , y ), DEFAULT_CHAR , 'scrolled away positions should be clear' )
228280
229281
230282def suite ():
0 commit comments