diff test/testc3.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents 05184b95fa16
children a747a45dcd78
line wrap: on
line diff
--- a/test/testc3.py	Fri Nov 15 13:52:32 2013 +0100
+++ b/test/testc3.py	Thu Nov 21 11:57:27 2013 +0100
@@ -1,7 +1,11 @@
 import c3
-import time, ppci, x86, ir
+import time
+import ppci
+import x86
+import ir
 import unittest
 import glob
+import io
 
 testsrc = """module test;
 
@@ -64,29 +68,30 @@
 
 class testLexer(unittest.TestCase):
     def testUnexpectedCharacter(self):
-        snippet = """ var s \u6c34 """
+        snippet = io.StringIO(""" var s \u6c34 """)
         with self.assertRaises(ppci.CompilerError):
             list(c3.lexer.tokenize(snippet))
 
     def testBlockComment(self):
-        snippet = """
+        snippet = io.StringIO("""
           /* Demo */
           var int x = 0;
-        """
+        """)
         toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
         self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)
 
     def testBlockCommentMultiLine(self):
-        snippet = """
+        snippet = io.StringIO("""
           /* Demo
           bla1
           bla2
           */
           var int x = 0;
-        """
+        """)
         toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
         self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)
 
+
 class testBuilder(unittest.TestCase):
     def setUp(self):
         self.diag = ppci.DiagnosticsManager()
@@ -98,33 +103,35 @@
 
     def expectErrors(self, snippet, rows):
         """ Helper to test for expected errors on rows """
-        ircode = self.builder.build(snippet)
+        ircode = list(self.builder.build([io.StringIO(snippet)]))
         actualErrors = [err.row for err in self.diag.diags]
         if rows != actualErrors:
             self.diag.printErrors(snippet)
         self.assertSequenceEqual(rows, actualErrors)
-        self.assertFalse(ircode)
+        # self.assertFalse(all(ircode))
 
-    def expectOK(self, snippet, pack_dir=None):
-        ircode = self.builder.build(snippet, pack_dir=pack_dir)
+    def expectOK(self, snippet):
+        if type(snippet) is list:
+            ircode = self.builder.build(snippet)
+        else:
+            ircode = self.builder.build([io.StringIO(snippet)])
         if not ircode:
             self.diag.printErrors(snippet)
-        self.assertTrue(ircode)
+        self.assertTrue(all(ircode))
         return ircode
 
     def testPackage(self):
         p1 = """module p1;
         type int A;
         """
-        self.assertTrue(self.builder.build(p1))
         p2 = """module p2;
         import p1;
         var A b;
         """
-        self.expectOK(p2)
+        self.expectOK([io.StringIO(s) for s in (p1, p2)])
 
     def testFunctArgs(self):
-      snippet = """
+        snippet = """
          module testargs;
          function void t2(int a, double b)
          {
@@ -132,11 +139,11 @@
             t2(2);
             t2(1, 1.2);
          }
-      """
-      self.expectErrors(snippet, [5, 6])
+        """
+        self.expectErrors(snippet, [5, 6])
 
     def testExpressions(self):
-      snippet = """
+        snippet = """
          module test;
          function void t(int a, double b)
          {
@@ -147,11 +154,11 @@
             c = a;
             c = b > 1;
          }
-      """
-      self.expectErrors(snippet, [8, 9, 10])
+        """
+        self.expectErrors(snippet, [8, 9, 10])
 
     def testExpression1(self):
-      snippet = """
+        snippet = """
          module testexpr1;
          function void t()
          {
@@ -160,34 +167,34 @@
             b = a * 2 + a * a;
             c = b * a - 3;
          }
-      """
-      self.expectOK(snippet)
+        """
+        self.expectOK(snippet)
 
     def testEmpty(self):
-      snippet = """
-      module A
-      """
-      self.expectErrors(snippet, [3])
+        snippet = """
+        module A
+        """
+        self.expectErrors(snippet, [3])
 
     def testEmpty2(self):
-      snippet = ""
-      self.expectErrors(snippet, [1])
+        snippet = ""
+        self.expectErrors(snippet, [1])
 
     def testRedefine(self):
-      snippet = """
-      module test;
-      var int a;
-      var int b;
-      var int a;
-      """
-      self.expectErrors(snippet, [5])
+        snippet = """
+        module test;
+        var int a;
+        var int b;
+        var int a;
+        """
+        self.expectErrors(snippet, [5])
 
     def testWhile(self):
-      snippet = """
-      module tstwhile;
-      var int a;
-      function void t()
-      {
+        snippet = """
+        module tstwhile;
+        var int a;
+        function void t()
+         {
          var int i = 0;
          while (i < 1054)
          {
@@ -202,15 +209,15 @@
          while(false)
          {
          }
-      }
-      """
-      self.expectOK(snippet)
+        }
+        """
+        self.expectOK(snippet)
 
     def testIf(self):
         snippet = """
-      module tstIFF;
-      function void t(int b)
-      {
+        module tstIFF;
+        function void t(int b)
+         {
          var int a;
          a = 2;
          if (a > b)
@@ -226,7 +233,7 @@
          }
 
          return b;
-      }
+        }
         """
         self.expectOK(snippet)
 
@@ -426,8 +433,7 @@
         }
 
         """
-        ircode = self.expectOK(snippet)
-        self.assertEqual(1, len(ircode.Functions))
+        self.expectOK(snippet)
 
 if __name__ == '__main__':
     unittest.main()