Commit e4cd5f18 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by GitHub

Add tests for 'None' value argument in optimised string methods. (GH-4743)

Also refactor the tests to (always) use a direct comparison between the Python generated result and the result from the optimised Cython code.

Adds tests for https://github.com/cython/cython/pull/4740
See https://github.com/cython/cython/issues/4737
parent b6ca1a2e
...@@ -26,16 +26,12 @@ def print_all(l): ...@@ -26,16 +26,12 @@ def print_all(l):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def split(unicode s): def split(unicode s):
""" """
>>> print_all( text.split() ) >>> def test_split():
ab ... py = text.split()
jd ... cy = split(text)
sdflk ... assert py == cy, (py, cy)
as ... return cy
sa >>> print_all( test_split() )
sadas
asdas
fsdf
>>> print_all( split(text) )
ab ab
jd jd
sdflk sdflk
...@@ -51,24 +47,16 @@ def split(unicode s): ...@@ -51,24 +47,16 @@ def split(unicode s):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def split_sep(unicode s, sep): def split_sep(unicode s, sep):
""" """
>>> print_all( text.split(sep) ) >>> def test_split_sep(sep):
... py = text.split(sep)
... cy = split_sep(text, sep)
... assert py == cy, (py, cy)
... return cy
>>> print_all( test_split_sep(sep) )
ab jd ab jd
sdflk as sa sdflk as sa
sadas asdas fsdf\x20 sadas asdas fsdf\x20
>>> print_all( split_sep(text, sep) ) >>> print_all( test_split_sep(None) )
ab jd
sdflk as sa
sadas asdas fsdf\x20
>>> print_all( text.split(None) )
ab
jd
sdflk
as
sa
sadas
asdas
fsdf
>>> print_all( split_sep(text, None) )
ab ab
jd jd
sdflk sdflk
...@@ -88,11 +76,17 @@ def split_sep(unicode s, sep): ...@@ -88,11 +76,17 @@ def split_sep(unicode s, sep):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def split_sep_max(unicode s, sep, max): def split_sep_max(unicode s, sep, max):
""" """
>>> print_all( text.split(sep, 1) ) >>> def test_split_sep_max(sep, max):
... py = text.split(sep, max)
... cy = split_sep_max(text, sep, max)
... assert py == cy, (py, cy)
... return cy
>>> print_all( test_split_sep_max(sep, 1) )
ab jd ab jd
sdflk as sa sadas asdas fsdf\x20 sdflk as sa sadas asdas fsdf\x20
>>> print_all( split_sep_max(text, sep, 1) ) >>> print_all( test_split_sep_max(None, 2) )
ab jd ab
jd
sdflk as sa sadas asdas fsdf\x20 sdflk as sa sadas asdas fsdf\x20
>>> print_all( text.split(None, 2) ) >>> print_all( text.split(None, 2) )
ab ab
...@@ -112,16 +106,15 @@ def split_sep_max(unicode s, sep, max): ...@@ -112,16 +106,15 @@ def split_sep_max(unicode s, sep, max):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def split_sep_max_int(unicode s, sep): def split_sep_max_int(unicode s, sep):
""" """
>>> print_all( text.split(sep, 1) ) >>> def test_split_sep_max_int(sep):
ab jd ... py = text.split(sep, 1)
sdflk as sa sadas asdas fsdf\x20 ... cy = split_sep_max_int(text, sep)
>>> print_all( split_sep_max_int(text, sep) ) ... assert py == cy, (py, cy)
... return cy
>>> print_all( test_split_sep_max_int(sep) )
ab jd ab jd
sdflk as sa sadas asdas fsdf\x20 sdflk as sa sadas asdas fsdf\x20
>>> print_all( text.split(None, 1) ) >>> print_all( test_split_sep_max_int(None) )
ab
jd sdflk as sa sadas asdas fsdf\x20
>>> print_all( split_sep_max_int(text, None) )
ab ab
jd sdflk as sa sadas asdas fsdf\x20 jd sdflk as sa sadas asdas fsdf\x20
""" """
...@@ -134,15 +127,14 @@ def split_sep_max_int(unicode s, sep): ...@@ -134,15 +127,14 @@ def split_sep_max_int(unicode s, sep):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def splitlines(unicode s): def splitlines(unicode s):
""" """
>>> len(multiline_text.splitlines()) >>> def test_splitlines(s):
... py = s.splitlines()
... cy = splitlines(s)
... assert py == cy, (py, cy)
... return cy
>>> len(test_splitlines(multiline_text))
3 3
>>> print_all( multiline_text.splitlines() ) >>> print_all( test_splitlines(multiline_text) )
ab jd
sdflk as sa
sadas asdas fsdf\x20
>>> len(splitlines(multiline_text))
3
>>> print_all( splitlines(multiline_text) )
ab jd ab jd
sdflk as sa sdflk as sa
sadas asdas fsdf\x20 sadas asdas fsdf\x20
...@@ -153,17 +145,14 @@ def splitlines(unicode s): ...@@ -153,17 +145,14 @@ def splitlines(unicode s):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def splitlines_keep(unicode s, keep): def splitlines_keep(unicode s, keep):
""" """
>>> len(multiline_text.splitlines(True)) >>> def test_splitlines_keep(s, keep):
... py = s.splitlines(keep)
... cy = splitlines_keep(s, keep)
... assert py == cy, (py, cy)
... return cy
>>> len(test_splitlines_keep(multiline_text, True))
3 3
>>> print_all( multiline_text.splitlines(True) ) >>> print_all( test_splitlines_keep(multiline_text, True) )
ab jd
<BLANKLINE>
sdflk as sa
<BLANKLINE>
sadas asdas fsdf\x20
>>> len(splitlines_keep(multiline_text, True))
3
>>> print_all( splitlines_keep(multiline_text, True) )
ab jd ab jd
<BLANKLINE> <BLANKLINE>
sdflk as sa sdflk as sa
...@@ -179,21 +168,14 @@ def splitlines_keep(unicode s, keep): ...@@ -179,21 +168,14 @@ def splitlines_keep(unicode s, keep):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def splitlines_keep_bint(unicode s): def splitlines_keep_bint(unicode s):
""" """
>>> len(multiline_text.splitlines(True)) >>> def test_splitlines_keep_bint(s):
3 ... py = s.splitlines(True) + ['--'] + s.splitlines(False)
>>> print_all( multiline_text.splitlines(True) ) ... cy = splitlines_keep_bint(s)
ab jd ... assert py == cy, (py, cy)
<BLANKLINE> ... return cy
sdflk as sa >>> len(test_splitlines_keep_bint(multiline_text))
<BLANKLINE>
sadas asdas fsdf\x20
>>> print_all( multiline_text.splitlines(False) )
ab jd
sdflk as sa
sadas asdas fsdf\x20
>>> len(splitlines_keep_bint(multiline_text))
7 7
>>> print_all( splitlines_keep_bint(multiline_text) ) >>> print_all( test_splitlines_keep_bint(multiline_text) )
ab jd ab jd
<BLANKLINE> <BLANKLINE>
sdflk as sa sdflk as sa
...@@ -220,12 +202,15 @@ pipe_sep = u'|' ...@@ -220,12 +202,15 @@ pipe_sep = u'|'
) )
def join(unicode sep, l): def join(unicode sep, l):
""" """
>>> def test_join(sep, l):
... py = sep.join(l)
... cy = join(sep, l)
... assert py == cy, (py, cy)
... return cy
>>> l = text.split() >>> l = text.split()
>>> len(l) >>> len(l)
8 8
>>> print( pipe_sep.join(l) ) >>> print( test_join(pipe_sep, l) )
ab|jd|sdflk|as|sa|sadas|asdas|fsdf
>>> print( join(pipe_sep, l) )
ab|jd|sdflk|as|sa|sadas|asdas|fsdf ab|jd|sdflk|as|sa|sadas|asdas|fsdf
""" """
return sep.join(l) return sep.join(l)
...@@ -240,12 +225,15 @@ def join(unicode sep, l): ...@@ -240,12 +225,15 @@ def join(unicode sep, l):
) )
def join_sep(l): def join_sep(l):
""" """
>>> def test_join_sep(l):
... py = '|'.join(l)
... cy = join_sep(l)
... assert py == cy, (py, cy)
... return cy
>>> l = text.split() >>> l = text.split()
>>> len(l) >>> len(l)
8 8
>>> print( '|'.join(l) ) >>> print( test_join_sep(l) )
ab|jd|sdflk|as|sa|sadas|asdas|fsdf
>>> print( join_sep(l) )
ab|jd|sdflk|as|sa|sadas|asdas|fsdf ab|jd|sdflk|as|sa|sadas|asdas|fsdf
""" """
result = u'|'.join(l) result = u'|'.join(l)
...@@ -264,12 +252,15 @@ def join_sep(l): ...@@ -264,12 +252,15 @@ def join_sep(l):
) )
def join_sep_genexpr(l): def join_sep_genexpr(l):
""" """
>>> def test_join_sep_genexpr(l):
... py = '|'.join(s + ' ' for s in l)
... cy = join_sep_genexpr(l)
... assert py == cy, (py, cy)
... return cy
>>> l = text.split() >>> l = text.split()
>>> len(l) >>> len(l)
8 8
>>> print( '<<%s>>' % '|'.join(s + ' ' for s in l) ) >>> print( '<<%s>>' % test_join_sep_genexpr(l) )
<<ab |jd |sdflk |as |sa |sadas |asdas |fsdf >>
>>> print( '<<%s>>' % join_sep_genexpr(l) )
<<ab |jd |sdflk |as |sa |sadas |asdas |fsdf >> <<ab |jd |sdflk |as |sa |sadas |asdas |fsdf >>
""" """
result = u'|'.join(s + u' ' for s in l) result = u'|'.join(s + u' ' for s in l)
...@@ -287,11 +278,14 @@ def join_sep_genexpr(l): ...@@ -287,11 +278,14 @@ def join_sep_genexpr(l):
) )
def join_sep_genexpr_dictiter(dict d): def join_sep_genexpr_dictiter(dict d):
""" """
>>> def test_join_sep_genexpr_dictiter(d):
... py = '|'.join( sorted(' '.join('%s:%s' % (k, v) for k, v in d.items()).split()) )
... cy = '|'.join( sorted(join_sep_genexpr_dictiter(d).split()) )
... assert py == cy, (py, cy)
... return cy
>>> l = text.split() >>> l = text.split()
>>> d = dict(zip(range(len(l)), l)) >>> d = dict(zip(range(len(l)), l))
>>> print('|'.join( sorted(' '.join('%s:%s' % (k, v) for k, v in d.items()).split()) )) >>> print( test_join_sep_genexpr_dictiter(d) )
0:ab|1:jd|2:sdflk|3:as|4:sa|5:sadas|6:asdas|7:fsdf
>>> print('|'.join( sorted(join_sep_genexpr_dictiter(d).split())) )
0:ab|1:jd|2:sdflk|3:as|4:sa|5:sadas|6:asdas|7:fsdf 0:ab|1:jd|2:sdflk|3:as|4:sa|5:sadas|6:asdas|7:fsdf
""" """
result = u' '.join('%s:%s' % (k, v) for k, v in d.iteritems()) result = u' '.join('%s:%s' % (k, v) for k, v in d.iteritems())
...@@ -304,12 +298,15 @@ def join_sep_genexpr_dictiter(dict d): ...@@ -304,12 +298,15 @@ def join_sep_genexpr_dictiter(dict d):
) )
def join_unbound(unicode sep, l): def join_unbound(unicode sep, l):
""" """
>>> def test_join_unbound(sep, l):
... py = sep.join(l)
... cy = join_unbound(sep, l)
... assert py == cy, (py, cy)
... return cy
>>> l = text.split() >>> l = text.split()
>>> len(l) >>> len(l)
8 8
>>> print( pipe_sep.join(l) ) >>> print( test_join_unbound(pipe_sep, l) )
ab|jd|sdflk|as|sa|sadas|asdas|fsdf
>>> print( join_unbound(pipe_sep, l) )
ab|jd|sdflk|as|sa|sadas|asdas|fsdf ab|jd|sdflk|as|sa|sadas|asdas|fsdf
""" """
join = unicode.join join = unicode.join
...@@ -326,28 +323,25 @@ def join_unbound(unicode sep, l): ...@@ -326,28 +323,25 @@ def join_unbound(unicode sep, l):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def startswith(unicode s, sub): def startswith(unicode s, sub):
""" """
>>> text.startswith('ab ') >>> def test_startswith(s, sub):
... py = s.startswith(sub)
... cy = startswith(s, sub)
... assert py == cy, (py, cy)
... return cy
>>> test_startswith(text, 'ab ')
True True
>>> startswith(text, 'ab ') >>> test_startswith(text, 'ab X')
'MATCH'
>>> text.startswith('ab X')
False False
>>> startswith(text, 'ab X')
'NO MATCH'
>>> text.startswith(('ab', 'ab ')) >>> test_startswith(text, ('ab', 'ab '))
True True
>>> startswith(text, ('ab', 'ab ')) >>> not test_startswith(text, (' ab', 'ab X'))
'MATCH'
>>> not text.startswith((' ab', 'ab X'))
True True
>>> startswith(text, (' ab', 'ab X'))
'NO MATCH'
""" """
if s.startswith(sub): if s.startswith(sub):
return 'MATCH' return True
else: else:
return 'NO MATCH' return False
@cython.test_fail_if_path_exists( @cython.test_fail_if_path_exists(
"//CoerceToPyTypeNode", "//CoerceToPyTypeNode",
...@@ -357,28 +351,33 @@ def startswith(unicode s, sub): ...@@ -357,28 +351,33 @@ def startswith(unicode s, sub):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def startswith_start_end(unicode s, sub, start, end): def startswith_start_end(unicode s, sub, start, end):
""" """
>>> text.startswith('b ', 1, 5) >>> def test_startswith_start_end(s, sub, start, end):
... py = s.startswith(sub, start, end)
... cy = startswith_start_end(s, sub, start, end)
... assert py == cy, (py, cy)
... return cy
>>> test_startswith_start_end(text, 'b ', 1, 5)
True True
>>> startswith_start_end(text, 'b ', 1, 5) >>> test_startswith_start_end(text, 'ab ', -1000, 5000)
'MATCH'
>>> text.startswith('ab ', -1000, 5000)
True True
>>> startswith_start_end(text, 'ab ', -1000, 5000) >>> test_startswith_start_end(text, 'b X', 1, 5)
'MATCH'
>>> text.startswith('b X', 1, 5)
False False
>>> startswith_start_end(text, 'b X', 1, 5)
'NO MATCH'
>>> text.startswith('ab ', None, None) >>> test_startswith_start_end(text, 'ab ', None, None)
True
>>> test_startswith_start_end(text, 'ab ', 1, None)
False
>>> test_startswith_start_end(text, 'b ', 1, None)
True
>>> test_startswith_start_end(text, 'ab ', None, 3)
True True
>>> startswith_start_end(text, 'ab ', None, None) >>> test_startswith_start_end(text, 'ab ', None, 2)
'MATCH' False
""" """
if s.startswith(sub, start, end): if s.startswith(sub, start, end):
return 'MATCH' return True
else: else:
return 'NO MATCH' return False
# unicode.endswith(s, prefix, [start, [end]]) # unicode.endswith(s, prefix, [start, [end]])
...@@ -391,28 +390,25 @@ def startswith_start_end(unicode s, sub, start, end): ...@@ -391,28 +390,25 @@ def startswith_start_end(unicode s, sub, start, end):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def endswith(unicode s, sub): def endswith(unicode s, sub):
""" """
>>> text.endswith('fsdf ') >>> def test_endswith(s, sub):
... py = s.endswith(sub)
... cy = endswith(s, sub)
... assert py == cy, (py, cy)
... return cy
>>> test_endswith(text, 'fsdf ')
True True
>>> endswith(text, 'fsdf ') >>> test_endswith(text, 'fsdf X')
'MATCH'
>>> text.endswith('fsdf X')
False False
>>> endswith(text, 'fsdf X')
'NO MATCH'
>>> text.endswith(('fsdf', 'fsdf ')) >>> test_endswith(text, ('fsdf', 'fsdf '))
True
>>> endswith(text, ('fsdf', 'fsdf '))
'MATCH'
>>> not text.endswith(('fsdf', 'fsdf X'))
True True
>>> endswith(text, ('fsdf', 'fsdf X')) >>> test_endswith(text, ('fsdf', 'fsdf X'))
'NO MATCH' False
""" """
if s.endswith(sub): if s.endswith(sub):
return 'MATCH' return True
else: else:
return 'NO MATCH' return False
@cython.test_fail_if_path_exists( @cython.test_fail_if_path_exists(
"//CoerceToPyTypeNode", "//CoerceToPyTypeNode",
...@@ -422,38 +418,39 @@ def endswith(unicode s, sub): ...@@ -422,38 +418,39 @@ def endswith(unicode s, sub):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def endswith_start_end(unicode s, sub, start, end): def endswith_start_end(unicode s, sub, start, end):
""" """
>>> text.endswith('fsdf', 10, len(text)-1) >>> def test_endswith_start_end(s, sub, start, end):
... py = s.endswith(sub, start, end)
... cy = endswith_start_end(s, sub, start, end)
... assert py == cy, (py, cy)
... return cy
>>> test_endswith_start_end(text, 'fsdf', 10, len(text)-1)
True True
>>> endswith_start_end(text, 'fsdf', 10, len(text)-1) >>> test_endswith_start_end(text, 'fsdf ', 10, len(text)-1)
'MATCH'
>>> text.endswith('fsdf ', 10, len(text)-1)
False False
>>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
'NO MATCH'
>>> text.endswith('fsdf ', -1000, 5000) >>> test_endswith_start_end(text, 'fsdf ', -1000, 5000)
True True
>>> endswith_start_end(text, 'fsdf ', -1000, 5000)
'MATCH'
>>> text.endswith(('fsd', 'fsdf'), 10, len(text)-1) >>> test_endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
True
>>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
'MATCH'
>>> not text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
True True
>>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1) >>> test_endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
'NO MATCH' False
>>> text.endswith('fsdf ', None, None) >>> test_endswith_start_end(text, 'fsdf ', None, None)
True
>>> test_endswith_start_end(text, 'fsdf ', 32, None)
True True
>>> endswith_start_end(text, 'fsdf ', None, None) >>> test_endswith_start_end(text, 'fsdf ', 33, None)
'MATCH' False
>>> test_endswith_start_end(text, 'fsdf ', None, 37)
True
>>> test_endswith_start_end(text, 'fsdf ', None, 36)
False
""" """
if s.endswith(sub, start, end): if s.endswith(sub, start, end):
return 'MATCH' return True
else: else:
return 'NO MATCH' return False
# unicode.__contains__(s, sub) # unicode.__contains__(s, sub)
...@@ -603,9 +600,12 @@ def mod_format_tuple(*values): ...@@ -603,9 +600,12 @@ def mod_format_tuple(*values):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def find(unicode s, substring): def find(unicode s, substring):
""" """
>>> text.find('sa') >>> def test_find(s, substring):
16 ... py = s.find(substring)
>>> find(text, 'sa') ... cy = find(s, substring)
... assert py == cy, (py, cy)
... return cy
>>> test_find(text, 'sa')
16 16
""" """
cdef Py_ssize_t pos = s.find(substring) cdef Py_ssize_t pos = s.find(substring)
...@@ -618,10 +618,23 @@ def find(unicode s, substring): ...@@ -618,10 +618,23 @@ def find(unicode s, substring):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def find_start_end(unicode s, substring, start, end): def find_start_end(unicode s, substring, start, end):
""" """
>>> text.find('sa', 17, 25) >>> def test_find_start_end(s, substring, start, end):
... py = s.find(substring, start, end)
... cy = find_start_end(s, substring, start, end)
... assert py == cy, (py, cy)
... return cy
>>> test_find_start_end(text, 'sa', 17, 25)
20 20
>>> find_start_end(text, 'sa', 17, 25) >>> test_find_start_end(text, 'sa', None, None)
16
>>> test_find_start_end(text, 'sa', 16, None)
16
>>> test_find_start_end(text, 'sa', 17, None)
20 20
>>> test_find_start_end(text, 'sa', None, 16)
-1
>>> test_find_start_end(text, 'sa', None, 19)
16
""" """
cdef Py_ssize_t pos = s.find(substring, start, end) cdef Py_ssize_t pos = s.find(substring, start, end)
return pos return pos
...@@ -637,9 +650,12 @@ def find_start_end(unicode s, substring, start, end): ...@@ -637,9 +650,12 @@ def find_start_end(unicode s, substring, start, end):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def rfind(unicode s, substring): def rfind(unicode s, substring):
""" """
>>> text.rfind('sa') >>> def test_rfind(s, substring):
20 ... py = s.rfind(substring)
>>> rfind(text, 'sa') ... cy = rfind(s, substring)
... assert py == cy, (py, cy)
... return cy
>>> test_rfind(text, 'sa')
20 20
""" """
cdef Py_ssize_t pos = s.rfind(substring) cdef Py_ssize_t pos = s.rfind(substring)
...@@ -652,9 +668,22 @@ def rfind(unicode s, substring): ...@@ -652,9 +668,22 @@ def rfind(unicode s, substring):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def rfind_start_end(unicode s, substring, start, end): def rfind_start_end(unicode s, substring, start, end):
""" """
>>> text.rfind('sa', 14, 19) >>> def test_rfind_start_end(s, substring, start, end):
... py = s.rfind(substring, start, end)
... cy = rfind_start_end(s, substring, start, end)
... assert py == cy, (py, cy)
... return cy
>>> test_rfind_start_end(text, 'sa', 14, 19)
16 16
>>> rfind_start_end(text, 'sa', 14, 19) >>> test_rfind_start_end(text, 'sa', None, None)
20
>>> test_rfind_start_end(text, 'sa', 16, None)
20
>>> test_rfind_start_end(text, 'sa', 21, None)
-1
>>> test_rfind_start_end(text, 'sa', None, 22)
20
>>> test_rfind_start_end(text, 'sa', None, 21)
16 16
""" """
cdef Py_ssize_t pos = s.rfind(substring, start, end) cdef Py_ssize_t pos = s.rfind(substring, start, end)
...@@ -671,9 +700,12 @@ def rfind_start_end(unicode s, substring, start, end): ...@@ -671,9 +700,12 @@ def rfind_start_end(unicode s, substring, start, end):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def count(unicode s, substring): def count(unicode s, substring):
""" """
>>> text.count('sa') >>> def test_count(s, substring):
2 ... py = s.count(substring)
>>> count(text, 'sa') ... cy = count(s, substring)
... assert py == cy, (py, cy)
... return cy
>>> test_count(text, 'sa')
2 2
""" """
cdef Py_ssize_t pos = s.count(substring) cdef Py_ssize_t pos = s.count(substring)
...@@ -686,14 +718,25 @@ def count(unicode s, substring): ...@@ -686,14 +718,25 @@ def count(unicode s, substring):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def count_start_end(unicode s, substring, start, end): def count_start_end(unicode s, substring, start, end):
""" """
>>> text.count('sa', 14, 21) >>> def test_count_start_end(s, substring, start, end):
... py = s.count(substring, start, end)
... cy = count_start_end(s, substring, start, end)
... assert py == cy, (py, cy)
... return cy
>>> test_count_start_end(text, 'sa', 14, 21)
1 1
>>> text.count('sa', 14, 22) >>> test_count_start_end(text, 'sa', 14, 22)
2
>>> test_count_start_end(text, 'sa', None, None)
2 2
>>> count_start_end(text, 'sa', 14, 21) >>> test_count_start_end(text, 'sa', 14, None)
2
>>> test_count_start_end(text, 'sa', 17, None)
1 1
>>> count_start_end(text, 'sa', 14, 22) >>> test_count_start_end(text, 'sa', None, 23)
2 2
>>> test_count_start_end(text, 'sa', None, 20)
1
""" """
cdef Py_ssize_t pos = s.count(substring, start, end) cdef Py_ssize_t pos = s.count(substring, start, end)
return pos return pos
...@@ -708,9 +751,12 @@ def count_start_end(unicode s, substring, start, end): ...@@ -708,9 +751,12 @@ def count_start_end(unicode s, substring, start, end):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def replace(unicode s, substring, repl): def replace(unicode s, substring, repl):
""" """
>>> print( text.replace('sa', 'SA') ) >>> def test_replace(s, substring, repl):
ab jd sdflk as SA SAdas asdas fsdf\x20 ... py = s.replace(substring, repl)
>>> print( replace(text, 'sa', 'SA') ) ... cy = replace(s, substring, repl)
... assert py == cy, (py, cy)
... return cy
>>> print( test_replace(text, 'sa', 'SA') )
ab jd sdflk as SA SAdas asdas fsdf\x20 ab jd sdflk as SA SAdas asdas fsdf\x20
""" """
return s.replace(substring, repl) return s.replace(substring, repl)
...@@ -722,9 +768,12 @@ def replace(unicode s, substring, repl): ...@@ -722,9 +768,12 @@ def replace(unicode s, substring, repl):
"//PythonCapiCallNode") "//PythonCapiCallNode")
def replace_maxcount(unicode s, substring, repl, maxcount): def replace_maxcount(unicode s, substring, repl, maxcount):
""" """
>>> print( text.replace('sa', 'SA', 1) ) >>> def test_replace_maxcount(s, substring, repl, maxcount):
ab jd sdflk as SA sadas asdas fsdf\x20 ... py = s.replace(substring, repl, maxcount)
>>> print( replace_maxcount(text, 'sa', 'SA', 1) ) ... cy = replace_maxcount(s, substring, repl, maxcount)
... assert py == cy, (py, cy)
... return cy
>>> print( test_replace_maxcount(text, 'sa', 'SA', 1) )
ab jd sdflk as SA sadas asdas fsdf\x20 ab jd sdflk as SA sadas asdas fsdf\x20
""" """
return s.replace(substring, repl, maxcount) return s.replace(substring, repl, maxcount)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment