comparison test/testasm.py @ 382:0c44e494ef58

Made lexer more generic
author Windel Bouwman
date Sun, 27 Apr 2014 12:24:21 +0200
parents 6df89163e114
children 173e20a47fda
comparison
equal deleted inserted replaced
381:6df89163e114 382:0c44e494ef58
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import unittest 3 import unittest
4 from ppci import CompilerError 4 from ppci import CompilerError
5 from ppci.assembler import tokenize 5 from ppci.assembler import AsmLexer
6 from ppci.objectfile import ObjectFile 6 from ppci.objectfile import ObjectFile
7 from ppci.outstream import BinaryOutputStream 7 from ppci.outstream import BinaryOutputStream
8 from ppci.target.basetarget import Label 8 from ppci.target.basetarget import Label
9 from ppci.buildfunctions import link 9 from ppci.buildfunctions import link
10 10
11 11
12 class AssemblerLexingCase(unittest.TestCase): 12 class AssemblerLexingCase(unittest.TestCase):
13 """ Tests the assemblers lexer """ 13 """ Tests the assemblers lexer """
14 14
15 def setUp(self):
16 self.lexer = AsmLexer([])
17
18 def do(self, asmline, toks):
19 output = []
20 self.lexer.feed(asmline)
21 while 'EOF' not in output:
22 output.append(self.lexer.next_token().typ)
23 self.assertSequenceEqual(toks, output)
24
15 def testLex0(self): 25 def testLex0(self):
16 """ Check if the lexer is OK """ 26 """ Check if the lexer is OK """
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID', 'EOF'] 27 asmline = 'mov rax, rbx '
18 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks) 28 toks = ['ID', 'ID', ',', 'ID', 'EOF']
29 self.do(asmline, toks)
19 30
20 def testLex1(self): 31 def testLex1(self):
21 """ Test if lexer correctly maps some tokens """ 32 """ Test if lexer correctly maps some tokens """
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF'] 33 asmline = 'lab1: mov rax, rbx '
23 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks) 34 toks = ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF']
35 self.do(asmline, toks)
24 36
25 def testLex2(self): 37 def testLex2(self):
26 """ Test if lexer correctly maps some tokens """ 38 """ Test if lexer correctly maps some tokens """
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF'] 39 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
28 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks) 40 self.do(asmline, toks)
29 41
30 def testLex3(self): 42 def testLex3(self):
31 """ Test if lexer fails on a token that is invalid """ 43 """ Test if lexer fails on a token that is invalid """
32 asmline = '0z4: mov rax, rbx $ ' 44 asmline = '0z4: mov rax, rbx $ '
33 with self.assertRaises(CompilerError): 45 with self.assertRaises(CompilerError):
34 list(tokenize(asmline, [])) 46 self.do(asmline, [])
35 47
36 48
37 class OustreamTestCase(unittest.TestCase): 49 class OustreamTestCase(unittest.TestCase):
38 def test1(self): 50 def test1(self):
39 obj = ObjectFile() 51 obj = ObjectFile()