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)) 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
@pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(x=tuples(integers(), integers(), floats(), floats()), y=tuples(integers(), floats(), floats(), integers())) def test_check_filter_raises_ValueError_if_filter_is_invalid(x, y): assume(any(float(i) >= float(j) for i, j in zip(x, y))) with raises(ValueError) as err: check_filter(list(zip(x, y))) assert str(err.value) == 'Error in --filter: low >= high' def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_the_range_bounds_example(): assert keep_entry_range('a56b23c89', [0], [100], int, re.compile(r'\d+')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(tuples(text(), integers(1, 99), text(), integers(1, 99), text())) def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_the_range_bounds(x): s = ''.join(map(py23_str, x)) assume(any(0 < int(i) < 100 for i in re.findall(r'\d+', s) if re.match(r'\d+$', i))) assert keep_entry_range(s, [0], [100], int, re.compile(r'\d+')) def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_any_range_bounds_example(): assert keep_entry_range('a56b23c89', [1, 88], [20, 90], int, re.compile(r'\d+')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(tuples(text(), integers(2, 89), text(), integers(2, 89), text())) def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_any_range_bounds(x): s = ''.join(map(py23_str, x)) assume(any((1 < int(i) < 20) or (88 < int(i) < 90) for i in re.findall(r'\d+', s) if re.match(r'\d+$', i)))
@given(x=tuples(integers(), integers(), floats(), floats()), y=tuples(integers(), floats(), floats(), integers())) def test_check_filter_raises_ValueError_if_filter_is_invalid(x, y): assume(any(i >= j for i, j in zip(x, y))) with raises(ValueError) as err: check_filter(list(zip(x, y))) assert str(err.value) == 'Error in --filter: low >= high' def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_the_range_bounds_example( ): assert keep_entry_range('a56b23c89', [0], [100], int, re.compile(r'\d+')) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(tuples(text(), integers(1, 99), text(), integers(1, 99), text())) def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_the_range_bounds( x): s = ''.join(map(py23_str, x)) assume( any(0 < int(i) < 100 for i in re.findall(r'\d+', s) if re.match(r'\d+$', i))) assert keep_entry_range(s, [0], [100], int, re.compile(r'\d+')) def test_keep_entry_range_returns_True_if_any_portion_of_input_is_between_any_range_bounds_example( ): assert keep_entry_range('a56b23c89', [1, 88], [20, 90], int, re.compile(r'\d+'))
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_fast_float_with_key_applies_to_string_example(): assert fast_float('invalid', key=len) == len('invalid') @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text()) def test_fast_float_with_key_applies_to_string(x): assume(not is_float(x)) assume(bool(x))
def test_chain_functions_combines_functions_in_given_order(): x = 2345 assert chain_functions([str, len, op_neg])(x) == -len(str(x)) # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_groupletters_returns_letters_with_lowercase_transform_of_letter_example(): assert _groupletters('HELLO') == 'hHeElLlLoO' assert _groupletters('hello') == 'hheelllloo' @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text()) def test_groupeletters_returns_letters_with_lowercase_transform_of_letter(x): assume(bool(x)) assert _groupletters(x) == ''.join(chain.from_iterable([low(y), y] for y in 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():
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))
floats, integers, use_hypothesis, ) # Each test has an "example" version for demonstrative purposes, # and a test that uses the hypothesis module. def test_post_string_parse_function_with_iterable_returns_tuple_with_no_options_example(): assert _post_string_parse_function(0, '')(iter([7]), '') == (7, ) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text()) def test_post_string_parse_function_with_iterable_returns_tuple_with_no_options(x): assert _post_string_parse_function(0, '')(iter([x]), '') == (x, ) # UNGROUPLETTERS without LOCALE does nothing, as does LOCALE without UNGROUPLETTERS assert _post_string_parse_function(ns.UNGROUPLETTERS, '')(iter([x]), '') == _post_string_parse_function(0, '')(iter([x]), '') assert _post_string_parse_function(ns.LOCALE, '')(iter([x]), '') == _post_string_parse_function(0, '')(iter([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))
@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)) def test__natsort_key_with_text_input_takes_string_path(x): assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) assert _natsort_key(s, None, string_func, bytes_func, num_func) == string_func(s) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(lists(elements=text(), min_size=1, max_size=10)) def test__natsort_key_with_nested_input_takes_nested_path(x): assert _natsort_key(x, None, string_func, bytes_func, num_func) == tuple(string_func(s) for s in x) @pytest.mark.skipif(not use_hypothesis, reason='requires python2.7 or greater') @given(text()) def test__natsort_key_with_key_argument_applies_key_before_processing(x):
) 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)