view python/testasm.py @ 194:b01429a5d695

Fixed test
author Windel Bouwman
date Wed, 29 May 2013 22:36:37 +0200
parents f091e7d70996
children 37ac6c016e0f
line wrap: on
line source

#!/usr/bin/python

import unittest
import libasm
import ppci

class AssemblerTestCase(unittest.TestCase):
    """ 
        Tests the assembler parts
    """
    def setUp(self):
        pass

    def testLex0(self):
        """ Check if the lexer is OK """
        asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
        self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)

    def testLex1(self):
        """ Test if lexer correctly maps some tokens """
        asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
        self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)

    def testLex1(self):
        """ Test if lexer correctly maps some tokens """
        asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
        self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)

    def testLex2(self):
        """ Test if lexer fails on a token that is invalid """
        asmline = '0z4: mov rax, rbx $ '
        with self.assertRaises(ppci.CompilerError):
            list(libasm.tokenize(asmline))

    def testParse(self):
        asmline = 'lab1: mov rax, rbx'
        a = libasm.Assembler()
        a.parse_line(asmline)

    def testParse2(self):
        asmline = 'a: mov rax, [rbx + 2]'
        a = libasm.Assembler()
        a.parse_line(asmline)

    def testParse3(self):
        # A label must be optional:
        asmline = 'mov rax, 1'
        a = libasm.Assembler()
        a.parse_line(asmline)

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