return False else: return True else: return True # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_fast_float_leaves_float_asis_example(): assert fast_float(45.8) == 45.8 @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats()) def test_fast_float_leaves_float_asis(x): assume(not isnan(x)) # But inf is included assert fast_float(x) == x def test_fast_float_converts_float_string_to_float_example(): assert fast_float('45.8') == 45.8 assert fast_float('-45') == -45.0 assert fast_float('45.8e-2') == 45.8e-2 assert isnan(fast_float('nan')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats()) def test_fast_float_converts_float_string_to_float(x):
# 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) assert str(err.value) == 'low >= high' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=floats(), y=floats()) def test_range_check_raises_ValueError_if_second_is_less_than_first(x, y): assume(x >= y)
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), )
def test_fast_float_returns_nan_alternate_if_nan_option_is_given(): assert fast_float('nan', nan=7) == 7 def test_fast_float_converts_float_string_to_float_example(): assert fast_float('45.8') == 45.8 assert fast_float('-45') == -45.0 assert fast_float('45.8e-2', key=len) == 45.8e-2 assert isnan(fast_float('nan')) assert isnan(fast_float('+nan')) assert isnan(fast_float('-NaN')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats()) def test_fast_float_converts_float_string_to_float(x): assume(not isnan(x)) # But inf is included assert fast_float(repr(x)) == x def test_fast_float_leaves_string_as_is_example(): 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)) assume(bool(x)) assert fast_float(x) == x
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(): with raises(ValueError) as err: range_check(7, 2) assert str(err.value) == 'low >= high' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=floats(), y=floats()) def test_range_check_raises_ValueError_if_second_is_less_than_first(x, y):
x): assume(all(x)) z = py23_str(pathlib.Path(*x[:-2])) + '.' + x[-1] y = list(pathlib.Path(z).parts) assert _path_splitter(z) == y[:-1] + [pathlib.Path(z).stem ] + [pathlib.Path(z).suffix] def test_number_extracter_raises_TypeError_if_given_a_number_example(): with raises(TypeError): assert _number_extracter(50.0, _float_sign_exp_re, *float_nosafe_nolocale_nogroup) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(floats()) def test_number_extracter_raises_TypeError_if_given_a_number(x): with raises(TypeError): assert _number_extracter(x, _float_sign_exp_re, *float_nosafe_nolocale_nogroup) def test_number_extracter_includes_plus_sign_and_exponent_in_float_definition_for_signed_exp_floats_example( ): assert _number_extracter( 'a5+5.034e-1', _float_sign_exp_re, *float_nosafe_nolocale_nogroup) == ['a', 5.0, 0.5034] @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(elements=floats() | text() | integers(), min_size=1, max_size=10))
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)
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)