Skip to content

Commit

Permalink
multiline parser input tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sn6uv committed Apr 6, 2016
1 parent e1958dd commit b23342e
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import random
import unittest

from mathics.core.parser import parse, ScanError, IncompleteSyntaxError, InvalidSyntaxError
from mathics.core.parser import parse, parse_lines, ScanError, IncompleteSyntaxError, InvalidSyntaxError
from mathics.core.expression import (Expression, Real, Integer, String,
Rational, Symbol)
from mathics.core.definitions import Definitions
Expand All @@ -23,33 +23,29 @@ def setUpModule():
definitions = Definitions(add_builtin=True)


_parse = parse


def parse(s):
return _parse(s, definitions)


class ParserTests(unittest.TestCase):
def parse(self, s):
return parse(s, definitions)

def check(self, expr1, expr2):
if isinstance(expr1, six.string_types):
expr1 = parse(expr1)
expr1 = self.parse(expr1)
if isinstance(expr2, six.string_types):
expr2 = parse(expr2)
expr2 = self.parse(expr2)

if expr1 is None:
self.assertTrue(expr2 is None)
else:
self.assertTrue(expr1.same(expr2))

def lex_error(self, string):
self.assertRaises(ScanError, parse, string)
self.assertRaises(ScanError, self.parse, string)

def incomplete_error(self, string):
self.assertRaises(IncompleteSyntaxError, parse, string)
self.assertRaises(IncompleteSyntaxError, self.parse, string)

def invalid_error(self, string):
self.assertRaises(InvalidSyntaxError, parse, string)
self.assertRaises(InvalidSyntaxError, self.parse, string)


class NumberTests(ParserTests):
Expand Down Expand Up @@ -148,8 +144,8 @@ def testString(self):
self.invalid_error(r'\""')

def testNone(self):
self.assertIs(parse(''), None)
self.assertIs(parse('(*fdasf *)'), None)
self.assertIs(self.parse(''), None)
self.assertIs(self.parse('(*fdasf *)'), None)

def testMessage(self):
self.check('1 :: "abc"', Expression('MessageName', Integer(1), String("abc")))
Expand Down Expand Up @@ -462,6 +458,28 @@ def testBracketIncompleteInvalid(self):
self.incomplete_error('{x')
self.invalid_error('[[x')

def test_trailing_backslash(self):
self.incomplete_error('x \\')


class MultiLineParserTests(ParserTests):
def parse(self, s):
exprs = list(parse_lines(s, definitions))
assert len(exprs) == 1
return exprs[0]

def test_trailing_backslash(self):
self.incomplete_error('x \\')
self.check('x \\\ny', Expression('Times', Symbol('Global`x'), Symbol('Global`y')))

def test_continuation(self):
self.incomplete_error('Sin[')
self.check('Sin[\n0]', Expression('Sin', Integer(0)))

@unittest.expectedFailure
def test_blanknewline(self):
# currently handled in the frontend
self.incomplete_error('Sin[\n\n0]')

if __name__ == "__main__":
unittest.main()

0 comments on commit b23342e

Please sign in to comment.