def test_parse_label(self): parser = Parser() lines_to_parse = ['(LABEL)'] parsed_lines = parser.parse(lines_to_parse) self.assertEqual(parsed_lines, [Label('LABEL')])
def test_parse_lines_c_instruction2(self): parser = Parser() lines_to_parse = ['D;JGT'] parsed_lines = parser.parse(lines_to_parse) self.assertEqual(parsed_lines, [CInstruction('D', None, 'JGT')])
def test_parse_comment(self): parser = Parser() lines_to_parse = ['//test'] parsed_lines = parser.parse(lines_to_parse) self.assertEqual(parsed_lines, [])
def test_parse_lines_a_instruction2(self): parser = Parser() lines_to_parse = ['@2'] parsed_lines = parser.parse(lines_to_parse) self.assertEqual(parsed_lines, [AInstruction('2')])
def test_parse_lines_c_instruction1(self): parser = Parser() lines_to_parse = ['D=M'] parsed_lines = parser.parse(lines_to_parse) self.assertEqual(parsed_lines, [CInstruction('M', 'D', None)])
def testSymbol(self): parser = Parser('@abc') parser.advance() self.assertEqual('abc', parser.symbol) parser = Parser('(abc)') parser.advance() self.assertEqual('abc', parser.symbol) parser = Parser('foobar') parser.advance() self.assertRaises(RuntimeError, lambda: parser.symbol)
def testHasMoreCommandsAndAdvance(self): parser = Parser('') self.assertFalse(parser.hasMoreCommands) parser = Parser('hello') self.assertEqual(None, parser.currentCommand) self.assertTrue(parser.hasMoreCommands) parser.advance() self.assertFalse(parser.hasMoreCommands) self.assertRaises(EOFError, parser.advance) parser = Parser('// this is a comment') self.assertFalse(parser.hasMoreCommands) parser = Parser('foobar // this is a command') self.assertTrue(parser.hasMoreCommands)
def testCleanLine(self): self.assertEqual(None, Parser.cleanLine('')) self.assertEqual('123', Parser.cleanLine('123')) self.assertEqual(None, Parser.cleanLine('// 123')) self.assertEqual(None, Parser.cleanLine('//123')) self.assertEqual('123', Parser.cleanLine('123//123')) self.assertEqual('123', Parser.cleanLine('123 //123')) self.assertEqual('123', Parser.cleanLine('123 // 123'))
def test_is_whitespace(self): self.assertTrue(Parser.is_whitespace(' ')) self.assertTrue(Parser.is_whitespace('')) self.assertTrue(Parser.is_whitespace('// a comment')) self.assertFalse(Parser.is_whitespace('@i')) self.assertFalse(Parser.is_whitespace('A=M+D')) self.assertFalse(Parser.is_whitespace(' @asd '))
def testInit(self): Parser('') Parser(open('./tests.py'))
def testDestCompJump(self): parser = Parser('dest=comp;JMP') parser.advance() self.assertEqual('dest', parser.dest) self.assertEqual('comp', parser.comp) self.assertEqual('JMP', parser.jump) parser = Parser('dest = comp ; JMP') parser.advance() self.assertEqual('dest', parser.dest) self.assertEqual('comp', parser.comp) self.assertEqual('JMP', parser.jump) parser = Parser('dest=comp') parser.advance() self.assertEqual('dest', parser.dest) self.assertEqual('comp', parser.comp) self.assertEqual('null', parser.jump) parser = Parser('comp;JMP') parser.advance() self.assertEqual('null', parser.dest) self.assertEqual('comp', parser.comp) self.assertEqual('JMP', parser.jump) parser = Parser('comp') parser.advance() self.assertEqual('null', parser.dest) self.assertEqual('comp', parser.comp) self.assertEqual('null', parser.jump) parser = Parser('@abc') parser.advance() self.assertRaises(RuntimeError, lambda: parser.dest) self.assertRaises(RuntimeError, lambda: parser.comp) self.assertRaises(RuntimeError, lambda: parser.jump)
def init(stream): return Parser(stream)