Example #1
0
    def testTripleFunctionWithIntermediateCleanup(self):
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)

        repl.runCommandLine('def triple(x):')
        self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)

        repl.runCommandLine('  return int(x) * 3')
        self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)

        repl.runCommandLine('')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('echo a b c | wc -w |')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        pl.toggleDebug()
        repl.runCommandLine('ll = lambda x: x[0]')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('ll(_)')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('triple(_)')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        self.assertEqual(9, pl.stdin)
Example #2
0
 def testAttributes(self):
     """
     A Batch instance must have the expected attributes.
     """
     pl = Pipeline(loadInitFile=False)
     batch = Batch(pl)
     self.assertIs(pl, batch.pipeline)
Example #3
0
 def testAttributes(self):
     """
     A REPL instance must have the expected attributes.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     self.assertIs(pl, repl.pipeline)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #4
0
 def testSetPrompts(self):
     """
     Setting the prompts via __init__ must work.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pipeline=pl, ps1='x', ps2='y')
     self.assertEqual('x', repl.prompt)
     self.assertEqual('x', sys.ps1)
     self.assertEqual('y', sys.ps2)
Example #5
0
 def testEchoCatCat(self):
     """
     We should be able to pipe echo output to cat twice.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('echo hi | cat | cat')
     self.assertEqual(['hi'], pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #6
0
 def testNumber(self):
     """
     The REPL instance must be in the expected state after processing a
     number.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('4')
     self.assertAlmostEqual(4, pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #7
0
 def testNumberPipeOneLine(self):
     """
     The REPL instance must be in the expected state after processing a
     command line containing a number followed by a pipe symbol.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('4 |')
     self.assertAlmostEqual(4, pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #8
0
 def testAddUnderscoreVar(self):
     """
     We should be able to add to the _ variable even when not in
     a pipeline.
     """
     commands = StringIO('7\n_ + 10\n')
     out = StringIO()
     pl = Pipeline(loadInitFile=False, outfp=out)
     Batch(pl).run(commands)
     self.assertEqual('7\n17\n', out.getvalue())
Example #9
0
 def testToggleDebug(self):
     """
     The REPL instance must be able to toggle the pipeline debug setting.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('%d')
     self.assertIs(True, pl.debug)
     repl.runCommandLine('%d')
     self.assertIs(False, pl.debug)
Example #10
0
 def testIncompleteDict(self):
     """
     The REPL instance must be in the expected state after processing a
     an incomplete dict.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('{ "a": 6, "b": ')
     # Is this next line correct? Should it be None?
     self.assertIs(None, pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)
Example #11
0
 def testAddUnderscoreVar(self):
     """
     We should be able to add to the _ variable even when not in
     a pipeline.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('7')
     repl.runCommandLine('_ + 10')
     self.assertEqual(17, pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #12
0
 def testDictOnTwoLines(self):
     """
     The REPL instance must be in the expected state after processing a
     a dict given on two lines.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('{ "a": 6, "b": ')
     repl.runCommandLine('7 }')
     self.assertEqual({'a': 6, 'b': 7}, pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #13
0
    def testTripleFunction(self):
        commands = StringIO('''
def triple(x):
    return int(x) * 3

echo a b c | wc -w | triple(_[0])
''')
        out = StringIO()
        pl = Pipeline(loadInitFile=False, outfp=out)
        Batch(pl).run(commands)
        self.assertEqual('9\n', out.getvalue())
Example #14
0
 def testToggleTracebacks(self):
     """
     The REPL instance must be able to toggle the printing of tracebacks.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('%t')
     self.assertIs(True, pl.printTracebacks)
     self.assertIs(True, pl.debug)
     repl.runCommandLine('%t')
     self.assertIs(False, pl.printTracebacks)
     # Note that turning off traceback printing does not turn off debug.
     self.assertIs(True, pl.debug)
Example #15
0
    def testAreaFunction(self):
        """Define an area function and call it."""
        commands = StringIO('''
from math import pi
def area(r):
    return r ** 2 * pi

area(2.0)
''')
        out = StringIO()
        pl = Pipeline(loadInitFile=False, outfp=out)
        Batch(pl).run(commands)
        self.assertTrue(out.getvalue().startswith('12.56637'))
Example #16
0
 def testNumberPipeTwoLines(self):
     """
     The REPL instance must be in the expected state after processing a
     command line containing a number and then a second command line with
     just a pipe symbol.
     """
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('4')
     self.assertFalse(pl.inPipeline)
     self.assertEqual(4, pl.stdin)
     repl.runCommandLine('')
     self.assertEqual(4, pl.stdin)
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #17
0
    def testInitialWhitespaceCommands(self):
        """
        We should be able to repeatedly enter whitespace commands in a new
        session, while stdin remains as None and the prompt is unchanged.
        """
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)

        repl.runCommandLine('  ')
        self.assertEqual(None, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('  \t  ')
        self.assertEqual(None, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #18
0
    def testTripleFunctionWithIntermediateCleanup(self):
        commands = StringIO('''
def triple(x):
    return x * 3

echo a b c | wc -w

# Write a little function to convert the first line of _ to an int.
f = lambda line: int(line[0])

| f(_) | triple(_)
''')
        out = StringIO()
        pl = Pipeline(loadInitFile=False, outfp=out, usePtys=False)
        Batch(pl).run(commands)
        self.assertEqual('3\n9\n', out.getvalue())
Example #19
0
    def testInterpretingSection(self):
        """
        Test the commands carried out in the 'How commands are interpreted'
        section.
        """
        commands = StringIO('''
4
_
[3, 6, 9]
print('hello')
echo hello too
''')
        out = StringIO()
        pl = Pipeline(loadInitFile=False, outfp=out)
        Batch(pl).run(commands)
        self.assertEqual('4\n4\n[3, 6, 9]\nhello\n', out.getvalue())
Example #20
0
    def testTripleFunction(self):
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)

        repl.runCommandLine('def triple(x):')
        self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)

        repl.runCommandLine('  return int(x) * 3')
        self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)

        repl.runCommandLine('')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('echo a b c | wc -w | triple(_[0])')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        self.assertEqual(9, pl.stdin)
Example #21
0
    def testWhitespaceCommands(self):
        """
        We should be able to repeatedly enter whitespace commands without
        changing the pending stdin value or the prompt.
        """
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)

        repl.runCommandLine('4')
        self.assertEqual(4, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine(' ')
        self.assertEqual(4, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('\t\t')
        self.assertEqual(4, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #22
0
    def testEmptyCommands(self):
        """
        We should be able to hit ENTER repeatedly without changing
        the pending stdin value or the prompt.
        """
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)

        repl.runCommandLine('4')
        self.assertEqual(4, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('')
        self.assertEqual(4, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('')
        self.assertEqual(4, pl.stdin)
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #23
0
    def testAreaFunction(self):
        """Define an area function and call it."""
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)

        repl.runCommandLine('from math import pi')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('def area(r):')
        self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)

        repl.runCommandLine('  return r ** 2 * pi')
        self.assertEqual(REPL.DEFAULT_PS2, repl.prompt)

        repl.runCommandLine('')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        repl.runCommandLine('area(2.0)')
        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)

        self.assertAlmostEqual(12.566370614359172, pl.stdin)
Example #24
0
    def testInterpretingSection(self):
        """
        Test the commands carried out in the 'How commands are interpreted'
        section.
        """
        pl = Pipeline(loadInitFile=False)
        repl = REPL(pl)
        repl.runCommandLine('4')
        self.assertEqual(4, pl.stdin)

        repl.runCommandLine('_')
        self.assertEqual(4, pl.stdin)

        repl.runCommandLine('[3, 6, 9]')
        self.assertEqual([3, 6, 9], pl.stdin)

        repl.runCommandLine("print('hello')")
        self.assertEqual('hello', pl.stdin)

        repl.runCommandLine('echo hello too')
        self.assertEqual(['hello too'], pl.stdin)

        self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
Example #25
0
 def testEchoPipedToWc(self):
     commands = StringIO('echo a b c | wc -w')
     out = StringIO()
     pl = Pipeline(loadInitFile=False, outfp=out, usePtys=False)
     Batch(pl).run(commands)
     self.assertEqual('3\n', out.getvalue())
Example #26
0
 def testAbsArithmetic(self):
     commands = StringIO('-6 | abs(_) | _ * 7')
     out = StringIO()
     pl = Pipeline(loadInitFile=False, outfp=out)
     Batch(pl).run(commands)
     self.assertEqual('42\n', out.getvalue())
Example #27
0
 def testAbsArithmetic(self):
     pl = Pipeline(loadInitFile=False)
     repl = REPL(pl)
     repl.runCommandLine('-6 | abs(_) | _ * 7')
     self.assertEqual(REPL.DEFAULT_PS1, repl.prompt)
     self.assertEqual(42, pl.stdin)
Example #28
0
 def __init__(self, pipeline=None):
     self.pipeline = pipeline or Pipeline()
Example #29
0
 def __init__(self, pipeline=None, ps1=DEFAULT_PS1, ps2=DEFAULT_PS2):
     self.pipeline = pipeline or Pipeline()
     sys.ps1 = self.prompt = ps1
     sys.ps2 = ps2