Procházet zdrojové kódy

Added some unit tests for utils.

Taddeus Kroes před 14 roky
rodič
revize
094a99f116
3 změnil soubory, kde provedl 28 přidání a 11 odebrání
  1. 1 0
      .gitignore
  2. 4 11
      src/utils.py
  3. 23 0
      tests/test_utils.py

+ 1 - 0
.gitignore

@@ -1,5 +1,6 @@
 *.swp
 *.pdf
+*.pyc
 *~
 .coverage
 coverage/

+ 4 - 11
src/utils.py

@@ -51,10 +51,10 @@ class Statement:
         return self.is_command() \
                and re.match('^(add|sub|mult|div|abs|neg)(u|\.d)?$', self.name)
 
-    def jump_target(self, arg):
+    def jump_target(self):
         """Get the jump target of this statement."""
-        if re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', self.name):
-            return self[1]
+        if self.is_jump():
+            return self[-1]
         else:
             raise Exception('Command "%s" has no jump target' % self.name)
 
@@ -66,7 +66,7 @@ class Statement:
         if self.is_load() or self.is_arith():
             return [self[0]]
 
-    def get_use(self, arg):
+    def get_use(self):
         """Get the use[S] of this statement."""
         return []
 
@@ -157,10 +157,3 @@ def find_basic_blocks(statements):
     blocks.append(Block(statements[leaders[-1]:]))
 
     return blocks
-
-
-#while not block.end():
-#    i, s = block.read()
-#
-#    if block.peek():
-#        block.replace(i, i + 3, [nieuwe statements])

+ 23 - 0
tests/test_utils.py

@@ -20,3 +20,26 @@ class TestUtils(unittest.TestCase):
         self.assertEqual(map(lambda b: b.statements, find_basic_blocks(s)), \
                 [B(s[:2]).statements, B(s[2:4]).statements, \
                  B(s[4:]).statements])
+
+    def test_eq(self):
+        self.assertTrue(S('command', 'foo') == S('command', 'foo'))
+        self.assertFalse(S('command', 'foo') == S('command', 'bar'))
+
+    def test_stype(self):
+        self.assertTrue(S('comment', 'foo', inline=False).is_comment())
+        self.assertTrue(S('directive', 'foo').is_directive())
+        self.assertTrue(S('label', 'foo').is_label())
+        self.assertTrue(S('command', 'foo').is_command())
+
+        self.assertFalse(S('command', 'foo').is_comment())
+        self.assertFalse(S('label', 'foo').is_directive())
+        self.assertFalse(S('comment', 'foo', inline=False).is_label())
+        self.assertFalse(S('directive', 'foo').is_command())
+
+    def test_is_inline_comment(self):
+        self.assertTrue(S('comment', 'foo', inline=True).is_inline_comment())
+        self.assertFalse(S('comment', 'foo', inline=False).is_inline_comment())
+
+    def test_jump_target(self):
+        self.assertEqual(S('command', 'j', 'foo').jump_target(), 'foo')
+        self.assertRaises(Exception, S('command', 'foo').jump_target)