From: Robert Bradshaw Date: Sat, 11 Dec 2010 00:04:51 +0000 (-0800) Subject: Some unit tests for the build system. X-Git-Tag: 0.14.rc0~9 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=efe9d6a92a776bf718063996a23a5e6213da3033;p=cython.git Some unit tests for the build system. --- diff --git a/Cython/Build/Dependencies.py b/Cython/Build/Dependencies.py index 207f72cd..a2ff4bfb 100644 --- a/Cython/Build/Dependencies.py +++ b/Cython/Build/Dependencies.py @@ -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 index 00000000..a5893798 --- /dev/null +++ b/Cython/Build/Tests/TestStripLiterals.py @@ -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 index 00000000..e69de29b diff --git a/setup.py b/setup.py index 00ba4dd5..a816e635 100644 --- a/setup.py +++ b/setup.py @@ -264,6 +264,7 @@ packages = [ 'Cython.Distutils', 'Cython.Plex', 'Cython.Tests', + 'Cython.Build.Tests', 'Cython.Compiler.Tests', ]