Some unit tests for the build system.
authorRobert Bradshaw <robertwb@math.washington.edu>
Sat, 11 Dec 2010 00:04:51 +0000 (16:04 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sat, 11 Dec 2010 00:04:51 +0000 (16:04 -0800)
Cython/Build/Dependencies.py
Cython/Build/Tests/TestStripLiterals.py [new file with mode: 0644]
Cython/Build/Tests/__init__.py [new file with mode: 0644]
setup.py

index 207f72cdf8ee53c9492e79ee5207041fb8e0f1a4..a2ff4bfb98b5f558c4cc9e3ef254e7960b48d8dc 100644 (file)
@@ -168,7 +168,7 @@ def strip_string_literals(code, prefix='__Pyx_L'):
 
         # Try to close the quote.
         elif in_quote:
-            if code[q-1] == '\\':
+            if code[q-1] == '\\' and not raw:
                 k = 2
                 while q >= k and code[q-k] == '\\':
                     k += 1
@@ -177,7 +177,7 @@ def strip_string_literals(code, prefix='__Pyx_L'):
                     continue
             if code[q:q+len(in_quote)] == in_quote:
                 counter += 1
-                label = "%s%s" % (prefix, counter)
+                label = "%s%s_" % (prefix, counter)
                 literals[label] = code[start+len(in_quote):q]
                 new_code.append("%s%s%s" % (in_quote, label, in_quote))
                 q += len(in_quote)
@@ -193,7 +193,7 @@ def strip_string_literals(code, prefix='__Pyx_L'):
                 end = None
             new_code.append(code[start:hash_mark+1])
             counter += 1
-            label = "%s%s" % (prefix, counter)
+            label = "%s%s_" % (prefix, counter)
             literals[label] = code[hash_mark+1:end]
             new_code.append(label)
             if end is None:
@@ -208,11 +208,11 @@ def strip_string_literals(code, prefix='__Pyx_L'):
                 in_quote = code[q]*3
             else:
                 in_quote = code[q]
-            end = q
-            while end>0 and code[end-1] in 'rRbBuU':
-                if code[end-1] in 'rR':
+            end = marker = q
+            while marker > 0 and code[marker-1] in 'rRbBuU':
+                if code[marker-1] in 'rR':
                     raw = True
-                end -= 1
+                marker -= 1
             new_code.append(code[start:end])
             start = q
             q += len(in_quote)
diff --git a/Cython/Build/Tests/TestStripLiterals.py b/Cython/Build/Tests/TestStripLiterals.py
new file mode 100644 (file)
index 0000000..a589379
--- /dev/null
@@ -0,0 +1,57 @@
+from Cython.Build.Dependencies import strip_string_literals
+
+from Cython.TestUtils import CythonTest
+
+class TestStripLiterals(CythonTest):
+
+    def t(self, before, expected):
+        actual, literals = strip_string_literals(before, prefix="_L")
+        self.assertEquals(expected, actual)
+        for key, value in literals.items():
+            actual = actual.replace(key, value)
+        self.assertEquals(before, actual)
+
+    def test_empty(self):
+        self.t("", "")
+
+    def test_single_quote(self):
+        self.t("'x'", "'_L1_'")
+
+    def test_double_quote(self):
+        self.t('"x"', '"_L1_"')
+
+    def test_nested_quotes(self):
+        self.t(""" '"' "'" """, """ '_L1_' "_L2_" """)
+
+    def test_triple_quote(self):
+        self.t(" '''a\n''' ", " '''_L1_''' ")
+
+    def test_backslash(self):
+        self.t(r"'a\'b'", "'_L1_'")
+        self.t(r"'a\\'", "'_L1_'")
+        self.t(r"'a\\\'b'", "'_L1_'")
+
+    def test_unicode(self):
+        self.t("u'abc'", "u'_L1_'")
+
+    def test_raw(self):
+        self.t(r"r'abc\'", "r'_L1_'")
+
+    def test_raw_unicode(self):
+        self.t(r"ru'abc\'", "ru'_L1_'")
+
+    def test_comment(self):
+        self.t("abc # foo", "abc #_L1_")
+
+    def test_comment_and_quote(self):
+        self.t("abc # 'x'", "abc #_L1_")
+        self.t("'abc#'", "'_L1_'")
+
+    def test_include(self):
+        self.t("include 'a.pxi' # something here",
+               "include '_L1_' #_L2_")
+
+    def test_extern(self):
+        self.t("cdef extern from 'a.h': # comment",
+               "cdef extern from '_L1_': #_L2_")
+
diff --git a/Cython/Build/Tests/__init__.py b/Cython/Build/Tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
index 00ba4dd5aa0aeb784be5ec6f99348e258218c03f..a816e6354077984e727a5bd547237bfefb278dc2 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -264,6 +264,7 @@ packages = [
     'Cython.Distutils',
     'Cython.Plex',
     'Cython.Tests',
+    'Cython.Build.Tests',
     'Cython.Compiler.Tests',
 ]