Beispiel #1
0
    def test_multiline_passthrough(self):
        isp = self.isp

        class CommentTransformer(InputTransformer):
            def __init__(self):
                self._lines = []

            def push(self, line):
                self._lines.append(line + '#')

            def reset(self):
                text = '\n'.join(self._lines)
                self._lines = []
                return text

        isp.physical_line_transforms.insert(0, CommentTransformer())

        for raw, expected in [
            ("a=5", "a=5#"),
            ("%ls foo",
             "get_ipython().run_line_magic(%r, %r)" % (u'ls', u'foo#')),
            ("!ls foo\n%ls bar",
             "get_ipython().system(%r)\nget_ipython().run_line_magic(%r, %r)" %
             (u'ls foo#', u'ls', u'bar#')),
            ("1\n2\n3\n%ls foo\n4\n5",
             "1#\n2#\n3#\nget_ipython().run_line_magic(%r, %r)\n4#\n5#" %
             (u'ls', u'foo#')),
        ]:
            out = isp.transform_cell(raw)
            self.assertEqual(out.rstrip(), expected.rstrip())
Beispiel #2
0
 def test_multiline_passthrough(self):
     isp = self.isp
     class CommentTransformer(InputTransformer):
         def __init__(self):
             self._lines = []
         
         def push(self, line):
             self._lines.append(line + '#')
         
         def reset(self):
             text = '\n'.join(self._lines)
             self._lines = []
             return text
     
     isp.physical_line_transforms.insert(0, CommentTransformer())
     
     for raw, expected in [
         ("a=5", "a=5#"),
         ("%ls foo", "get_ipython().magic(%r)" % u'ls foo#'),
         ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().magic(%r)" % (
             u'ls foo#', u'ls bar#'
         )),
         ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().magic(%r)\n4#\n5#" % u'ls foo#'),
     ]:
         out = isp.transform_cell(raw)
         self.assertEqual(out.rstrip(), expected.rstrip())
Beispiel #3
0
    def test_syntax_multiline_cell(self):
        isp = self.isp
        for example in syntax_ml.values():

            out_t_parts = []
            for line_pairs in example:
                raw = '\n'.join(r for r, _ in line_pairs if r is not None)
                out_t = '\n'.join(t for _, t in line_pairs if t is not None)
                out = isp.transform_cell(raw)
                # Match ignoring trailing whitespace
                self.assertEqual(out.rstrip(), out_t.rstrip())
Beispiel #4
0
    def test_syntax_multiline_cell(self):
        isp = self.isp
        for example in syntax_ml.values():

            out_t_parts = []
            for line_pairs in example:
                raw = '\n'.join(r for r, _ in line_pairs if r is not None)
                out_t = '\n'.join(t for _,t in line_pairs if t is not None)
                out = isp.transform_cell(raw)
                # Match ignoring trailing whitespace
                self.assertEqual(out.rstrip(), out_t.rstrip())
 def test_cellmagic_preempt(self):
     isp = self.isp
     for raw, name, line, cell in [
         ("%%cellm a\nIn[1]:", "cellm", "a", "In[1]:"),
         ("%%cellm \nline\n>>>hi", "cellm", "", "line\n>>>hi"),
         (">>>%%cellm \nline\n>>>hi", "cellm", "", "line\nhi"),
         ("%%cellm \n>>>hi", "cellm", "", "hi"),
         ("%%cellm \nline1\nline2", "cellm", "", "line1\nline2"),
         ("%%cellm \nline1\\\\\nline2", "cellm", "", "line1\\\\\nline2"),
     ]:
         expected = "get_ipython().run_cell_magic(%r, %r, %r)" % (name, line, cell)
         out = isp.transform_cell(raw)
         self.assertEqual(out.rstrip(), expected.rstrip())
Beispiel #6
0
 def test_cellmagic_preempt(self):
     isp = self.isp
     for raw, name, line, cell in [
         ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
         ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
         (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
         ("%%cellm \n>>> hi", u'cellm', u'', u'>>> hi'),
         ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
         ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
     ]:
         expected = "get_ipython().run_cell_magic(%r, %r, %r)" % (
             name, line, cell)
         out = isp.transform_cell(raw)
         self.assertEqual(out.rstrip(), expected.rstrip())