Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert methods for drawing and compiled paths #123

Merged
merged 14 commits into from
Mar 3, 2014
67 changes: 67 additions & 0 deletions enable/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def create_mock_window(self):
window.control.set_pointer = Mock()
return window

def create_a_mock_gc(self, width, height):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest naming this create_mock_gc, for consistency with create_mock_window.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or perhaps even create_mock_graphics_context, though gc seems sufficiently clear in this context.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will go with create_mock_gc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

gc = PlotGraphicsContext((width, height))
gc.clear((0.0, 0.0, 0.0, 0.0))
gc.stroke_path = Mock()
gc.draw_path = Mock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the methods which will be mocked could be a list passed in by the caller?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was you might want to mock any number of GraphicsContext methods, so the caller could pass in a list of method names. Like this:
self.create_mock_gc(width, height, ['stroke_path', 'draw_path', 'line_to'])
and in this method:

for name mock_methods:
    setattr(gc, name, Mock())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not found yet a need for such behaviour, but if you think that it would be useful, I will add it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return gc

def create_key_press(self, key, window=None, alt_down=False,
control_down=False, shift_down=False):
""" Creates a KeyEvent for the given Key.
Expand Down Expand Up @@ -379,3 +386,63 @@ def _key_event_dispatch(self, interactor, event):
interactor.dispatch(event, 'key_pressed')
else:
focus_owner.dispatch(event, 'key_pressed')

def assertPathsAreProcessed(self, drawable, width=200, height=200):
""" Check that drawing does not leave paths unused in the GC cache.

Parameters
----------
drawable :
A drawable object that has a draw method.

width : int, optional
The width of the array buffer

height : int, optional
The height of the array buffer

"""
gc = PlotGraphicsContext((width, height))
drawable.draw(gc)
compiled_path = gc._get_path()
self.assertEqual(
compiled_path.total_vertices(), 0,
msg='There are compiled paths that '
'have not been processed: {0}'.format(compiled_path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the str of a compiled path look like? Is it useful to include it in the message? Would it make sense to also include the return value from the total_vertices call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have augmented the error message


def assertPathsAreCreated(self, drawable, width=200, height=200):
""" Check that drawing creates paths.

Parameters
----------
drawable :
A drawable object that has a draw method.

width : int, optional
The width of the array buffer

height : int, optional
The height of the array buffer

"""
gc = self.create_a_mock_gc(width, height)
drawable.draw(gc)
compiled_path = gc._get_path()
self.assertGreater(
compiled_path.total_vertices(), 0,
msg='There are no compiled paths '
'created: {0}'.format(compiled_path))

def _mouse_event_dispatch(self, interactor, event, suffix):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to add these two methods? As far as I can see, they're private methods that aren't used anywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. No they're not; they're duplicates of existing methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

mouse_owner = event.window.mouse_owner
if mouse_owner is None:
interactor.dispatch(event, suffix)
else:
mouse_owner.dispatch(event, suffix)

def _key_event_dispatch(self, interactor, event):
focus_owner = event.window.focus_owner
if focus_owner is None:
interactor.dispatch(event, 'key_pressed')
else:
focus_owner.dispatch(event, 'key_pressed')