def test_block(self): actual = parser.parse( textwrap.dedent("""\ [ 1 2 "a" ] """)) expected = parser.Queue([ parser.Block([ parser.Number(1.0), parser.Number(2.0), parser.String("a"), ]) ]) self.assertEqual(actual, expected)
def test_blocks_need_spaces(self): with self.assertRaises(parser.IncompleteParseError): parser.parse("[a]") actual = parser.parse("[ 1 ]") expected = parser.Queue([parser.Block([parser.Number(1.0)])]) self.assertEqual(actual, expected)
def test_comment(self): source = textwrap.dedent("""\ 1 print # I am a comment """) actual = parser.parse(source) expected = parser.Queue([parser.Number(1), parser.Identifier("print")]) self.assertEqual(actual, expected)
def test_simple_math_1(self): source = textwrap.dedent("""\ 1 1 + 3 - """) actual = parser.parse(source) expected = parser.Queue([ parser.Number(1), parser.Number(1), math_operations.Add(), parser.Number(3), math_operations.Sub(), ]) self.assertEqual(actual, expected)
def test_simple_math_2(self): source = textwrap.dedent("""\ 1 1 3 # [ 1 1 3 ] + # [ 3 2 ] - # [ 1 ] """) actual = parser.parse(source) expected = parser.Queue([ parser.Number(1), parser.Number(1), parser.Number(3), parser.Identifier("+"), parser.Identifier("-"), ]) self.assertEqual(actual, expected)
def test_numbers(self): accepted = ["0", "1", "123", "-1", "-0"] for number in accepted: with self.subTest(number): actual = parser.parse(number) expected = parser.Queue([parser.Number(int(number))]) self.assertEqual(actual, expected) rejected = ["0.", ".0"] for number in rejected: with self.assertRaises(parser.IncompleteParseError): parser.parse(number) floats = ["123.321", "12.0", "-123.321", "-12.0"] for number in floats: with self.assertRaises(parser.VisitationError): parser.parse(number)
def test_single_number(self): actual = parser.parse("1") expected = parser.Queue([parser.Number(1)]) self.assertEqual(actual, expected)