Pythonize me captin'

Made the method comments more pythonic by moving them
within the method definitions.
This commit is contained in:
Jonathan Frederic 2014-03-14 11:35:43 -07:00
parent baa28a0b81
commit 7c6c0b22b8

View File

@ -2,15 +2,15 @@
// Utility functions for the HTML notebook's CasperJS tests. // Utility functions for the HTML notebook's CasperJS tests.
// //
// Get the URL of a notebook server on which to run tests.
casper.get_notebook_server = function () { casper.get_notebook_server = function () {
// Get the URL of a notebook server on which to run tests.
port = casper.cli.get("port"); port = casper.cli.get("port");
port = (typeof port === 'undefined') ? '8888' : port; port = (typeof port === 'undefined') ? '8888' : port;
return 'http://127.0.0.1:' + port; return 'http://127.0.0.1:' + port;
}; };
// Create and open a new notebook.
casper.open_new_notebook = function () { casper.open_new_notebook = function () {
// Create and open a new notebook.
var baseUrl = this.get_notebook_server(); var baseUrl = this.get_notebook_server();
this.start(baseUrl); this.start(baseUrl);
this.thenClick('button#new_notebook'); this.thenClick('button#new_notebook');
@ -34,15 +34,15 @@ casper.open_new_notebook = function () {
}); });
}; };
// Return whether or not the kernel is running.
casper.kernel_running = function kernel_running() { casper.kernel_running = function kernel_running() {
// Return whether or not the kernel is running.
return this.evaluate(function kernel_running() { return this.evaluate(function kernel_running() {
return IPython.notebook.kernel.running; return IPython.notebook.kernel.running;
}); });
}; };
// Shut down the current notebook's kernel.
casper.shutdown_current_kernel = function () { casper.shutdown_current_kernel = function () {
// Shut down the current notebook's kernel.
this.thenEvaluate(function() { this.thenEvaluate(function() {
IPython.notebook.kernel.kill(); IPython.notebook.kernel.kill();
}); });
@ -50,8 +50,9 @@ casper.shutdown_current_kernel = function () {
this.wait(1000); this.wait(1000);
}; };
// Delete created notebook.
casper.delete_current_notebook = function () { casper.delete_current_notebook = function () {
// Delete created notebook.
// For some unknown reason, this doesn't work?!? // For some unknown reason, this doesn't work?!?
this.thenEvaluate(function() { this.thenEvaluate(function() {
IPython.notebook.delete(); IPython.notebook.delete();
@ -59,6 +60,7 @@ casper.delete_current_notebook = function () {
}; };
casper.wait_for_busy = function () { casper.wait_for_busy = function () {
// Waits for the notebook to enter a busy state.
this.waitFor(function () { this.waitFor(function () {
return this.evaluate(function () { return this.evaluate(function () {
return IPython._status == 'busy'; return IPython._status == 'busy';
@ -67,6 +69,7 @@ casper.wait_for_busy = function () {
}; };
casper.wait_for_idle = function () { casper.wait_for_idle = function () {
// Waits for the notebook to idle.
this.waitFor(function () { this.waitFor(function () {
return this.evaluate(function () { return this.evaluate(function () {
return IPython._status == 'idle'; return IPython._status == 'idle';
@ -74,8 +77,8 @@ casper.wait_for_idle = function () {
}); });
}; };
// wait for the nth output in a given cell
casper.wait_for_output = function (cell_num, out_num) { casper.wait_for_output = function (cell_num, out_num) {
// wait for the nth output in a given cell
this.wait_for_idle(); this.wait_for_idle();
out_num = out_num || 0; out_num = out_num || 0;
this.then(function() { this.then(function() {
@ -94,14 +97,14 @@ casper.wait_for_output = function (cell_num, out_num) {
}); });
}; };
// wait for a widget msg que to reach 0
//
// Parameters
// ----------
// widget_info : object
// Object which contains info related to the widget. The model_id property
// is used to identify the widget.
casper.wait_for_widget = function (widget_info) { casper.wait_for_widget = function (widget_info) {
// wait for a widget msg que to reach 0
//
// Parameters
// ----------
// widget_info : object
// Object which contains info related to the widget. The model_id property
// is used to identify the widget.
this.waitFor(function () { this.waitFor(function () {
var pending = this.evaluate(function (m) { var pending = this.evaluate(function (m) {
return IPython.notebook.kernel.widget_manager.get_model(m).pending_msgs; return IPython.notebook.kernel.widget_manager.get_model(m).pending_msgs;
@ -115,8 +118,8 @@ casper.wait_for_widget = function (widget_info) {
}); });
}; };
// return an output of a given cell
casper.get_output_cell = function (cell_num, out_num) { casper.get_output_cell = function (cell_num, out_num) {
// return an output of a given cell
out_num = out_num || 0; out_num = out_num || 0;
var result = casper.evaluate(function (c, o) { var result = casper.evaluate(function (c, o) {
var cell = IPython.notebook.get_cell(c); var cell = IPython.notebook.get_cell(c);
@ -137,33 +140,33 @@ casper.get_output_cell = function (cell_num, out_num) {
} }
}; };
// return the number of cells in the notebook
casper.get_cells_length = function () { casper.get_cells_length = function () {
// return the number of cells in the notebook
var result = casper.evaluate(function () { var result = casper.evaluate(function () {
return IPython.notebook.get_cells().length; return IPython.notebook.get_cells().length;
}); });
return result; return result;
}; };
// Set the text content of a cell.
casper.set_cell_text = function(index, text){ casper.set_cell_text = function(index, text){
// Set the text content of a cell.
this.evaluate(function (index, text) { this.evaluate(function (index, text) {
var cell = IPython.notebook.get_cell(index); var cell = IPython.notebook.get_cell(index);
cell.set_text(text); cell.set_text(text);
}, index, text); }, index, text);
}; };
// Get the text content of a cell.
casper.get_cell_text = function(index){ casper.get_cell_text = function(index){
// Get the text content of a cell.
return this.evaluate(function (index) { return this.evaluate(function (index) {
var cell = IPython.notebook.get_cell(index); var cell = IPython.notebook.get_cell(index);
return cell.get_text(); return cell.get_text();
}, index); }, index);
}; };
// Inserts a cell at the bottom of the notebook
// Returns the new cell's index.
casper.insert_cell_at_bottom = function(cell_type){ casper.insert_cell_at_bottom = function(cell_type){
// Inserts a cell at the bottom of the notebook
// Returns the new cell's index.
cell_type = cell_type || 'code'; cell_type = cell_type || 'code';
return this.evaluate(function (cell_type) { return this.evaluate(function (cell_type) {
@ -172,9 +175,9 @@ casper.insert_cell_at_bottom = function(cell_type){
}, cell_type); }, cell_type);
}; };
// Insert a cell at the bottom of the notebook and set the cells text.
// Returns the new cell's index.
casper.append_cell = function(text, cell_type) { casper.append_cell = function(text, cell_type) {
// Insert a cell at the bottom of the notebook and set the cells text.
// Returns the new cell's index.
var index = this.insert_cell_at_bottom(cell_type); var index = this.insert_cell_at_bottom(cell_type);
if (text !== undefined) { if (text !== undefined) {
this.set_cell_text(index, text); this.set_cell_text(index, text);
@ -182,9 +185,9 @@ casper.append_cell = function(text, cell_type) {
return index; return index;
}; };
// Asynchronously executes a cell by index.
// Returns the cell's index.
casper.execute_cell = function(index){ casper.execute_cell = function(index){
// Asynchronously executes a cell by index.
// Returns the cell's index.
var that = this; var that = this;
this.then(function(){ this.then(function(){
that.evaluate(function (index) { that.evaluate(function (index) {
@ -195,11 +198,11 @@ casper.execute_cell = function(index){
return index; return index;
}; };
// Synchronously executes a cell by index.
// Optionally accepts a then_callback parameter. then_callback will get called
// when the cell has finished executing.
// Returns the cell's index.
casper.execute_cell_then = function(index, then_callback) { casper.execute_cell_then = function(index, then_callback) {
// Synchronously executes a cell by index.
// Optionally accepts a then_callback parameter. then_callback will get called
// when the cell has finished executing.
// Returns the cell's index.
var return_val = this.execute_cell(index); var return_val = this.execute_cell(index);
this.wait_for_idle(); this.wait_for_idle();
@ -214,18 +217,18 @@ casper.execute_cell_then = function(index, then_callback) {
return return_val; return return_val;
}; };
// Utility function that allows us to easily check if an element exists
// within a cell. Uses JQuery selector to look for the element.
casper.cell_element_exists = function(index, selector){ casper.cell_element_exists = function(index, selector){
// Utility function that allows us to easily check if an element exists
// within a cell. Uses JQuery selector to look for the element.
return casper.evaluate(function (index, selector) { return casper.evaluate(function (index, selector) {
var $cell = IPython.notebook.get_cell(index).element; var $cell = IPython.notebook.get_cell(index).element;
return $cell.find(selector).length > 0; return $cell.find(selector).length > 0;
}, index, selector); }, index, selector);
}; };
// Utility function that allows us to execute a jQuery function on an
// element within a cell.
casper.cell_element_function = function(index, selector, function_name, function_args){ casper.cell_element_function = function(index, selector, function_name, function_args){
// Utility function that allows us to execute a jQuery function on an
// element within a cell.
return casper.evaluate(function (index, selector, function_name, function_args) { return casper.evaluate(function (index, selector, function_name, function_args) {
var $cell = IPython.notebook.get_cell(index).element; var $cell = IPython.notebook.get_cell(index).element;
var $el = $cell.find(selector); var $el = $cell.find(selector);
@ -233,9 +236,10 @@ casper.cell_element_function = function(index, selector, function_name, function
}, index, selector, function_name, function_args); }, index, selector, function_name, function_args);
}; };
// Validate the entire dual mode state of the notebook. Make sure no more than
// one cell is selected, focused, in edit mode, etc...
casper.validate_notebook_state = function(message, mode, cell_index) { casper.validate_notebook_state = function(message, mode, cell_index) {
// Validate the entire dual mode state of the notebook. Make sure no more than
// one cell is selected, focused, in edit mode, etc...
// General tests. // General tests.
this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(), this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
message + '; keyboard and notebook modes match'); message + '; keyboard and notebook modes match');
@ -274,15 +278,16 @@ casper.validate_notebook_state = function(message, mode, cell_index) {
} }
}; };
// Select a cell in the notebook.
casper.select_cell = function(index) { casper.select_cell = function(index) {
// Select a cell in the notebook.
this.evaluate(function (i) { this.evaluate(function (i) {
IPython.notebook.select(i); IPython.notebook.select(i);
}, {i: index}); }, {i: index});
}; };
// Emulate a click on a cell's editor.
casper.click_cell_editor = function(index) { casper.click_cell_editor = function(index) {
// Emulate a click on a cell's editor.
// Code Mirror does not play nicely with emulated brower events. // Code Mirror does not play nicely with emulated brower events.
// Instead of trying to emulate a click, here we run code similar to // Instead of trying to emulate a click, here we run code similar to
// the code used in Code Mirror that handles the mousedown event on a // the code used in Code Mirror that handles the mousedown event on a
@ -294,22 +299,22 @@ casper.click_cell_editor = function(index) {
}, {i: index}); }, {i: index});
}; };
// Set the Code Mirror instance cursor's location.
casper.set_cell_editor_cursor = function(index, line_index, char_index) { casper.set_cell_editor_cursor = function(index, line_index, char_index) {
// Set the Code Mirror instance cursor's location.
this.evaluate(function (i, l, c) { this.evaluate(function (i, l, c) {
IPython.notebook.get_cell(i).code_mirror.setCursor(l, c); IPython.notebook.get_cell(i).code_mirror.setCursor(l, c);
}, {i: index, l: line_index, c: char_index}); }, {i: index, l: line_index, c: char_index});
}; };
// Focus the notebook div.
casper.focus_notebook = function() { casper.focus_notebook = function() {
// Focus the notebook div.
this.evaluate(function (){ this.evaluate(function (){
$('#notebook').focus(); $('#notebook').focus();
}, {}); }, {});
}; };
// Emulate a keydown in the notebook.
casper.trigger_keydown = function() { casper.trigger_keydown = function() {
// Emulate a keydown in the notebook.
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
this.evaluate(function (k) { this.evaluate(function (k) {
var element = $(document); var element = $(document);
@ -319,25 +324,25 @@ casper.trigger_keydown = function() {
} }
}; };
// Get the mode of the keyboard manager.
casper.get_keyboard_mode = function() { casper.get_keyboard_mode = function() {
// Get the mode of the keyboard manager.
return this.evaluate(function() { return this.evaluate(function() {
return IPython.keyboard_manager.mode; return IPython.keyboard_manager.mode;
}, {}); }, {});
}; };
// Get the mode of the notebook.
casper.get_notebook_mode = function() { casper.get_notebook_mode = function() {
// Get the mode of the notebook.
return this.evaluate(function() { return this.evaluate(function() {
return IPython.notebook.mode; return IPython.notebook.mode;
}, {}); }, {});
}; };
// Get a single cell.
//
// Note: Handles to DOM elements stored in the cell will be useless once in
// CasperJS context.
casper.get_cell = function(index) { casper.get_cell = function(index) {
// Get a single cell.
//
// Note: Handles to DOM elements stored in the cell will be useless once in
// CasperJS context.
return this.evaluate(function(i) { return this.evaluate(function(i) {
var cell = IPython.notebook.get_cell(i); var cell = IPython.notebook.get_cell(i);
if (cell) { if (cell) {
@ -347,8 +352,8 @@ casper.get_cell = function(index) {
}, {i : index}); }, {i : index});
}; };
// Make sure a cell's editor is the only editor focused on the page.
casper.is_cell_editor_focused = function(index) { casper.is_cell_editor_focused = function(index) {
// Make sure a cell's editor is the only editor focused on the page.
return this.evaluate(function(i) { return this.evaluate(function(i) {
var focused_textarea = $('#notebook .CodeMirror-focused textarea'); var focused_textarea = $('#notebook .CodeMirror-focused textarea');
if (focused_textarea.length > 1) { throw 'More than one Code Mirror editor is focused at once!'; } if (focused_textarea.length > 1) { throw 'More than one Code Mirror editor is focused at once!'; }
@ -364,22 +369,22 @@ casper.is_cell_editor_focused = function(index) {
}, {i : index}); }, {i : index});
}; };
// Check if a cell is the only cell selected.
// Pass null as the index to check if no cells are selected.
casper.is_only_cell_selected = function(index) { casper.is_only_cell_selected = function(index) {
// Check if a cell is the only cell selected.
// Pass null as the index to check if no cells are selected.
return this.is_only_cell_on(index, 'selected', 'unselected'); return this.is_only_cell_on(index, 'selected', 'unselected');
}; };
// Check if a cell is the only cell in edit mode.
// Pass null as the index to check if all of the cells are in command mode.
casper.is_only_cell_edit = function(index) { casper.is_only_cell_edit = function(index) {
// Check if a cell is the only cell in edit mode.
// Pass null as the index to check if all of the cells are in command mode.
return this.is_only_cell_on(index, 'edit_mode', 'command_mode'); return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
}; };
// Check if a cell is the only cell with the `on_class` DOM class applied to it.
// All of the other cells are checked for the `off_class` DOM class.
// Pass null as the index to check if all of the cells have the `off_class`.
casper.is_only_cell_on = function(i, on_class, off_class) { casper.is_only_cell_on = function(i, on_class, off_class) {
// Check if a cell is the only cell with the `on_class` DOM class applied to it.
// All of the other cells are checked for the `off_class` DOM class.
// Pass null as the index to check if all of the cells have the `off_class`.
var cells_length = this.get_cells_length(); var cells_length = this.get_cells_length();
for (var j = 0; j < cells_length; j++) { for (var j = 0; j < cells_length; j++) {
if (j === i) { if (j === i) {
@ -395,8 +400,8 @@ casper.is_only_cell_on = function(i, on_class, off_class) {
return true; return true;
}; };
// Check if a cell has a class.
casper.cell_has_class = function(index, classes) { casper.cell_has_class = function(index, classes) {
// Check if a cell has a class.
return this.evaluate(function(i, c) { return this.evaluate(function(i, c) {
var cell = IPython.notebook.get_cell(i); var cell = IPython.notebook.get_cell(i);
if (cell) { if (cell) {
@ -406,8 +411,8 @@ casper.cell_has_class = function(index, classes) {
}, {i : index, c: classes}); }, {i : index, c: classes});
}; };
// Wrap a notebook test to reduce boilerplate.
casper.notebook_test = function(test) { casper.notebook_test = function(test) {
// Wrap a notebook test to reduce boilerplate.
this.open_new_notebook(); this.open_new_notebook();
this.then(test); this.then(test);
@ -465,8 +470,8 @@ casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
this.echo("Is the notebook server running?"); this.echo("Is the notebook server running?");
}); });
// Pass `console.log` calls from page JS to casper.
casper.print_log = function () { casper.print_log = function () {
// Pass `console.log` calls from page JS to casper.
this.on('remote.message', function(msg) { this.on('remote.message', function(msg) {
this.echo('Remote message caught: ' + msg); this.echo('Remote message caught: ' + msg);
}); });