Skip to content

Commit

Permalink
Merge pull request #123 from enthought/enh-path-assertion-methods
Browse files Browse the repository at this point in the history
Add assert methods for drawing and compiled paths
  • Loading branch information
jwiggins committed Mar 3, 2014
2 parents 0c27475 + 6252681 commit a5b8cb9
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
4 changes: 3 additions & 1 deletion enable/testing.py
Expand Up @@ -4,6 +4,8 @@

from enable.abstract_window import AbstractWindow
from enable.events import MouseEvent, KeyEvent
from kiva.testing import KivaTestAssistant


class _MockWindow(AbstractWindow):

Expand All @@ -15,7 +17,7 @@ def _redraw(self, coordinates=None):
pass


class EnableTestAssistant(object):
class EnableTestAssistant(KivaTestAssistant):
""" Mixin helper for enable/chaco components.
"""
Expand Down
85 changes: 85 additions & 0 deletions kiva/testing.py
@@ -0,0 +1,85 @@
# Copyright (c) 2008-2013 by Enthought, Inc.
# All rights reserved.
from mock import Mock

from kiva.image import GraphicsContext


class KivaTestAssistant(object):
""" Mixin test helper for kiva drawing tests.
"""

def create_mock_gc(
self, width, height, methods=()):
""" Create an image graphics context that with mocked methods.
Parameters
----------
width, height :
The size of the graphics context canvas.
methods : iterable
the methods which are going to be mocked with a Mock object.
"""
gc = GraphicsContext((int(width), int(height)))
gc.clear((0.0, 0.0, 0.0, 0.0))
for method in methods:
setattr(gc, method, Mock())
return gc

def assertPathsAreProcessed(self, drawable, width=200, height=200):
""" Check that all the paths have been compiled and processed.
Parameters
----------
drawable :
A drawable object that has a draw method.
width : int, optional
The width of the array buffer (default is 200).
height : int, optional
The height of the array buffer (default is 200).
note ::
A drawable that draws nothing will pass this check.
"""
gc = GraphicsContext((width, height))
drawable.draw(gc)
compiled_path = gc._get_path()
total_vertices = compiled_path.total_vertices()
self.assertEqual(
total_vertices, 0,
msg='There are {0} vertices in compiled paths {1} that '
'have not been processed'.format(total_vertices, compiled_path))

def assertPathsAreCreated(self, drawable, width=200, height=200):
""" Check that drawing creates paths.
When paths and lines creation methods are used from a graphics
context the drawing paths are compiled and processed. By using
a mock graphics context we can check if something has been drawn.
Parameters
----------
drawable :
A drawable object that has a draw method.
width : int, optional
The width of the array buffer (default is 200).
height : int, optional
The height of the array buffer (default is 200).
"""
gc = self.create_mock_gc(width, height, ('draw_path', 'stroke_path'))
drawable.draw(gc)
compiled_path = gc._get_path()
self.assertTrue(
compiled_path.total_vertices() > 0,
msg='There are no compiled paths '
'created: {0}'.format(compiled_path))
55 changes: 55 additions & 0 deletions kiva/tests/test_kiva_test_assistant.py
@@ -0,0 +1,55 @@
import unittest

from kiva.testing import KivaTestAssistant


class Drawable(object):

def __init__(self, should_draw=True, should_process=True):
self.should_draw = should_draw
self.should_process = should_process

def draw(self, gc):
with gc:
if self.should_draw:
gc.move_to(-5,0)
gc.line_to(5,0)
gc.move_to(0,5)
gc.line_to(0,-5)
gc.move_to(0,0)
# The path will not be processed and remain in the gc cache
# if we do not execute the stroke_path command.
if self.should_process:
gc.stroke_path()


class TestKivaTestAssistant(KivaTestAssistant, unittest.TestCase):

def test_path_created_assertions(self):
drawable = Drawable(should_draw=False)

# drawing nothing
self.assertRaises(AssertionError,
self.assertPathsAreCreated,
drawable)

#drawing something
drawable.should_draw = True
self.assertPathsAreCreated(drawable)

def test_paths_processed_assertions(self):
drawable = Drawable(should_draw=True, should_process=False)

# not finishing the path
self.assertRaises(
AssertionError,
self.assertPathsAreProcessed,
drawable)

#drawing something
drawable.should_process = True
self.assertPathsAreProcessed(drawable)


if __name__ == '__main__':
unittest.main()

0 comments on commit a5b8cb9

Please sign in to comment.