extended test case to make sure the unicode method optimisations get applied
[cython.git] / tests / run / unicodemethods.pyx
1 # -*- coding: utf-8 -*-
2
3 cimport cython
4
5 text = u'ab jd  sdflk as sa  sadas asdas fsdf '
6 sep = u'  '
7
8 multiline_text = u'''\
9 ab jd
10 sdflk as sa
11 sadas asdas fsdf '''
12
13 def print_all(l):
14     for s in l:
15         print(s)
16
17
18 # unicode.split(s, [sep, [maxsplit]])
19
20 @cython.test_assert_path_exists(
21     "//PythonCapiCallNode")
22 def split(unicode s):
23     """
24     >>> print_all( text.split() )
25     ab
26     jd
27     sdflk
28     as
29     sa
30     sadas
31     asdas
32     fsdf
33     >>> print_all( split(text) )
34     ab
35     jd
36     sdflk
37     as
38     sa
39     sadas
40     asdas
41     fsdf
42     """
43     return s.split()
44
45 @cython.test_assert_path_exists(
46     "//PythonCapiCallNode")
47 def split_sep(unicode s, sep):
48     """
49     >>> print_all( text.split(sep) )
50     ab jd
51     sdflk as sa
52     sadas asdas fsdf 
53     >>> print_all( split_sep(text, sep) )
54     ab jd
55     sdflk as sa
56     sadas asdas fsdf 
57     """
58     return s.split(sep)
59
60 @cython.test_fail_if_path_exists(
61     "//CoerceToPyTypeNode",
62     "//CastNode", "//TypecastNode")
63 @cython.test_assert_path_exists(
64     "//CoerceFromPyTypeNode",
65     "//PythonCapiCallNode")
66 def split_sep_max(unicode s, sep, max):
67     """
68     >>> print_all( text.split(sep, 1) )
69     ab jd
70     sdflk as sa  sadas asdas fsdf 
71     >>> print_all( split_sep_max(text, sep, 1) )
72     ab jd
73     sdflk as sa  sadas asdas fsdf 
74     """
75     return s.split(sep, max)
76
77 @cython.test_fail_if_path_exists(
78     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
79     "//CastNode", "//TypecastNode")
80 @cython.test_assert_path_exists(
81     "//PythonCapiCallNode")
82 def split_sep_max_int(unicode s, sep):
83     """
84     >>> print_all( text.split(sep, 1) )
85     ab jd
86     sdflk as sa  sadas asdas fsdf 
87     >>> print_all( split_sep_max_int(text, sep) )
88     ab jd
89     sdflk as sa  sadas asdas fsdf 
90     """
91     return s.split(sep, 1)
92
93
94 # unicode.splitlines(s, [keepends])
95
96 @cython.test_assert_path_exists(
97     "//PythonCapiCallNode")
98 def splitlines(unicode s):
99     """
100     >>> len(multiline_text.splitlines())
101     3
102     >>> print_all( multiline_text.splitlines() )
103     ab jd
104     sdflk as sa
105     sadas asdas fsdf 
106     >>> len(splitlines(multiline_text))
107     3
108     >>> print_all( splitlines(multiline_text) )
109     ab jd
110     sdflk as sa
111     sadas asdas fsdf 
112     """
113     return s.splitlines()
114
115 @cython.test_assert_path_exists(
116     "//PythonCapiCallNode")
117 def splitlines_keep(unicode s, keep):
118     """
119     >>> len(multiline_text.splitlines(True))
120     3
121     >>> print_all( multiline_text.splitlines(True) )
122     ab jd
123     <BLANKLINE>
124     sdflk as sa
125     <BLANKLINE>
126     sadas asdas fsdf 
127     >>> len(splitlines_keep(multiline_text, True))
128     3
129     >>> print_all( splitlines_keep(multiline_text, True) )
130     ab jd
131     <BLANKLINE>
132     sdflk as sa
133     <BLANKLINE>
134     sadas asdas fsdf 
135     """
136     return s.splitlines(keep)
137
138 @cython.test_fail_if_path_exists(
139 # boolean conversion isn't currently smart enough for this ...
140 #    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
141     "//CastNode", "//TypecastNode")
142 @cython.test_assert_path_exists(
143     "//PythonCapiCallNode")
144 def splitlines_keep_bint(unicode s):
145     """
146     >>> len(multiline_text.splitlines(True))
147     3
148     >>> print_all( multiline_text.splitlines(True) )
149     ab jd
150     <BLANKLINE>
151     sdflk as sa
152     <BLANKLINE>
153     sadas asdas fsdf 
154     >>> print_all( multiline_text.splitlines(False) )
155     ab jd
156     sdflk as sa
157     sadas asdas fsdf 
158     >>> len(splitlines_keep_bint(multiline_text))
159     7
160     >>> print_all( splitlines_keep_bint(multiline_text) )
161     ab jd
162     <BLANKLINE>
163     sdflk as sa
164     <BLANKLINE>
165     sadas asdas fsdf 
166     --
167     ab jd
168     sdflk as sa
169     sadas asdas fsdf 
170     """
171     return s.splitlines(True) + ['--'] + s.splitlines(False)
172
173
174 # unicode.join(s, iterable)
175
176 pipe_sep = u'|'
177
178 @cython.test_fail_if_path_exists(
179     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
180     "//CastNode", "//TypecastNode")
181 @cython.test_assert_path_exists(
182     "//PythonCapiCallNode")
183 def join(unicode sep, l):
184     """
185     >>> l = text.split()
186     >>> len(l)
187     8
188     >>> print( pipe_sep.join(l) )
189     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
190     >>> print( join(pipe_sep, l) )
191     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
192     """
193     return sep.join(l)
194
195 @cython.test_fail_if_path_exists(
196     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
197     "//CastNode", "//TypecastNode", "//NoneCheckNode")
198 @cython.test_assert_path_exists(
199     "//PythonCapiCallNode")
200 def join_sep(l):
201     """
202     >>> l = text.split()
203     >>> len(l)
204     8
205     >>> print( '|'.join(l) )
206     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
207     >>> print( join_sep(l) )
208     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
209     """
210     return u'|'.join(l)