def test_init(self): """ Parser setup should read the first line of the file""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') self.assertEqual(self.testParser.getLine(0), "@2") self.assertEqual(self.testParser.getLine(3), "D=D+A") self.assertEqual(testParser.getNumberLines(), 6)
def test_destNoDest(self): """ Return the dest mnemonic for a C command - this case has no dest mnemonic - should return None""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.dest(), 'None')
def test_advance(self): """ Test that advance moves us to the second line of the file""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') self.assertEqual(self.testParser.getLine(), "@2") self.testParser.advance() self.assertEqual(self.testParser.getLine(), "D=A")
def test_symbolNotACommand(self): """ return symbol for A commands""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') self.assertEqual(testParser.symbol(), "0") testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.symbol(), "-1")
def test_init(self): file_path = './add/Add.asm' test_parser = Parser(file_path) f = open('./add/Add.asm', "r", encoding="utf-8") self.assertEqual(test_parser.file, f.readlines()) self.assertEqual(test_parser.length, 13) self.assertEqual(test_parser.line_counter, -1) self.assertEqual(test_parser.current_command, None) f.close()
def test_commandType(self): """ Simple test, no jumps""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') goodRes = ['A', 'C', 'A', 'C', 'A', 'C'] first = True for res in goodRes: if not first: testParser.advance() print str(res) + " : " + str(testParser.getLine()) self.assertEqual(testParser.commandType(), res) first = False
def test_commandTypeLabel(self): """ More complicated test, includes Labels""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') goodRes = [ 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'L', 'A', 'C', 'L', 'A', 'C', 'L', 'A', 'C' ] first = True for res in goodRes: if not first: testParser.advance() print str(res) + " : " + str(testParser.getLine()) self.assertEqual(testParser.commandType(), res) first = False
def test_jumpComments(self): """ Return the jump mnemonic for a C command """ testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() self.assertEqual(testParser.jump(), 'NOJUMP') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.jump(), 'JGT') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.jump(), 'JMP')
def test_compComments(self): """ Return the comp mnenomic for a C command """ testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() self.assertEqual(testParser.comp(), 'M') testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), 'D-M') testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), 'D') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), '0')
def test_process_file(self): file_path = './add/Add.asm' test_parser = Parser(file_path) processed_file = ['@2', 'D=A', '@3', 'D=D+A', '@0', 'M=D'] test_parser.process_file() self.assertEqual(test_parser.proc_file, processed_file)
def test_dest(self): file_path = './add/Add.asm' test_parser = Parser(file_path) test_parser.process_file() test_parser.advance() self.assertEqual(test_parser.dest(), None) test_parser.advance() self.assertEqual(test_parser.dest(), 'D') test_parser.advance() self.assertEqual(test_parser.dest(), None) test_parser.advance() self.assertEqual(test_parser.dest(), 'D') test_parser.advance() self.assertEqual(test_parser.dest(), None) test_parser.advance() self.assertEqual(test_parser.dest(), 'M')
def test_symbol(self): file_path = './add/Add.asm' test_parser = Parser(file_path) test_parser.process_file() test_parser.advance() self.assertEqual(test_parser.symbol(), '2') test_parser.advance() self.assertEqual(test_parser.symbol(), None) test_parser.advance() self.assertEqual(test_parser.symbol(), '3') test_parser.advance() self.assertEqual(test_parser.symbol(), None) test_parser.advance() self.assertEqual(test_parser.symbol(), '0') test_parser.advance() self.assertEqual(test_parser.symbol(), None)
def test_command_type(self): file_path = './add/Add.asm' test_parser = Parser(file_path) test_parser.process_file() test_parser.advance() self.assertEqual(test_parser.commandType(), 'A_COMMAND') test_parser.advance() self.assertEqual(test_parser.commandType(), 'C_COMMAND') test_parser.advance() self.assertEqual(test_parser.commandType(), 'A_COMMAND') test_parser.advance() self.assertEqual(test_parser.commandType(), 'C_COMMAND') test_parser.advance() self.assertEqual(test_parser.commandType(), 'A_COMMAND') test_parser.advance() self.assertEqual(test_parser.commandType(), 'C_COMMAND')
def test_advance(self): file_path = './add/Add.asm' test_parser = Parser(file_path) test_parser.process_file() self.assertEqual(test_parser.line_counter, -1) test_parser.advance() self.assertEqual(test_parser.line_counter, 0) test_parser.line_counter = test_parser.length - 1 test_parser.advance() self.assertEqual(test_parser.line_counter, test_parser.length - 1) test_parser.advance() self.assertEqual(test_parser.line_counter, test_parser.length - 1)
def test_has_more_commands(self): file_path = './add/Add.asm' test_parser = Parser(file_path) test_parser.process_file() self.assertTrue(test_parser.hasMoreCommands()) test_parser.line_counter = test_parser.length - test_parser.length self.assertTrue(test_parser.hasMoreCommands()) test_parser.line_counter = test_parser.length - 1 self.assertFalse(test_parser.hasMoreCommands()) test_parser.line_counter = test_parser.length self.assertFalse(test_parser.hasMoreCommands()) test_parser.line_counter = test_parser.length + 1 self.assertFalse(test_parser.hasMoreCommands())
def test_hasMoreCommands(self): testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') self.assertTrue( testParser.hasMoreCommands()) # Line 1 - should be true testParser.advance() self.assertTrue( testParser.hasMoreCommands()) # Line 2 - should be true testParser.advance() testParser.advance() self.assertTrue( testParser.hasMoreCommands()) # Line 4 - should be true testParser.advance() testParser.advance() self.assertFalse( testParser.hasMoreCommands()) # Line 6 - should be false
class MyTestCase(unittest.TestCase): def setUp(self): self.testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') def test_removeCommentsAndWhitespace(self): test_list = ['//ssss', ' ', 'one', 'two', '//three', 'three'] output = Parser.removeCommentsAndWhitespace(test_list) self.assertEqual(output, ['one', 'two', 'three']) def test_init(self): """ Parser setup should read the first line of the file""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') self.assertEqual(self.testParser.getLine(0), "@2") self.assertEqual(self.testParser.getLine(3), "D=D+A") self.assertEqual(testParser.getNumberLines(), 6) def test_advance(self): """ Test that advance moves us to the second line of the file""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') self.assertEqual(self.testParser.getLine(), "@2") self.testParser.advance() self.assertEqual(self.testParser.getLine(), "D=A") def test_hasMoreCommands(self): testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') self.assertTrue( testParser.hasMoreCommands()) # Line 1 - should be true testParser.advance() self.assertTrue( testParser.hasMoreCommands()) # Line 2 - should be true testParser.advance() testParser.advance() self.assertTrue( testParser.hasMoreCommands()) # Line 4 - should be true testParser.advance() testParser.advance() self.assertFalse( testParser.hasMoreCommands()) # Line 6 - should be false def test_commandType(self): """ Simple test, no jumps""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm') goodRes = ['A', 'C', 'A', 'C', 'A', 'C'] first = True for res in goodRes: if not first: testParser.advance() print str(res) + " : " + str(testParser.getLine()) self.assertEqual(testParser.commandType(), res) first = False def test_commandTypeJump(self): """ More complicated test, includes jumps""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') goodRes = [ 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C' ] first = True for res in goodRes: if not first: testParser.advance() print str(res) + " : " + str(testParser.getLine()) self.assertEqual(testParser.commandType(), res) first = False def test_commandTypeLabel(self): """ More complicated test, includes Labels""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') goodRes = [ 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'A', 'C', 'L', 'A', 'C', 'L', 'A', 'C', 'L', 'A', 'C' ] first = True for res in goodRes: if not first: testParser.advance() print str(res) + " : " + str(testParser.getLine()) self.assertEqual(testParser.commandType(), res) first = False def test_symbol(self): """ return symbol for A commands""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') self.assertEqual(testParser.symbol(), "0") testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.symbol(), "10") def test_symbolNotACommand(self): """ return symbol for A commands""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') self.assertEqual(testParser.symbol(), "0") testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.symbol(), "-1") def test_symbolL(self): """ return symbol for L commands""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.symbol(), "OUTPUT_FIRST") def test_dest(self): """ Return the dest mnemonic for a C command""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') testParser.advance() self.assertEqual(testParser.dest(), 'D') def test_destNoDest(self): """ Return the dest mnemonic for a C command - this case has no dest mnemonic - should return None""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.dest(), 'None') def test_comp(self): """ Return the comp mnenomic for a C command """ testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') testParser.advance() self.assertEqual(testParser.comp(), 'M') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), 'D') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), '0') def test_jump(self): """ Return the jump mnemonic for a C command """ testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') testParser.advance() self.assertEqual(testParser.jump(), 'NOJUMP') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.jump(), 'JGT') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.jump(), 'JMP') def test_destComments(self): """ Return the dest mnemonic for a C command - this case has no dest mnemonic - should return None""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() self.assertEqual(testParser.dest(), 'D') def test_compComments(self): """ Return the comp mnenomic for a C command """ testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() self.assertEqual(testParser.comp(), 'M') testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), 'D-M') testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), 'D') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.comp(), '0') def test_jumpComments(self): """ Return the jump mnemonic for a C command """ testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() self.assertEqual(testParser.jump(), 'NOJUMP') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.jump(), 'JGT') testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.jump(), 'JMP') if __name__ == '__main__': unittest.main()
def test_symbolL(self): """ return symbol for L commands""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/Max.asm') testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() testParser.advance() self.assertEqual(testParser.symbol(), "OUTPUT_FIRST")
def setUp(self): self.testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/add/Add.asm')
def test_dest(self): """ Return the dest mnemonic for a C command""" testParser = Parser( '/Users/Sean/Desktop/nand2tetris/projects/06/max/MaxL.asm') testParser.advance() self.assertEqual(testParser.dest(), 'D')
def test_removeCommentsAndWhitespace(self): test_list = ['//ssss', ' ', 'one', 'two', '//three', 'three'] output = Parser.removeCommentsAndWhitespace(test_list) self.assertEqual(output, ['one', 'two', 'three'])
def test_jump(self): file_path = './add/Add.asm' test_parser = Parser(file_path) test_parser.process_file() test_parser.advance() self.assertEqual(test_parser.jump(), None) test_parser.advance() self.assertEqual(test_parser.jump(), None) test_parser.advance() self.assertEqual(test_parser.jump(), None) test_parser.advance() self.assertEqual(test_parser.jump(), None) test_parser.advance() self.assertEqual(test_parser.jump(), None) test_parser.advance() self.assertEqual(test_parser.jump(), None)