Home | History | Annotate | Download | only in tests
      1 from tests import TestCase, add
      2 from yasm import SymbolTable, Expression, YasmError
      3 
      4 class TSymbolTable(TestCase):
      5     def setUp(self):
      6         self.symtab = SymbolTable()
      7 
      8     def test_keys(self):
      9         self.assertEquals(len(self.symtab.keys()), 0)
     10         self.symtab.declare("foo", None, 0)
     11         keys = self.symtab.keys()
     12         self.assertEquals(len(keys), 1)
     13         self.assertEquals(keys[0], "foo")
     14 
     15     def test_contains(self):
     16         self.assert_("foo" not in self.symtab)
     17         self.symtab.declare("foo", None, 0)
     18         self.assert_("foo" in self.symtab)
     19 
     20     def test_exception(self):
     21         expr = Expression('+', 1, 2)
     22         self.symtab.define_equ("foo", expr, 0)
     23         self.assertRaises(YasmError, self.symtab.define_equ, "foo", expr, 0)
     24         self.symtab.define_equ("bar", expr, 0) # cleared
     25         self.assertRaises(YasmError, self.symtab.define_special, "bar",
     26                 'global')
     27 
     28     def test_iters(self):
     29         tab = self.symtab
     30         tab.declare("foo", None, 0)
     31         tab.declare("bar", None, 0)
     32         tab.declare("baz", None, 0)
     33 
     34         # while ordering is not known, it must be consistent
     35         self.assertEquals(list(tab.keys()), list(tab.iterkeys()))
     36         self.assertEquals(list(tab.values()), list(tab.itervalues()))
     37         self.assertEquals(list(tab.items()), list(tab.iteritems()))
     38         self.assertEquals(list(tab.iteritems()), zip(tab.keys(), tab.values()))
     39 
     40 add(TSymbolTable)
     41 
     42 class TSymbolAttr(TestCase):
     43     def setUp(self):
     44         self.symtab = SymbolTable()
     45         self.declsym = self.symtab.declare("foo", None, 0)
     46 
     47     def test_visibility(self):
     48         sym = self.symtab.declare("local1", None, 0)
     49         self.assertEquals(sym.visibility, set())
     50         sym = self.symtab.declare("local2", '', 0)
     51         self.assertEquals(sym.visibility, set())
     52         sym = self.symtab.declare("local3", 'local', 0)
     53         self.assertEquals(sym.visibility, set())
     54         sym = self.symtab.declare("global", 'global', 0)
     55         self.assertEquals(sym.visibility, set(['global']))
     56         sym = self.symtab.declare("common", 'common', 0)
     57         self.assertEquals(sym.visibility, set(['common']))
     58         sym = self.symtab.declare("extern", 'extern', 0)
     59         self.assertEquals(sym.visibility, set(['extern']))
     60         sym = self.symtab.declare("dlocal", 'dlocal', 0)
     61         self.assertEquals(sym.visibility, set(['dlocal']))
     62 
     63         self.assertRaises(ValueError,
     64                           lambda: self.symtab.declare("extern2", 'foo', 0))
     65     def test_name(self):
     66         self.assertEquals(self.declsym.name, "foo")
     67 
     68     def test_equ(self):
     69         self.assertRaises(AttributeError, lambda: self.declsym.equ)
     70 
     71     def test_label(self):
     72         self.assertRaises(AttributeError, lambda: self.declsym.label)
     73 
     74     def test_is_special(self):
     75         self.assertEquals(self.declsym.is_special, False)
     76 
     77     def test_is_curpos(self):
     78         self.assertEquals(self.declsym.is_curpos, False)
     79 
     80 add(TSymbolAttr)
     81