def test__natsort_key_with_IGNORECASE_lowercases_text(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) try: assert _natsort_key(s, None, ns.IGNORECASE) == tuple(_number_extracter(s.casefold(), _int_nosign_re, *int_nosafe_nolocale_nogroup)) except AttributeError: assert _natsort_key(s, None, ns.IGNORECASE) == tuple(_number_extracter(s.lower(), _int_nosign_re, *int_nosafe_nolocale_nogroup))
def test__natsort_key_with_float_and_unsigned_splits_input_into_string_and_unsigned_float(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 ns.U == ns.UNSIGNED assert _natsort_key(s, None, ns.F | ns.U) == tuple(_number_extracter(s, _float_nosign_exp_re, *float_nosafe_nolocale_nogroup)) # Default is unsigned search assert _natsort_key(s, None, ns.F) == tuple(_number_extracter(s, _float_nosign_exp_re, *float_nosafe_nolocale_nogroup))
def test__natsort_key_with_int_splits_input_into_string_and_unsigned_int(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 ns.I == ns.INT assert _natsort_key(s, None, ns.INT) == tuple(_number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_nogroup)) # Default is int search assert _natsort_key(s, None, ns.NOEXP) == tuple(_number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_nogroup)) # NOEXP is ignored for integers assert _natsort_key(s, None, ns.I | ns.NOEXP) == tuple(_number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_nogroup))
def test__natsort_key_with_LOCALE_transforms_floats_according_to_the_current_locale_and_strxfrms_strings(x): # Locale aware sorting 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) load_locale('en_US') if dumb_sort(): assert _natsort_key(s, None, ns.LOCALE | ns.F) == tuple(_number_extracter(s.swapcase(), _float_nosign_exp_re, *float_nosafe_locale_group)) else: assert _natsort_key(s, None, ns.LOCALE | ns.F) == tuple(_number_extracter(s, _float_nosign_exp_re, *float_nosafe_locale_nogroup)) locale.setlocale(locale.LC_NUMERIC, str(''))
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_float(x): assume(len(x) <= 10) 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) t = float_splitter(s, True, True, False, '') t = [''.join([low(z) + z for z in y]) if type(y) != float else y for y in t] assert _number_extracter(s, _float_sign_exp_re, *float_nosafe_nolocale_group) == t
def test__natsort_key_with_GROUPLETTERS_and_LOWERCASEFIRST_inverts_text_first_then_doubles_letters_with_lowercase_letter_first(x): assume(len(x) <= 10) assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(ichain([repr(y)] if type(y) in (float, long, int) else [low(y), y] for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = _number_extracter(s.swapcase(), _int_nosign_re, *int_nosafe_nolocale_nogroup) assert _natsort_key(s, None, ns.G | ns.LF) == tuple(''.join(low(z) + z for z in y) if type(y) not in (float, long, int) else y for y in t)
def test__natsort_key_with_float_and_signed_splits_input_into_string_and_signed_float_with_exponent(x): assume(len(x) <= 10) 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 ns.F == ns.FLOAT assert ns.S == ns.SIGNED assert _natsort_key(s, None, ns.F | ns.S) == tuple(_number_extracter(s, _float_sign_exp_re, *float_nosafe_nolocale_nogroup))
def test__natsort_key_with_tuple_input_returns_nested_tuples(x): # Iterables are parsed recursively so you can sort lists of lists. assume(len(x) <= 10) 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) t = tuple(_number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_nogroup)) assert _natsort_key((s, s), None, ns.I) == (t, t)
def test__natsort_key_with_tuple_input_but_itemgetter_key_returns_split_second_element(x): # A key is applied before recursion, but not in the recursive calls. assume(len(x) <= 10) 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) t = tuple(_number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_nogroup)) assert _natsort_key((s, s), itemgetter(1), ns.I) == t
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters(x): assume(len(x) <= 10) load_locale('en_US') s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = int_splitter(s, False, False, null_string) t = [y if i == 0 and y is null_string else locale_convert(y, (fast_int, isint), True) for i, y in enumerate(t)] assert _number_extracter(s, _int_nosign_re, *int_nosafe_locale_group) == t locale.setlocale(locale.LC_NUMERIC, str(''))
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_int(x): assume(len(x) <= 10) try: low = py23_str.casefold except AttributeError: low = py23_str.lower s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = int_splitter(s, False, False, '') t = [''.join([low(z) + z for z in y]) if type(y) not in (int, long) else y for y in t] assert _number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_group) == t
def test_number_extracter_extracts_numbers_and_strxfrms_letter_doubled_strings_with_use_locale_and_groupletters_example(): load_locale('en_US') if use_pyicu: from natsort.locale_help import get_pyicu_transform from locale import getlocale strxfrm = get_pyicu_transform(getlocale()) else: from natsort.locale_help import strxfrm assert _number_extracter('A5+5.034E-1', _int_nosign_re, *int_nosafe_locale_group) == [strxfrm('aA'), 5, strxfrm('++'), 5, strxfrm('..'), 34, strxfrm('eE--'), 1] locale.setlocale(locale.LC_NUMERIC, str(''))
def test_number_extracter_extracts_numbers_and_strxfrms_strings_with_use_locale(x): load_locale('en_US') s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = int_splitter(s, False, False, null_string) try: # Account for locale bug on Python 3.2 t = [y if i == 0 and y is null_string else locale_convert(y, (fast_int, isint), False) for i, y in enumerate(t)] assert _number_extracter(s, _int_nosign_re, *int_nosafe_locale_nogroup) == t except OverflowError: pass locale.setlocale(locale.LC_NUMERIC, str(''))
def test_number_extracter_extracts_numbers_and_strxfrms_strings_with_use_locale(): locale.setlocale(locale.LC_NUMERIC, str('en_US.UTF-8')) if use_pyicu: from natsort.locale_help import get_pyicu_transform from locale import getlocale strxfrm = get_pyicu_transform(getlocale()) else: from natsort.locale_help import strxfrm assert _number_extracter('A5+5.034E-1', _int_nosign_re, *ittf) == [strxfrm('A'), 5, strxfrm('+'), 5, strxfrm('.'), 34, strxfrm('E-'), 1] locale.setlocale(locale.LC_NUMERIC, str(''))
def test__natsort_key_with_GROUPLETTERS_doubles_text_with_lowercase_letter_first(x): try: low = py23_str.casefold except AttributeError: low = py23_str.lower assume(len(x) <= 10) assume(not any(type(y) == float and isnan(y) for y in x)) s = ''.join(ichain([repr(y)] if type(y) in (float, long, int) else [low(y), y] for y in x)) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) t = _number_extracter(s, _int_nosign_re, *int_nosafe_nolocale_nogroup) assert _natsort_key(s, None, ns.GROUPLETTERS) == tuple(''.join(low(z) + z for z in y) if type(y) not in (float, long, int) else y for y in t)
def test__natsort_key_with_LOCALE_and_UNGROUPLETTERS_places_space_before_string_with_capital_first_letter(x): # Locale aware sorting 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) load_locale('en_US') if dumb_sort(): t = tuple(_number_extracter(s.swapcase(), _float_nosign_exp_re, *float_nosafe_locale_group)) else: t = tuple(_number_extracter(s, _float_nosign_exp_re, *float_nosafe_locale_nogroup)) if not t: r = (t, t) elif t[0] in (null_string, get_strxfrm()(b'\x00') if sys.version[0] == '2' and not use_pyicu else null_string): r = ((b'' if use_pyicu else '',), t) else: r = ((s[0],), t) assert _natsort_key(s, None, ns.LOCALE | ns.UNGROUPLETTERS | ns.F) == r # The below are all aliases for UNGROUPLETTERS assert ns.UNGROUPLETTERS == ns.UG assert ns.UNGROUPLETTERS == ns.CAPITALFIRST assert ns.UNGROUPLETTERS == ns.C locale.setlocale(locale.LC_NUMERIC, str(''))
def test__natsort_key_with_key_applies_key_function_before_splitting(x): assume(len(x) <= 10) 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, lambda x: x.upper(), ns.I) == tuple(_number_extracter(s.upper(), _int_nosign_re, *int_nosafe_nolocale_nogroup))
def test_number_extracter_inserts_empty_string_between_ints_for_py3safe_option( ): assert _number_extracter('a5+5.034e-1', _int_sign_re, *itff) == ['a', 5, '', 5, '.', 34, 'e', -1]
def test_number_extracter_raises_TypeError_if_given_a_number(): with raises(TypeError): assert _number_extracter(50.0, _float_sign_exp_re, *ffff)
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_float_example(): assert _number_extracter('A5+5.034E-1', _float_sign_exp_re, *float_nosafe_nolocale_group) == ['aA', 5.0, 0.5034]
def test_number_extracter_inserts_no_empty_string_py3safe_option_because_no_numbers_are_adjascent_example(): assert _number_extracter('a5+5.034e-1', _float_nosign_exp_re, *float_safe_nolocale_nogroup) == ['a', 5.0, '+', 0.5034]
def test_number_extracter_inserts_empty_string_between_ints_for_py3safe_option_example(): assert _number_extracter('a5+5.034e-1', _int_sign_re, *int_safe_nolocale_nogroup) == ['a', 5, '', 5, '.', 34, 'e', -1]
def test_number_extracter_excludes_plus_sign_and_exponent_in_float_definition_for_unsigned_noexp_floats_example( ): assert _number_extracter( 'a5+5.034e-1', _float_nosign_noexp_re, *float_nosafe_nolocale_nogroup) == ['a', 5.0, '+', 5.034, 'e-', 1.0]
def test_number_extracter_inserts_no_empty_string_py3safe_option_because_no_numbers_are_adjascent_example( ): assert _number_extracter( 'a5+5.034e-1', _float_nosign_exp_re, *float_safe_nolocale_nogroup) == ['a', 5.0, '+', 0.5034]
def test_number_extracter_inserts_empty_string_between_ints_for_py3safe_option( x): s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) assert _number_extracter(s, _int_sign_re, *int_safe_nolocale_nogroup) == int_splitter( s, True, True, '')
def test_number_extracter_inserts_empty_string_between_ints_for_py3safe_option_example( ): assert _number_extracter( 'a5+5.034e-1', _int_sign_re, *int_safe_nolocale_nogroup) == ['a', 5, '', 5, '.', 34, 'e', -1]
def test_number_extracter_inserts_empty_string_between_floats_for_py3safe_option_example( ): assert _number_extracter( 'a5+5.034e-1', _float_sign_exp_re, *float_safe_nolocale_nogroup) == ['a', 5.0, '', 0.5034]
def test_number_extracter_includes_plus_and_minus_sign_in_int_definition_for_signed_ints( x): s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) assert _number_extracter(s, _int_sign_re, *int_nosafe_nolocale_nogroup) == int_splitter( s, True, False, '')
def test_number_extracter_includes_plus_and_minus_sign_in_int_definition_for_signed_ints_example( ): assert _number_extracter( 'a5+5.034e-1', _int_sign_re, *int_nosafe_nolocale_nogroup) == ['a', 5, 5, '.', 34, 'e', -1]
def test_number_extracter_adds_leading_empty_string_if_input_begins_with_a_number_and_empty_string_between_numbers_for_py3safe_exmple( ): assert _number_extracter( '6a5+5.034e-1', _float_sign_exp_re, *float_safe_nolocale_nogroup) == ['', 6.0, 'a', 5.0, '', 0.5034]
def test__natsort_key_with_LOWERCASEFIRST_inverts_text_case(x): assume(len(x) <= 10) 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, ns.LOWERCASEFIRST) == tuple(_number_extracter(s.swapcase(), _int_nosign_re, *int_nosafe_nolocale_nogroup))
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_float_example( ): assert _number_extracter( 'A5+5.034E-1', _float_sign_exp_re, *float_nosafe_nolocale_group) == ['aA', 5.0, 0.5034]
def test_number_extracter_inserts_empty_string_between_floats_for_py3safe_option_example(): assert _number_extracter('a5+5.034e-1', _float_sign_exp_re, *float_safe_nolocale_nogroup) == ['a', 5.0, '', 0.5034]
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]
def test_number_extracter_inserts_empty_string_between_ints_for_py3safe_option(x): assume(len(x) <= 10) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) assert _number_extracter(s, _int_sign_re, *int_safe_nolocale_nogroup) == int_splitter(s, True, True, '')
def test__natsort_key_with_key_applies_key_function_before_splitting(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, lambda x: x.upper(), ns.I) == tuple( _number_extracter(s.upper(), _int_nosign_re, *int_nosafe_nolocale_nogroup))
def test_number_extracter_adds_leading_empty_string_if_input_begins_with_a_number_and_empty_string_between_numbers_for_py3safe_exmple(): assert _number_extracter('6a5+5.034e-1', _float_sign_exp_re, *float_safe_nolocale_nogroup) == ['', 6.0, 'a', 5.0, '', 0.5034]
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_doubles_letters_with_lowercase_version_with_groupletters_for_int_example(): assert _number_extracter('A5+5.034E-1', _int_nosign_re, *int_nosafe_nolocale_group) == ['aA', 5, '++', 5, '..', 34, 'eE--', 1]
def test_number_extracter_includes_plus_sign_and_exponent_in_float_definition_for_signed_exp_floats( ): assert _number_extracter('a5+5.034e-1', _float_sign_exp_re, *ffff) == ['a', 5.0, 0.5034]
def test_number_extracter_adds_leading_empty_string_if_input_begins_with_a_number( ): assert _number_extracter('6a5+5.034e-1', _float_sign_exp_re, *ffff) == ['', 6.0, 'a', 5.0, 0.5034]
def test_number_extracter_excludes_plus_sign_and_exponent_in_float_definition_for_unsigned_noexp_floats_example(): assert _number_extracter('a5+5.034e-1', _float_nosign_noexp_re, *float_nosafe_nolocale_nogroup) == ['a', 5.0, '+', 5.034, 'e-', 1.0]
def test_number_extracter_inserts_empty_string_between_floats_for_py3safe_option( ): assert _number_extracter('a5+5.034e-1', _float_sign_exp_re, *ftff) == ['a', 5.0, '', 0.5034]
def test_number_extracter_excludes_plus_sign_and_exponent_in_float_definition_for_unsigned_noexp_floats(x): assume(len(x) <= 10) 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 _number_extracter(s, _float_nosign_noexp_re, *float_nosafe_nolocale_nogroup) == float_splitter(s, False, False, False, '')
def test_number_extracter_includes_plus_and_minus_sign_in_int_definition_for_signed_ints( ): assert _number_extracter('a5+5.034e-1', _int_sign_re, *ifff) == ['a', 5, 5, '.', 34, 'e', -1]
def test_number_extracter_includes_plus_and_minus_sign_in_int_definition_for_signed_ints_example(): assert _number_extracter('a5+5.034e-1', _int_sign_re, *int_nosafe_nolocale_nogroup) == ['a', 5, 5, '.', 34, 'e', -1]
def test__natsort_key_with_TYPESAFE_inserts_spaces_between_numbers(x): # Turn on TYPESAFE to put a '' between adjacent numbers assume(len(x) <= 10) 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, ns.TYPESAFE | ns.S) == tuple(_number_extracter(s, _int_sign_re, *int_safe_nolocale_nogroup))
def test_number_extracter_includes_plus_and_minus_sign_in_int_definition_for_signed_ints(x): assume(len(x) <= 10) s = ''.join(repr(y) if type(y) in (float, long, int) else y for y in x) assert _number_extracter(s, _int_sign_re, *int_nosafe_nolocale_nogroup) == int_splitter(s, True, False, '')
def test_number_extracter_doubles_letters_with_lowercase_version_with_groupletters_for_int_example( ): assert _number_extracter( 'A5+5.034E-1', _int_nosign_re, *int_nosafe_nolocale_group) == ['aA', 5, '++', 5, '..', 34, 'eE--', 1]
def test_number_extracter_includes_plus_and_minus_sign_in_float_definition_but_excludes_exponent_for_signed_noexp_floats_example( ): assert _number_extracter( 'a5+5.034e-1', _float_sign_noexp_re, *float_nosafe_nolocale_nogroup) == ['a', 5.0, 5.034, 'e', -1.0]