def test_indent2(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.indent_spaces, 4)
 def test_replace_mode(self):
     isp = self.isp
     isp.input_mode = 'cell'
     isp.push('x=1')
     self.assertEqual(isp.source, 'x=1\n')
     isp.push('x=2')
     self.assertEqual(isp.source, 'x=2\n')
 def test_replace_mode(self):
     isp = self.isp
     isp.input_mode = "cell"
     isp.push("x=1")
     self.assertEqual(isp.source, "x=1\n")
     isp.push("x=2")
     self.assertEqual(isp.source, "x=2\n")
 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.assertFalse(isp.push_accepts_more())
 def test_reset(self):
     isp = self.isp
     isp.push('x=1')
     isp.reset()
     self.assertEqual(isp._buffer, [])
     self.assertEqual(isp.indent_spaces, 0)
     self.assertEqual(isp.source, '')
     self.assertEqual(isp.code, None)
     self.assertEqual(isp._is_complete, False)
Example #6
0
    def test_indent3(self):
        # In cell mode, inputs must be fed in whole blocks, so skip this test
        if self.isp.input_mode == 'cell': return

        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.indent_spaces, 4)
 def test_syntax(self):
     """Call all single-line syntax tests from the main object"""
     isp = self.isp
     for example in syntax.itervalues():
         for raw, out_t in example:
             if raw.startswith(' '):
                 continue
             
             isp.push(raw)
             out = isp.source_reset().rstrip()
             self.assertEqual(out, out_t)
    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, out_raw = isp.source_raw_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_multiline(self):
        isp = self.isp
        for example in syntax_ml.itervalues():
            out_t_parts = []
            for line_pairs in example:
                for raw, out_t_part in line_pairs:
                    isp.push(raw)
                    out_t_parts.append(out_t_part)

                out = isp.source_reset().rstrip()
                out_t = '\n'.join(out_t_parts).rstrip()
                self.assertEqual(out, out_t)
Example #10
0
 def test_dedent_return(self):
     isp = self.isp # shorthand
     # should NOT cause dedent
     isp.push('if 1:\n    returning = 4')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('if 1:\n     return 5 + 493')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n     return')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n     return      ')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n     return(0)')
     self.assertEqual(isp.indent_spaces, 0)
    def test_syntax_multiline(self):
        isp = self.isp
        for example in syntax_ml.itervalues():
            out_t_parts = []
            raw_parts = []
            for line_pairs in example:
                for lraw, out_t_part in line_pairs:
                    isp.push(lraw)
                    out_t_parts.append(out_t_part)
                    raw_parts.append(lraw)

                out, out_raw = isp.source_raw_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)
Example #12
0
    def test_syntax_multiline(self):
        isp = self.isp
        for example in syntax_ml.itervalues():
            raw_parts = []
            out_t_parts = []
            for line_pairs in example:
                raw_parts, out_t_parts = zip(*line_pairs)

                raw = '\n'.join(r for r in raw_parts if r is not None)
                out_t = '\n'.join(o for o in out_t_parts if o is not None)

                isp.push(raw)
                out, out_raw = isp.source_raw_reset()
                # Match ignoring trailing whitespace
                self.assertEqual(out.rstrip(), out_t.rstrip())
                self.assertEqual(out_raw.rstrip(), raw.rstrip())
Example #13
0
 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.assertTrue(isp.push_accepts_more())
 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.assertTrue(isp.push_accepts_more())
     isp.push("    y = 3")
     self.assertTrue(isp.push_accepts_more())
     isp.push('')
     self.assertFalse(isp.push_accepts_more())
    def test_syntax_multiline(self):
        isp = self.isp
        for example in syntax_ml.itervalues():
            raw_parts = []
            out_t_parts = []
            for line_pairs in example:
                for raw, out_t_part in line_pairs:
                    raw_parts.append(raw)
                    out_t_parts.append(out_t_part)

                raw = '\n'.join(raw_parts)
                out_t = '\n'.join(out_t_parts)

                isp.push(raw)
                out = isp.source_reset()
                # Match ignoring trailing whitespace
                self.assertEqual(out.rstrip(), out_t.rstrip())
Example #16
0
    def test_push_accepts_more5(self):
        # In cell mode, inputs must be fed in whole blocks, so skip this test
        if self.isp.input_mode == 'cell': return

        isp = self.isp
        isp.push('try:')
        isp.push('    a = 5')
        isp.push('except:')
        isp.push('    raise')
        self.assertTrue(isp.push_accepts_more())
Example #17
0
 def test_dedent_raise(self):
     isp = self.isp # shorthand
     # should NOT cause dedent
     isp.push('if 1:\n    raised = 4')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('if 1:\n     raise TypeError()')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n     raise')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n     raise      ')
     self.assertEqual(isp.indent_spaces, 0)
    def test_push_accepts_more5(self):
        # In cell mode, inputs must be fed in whole blocks, so skip this test
        if self.isp.input_mode == "cell":
            return

        isp = self.isp
        isp.push("try:")
        isp.push("    a = 5")
        isp.push("except:")
        isp.push("    raise")
        self.assertTrue(isp.push_accepts_more())
Example #19
0
 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.indent_spaces, 4)
     isp.push('y=2\n')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\t\n    x=1')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('y=2\n')
     self.assertEqual(isp.indent_spaces, 0)
Example #20
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)
Example #21
0
    def test_indent4(self):
        # In cell mode, inputs must be fed in whole blocks, so skip this test
        if self.isp.input_mode == 'cell': return

        isp = self.isp
        # whitespace after ':' should not screw up indent level
        isp.push('if 1: \n    x=1')
        self.assertEqual(isp.indent_spaces, 4)
        isp.push('y=2\n')
        self.assertEqual(isp.indent_spaces, 0)
        isp.push('if 1:\t\n    x=1')
        self.assertEqual(isp.indent_spaces, 4)
        isp.push('y=2\n')
        self.assertEqual(isp.indent_spaces, 0)
Example #22
0
 def test_indent(self):
     isp = self.isp # shorthand
     isp.push('x=1')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n    x=1')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('y=2\n')
     self.assertEqual(isp.indent_spaces, 0)
 def test_push_accepts_more2(self):
     isp = self.isp
     isp.push("if 1:")
     self.assertTrue(isp.push_accepts_more())
     isp.push("  x=1")
     self.assertTrue(isp.push_accepts_more())
     isp.push("")
     self.assertFalse(isp.push_accepts_more())
 def test_push_accepts_more2(self):
     isp = self.isp
     isp.push('if 1:')
     self.assertTrue(isp.push_accepts_more())
     isp.push('  x=1')
     self.assertTrue(isp.push_accepts_more())
     isp.push('')
     self.assertFalse(isp.push_accepts_more())
 def test_indent(self):
     isp = self.isp  # shorthand
     isp.push("x=1")
     self.assertEqual(isp.indent_spaces, 0)
     isp.push("if 1:\n    x=1")
     self.assertEqual(isp.indent_spaces, 4)
     isp.push("y=2\n")
     self.assertEqual(isp.indent_spaces, 0)
Example #26
0
 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 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 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.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
Example #28
0
 def test_dedent_continue(self):
     isp = self.isp # shorthand
     # should NOT cause dedent
     isp.push('while 1:\n    continues = 5')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('while 1:\n     continue')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('while 1:\n     continue   ')
     self.assertEqual(isp.indent_spaces, 0)
Example #29
0
 def test_dedent_pass(self):
     isp = self.isp # shorthand
     # should NOT cause dedent
     isp.push('if 1:\n    passes = 5')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('if 1:\n     pass')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('if 1:\n     pass   ')
     self.assertEqual(isp.indent_spaces, 0)
Example #30
0
 def test_dedent_break(self):
     isp = self.isp # shorthand
     # should NOT cause dedent
     isp.push('while 1:\n    breaks = 5')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('while 1:\n     break')
     self.assertEqual(isp.indent_spaces, 0)
     isp.push('while 1:\n     break   ')
     self.assertEqual(isp.indent_spaces, 0)
Example #31
0
 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(self):
     isp = self.isp
     self.assertEqual(isp.push('x=1'), True)
 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)
Example #35
0
 def test_push(self):
     isp = self.isp
     self.assertTrue(isp.push('x=1'))
Example #36
0
    #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.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():
Example #37
0
 def test_push3(self):
     isp = self.isp
     isp.push('if True:')
     isp.push('  a = 1')
     self.assertFalse(isp.push('b = [1,'))
Example #38
0
 def test_push2(self):
     isp = self.isp
     self.assertFalse(isp.push('if 1:'))
     for line in ['  x=1', '# a comment', '  y=2']:
         print(line)
         self.assertTrue(isp.push(line))
Example #39
0
 def test_push_accepts_more(self):
     isp = self.isp
     isp.push('x=1')
     self.assertFalse(isp.push_accepts_more())
Example #40
0
 def test_dedent(self):
     isp = self.isp  # shorthand
     isp.push('if 1:')
     self.assertEqual(isp.indent_spaces, 4)
     isp.push('    pass')
     self.assertEqual(isp.indent_spaces, 0)
Example #41
0
 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_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)
Example #43
0
 def test_continuation(self):
     isp = self.isp
     isp.push("import os, \\")
     self.assertTrue(isp.push_accepts_more())
     isp.push("sys")
     self.assertFalse(isp.push_accepts_more())