Skip to content

Commit 1b89d3d

Browse files
author
4b796c65
committed
Put in the last drawing tests. This could be good enought to release.
1 parent 181d94c commit 1b89d3d

File tree

3 files changed

+75
-36
lines changed

3 files changed

+75
-36
lines changed

examples/interactive.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def write(self, string):
7474
elif event.ctrl:
7575
pass
7676
elif event.char == '\b':
77+
if cursor == 0:
78+
continue
7779
if buffer[:cursor][-4:] == ' ':
7880
buffer = buffer[:cursor-4] + buffer[cursor:]
7981
cursor -= 4

tdl/__init__.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,12 @@ def _normalizeRect(self, x, y, width, height):
165165
height = self.height - y
166166
assert isinstance(width, int), 'width must be an integer or None, got %s' % repr(width)
167167
assert isinstance(height, int), 'height must be an integer or None, got %s' % repr(height)
168-
if width < 0: # if width or height are backwards then flip them
169-
x += width
170-
width = abs(width)
171-
if height < 0:
172-
y += height
173-
height = abs(height)
168+
169+
assert width >= 0 and height >= 0, 'width and height cannot be negative'
170+
# later idea, negative numbers work like Python list indexing
171+
174172
assert x >= 0 and y >= 0 and x + width <= self.width and y + height <= self.height, \
175-
'Rect is out of bounds at (x=%i y=%i width=%i height=%i), Console bounds are (width=%i, height=%i)' % old + self.getSize()
173+
'Rect is out of bounds at (x=%i y=%i width=%i height=%i), Console bounds are (width=%i, height=%i)' % (old + self.getSize())
176174
return x, y, width, height
177175

178176
def _rectInBounds(self, x, y, width, height):
@@ -235,9 +233,6 @@ def blit(self, source, x=0, y=0, width=None, height=None, srcx=0, srcy=0):
235233
if height == None:
236234
height = min(self.height - y, source.height - srcy)
237235

238-
# _normalizeRect will break negative values in this case, so enforce
239-
# an assert on positive numbers in this one case
240-
assert width >= 0 and height >= 0, 'width and height cannot be negative'
241236
x, y, width, height = self._normalizeRect(x, y, width, height)
242237
srcx, srcy, width, height = source._normalizeRect(srcx, srcy, width, height)
243238

@@ -433,11 +428,12 @@ def drawRect(self, x, y, width, height, string=None, fgcolor=(255, 255, 255), bg
433428
height. If width or height are None then it will extend to the edge
434429
of the console. The rest are the same as drawChar.
435430
"""
436-
assert self._drawable(x, y)
437-
if not self._rectInBounds(x, y, width, height):
438-
raise TDLError('Rectange is out of bounds at (x=%i, y=%i, width=%s, height=%s), bounds are (%i, %i)' %
439-
((x, y, width, height) + self.getSize()))
440-
x, y, width, height = self._clampRect(x, y, width, height) # fill in width height
431+
#assert self._drawable(x, y)
432+
#if not self._rectInBounds(x, y, width, height):
433+
# raise TDLError('Rectange is out of bounds at (x=%i, y=%i, width=%s, height=%s), bounds are (%i, %i)' %
434+
# ((x, y, width, height) + self.getSize()))
435+
#x, y, width, height = self._clampRect(x, y, width, height) # fill in width height
436+
x, y, width, height = self._normalizeRect(x, y, width, height)
441437
assert _verify_colors(fgcolor, bgcolor)
442438
fgcolor, bgcolor = _formatColor(fgcolor), _formatColor(bgcolor)
443439
char = _formatChar(string)
@@ -450,11 +446,12 @@ def drawFrame(self, x, y, width, height, string=None, fgcolor=(255, 255, 255), b
450446
# hardcode alpha settings for now
451447
#bgblend=1
452448

453-
if not self._rectInBounds(x, y, width, height):
454-
raise TDLError('Frame is out of bounds at (x=%i, y=%i, width=%i, height=%i), bounds are (width=%i, height=%i)' %
455-
((x, y, width, height) + self.getSize()))
449+
#if not self._rectInBounds(x, y, width, height):
450+
# raise TDLError('Frame is out of bounds at (x=%i, y=%i, width=%i, height=%i), bounds are (width=%i, height=%i)' %
451+
# ((x, y, width, height) + self.getSize()))
456452

457-
x, y, width, height = self._clampRect(x, y, width, height) # fill in width height
453+
#x, y, width, height = self._clampRect(x, y, width, height) # fill in width height
454+
x, y, width, height = self._normalizeRect(x, y, width, height)
458455
assert _verify_colors(fgcolor, bgcolor)
459456
fgcolor, bgcolor = _formatColor(fgcolor), _formatColor(bgcolor)
460457
char = _formatChar(string)
@@ -572,8 +569,7 @@ def init(width, height, title='TDL', fullscreen=False, renderer='SDL'):
572569
RENDERERS = {'GLSL': 0, 'OPENGL': 1, 'SDL': 2}
573570
global _rootinitialized, _rootconsole
574571
if not _fontinitialized: # set the default font to the one that comes with tdl
575-
setFont(_unpackfile('terminal.png'),
576-
width=16, height=16, colomn=True)
572+
setFont(_unpackfile('terminal.png'), 16, 16, colomn=True)
577573

578574
if renderer.upper() not in RENDERERS:
579575
raise TDLError('No such render type "%s", expected one of "%s"' % (renderer, '", "'.join(RENDERERS)))
@@ -615,13 +611,13 @@ def flush():
615611
#event._eventsflushed = False
616612
_lib.TCOD_console_flush()
617613

618-
def setFont(path, width, height, colomn=False, greyscale=False, altLayout=False):
614+
def setFont(path, tileWidth, tileHeight, colomn=False, greyscale=False, altLayout=False):
619615
"""Changes the font to be used for this session
620616
This should be called before tdl.init
621617
622618
path - must be a string for where a bitmap file is found.
623619
624-
width, height - is the size of an individual tile.
620+
tileWidth, tileHeight - is the size of an individual tile.
625621
626622
colomn - defines if the characer order goes along the rows or colomns. It
627623
should be True if the codes are 0-15 in the first column. And should be
@@ -654,7 +650,7 @@ def setFont(path, width, height, colomn=False, greyscale=False, altLayout=False)
654650
# path = path.name # if given a file just grab the path from the obj
655651
if not os.path.exists(path):
656652
raise TDLError('no file exists at: "%s"' % path)
657-
_lib.TCOD_console_set_custom_font(_format_string(path), flags, width, height)
653+
_lib.TCOD_console_set_custom_font(_format_string(path), flags, tileWidth, tileHeight)
658654

659655
def getFullscreen():
660656
"""Returns if the window is fullscreen
@@ -708,15 +704,15 @@ def screenshot(file=None):
708704
else:
709705
raise TypeError('fileobj is an invalid type: %s' % type(fileobj))
710706

711-
def setFPS(fps):
712-
"""Set the frames per second.
707+
def setFPS(frameRate):
708+
"""Set the frame rate.
713709
714710
You can set this to have no limit by using 0.
715711
"""
716-
if fps is None:
717-
fps = 0
718-
assert isinstance(fps, int), 'fps must be an integer or None, got: %s' % repr(fps)
719-
_lib.TCOD_sys_set_fps(fps)
712+
if frameRate is None:
713+
frameRate = 0
714+
assert isinstance(frameRate, int), 'frameRate must be an integer or None, got: %s' % repr(frameRate)
715+
_lib.TCOD_sys_set_fps(frameRate)
720716

721717
def getFPS():
722718
"""Return the current frames per second of the running program.

testing/runRegressionTest.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
sys.path.insert(0, '..')
1010
import tdl
1111

12+
ERROR_RANGE = 100 # a number to test out of bound errors
13+
1214
class TDLTemplate(unittest.TestCase):
1315
"Nearly all tests need tdl.init to be called"
1416

@@ -47,7 +49,7 @@ def getUndrawables(self, console=None):
4749
if console is None:
4850
console = self.console
4951
w, h = console.getSize()
50-
RANGE = 1000 # distance from bounds to test, just needs to be some moderate number
52+
RANGE = ERROR_RANGE # distance from bounds to test, just needs to be some moderate number
5153
results = []
5254
for _ in range(8):
5355
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1), # every side
@@ -112,10 +114,9 @@ def test_drawCharWebcolor(self):
112114
for (x,y), data in record.items():
113115
self.assertEqual(data, self.console.getChar(x, y), 'drawChar should not overwrite any other tiles')
114116

117+
@unittest.skipIf(not __debug__, 'python run with optimized flag, skipping an AssertionError test')
115118
def test_drawCharErrors(self):
116119
"test out of bounds assertion errors"
117-
if not __debug__:
118-
self.skipTest('python run with optimized flag, skipping an AssertionError test')
119120
for x,y in self.getUndrawables():
120121
with self.assertRaisesRegexp(AssertionError, r"\(%i, %i\)" % (x, y)):
121122
self.console.drawChar(x, y, *(self.getRandomCharacter()))
@@ -141,14 +142,54 @@ def test_drawStrArray(self):
141142
if y == height:
142143
break # end of console
143144

145+
@unittest.skipIf(not __debug__, 'python run with optimized flag, skipping an AssertionError test')
144146
def test_drawStrErrors(self):
145147
"test out of bounds assertion errors"
146-
if not __debug__:
147-
self.skipTest('python run with optimized flag, skipping an AssertionError test')
148148
for x,y in self.getUndrawables():
149149
with self.assertRaisesRegexp(AssertionError, r"\(%i, %i\)" % (x, y)):
150150
self.console.drawStr(x, y, 'foo', self.getRandomColor(), self.getRandomColor())
151-
151+
152+
def test_drawRect(self):
153+
consoleCopy = tdl.Console(*(self.console.getSize()))
154+
for x,y in self.getDrawables():
155+
consoleCopy.blit(self.console) # copy the console to compare untouched areas
156+
ch, fg, bg = self.getRandomCharacter()
157+
width, height = self.console.getSize()
158+
width, height = random.randint(1, width - x), random.randint(1, height - y)
159+
self.console.drawRect(x, y, width, height, ch, fg, bg)
160+
for testX,testY in self.getDrawables():
161+
if x <= testX < x + width and y <= testY < y + height:
162+
self.assertEqual(self.console.getChar(testX, testY), (ch, fg, bg), 'rectangle are should be overwritten')
163+
else:
164+
self.assertEqual(self.console.getChar(testX, testY), consoleCopy.getChar(testX, testY), 'this area should remain untouched')
165+
166+
def test_drawFrame(self):
167+
consoleCopy = tdl.Console(*(self.console.getSize()))
168+
for x,y in self.getDrawables():
169+
consoleCopy.blit(self.console) # copy the console to compare untouched areas
170+
ch, fg, bg = self.getRandomCharacter()
171+
width, height = self.console.getSize()
172+
width, height = random.randint(1, width - x), random.randint(1, height - y)
173+
self.console.drawFrame(x, y, width, height, ch, fg, bg)
174+
for testX,testY in self.getDrawables():
175+
if x + 1 <= testX < x + width - 1 and y + 1 <= testY < y + height - 1:
176+
self.assertEqual(self.console.getChar(testX, testY), consoleCopy.getChar(testX, testY), 'inner frame should remain untouched')
177+
elif x <= testX < x + width and y <= testY < y + height:
178+
self.assertEqual(self.console.getChar(testX, testY), (ch, fg, bg), 'frame area should be overwritten')
179+
else:
180+
self.assertEqual(self.console.getChar(testX, testY), consoleCopy.getChar(testX, testY), 'outer frame should remain untouched')
181+
182+
@unittest.skipIf(not __debug__, 'python run with optimized flag, skipping an AssertionError test')
183+
def test_drawRectFrameErrors(self):
184+
for x,y in self.getDrawables():
185+
ch, fg, bg = self.getRandomCharacter()
186+
width, height = self.console.getSize()
187+
width, height = random.randint(x + width, x + width + ERROR_RANGE), random.randint(y + height, y + height + ERROR_RANGE)
188+
with self.assertRaises(AssertionError):
189+
self.console.drawRect(x, y, width, height, ch, fg, bg)
190+
with self.assertRaises(AssertionError):
191+
self.console.drawFrame(x, y, width, height, ch, fg, bg)
192+
152193
def test_scrolling(self):
153194
"""marks a spot and then scrolls the console, checks to make sure no
154195
other spots are marked, test also knows if it's out of bounds.

0 commit comments

Comments
 (0)