def test_syntax_error(self): isp = self.isp # Syntax errors immediately produce a 'ready' block, so the invalid # Python can be sent to the kernel for evaluation with possible ipython # special-syntax conversion. isp.push('run foo') self.assertEqual(isp.push_accepts_more(), False)
def test_indent3(self): isp = self.isp # When a multiline statement contains parens or multiline strings, we # shouldn't get confused. isp.push("if 1:") isp.push(" x = (1+\n 2)") self.assertEqual(isp.get_indent_spaces(), 4)
def test_reset(self): isp = self.isp isp.push('x=1') isp.reset() self.assertEqual(isp._buffer, []) self.assertEqual(isp.get_indent_spaces(), 0) self.assertEqual(isp.source, '') self.assertEqual(isp.code, None) self.assertEqual(isp._is_complete, False)
def test_dedent_return(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('if 1:\n returning = 4') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('if 1:\n return 5 + 493') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n return') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n return ') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n return(0)') self.assertEqual(isp.get_indent_spaces(), 0)
def test_syntax(self): """Call all single-line syntax tests from the main object""" isp = self.isp for example in syntax.values(): for raw, out_t in example: if raw.startswith(' '): continue isp.push(raw+'\n') out_raw = isp.source_raw out = isp.source_reset() self.assertEqual(out.rstrip(), out_t, tt.pair_fail_msg.format("inputsplitter",raw, out_t, out)) self.assertEqual(out_raw.rstrip(), raw.rstrip())
def test_syntax(self): """Call all single-line syntax tests from the main object""" isp = self.isp for example in syntax.values(): for raw, out_t in example: if raw.startswith(' '): continue isp.push(raw + '\n') out_raw = isp.source_raw out = isp.source_reset() self.assertEqual( out.rstrip(), out_t, tt.pair_fail_msg.format("inputsplitter", raw, out_t, out)) self.assertEqual(out_raw.rstrip(), raw.rstrip())
def test_push_accepts_more5(self): isp = self.isp isp.push('try:') isp.push(' a = 5') isp.push('except:') isp.push(' raise') # We want to be able to add an else: block at this point, so it should # wait for a blank line. self.assertEqual(isp.push_accepts_more(), True)
def test_push_accepts_more4(self): isp = self.isp # When a multiline statement contains parens or multiline strings, we # shouldn't get confused. # FIXME: we should be able to better handle de-dents in statements like # multiline strings and multiline expressions (continued with \ or # parens). Right now we aren't handling the indentation tracking quite # correctly with this, though in practice it may not be too much of a # problem. We'll need to see. isp.push("if 1:") isp.push(" x = (2+") isp.push(" 3)") self.assertEqual(isp.push_accepts_more(), True) isp.push(" y = 3") self.assertEqual(isp.push_accepts_more(), True) isp.push('') self.assertEqual(isp.push_accepts_more(), False)
def test_indent4(self): isp = self.isp # whitespace after ':' should not screw up indent level isp.push('if 1: \n x=1') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('y=2\n') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\t\n x=1') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('y=2\n') self.assertEqual(isp.get_indent_spaces(), 0)
def test_dedent_raise(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('if 1:\n raised = 4') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('if 1:\n raise TypeError()') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n raise') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n raise ') self.assertEqual(isp.get_indent_spaces(), 0)
def test_syntax_multiline(self): isp = self.isp for example in syntax_ml.values(): for line_pairs in example: out_t_parts = [] raw_parts = [] for lraw, out_t_part in line_pairs: if out_t_part is not None: out_t_parts.append(out_t_part) if lraw is not None: isp.push(lraw) raw_parts.append(lraw) out_raw = isp.source_raw out = isp.source_reset() out_t = '\n'.join(out_t_parts).rstrip() raw = '\n'.join(raw_parts).rstrip() self.assertEqual(out.rstrip(), out_t) self.assertEqual(out_raw.rstrip(), raw)
def test_push_accepts_more2(self): isp = self.isp isp.push('if 1:') self.assertEqual(isp.push_accepts_more(), True) isp.push(' x=1') self.assertEqual(isp.push_accepts_more(), True) isp.push('') self.assertEqual(isp.push_accepts_more(), False)
def test_indent(self): isp = self.isp # shorthand isp.push('x=1') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n x=1') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('y=2\n') self.assertEqual(isp.get_indent_spaces(), 0)
def mini_interactive_loop(input_func): """Minimal example of the logic of an interactive interpreter loop. This serves as an example, and it is used by the test system with a fake raw_input that simulates interactive input.""" from yap_ipython.core.inputsplitter import InputSplitter isp = InputSplitter() # In practice, this input loop would be wrapped in an outside loop to read # input indefinitely, until some exit/quit command was issued. Here we # only illustrate the basic inner loop. while isp.push_accepts_more(): indent = ' ' * isp.get_indent_spaces() prompt = '>>> ' + indent line = indent + input_func(prompt) isp.push(line) # Here we just return input so we can use it in a test suite, but a real # interpreter would instead send it for execution somewhere. src = isp.source_reset() #print 'Input source was:\n', src # dbg return src
def mini_interactive_loop(input_func): """Minimal example of the logic of an interactive interpreter loop. This serves as an example, and it is used by the test system with a fake raw_input that simulates interactive input.""" from yap_ipython.core.inputsplitter import InputSplitter isp = InputSplitter() # In practice, this input loop would be wrapped in an outside loop to read # input indefinitely, until some exit/quit command was issued. Here we # only illustrate the basic inner loop. while isp.push_accepts_more(): indent = ' '*isp.get_indent_spaces() prompt = '>>> ' + indent line = indent + input_func(prompt) isp.push(line) # Here we just return input so we can use it in a test suite, but a real # interpreter would instead send it for execution somewhere. src = isp.source_reset() #print 'Input source was:\n', src # dbg return src
def test_dedent_continue(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('while 1:\n continues = 5') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('while 1:\n continue') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('while 1:\n continue ') self.assertEqual(isp.get_indent_spaces(), 0)
def test_dedent_pass(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('if 1:\n passes = 5') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('if 1:\n pass') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n pass ') self.assertEqual(isp.get_indent_spaces(), 0)
def test_indent2(self): isp = self.isp isp.push('if 1:') self.assertEqual(isp.get_indent_spaces(), 4) isp.push(' x=1') self.assertEqual(isp.get_indent_spaces(), 4) # Blank lines shouldn't change the indent level isp.push(' ' * 2) self.assertEqual(isp.get_indent_spaces(), 4)
def test_indent2(self): isp = self.isp isp.push('if 1:') self.assertEqual(isp.get_indent_spaces(), 4) isp.push(' x=1') self.assertEqual(isp.get_indent_spaces(), 4) # Blank lines shouldn't change the indent level isp.push(' '*2) self.assertEqual(isp.get_indent_spaces(), 4)
def test_line_continuation(self): """ Test issue #2108.""" isp = self.isp # A blank line after a line continuation should not accept more isp.push("1 \\\n\n") self.assertEqual(isp.push_accepts_more(), False) # Whitespace after a \ is a SyntaxError. The only way to test that # here is to test that push doesn't accept more (as with # test_syntax_error() above). isp.push(r"1 \ ") self.assertEqual(isp.push_accepts_more(), False) # Even if the line is continuable (c.f. the regular Python # interpreter) isp.push(r"(1 \ ") self.assertEqual(isp.push_accepts_more(), False)
def test_continuation(self): isp = self.isp isp.push("import os, \\") self.assertEqual(isp.push_accepts_more(), True) isp.push("sys") self.assertEqual(isp.push_accepts_more(), False)
def test_push_accepts_more3(self): isp = self.isp isp.push("x = (2+\n3)") self.assertEqual(isp.push_accepts_more(), False)
def test_push3(self): isp = self.isp isp.push('if True:') isp.push(' a = 1') self.assertEqual(isp.push('b = [1,'), False)
def test_push_accepts_more(self): isp = self.isp isp.push('x=1') self.assertEqual(isp.push_accepts_more(), False)
def test_push2(self): isp = self.isp self.assertEqual(isp.push('if 1:'), False) for line in [' x=1', '# a comment', ' y=2']: print(line) self.assertEqual(isp.push(line), True)
def test_push(self): isp = self.isp self.assertEqual(isp.push('x=1'), True)
#isp, start_prompt = InputSplitter(), '>>> ' isp, start_prompt = IPythonInputSplitter(), 'In> ' autoindent = True #autoindent = False try: while True: prompt = start_prompt while isp.push_accepts_more(): indent = ' ' * isp.get_indent_spaces() if autoindent: line = indent + input(prompt + indent) else: line = input(prompt) isp.push(line) prompt = '... ' # Here we just return input so we can use it in a test suite, but a # real interpreter would instead send it for execution somewhere. #src = isp.source; raise EOFError # dbg raw = isp.source_raw src = isp.source_reset() print('Input source was:\n', src) print('Raw source was:\n', raw) except EOFError: print('Bye') # Tests for cell magics support
#isp, start_prompt = InputSplitter(), '>>> ' isp, start_prompt = IPythonInputSplitter(), 'In> ' autoindent = True #autoindent = False try: while True: prompt = start_prompt while isp.push_accepts_more(): indent = ' '*isp.get_indent_spaces() if autoindent: line = indent + input(prompt+indent) else: line = input(prompt) isp.push(line) prompt = '... ' # Here we just return input so we can use it in a test suite, but a # real interpreter would instead send it for execution somewhere. #src = isp.source; raise EOFError # dbg raw = isp.source_raw src = isp.source_reset() print('Input source was:\n', src) print('Raw source was:\n', raw) except EOFError: print('Bye') # Tests for cell magics support def test_last_blank():