コード例 #1
0
 def testAttributes(self):
     """
     A Batch instance must have the expected attributes.
     """
     pl = Pipeline(loadInitFile=False)
     batch = Batch(pl)
     self.assertIs(pl, batch.pipeline)
コード例 #2
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())
コード例 #3
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())
コード例 #4
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'))
コード例 #5
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())
コード例 #6
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())
コード例 #7
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())
コード例 #8
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())