assert fast_float('invalid') == 'invalid' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text()) def test_fast_float_leaves_string_as_is(x): assume(not is_float(x)) assert fast_float(x) == x def test_fast_int_leaves_int_asis_example(): assert fast_int(45) == 45 @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(integers()) def test_fast_int_leaves_int_asis(x): assert fast_int(x) == x def test_fast_int_leaves_float_string_as_is_example(): assert fast_int('45.8') == '45.8' assert fast_int('nan') == 'nan' assert fast_int('inf') == 'inf' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats()) def test_fast_int_leaves_float_string_as_is(x): assume(not x.is_integer()) assert fast_int(repr(x)) == repr(x)
# tmp/a1/path1 sort_and_print_entries(entries, Args(None, None, False, True, True)) e = [call(entries[i]) for i in reversed([2, 3, 1, 0, 5, 6, 4])] p.assert_has_calls(e) # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_range_check_returns_range_as_is_but_with_floats_if_first_is_less_than_second_example(): assert range_check(10, 11) == (10.0, 11.0) assert range_check(6.4, 30) == (6.4, 30.0) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=integers(), y=integers()) def test_range_check_returns_range_as_is_but_with_floats_if_first_is_less_than_second(x, y): assume(float(x) < float(y)) assert range_check(x, y) == (float(x), float(y)) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=floats(), y=floats()) def test_range_check_returns_range_as_is_but_with_floats_if_first_is_less_than_second2(x, y): assume(x < y) assert range_check(x, y) == (x, y) def test_range_check_raises_ValueError_if_second_is_less_than_first_example(): with raises(ValueError) as err: range_check(7, 2)
use_hypothesis, ) # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_parse_number_function_makes_function_that_returns_tuple_example(): assert _parse_number_function(0, '')(57) == ('', 57) assert _parse_number_function(0, '')(float('nan')) == ('', float('-inf')) assert _parse_number_function(ns.NANLAST, '')(float('nan')) == ('', float('+inf')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats() | integers()) def test_parse_number_function_makes_function_that_returns_tuple(x): assume(not isnan(x)) assert _parse_number_function(0, '')(x) == ('', x) def test_parse_number_function_with_PATH_makes_function_that_returns_nested_tuple_example(): assert _parse_number_function(ns.PATH, '')(57) == (('', 57), ) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats() | integers()) def test_parse_number_function_with_PATH_makes_function_that_returns_nested_tuple(x): assume(not isnan(x)) assert _parse_number_function(ns.PATH, '')(x) == (('', x), )
assert fast_int('inf') == 'inf' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats()) def test_fast_int_leaves_float_string_as_is(x): assume(not x.is_integer()) assert fast_int(repr(x)) == repr(x) def test_fast_int_converts_int_string_to_int_example(): assert fast_int('-45') == -45 assert fast_int('+45') == 45 @given(integers()) def test_fast_int_converts_int_string_to_int(x): assert fast_int(repr(x)) == x def test_fast_int_leaves_string_as_is_example(): assert fast_int('invalid') == 'invalid' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text()) def test_fast_int_leaves_string_as_is(x): assume(not is_int(x)) assume(bool(x)) assert fast_int(x) == x
def test_sep_inserter_does_nothing_if_no_numbers_example(): assert list(_sep_inserter(iter(['a', 'b', 'c']), '')) == ['a', 'b', 'c'] assert list(_sep_inserter(iter(['a']), '')) == ['a'] def test_sep_inserter_does_nothing_if_only_one_number_example(): assert list(_sep_inserter(iter(['a', 5]), '')) == ['a', 5] def test_sep_inserter_inserts_separator_string_between_two_numbers_example(): assert list(_sep_inserter(iter([5, 9]), '')) == ['', 5, '', 9] assert list(_sep_inserter(iter([5, 9]), null_string)) == [null_string, 5, null_string, 9] @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(elements=text() | integers())) def test_sep_inserter_inserts_separator_between_two_numbers(x): assume(bool(x)) assert list(_sep_inserter(iter(x), '')) == list(add_leading_space_if_first_is_num(sep_inserter(x, ''), '')) def test_path_splitter_splits_path_string_by_separator_example(): z = '/this/is/a/path' assert tuple(_path_splitter(z)) == tuple(pathlib.Path(z).parts) z = pathlib.Path('/this/is/a/path') assert tuple(_path_splitter(z)) == tuple(pathlib.Path(z).parts) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(sampled_from(string.ascii_letters), min_size=2)) def test_path_splitter_splits_path_string_by_separator(x):
e = [call(entries[i]) for i in reversed([2, 3, 1, 0, 5, 6, 4])] p.assert_has_calls(e) # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_range_check_returns_range_as_is_but_with_floats_if_first_is_less_than_second_example( ): assert range_check(10, 11) == (10.0, 11.0) assert range_check(6.4, 30) == (6.4, 30.0) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=integers(), y=integers()) def test_range_check_returns_range_as_is_but_with_floats_if_first_is_less_than_second( x, y): assume(x < y) assert range_check(x, y) == (float(x), float(y)) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=floats(), y=floats()) def test_range_check_returns_range_as_is_but_with_floats_if_first_is_less_than_second2( x, y): assume(x < y) assert range_check(x, y) == (x, y) def test_range_check_raises_ValueError_if_second_is_less_than_first_example():
def test_py3_safe_does_nothing_if_only_one_number_example(): assert _py3_safe(['a', 5], False, isint) == ['a', 5] def test_py3_safe_inserts_empty_string_between_two_numbers_example(): assert _py3_safe([5, 9], False, isint) == [5, '', 9] def test_py3_safe_with_use_locale_inserts_null_string_between_two_numbers_example( ): assert _py3_safe([5, 9], True, isint) == [5, null_string, 9] @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(elements=text() | integers())) def test_py3_safe_inserts_empty_string_between_two_numbers(x): assume(bool(x)) assert _py3_safe(x, False, isint) == sep_inserter(x, (int, long), '') def test_path_splitter_splits_path_string_by_separator_example(): z = '/this/is/a/path' assert _path_splitter(z) == list(pathlib.Path(z).parts) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(sampled_from(string.ascii_letters), min_size=2)) def test_path_splitter_splits_path_string_by_separator(x): assume(all(x)) z = py23_str(pathlib.Path(*x))
def test_post_string_parse_function_with_empty_tuple_returns_double_empty_tuple(): assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')((), '') == ((), ()) def test_post_string_parse_function_with_null_string_first_element_adds_empty_string_on_first_tuple_element(): assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')(('', 60), '') == (('',), ('', 60)) def test_post_string_parse_function_returns_first_element_in_first_tuple_element_example(): assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')(('this', 60), 'this60') == (('t',), ('this', 60)) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=text(), y=floats() | integers()) def test_post_string_parse_function_returns_first_element_in_first_tuple_element(x, y): assume(x) assume(not isnan(y)) assume(not isinf(y)) assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS, '')((x, y), ''.join(map(py23_str, [x, y]))) == ((x[0],), (x, y)) def test_post_string_parse_function_returns_first_element_in_first_tuple_element_caseswapped_with_DUMB_and_LOWERCASEFIRST_example(): assert _post_string_parse_function(ns.LOCALE | ns.UNGROUPLETTERS | ns._DUMB | ns.LOWERCASEFIRST, '')(('this', 60), 'this60') == (('T',), ('this', 60)) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=text(), y=floats() | integers()) def test_post_string_parse_function_returns_first_element_in_first_tuple_element_caseswapped_with_DUMB_and_LOWERCASEFIRST(x, y): assume(x)
assert _pre_split_function(ns.IGNORECASE | ns.LOWERCASEFIRST)(x) == x.swapcase().casefold() else: assert _pre_split_function(ns.IGNORECASE | ns.LOWERCASEFIRST)(x) == x.swapcase().lower() def test_pre_split_function_removes_thousands_separator_with_LOCALE_example(): load_locale('en_US') x = '12,543,642,642.534,534,980' # Without FLOAT it does not account for decimal. assert _pre_split_function(ns.LOCALE)(x) == '12543642642.534534980' x = '12,543,642,642.534,534,980' # LOCALEALPHA doesn't do anything... need LOCALENUM assert _pre_split_function(ns.LOCALEALPHA)(x) == '12,543,642,642.534,534,980' locale.setlocale(locale.LC_ALL, str('')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(elements=integers(), min_size=4, max_size=20)) def test_pre_split_function_removes_thousands_separator_with_LOCALE(x): load_locale('en_US') t = ''.join(map(methodcaller('rstrip', 'lL'), map(str, map(abs, x)))) # Remove negative signs trailing L s = '' for i, y in enumerate(reversed(t), 1): s = y + s if i % 3 == 0 and i != len(t): s = ',' + s assert _pre_split_function(ns.LOCALE)(s) == t locale.setlocale(locale.LC_ALL, str('')) def test_pre_split_function_removes_thousands_separator_and_is_float_aware_with_LOCALE_and_FLOAT_example(): x = '12,543,642,642.534,534,980' assert _pre_split_function(ns.LOCALE | ns.FLOAT)(x) == '12543642642.534,534980'
def test__natsort_key_with_tuple_of_paths_and_PATH_returns_triply_nested_tuple(): # PATH also handles recursion well. sfunc = _parse_path_function(string_func) bytes_func = _parse_bytes_function(ns.PATH) num_func = _parse_number_function(ns.PATH, '') assert _natsort_key(('/Folder', '/Folder (1)'), None, sfunc, bytes_func, num_func) == ((('/',), ('Folder',)), (('/',), ('Folder (', 1, ')'))) # The remaining tests provide no examples, just hypothesis tests. # They only confirm that _natsort_key uses the above building blocks. @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats() | integers()) def test__natsort_key_with_numeric_input_takes_number_path(x): assume(not isnan(x)) assert _natsort_key(x, None, string_func, bytes_func, num_func) == num_func(x) @pytest.mark.skipif(PY_VERSION < 3, reason='only valid on python3') @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(binary()) def test__natsort_key_with_bytes_input_takes_bytes_path(x): assume(x) assert _natsort_key(x, None, string_func, bytes_func, num_func) == bytes_func(x) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(elements=floats() | text() | integers(), min_size=1, max_size=10))
) from compat.locale import bad_uni_chars # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_post_split_function_returns_fast_int_example(): x = 'hello' assert _post_split_function(0)(x) is fast_int(x) assert _post_split_function(0)('5007') == fast_int('5007') @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text() | floats() | integers()) def test_post_split_function_returns_fast_int(x): assume(x) assert _post_split_function(0)(py23_str(x)) == fast_int(py23_str(x)) def test_post_split_function_with_FLOAT_returns_fast_float_example(): x = 'hello' assert _post_split_function(ns.FLOAT)(x) is fast_float(x) assert _post_split_function(ns.FLOAT)('5007') == fast_float('5007') @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text() | floats() | integers()) def test_post_split_function_with_FLOAT_returns_fast_float(x): assume(x)