191
|
1 #!/usr/bin/python
|
|
2
|
|
3 import unittest
|
|
4 import libasm
|
|
5 import ppci
|
|
6
|
|
7 class AssemblerTestCase(unittest.TestCase):
|
|
8 """
|
|
9 Tests the assembler parts
|
|
10 """
|
|
11 def setUp(self):
|
|
12 pass
|
|
13
|
|
14 def testLex0(self):
|
|
15 """ Check if the lexer is OK """
|
|
16 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
|
|
17 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
|
|
18
|
|
19 def testLex1(self):
|
193
|
20 """ Test if lexer correctly maps some tokens """
|
191
|
21 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
|
|
22 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
|
|
23
|
193
|
24 def testLex1(self):
|
|
25 """ Test if lexer correctly maps some tokens """
|
|
26 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
|
|
27 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
|
|
28
|
191
|
29 def testLex2(self):
|
193
|
30 """ Test if lexer fails on a token that is invalid """
|
191
|
31 asmline = '0z4: mov rax, rbx $ '
|
|
32 with self.assertRaises(ppci.CompilerError):
|
|
33 list(libasm.tokenize(asmline))
|
|
34
|
|
35 def testParse(self):
|
|
36 asmline = 'lab1: mov rax, rbx'
|
|
37 a = libasm.Assembler()
|
|
38 a.assembleLine(asmline)
|
|
39
|
193
|
40 def testParse2(self):
|
|
41 asmline = 'a: mov rax, [rbx + 2]'
|
|
42 a = libasm.Assembler()
|
|
43 a.assembleLine(asmline)
|
|
44
|
191
|
45 if __name__ == '__main__':
|
|
46 unittest.main()
|
|
47
|