Put back trailing whitespace in doctest output.
[cython.git] / tests / run / unicodemethods.pyx
index 6882b11a618c454cad1bb8a20758104b2097868b..12da08446522c7597ad88aef836e02ace4981ab9 100644 (file)
@@ -2,6 +2,10 @@
 
 cimport cython
 
+import sys
+
+PY_VERSION = sys.version_info
+
 text = u'ab jd  sdflk as sa  sadas asdas fsdf '
 sep = u'  '
 
@@ -176,9 +180,12 @@ pipe_sep = u'|'
 
 @cython.test_fail_if_path_exists(
     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
-    "//CastNode", "//TypecastNode")
+    "//CastNode", "//TypecastNode",
+    "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
 @cython.test_assert_path_exists(
-    "//PythonCapiCallNode")
+    "//SimpleCallNode",
+    "//SimpleCallNode//NoneCheckNode",
+    "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
 def join(unicode sep, l):
     """
     >>> l = text.split()
@@ -193,9 +200,11 @@ def join(unicode sep, l):
 
 @cython.test_fail_if_path_exists(
     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
-    "//CastNode", "//TypecastNode", "//NoneCheckNode")
+    "//CastNode", "//TypecastNode", "//NoneCheckNode",
+    "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
 @cython.test_assert_path_exists(
-    "//PythonCapiCallNode")
+    "//SimpleCallNode",
+    "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
 def join_sep(l):
     """
     >>> l = text.split()
@@ -208,6 +217,22 @@ def join_sep(l):
     """
     return u'|'.join(l)
 
+@cython.test_assert_path_exists(
+    "//SimpleCallNode",
+    "//SimpleCallNode//NameNode")
+def join_unbound(unicode sep, l):
+    """
+    >>> l = text.split()
+    >>> len(l)
+    8
+    >>> print( pipe_sep.join(l) )
+    ab|jd|sdflk|as|sa|sadas|asdas|fsdf
+    >>> print( join_unbound(pipe_sep, l) )
+    ab|jd|sdflk|as|sa|sadas|asdas|fsdf
+    """
+    join = unicode.join
+    return join(sep, l)
+
 
 # unicode.startswith(s, prefix, [start, [end]])
 
@@ -228,12 +253,12 @@ def startswith(unicode s, sub):
     >>> startswith(text, 'ab X')
     'NO MATCH'
 
-    >>> text.startswith(('ab', 'ab '))
+    >>> PY_VERSION < (2,5) or text.startswith(('ab', 'ab '))
     True
     >>> startswith(text, ('ab', 'ab '))
     'MATCH'
-    >>> text.startswith((' ab', 'ab X'))
-    False
+    >>> PY_VERSION < (2,5) or not text.startswith((' ab', 'ab X'))
+    True
     >>> startswith(text, (' ab', 'ab X'))
     'NO MATCH'
     """
@@ -284,12 +309,12 @@ def endswith(unicode s, sub):
     >>> endswith(text, 'fsdf X')
     'NO MATCH'
 
-    >>> text.endswith(('fsdf', 'fsdf '))
+    >>> PY_VERSION < (2,5) or text.endswith(('fsdf', 'fsdf '))
     True
     >>> endswith(text, ('fsdf', 'fsdf '))
     'MATCH'
-    >>> text.endswith(('fsdf', 'fsdf X'))
-    False
+    >>> PY_VERSION < (2,5) or not text.endswith(('fsdf', 'fsdf X'))
+    True
     >>> endswith(text, ('fsdf', 'fsdf X'))
     'NO MATCH'
     """
@@ -315,12 +340,12 @@ def endswith_start_end(unicode s, sub, start, end):
     >>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
     'NO MATCH'
 
-    >>> text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
+    >>> PY_VERSION < (2,5) or text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
     True
     >>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
     'MATCH'
-    >>> text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
-    False
+    >>> PY_VERSION < (2,5) or not text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
+    True
     >>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
     'NO MATCH'
     """
@@ -434,3 +459,34 @@ def count_start_end(unicode s, substring, start, end):
     """
     cdef Py_ssize_t pos = s.count(substring, start, end)
     return pos
+
+
+# unicode.replace(s, sub, repl, [maxcount])
+
+@cython.test_fail_if_path_exists(
+    "//CoerceFromPyTypeNode",
+    "//CastNode", "//TypecastNode")
+@cython.test_assert_path_exists(
+    "//PythonCapiCallNode")
+def replace(unicode s, substring, repl):
+    """
+    >>> print( text.replace('sa', 'SA') )
+    ab jd  sdflk as SA  SAdas asdas fsdf 
+    >>> print( replace(text, 'sa', 'SA') )
+    ab jd  sdflk as SA  SAdas asdas fsdf 
+    """
+    return s.replace(substring, repl)
+
+@cython.test_fail_if_path_exists(
+    "//CastNode", "//TypecastNode")
+@cython.test_assert_path_exists(
+    "//CoerceFromPyTypeNode",
+    "//PythonCapiCallNode")
+def replace_maxcount(unicode s, substring, repl, maxcount):
+    """
+    >>> print( text.replace('sa', 'SA', 1) )
+    ab jd  sdflk as SA  sadas asdas fsdf 
+    >>> print( replace_maxcount(text, 'sa', 'SA', 1) )
+    ab jd  sdflk as SA  sadas asdas fsdf 
+    """
+    return s.replace(substring, repl, maxcount)