mirror of
https://github.com/jupyter/notebook.git
synced 2024-12-27 04:20:22 +08:00
Refactor validate_dualmode_mode
Previously know as validate_notebook_mode changed name to validate_dualmode_mode to represent better the method. Added a docstring. Removed the handling of index being None.
This commit is contained in:
parent
fccc7d7420
commit
3a0cb7b113
@ -1,5 +1,5 @@
|
||||
"""Test"""
|
||||
from .utils import shift, validate_notebook_state
|
||||
from .utils import shift, validate_dualmode_state
|
||||
|
||||
def test_dualmode_clipboard(notebook):
|
||||
a = 'print("a")'
|
||||
@ -20,40 +20,40 @@ def test_dualmode_clipboard(notebook):
|
||||
|
||||
notebook.focus_cell(1)
|
||||
notebook.body.send_keys("x") #Cut
|
||||
validate_notebook_state(notebook, 'command', 1)
|
||||
validate_dualmode_state(notebook, 'command', 1)
|
||||
assert notebook.get_cell_contents(1) == b #Cell 2 is now where cell 1 was
|
||||
assert len(notebook.cells) == num_cells-1 #A cell was removed
|
||||
|
||||
notebook.focus_cell(2)
|
||||
notebook.body.send_keys("v") #Paste
|
||||
validate_notebook_state(notebook, 'command', 3)
|
||||
validate_dualmode_state(notebook, 'command', 3)
|
||||
assert notebook.get_cell_contents(3) == a #Cell 3 has the cut contents
|
||||
assert len(notebook.cells) == num_cells #A cell was added
|
||||
|
||||
notebook.body.send_keys("v") #Paste
|
||||
validate_notebook_state(notebook, 'command', 4)
|
||||
validate_dualmode_state(notebook, 'command', 4)
|
||||
assert notebook.get_cell_contents(4) == a #Cell a has the cut contents
|
||||
assert len(notebook.cells) == num_cells+1 #A cell was added
|
||||
|
||||
notebook.focus_cell(1)
|
||||
notebook.body.send_keys("c") #Copy
|
||||
validate_notebook_state(notebook, 'command', 1)
|
||||
validate_dualmode_state(notebook, 'command', 1)
|
||||
assert notebook.get_cell_contents(1) == b #Cell 1 is b
|
||||
|
||||
notebook.focus_cell(2)
|
||||
notebook.body.send_keys("c") #Copy
|
||||
validate_notebook_state(notebook, 'command', 2)
|
||||
validate_dualmode_state(notebook, 'command', 2)
|
||||
assert notebook.get_cell_contents(2) == c #Cell 2 is c
|
||||
|
||||
notebook.focus_cell(4)
|
||||
notebook.body.send_keys("v") #Paste
|
||||
validate_notebook_state(notebook, 'command', 5)
|
||||
validate_dualmode_state(notebook, 'command', 5)
|
||||
assert notebook.get_cell_contents(2) == c #Cell 2 has the copied contents
|
||||
assert notebook.get_cell_contents(5) == c #Cell 5 has the copied contents
|
||||
assert len(notebook.cells) == num_cells+2 #A cell was added
|
||||
|
||||
notebook.focus_cell(0)
|
||||
shift(notebook.browser, 'v') #Paste
|
||||
validate_notebook_state(notebook, 'command', 0)
|
||||
validate_dualmode_state(notebook, 'command', 0)
|
||||
assert notebook.get_cell_contents(0) == c #Cell 0 has the copied contents
|
||||
assert len(notebook.cells) == num_cells+3 #A cell was added
|
@ -2,7 +2,7 @@
|
||||
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
|
||||
from .utils import shift, cmdtrl, alt, validate_notebook_state
|
||||
from .utils import shift, cmdtrl, alt, validate_dualmode_state
|
||||
|
||||
|
||||
def test_dualmode_execute(notebook):
|
||||
@ -23,59 +23,59 @@ def test_dualmode_execute(notebook):
|
||||
base_index = 3
|
||||
notebook.focus_cell(base_index)
|
||||
shift(notebook.browser, Keys.ENTER) #creates one cell
|
||||
validate_notebook_state(notebook, 'edit', base_index + 1)
|
||||
validate_dualmode_state(notebook, 'edit', base_index + 1)
|
||||
|
||||
#Not last cell in notebook & starts in edit mode
|
||||
notebook.focus_cell(base_index)
|
||||
notebook.body.send_keys(Keys.ENTER) #Enter edit mode
|
||||
validate_notebook_state(notebook, 'edit', base_index)
|
||||
validate_dualmode_state(notebook, 'edit', base_index)
|
||||
shift(notebook.browser, Keys.ENTER) #creates one cell
|
||||
validate_notebook_state(notebook, 'command', base_index + 1)
|
||||
validate_dualmode_state(notebook, 'command', base_index + 1)
|
||||
|
||||
#Starts in command mode
|
||||
notebook.body.send_keys('k')
|
||||
validate_notebook_state(notebook, 'command', base_index)
|
||||
validate_dualmode_state(notebook, 'command', base_index)
|
||||
shift(notebook.browser, Keys.ENTER) #creates one cell
|
||||
validate_notebook_state(notebook, 'command', base_index + 1)
|
||||
validate_dualmode_state(notebook, 'command', base_index + 1)
|
||||
|
||||
|
||||
#Ctrl-enter
|
||||
#Last cell in notebook
|
||||
base_index += 1
|
||||
cmdtrl(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'command', base_index)
|
||||
validate_dualmode_state(notebook, 'command', base_index)
|
||||
|
||||
#Not last cell in notebook & stats in edit mode
|
||||
notebook.focus_cell(base_index - 1)
|
||||
notebook.body.send_keys(Keys.ENTER) #Enter edit mode
|
||||
validate_notebook_state(notebook, 'edit', base_index - 1)
|
||||
validate_dualmode_state(notebook, 'edit', base_index - 1)
|
||||
cmdtrl(notebook.browser, Keys.ENTER)
|
||||
|
||||
#Starts in command mode
|
||||
notebook.body.send_keys('j')
|
||||
validate_notebook_state(notebook, 'command', base_index)
|
||||
validate_dualmode_state(notebook, 'command', base_index)
|
||||
cmdtrl(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'command', base_index)
|
||||
validate_dualmode_state(notebook, 'command', base_index)
|
||||
|
||||
|
||||
#Alt-enter
|
||||
#Last cell in notebook
|
||||
alt(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'edit', base_index + 1)
|
||||
validate_dualmode_state(notebook, 'edit', base_index + 1)
|
||||
#Not last cell in notebook &starts in edit mode
|
||||
notebook.focus_cell(base_index)
|
||||
notebook.body.send_keys(Keys.ENTER) #Enter edit mode
|
||||
validate_notebook_state(notebook, 'edit', base_index)
|
||||
validate_dualmode_state(notebook, 'edit', base_index)
|
||||
alt(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'edit', base_index + 1)
|
||||
validate_dualmode_state(notebook, 'edit', base_index + 1)
|
||||
#starts in command mode
|
||||
notebook.body.send_keys(Keys.ESCAPE, 'k')
|
||||
validate_notebook_state(notebook, 'command', base_index)
|
||||
validate_dualmode_state(notebook, 'command', base_index)
|
||||
alt(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'edit', base_index + 1)
|
||||
validate_dualmode_state(notebook, 'edit', base_index + 1)
|
||||
|
||||
|
||||
#Notebook will now have 8 cells, the index of the last cell will be 7
|
||||
assert len(notebook) == 8 #Cells where added
|
||||
notebook.focus_cell(7)
|
||||
validate_notebook_state(notebook, 'command', 7)
|
||||
validate_dualmode_state(notebook, 'command', 7)
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
|
||||
from .utils import cmdtrl, shift, validate_notebook_state
|
||||
from .utils import cmdtrl, shift, validate_dualmode_state
|
||||
|
||||
def test_dualmode_markdown(notebook):
|
||||
def is_cell_rendered(index):
|
||||
@ -16,38 +16,38 @@ def test_dualmode_markdown(notebook):
|
||||
|
||||
#Markdown rendering / unrendering
|
||||
notebook.focus_cell(index)
|
||||
validate_notebook_state(notebook, 'command', index)
|
||||
validate_dualmode_state(notebook, 'command', index)
|
||||
notebook.body.send_keys("m")
|
||||
assert notebook.get_cell_type(index) == 'markdown'
|
||||
assert not is_cell_rendered(index) #cell is not rendered
|
||||
|
||||
notebook.body.send_keys(Keys.ENTER)#cell is unrendered
|
||||
assert not is_cell_rendered(index) #cell is not rendered
|
||||
validate_notebook_state(notebook, 'edit', index)
|
||||
validate_dualmode_state(notebook, 'edit', index)
|
||||
|
||||
cmdtrl(notebook.browser, Keys.ENTER)
|
||||
assert is_cell_rendered(index) #cell is rendered with crtl+enter
|
||||
validate_notebook_state(notebook, 'command', index)
|
||||
validate_dualmode_state(notebook, 'command', index)
|
||||
|
||||
notebook.body.send_keys(Keys.ENTER)#cell is unrendered
|
||||
assert not is_cell_rendered(index) #cell is not rendered
|
||||
|
||||
notebook.focus_cell(index - 1)
|
||||
assert not is_cell_rendered(index) #Select index-1; cell index is still not rendered
|
||||
validate_notebook_state(notebook, 'command', index - 1)
|
||||
validate_dualmode_state(notebook, 'command', index - 1)
|
||||
|
||||
notebook.focus_cell(index)
|
||||
validate_notebook_state(notebook, 'command', index)
|
||||
validate_dualmode_state(notebook, 'command', index)
|
||||
cmdtrl(notebook.browser, Keys.ENTER)
|
||||
assert is_cell_rendered(index)#Cell is rendered
|
||||
|
||||
notebook.focus_cell(index - 1)
|
||||
validate_notebook_state(notebook, 'command', index - 1)
|
||||
validate_dualmode_state(notebook, 'command', index - 1)
|
||||
|
||||
shift(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'command', index)
|
||||
validate_dualmode_state(notebook, 'command', index)
|
||||
assert is_cell_rendered(index)#Cell is rendered
|
||||
|
||||
shift(notebook.browser, Keys.ENTER)
|
||||
validate_notebook_state(notebook, 'edit', index + 1)
|
||||
validate_dualmode_state(notebook, 'edit', index + 1)
|
||||
assert is_cell_rendered(index)#Cell is rendered
|
||||
|
@ -222,10 +222,6 @@ class Notebook:
|
||||
JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value)
|
||||
return self.browser.execute_script(JS)
|
||||
|
||||
def get_cells_mode(self):
|
||||
JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})'
|
||||
return self.browser.execute_script(JS)
|
||||
|
||||
def get_cell_type(self, index=0):
|
||||
JS = 'return Jupyter.notebook.get_cell({}).cell_type'.format(index)
|
||||
return self.browser.execute_script(JS)
|
||||
@ -391,9 +387,14 @@ def trigger_keystrokes(browser, *keys):
|
||||
else: # single key stroke. Check if modifier eg. "up"
|
||||
browser.send_keys(getattr(Keys, keys[0].upper(), keys[0]))
|
||||
|
||||
def validate_notebook_state(notebook, mode, index):
|
||||
def validate_dualmode_state(notebook, mode, index):
|
||||
'''Validate the entire dual mode state of the notebook.
|
||||
Make sure no more than one cell is selected, focused, in edit mode, etc...
|
||||
|
||||
'''
|
||||
def is_only_cell_edit(index):
|
||||
cells_mode = notebook.get_cells_mode()
|
||||
JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.mode;})'
|
||||
cells_mode = notebook.browser.execute_script(JS)
|
||||
#None of the cells are in edit mode
|
||||
if(index == None):
|
||||
for mode in cells_mode:
|
||||
@ -417,7 +418,9 @@ def validate_notebook_state(notebook, mode, index):
|
||||
focused_cells = notebook.browser.execute_script(JS)
|
||||
if(index == None):
|
||||
return focused_cells == 0
|
||||
|
||||
assert focused_cells == 1
|
||||
|
||||
JS = "return $('#notebook .CodeMirror-focused textarea')[0];"
|
||||
focused_cell = notebook.browser.execute_script(JS)
|
||||
JS = "return IPython.notebook.get_cell(%s).code_mirror.getInputField()"%index
|
||||
@ -432,10 +435,9 @@ def validate_notebook_state(notebook, mode, index):
|
||||
notebook_mode = notebook.browser.execute_script(JS)
|
||||
|
||||
#validate selected cell
|
||||
if (index != None):
|
||||
JS = "return Jupyter.notebook.get_selected_cells_indices();"
|
||||
cell_index = notebook.browser.execute_script(JS)
|
||||
assert cell_index == [index] #only the index cell is selected
|
||||
JS = "return Jupyter.notebook.get_selected_cells_indices();"
|
||||
cell_index = notebook.browser.execute_script(JS)
|
||||
assert cell_index == [index] #only the index cell is selected
|
||||
|
||||
if(mode == 'command'):
|
||||
#validate mode
|
||||
|
Loading…
Reference in New Issue
Block a user