Ejemplo n.º 1
0
def run_session(filename):
    """Run session test.

    Output summary to stdout and return True if all test pass,
    otherwise return False."""

    env = Environment(namespace=Builtins.namespace())
    num_tests, num_passed = 0, 0
    with open(filename) as f:
        input_expression = ""
        # Iterate over non-blank lines
        for line in ifilter(None, imap(str.strip, f)):
            if line.startswith(';'):
                continue
            elif line.startswith('> '):
                # Lines are stripped, so add a space to get whitespace
                # between the lines.
                input_expression += " " + line[2:]
            elif input_expression:
                # We got a line not starting with '> ', this is the
                # expected result from the preceding input expression
                num_tests += 1

                parser = Parser(StringIO.StringIO(input_expression))
                for expr in parser.expressions():
                    result = env.eval(expr)
                    result = repr(result) if result is not None else ''

                expected = line.rstrip('\n')
                if result != expected:
                    print 'Failure: "%s" != "%s"' % (result, expected)
                else:
                    num_passed += 1
                input_expression = ""
            else:
                # If we got a non-empty line without an input
                # expression something is wrong
                print 'Unexpected line: "%s"' % line
    print "{:20} Ran {} tests, {} passed".format(os.path.basename(filename),
                                                 num_tests, num_passed)
    return num_tests == num_passed
Ejemplo n.º 2
0
 def test_remainder(self):
     result = Builtins.remainder([3, 3])
     self.assertEqual(0, result)
     result = Builtins.remainder([5, 3])
     self.assertEqual(2, result)
Ejemplo n.º 3
0
 def test_abs(self):
     result = Builtins.abs([3])
     self.assertEqual(3, result)
     result = Builtins.abs([-3])
     self.assertEqual(3, result)
Ejemplo n.º 4
0
 def test_equals(self):
     result = Builtins.equals([3, 3])
     self.assertTrue(result)
     result = Builtins.equals([3, 4])
     self.assertFalse(result)
Ejemplo n.º 5
0
 def test_less_than(self):
     result = Builtins.less_than([3, 4])
     self.assertTrue(result)
     result = Builtins.less_than([4, 4])
     self.assertFalse(result)
Ejemplo n.º 6
0
 def test_greater_than(self):
     result = Builtins.greater_than([4, 3])
     self.assertTrue(result)
     result = Builtins.greater_than([4, 4])
     self.assertFalse(result)
Ejemplo n.º 7
0
 def test_add_three_operands(self):
     result = Builtins.add([1, 2, 3])
     self.assertEqual(6, result)
Ejemplo n.º 8
0
 def test_runtime(self):
     t1 = Builtins.runtime(None)
     t2 = Builtins.runtime(None)
     self.assertGreaterEqual(t2, t1)
Ejemplo n.º 9
0
 def test_divide_three_operands(self):
     result = Builtins.divide([12, 3, 2])
     self.assertEqual(2, result)
Ejemplo n.º 10
0
 def test_divide_two_operands(self):
     result = Builtins.divide([6, 2])
     self.assertEqual(3, result)
Ejemplo n.º 11
0
 def test_multiply_three_operands(self):
     result = Builtins.multiply([1, 2, 3])
     self.assertEqual(6, result)
Ejemplo n.º 12
0
 def test_subtract_one_operand_negation(self):
     result = Builtins.subtract([1])
     self.assertEqual(-1, result)
Ejemplo n.º 13
0
 def test_subtract_three_operands(self):
     result = Builtins.subtract([3, 2, 1])
     self.assertEqual(0, result)
Ejemplo n.º 14
0
 def test_subtract_two_operands(self):
     result = Builtins.subtract([3, 2])
     self.assertEqual(1, result)
Ejemplo n.º 15
0
 def test_newline(self):
     stream = StringIO.StringIO()
     Builtins.newline(None, stream)
     stream.seek(0)
     self.assertEqual('\n', stream.read())
Ejemplo n.º 16
0
 def test_display(self):
     stream = StringIO.StringIO()
     Builtins.display([STRING_VAL], stream)
     stream.seek(0)
     self.assertEqual(STRING_VAL, stream.read())
Ejemplo n.º 17
0
 def test_not_true(self):
     result = Builtins.not_([False])
     self.assertTrue(result)
Ejemplo n.º 18
0
 def test_random(self):
     result = Builtins.random([1])
     self.assertEqual(0, result)
Ejemplo n.º 19
0
 def test_not_false(self):
     result = Builtins.not_([True])
     self.assertFalse(result)