def test_characters_of_specific_groups(): st = characters(whitelist_categories=("Lu", "Nd")) find_any(st, lambda c: unicodedata.category(c) == "Lu") find_any(st, lambda c: unicodedata.category(c) == "Nd") assert_no_examples(st, lambda c: unicodedata.category(c) not in ("Lu", "Nd"))
def test_may_fill_with_nan_when_unique_is_set(): find_any( nps.arrays( dtype=float, elements=st.floats(allow_nan=False), shape=10, unique=True, fill=st.just(float('nan'))), lambda x: np.isnan(x).any() )
def test_exclude_characters_of_specific_groups(): st = characters(blacklist_categories=("Lu", "Nd")) find_any(st, lambda c: unicodedata.category(c) != "Lu") find_any(st, lambda c: unicodedata.category(c) != "Nd") assert_no_examples(st, lambda c: unicodedata.category(c) in ("Lu", "Nd"))
def test_can_use_recursive_data_in_sets(rnd): nested_sets = st.recursive( st.booleans(), lambda js: st.frozensets(js, average_size=2.0), max_leaves=10 ) find_any(nested_sets, random=rnd) def flatten(x): if isinstance(x, bool): return frozenset((x,)) else: result = frozenset() for t in x: result |= flatten(t) if len(result) == 2: break return result assert rnd is not None x = find( nested_sets, lambda x: len(flatten(x)) == 2, random=rnd, settings=settings(database=None, max_shrinks=1000, max_examples=1000)) assert x in ( frozenset((False, True)), frozenset((False, frozenset((True,)))), frozenset((frozenset((False, True)),)) )
def test_groups(pattern, is_unicode, invert): if u'd' in pattern.lower(): group_pred = is_digit elif u'w' in pattern.lower(): group_pred = is_word else: # Special behaviour due to \x1c, INFORMATION SEPARATOR FOUR group_pred = is_unicode_space if is_unicode else is_space if invert: pattern = pattern.swapcase() _p = group_pred def group_pred(s): return not _p(s) pattern = u'^%s\\Z' % (pattern,) compiler = unicode_regex if is_unicode else ascii_regex strategy = st.from_regex(compiler(pattern)) find_any(strategy.filter(group_pred), is_ascii) if is_unicode: find_any(strategy, lambda s: group_pred(s) and not is_ascii(s)) assert_all_examples(strategy, group_pred)
def test_different_keys_are_not_shared(): find_any( st.tuples( st.shared(st.integers(), key=1), st.shared(st.integers(), key=2)), lambda x: x[0] != x[1] )
def test_exclude_characters_of_specific_groups(): st = characters(blacklist_categories=('Lu', 'Nd')) find_any(st, lambda c: unicodedata.category(c) != 'Lu') find_any(st, lambda c: unicodedata.category(c) != 'Nd') assert_no_examples(st, lambda c: unicodedata.category(c) in ('Lu', 'Nd'))
def test_keys_and_default_are_not_shared(): find_any( st.tuples( st.shared(st.integers(), key=1), st.shared(st.integers())), lambda x: x[0] != x[1] )
def test_characters_of_specific_groups(): st = characters(whitelist_categories=('Lu', 'Nd')) find_any(st, lambda c: unicodedata.category(c) == 'Lu') find_any(st, lambda c: unicodedata.category(c) == 'Nd') assert_no_examples( st, lambda c: unicodedata.category(c) not in ('Lu', 'Nd'))
def test_uniqueness_does_not_affect_other_rows_2(): data_frames = pdst.data_frames([ pdst.column('A', dtype=int, unique=False), pdst.column('B', dtype=int, unique=True)], rows=st.tuples(st.integers(0, 10), st.integers(0, 10)), index=pdst.range_indexes(2, 2) ) find_any(data_frames, lambda x: x['A'][0] == x['A'][1])
def test_can_draw_sets_of_hard_to_find_elements(rnd): rarebool = floats(0, 1).map(lambda x: x <= 0.05) find_any( sets(rarebool, min_size=2), lambda x: True, random=rnd, settings=settings(database=None), )
def test_whitelisted_characters_override(): good_characters = u'teтестst' st = characters(min_codepoint=ord('0'), max_codepoint=ord('9'), whitelist_characters=good_characters) find_any(st, lambda c: c in good_characters) find_any(st, lambda c: c in '0123456789') assert_no_examples(st, lambda c: c not in good_characters + '0123456789')
def test_can_generate_ignored_tokens(): list_grammar = r""" list : "[" [STRING ("," STRING)*] "]" STRING : /"[a-z]*"/ WS : /[ \t\r\n]+/ %ignore WS """ strategy = from_lark(Lark(list_grammar, start="list")) # A JSON list of strings in canoncial form which does not round-trip, # must contain ignorable whitespace in the initial string. find_any(strategy, lambda s: "\t" in s)
def test_whitelisted_characters_override(): good_characters = u"teтестst" st = characters( min_codepoint=ord("0"), max_codepoint=ord("9"), whitelist_characters=good_characters, ) find_any(st, lambda c: c in good_characters) find_any(st, lambda c: c in "0123456789") assert_no_examples(st, lambda c: c not in good_characters + "0123456789")
def test_can_use_recursive_data_in_sets(rnd): nested_sets = st.recursive(st.booleans(), st.frozensets, max_leaves=3) find_any(nested_sets, random=rnd) def flatten(x): if isinstance(x, bool): return frozenset((x,)) else: result = frozenset() for t in x: result |= flatten(t) if len(result) == 2: break return result assert rnd is not None x = minimal(nested_sets, lambda x: len(flatten(x)) == 2, random=rnd) assert x in ( frozenset((False, True)), frozenset((False, frozenset((True,)))), frozenset((frozenset((False, True)),)), )
def test_subpattern_flags(): strategy = st.from_regex(u'(?i)a(?-i:b)') # "a" is case insensitive find_any(strategy, lambda s: s[0] == u'a') find_any(strategy, lambda s: s[0] == u'A') # "b" is case sensitive find_any(strategy, lambda s: s[1] == u'b') assert_no_examples(strategy, lambda s: s[1] == u'B')
def test_subpattern_flags(): strategy = st.from_regex(u"(?i)\\Aa(?-i:b)\\Z") # "a" is case insensitive find_any(strategy, lambda s: s[0] == u"a") find_any(strategy, lambda s: s[0] == u"A") # "b" is case sensitive find_any(strategy, lambda s: s[1] == u"b") assert_no_examples(strategy, lambda s: s[1] == u"B")
def test_step_will_be_negative(size): find_any(st.slices(size), lambda x: x.step < 0, settings(max_examples=10**6))
def test_can_find_on_the_minute(): find_any(datetimes(), lambda x: x.second == 0)
def test_start_will_equal_size(size): find_any(st.slices(size), lambda x: x.start == size - 1, settings(max_examples=10**6))
def test_can_generate_time_with_fold_1(): find_any(times(), lambda d: d.fold)
def test_single_date(val): assert find_any(dates(val, val)) is val
def test_single_timedelta(val): assert find_any(timedeltas(val, val)) is val
def test_any_with_dotall_generate_newline_binary(pattern): find_any( st.from_regex(pattern), lambda s: s == b"\n", settings(max_examples=10 ** 6) )
def test_marks_unknown_features_as_enabled(): x = find_any(STRAT, lambda v: True) assert x.is_enabled("fish")
def test_can_all_be_enabled(): find_any(STRAT, lambda x: all(x.is_enabled(i) for i in range(100)))
def test_half_bounded_generates_zero(): find_any(st.floats(min_value=-1.0), lambda x: x == 0.0) find_any(st.floats(max_value=1.0), lambda x: x == 0.0)
def test_half_bounded_generates_endpoint(): find_any(st.floats(min_value=-1.0), lambda x: x == -1.0) find_any(st.floats(max_value=-1.0), lambda x: x == -1.0)
def test_can_fill_series(): nan_backed = pdst.series(elements=st.floats(allow_nan=False), fill=st.just(float("nan"))) find_any(nan_backed, lambda x: np.isnan(x).any())
def test_can_draw_empty_set_from_unsatisfiable_strategy(): assert find_any(sets(integers().filter(lambda s: False))) == set()
def test_start_will_equal_0(size): find_any(st.slices(size), lambda x: x.start == 0)
def test_can_find_each_month(month): find_any(dates(), lambda x: x.month == month, settings(max_examples=10**6))
def test_can_generate_naive_time(): find_any(times(), lambda d: not d.tzinfo)
def test_can_pad_empty_strings(): find_any(st.from_regex(u''), bool) find_any(st.from_regex(b''), bool)
def test_end(): strategy = st.from_regex("\\Aabc$") find_any(strategy, lambda s: s == "abc") find_any(strategy, lambda s: s == "abc\n")
def test_given_multiline_regex_can_insert_after_dollar(): find_any( st.from_regex(re.compile(u"\Ahi$", re.MULTILINE)), lambda x: '\n' in x and x.split(u"\n")[1] )
def test_can_find_off_the_second(): find_any(timedeltas(), lambda x: x.seconds != 0)
def test_bare_caret_can_produce(): find_any(st.from_regex(u'^'), bool)
def test_generate_arrays_from_shapes_strategy(xp, xps): """Generate arrays from shapes strategy.""" find_any(xps.arrays(xp.int8, xps.array_shapes()))
def test_shared_union(): # This gets parsed as [(ANY, None), (BRANCH, (None, [[], []]))], the # interesting feature of which is that it contains empty sub-expressions # in the branch. find_any(st.from_regex('.|.'))
def test_unsized_strings_length_gt_one(dtype): # See https://github.com/HypothesisWorks/hypothesis/issues/2229 find_any(nps.arrays(dtype=dtype, shape=1), lambda arr: len(arr[0]) >= 2)
def test_step_will_be_positive(size): find_any(st.slices(size), lambda x: x.step > 0)
def test_generic_origin_empty(): with pytest.raises(ResolutionFailed): find_any(st.builds(using_generic))
def test_generic_sequence_of_integers_may_be_lists_or_bytes(): strat = st.from_type(Sequence[int]) find_any(strat, lambda x: isinstance(x, bytes)) find_any(strat, lambda x: isinstance(x, list))
def test_can_pad_strings_with_newlines(): find_any(st.from_regex(u'^$'), bool) find_any(st.from_regex(b'^$'), bool)
def test_find_any_non_empty(strategy): find_any(strategy, lambda s: len(s) > 0)
def test_given_multiline_regex_can_insert_before_caret(): find_any( st.from_regex(re.compile(u"^hi\Z", re.MULTILINE)), lambda x: '\n' in x and x.split(u"\n")[0] )
def test_can_fill_series(): nan_backed = pdst.series( elements=st.floats(allow_nan=False), fill=st.just(float("nan")) ) find_any(nan_backed, lambda x: np.isnan(x).any())
def test_bare_dollar_can_produce(): find_any(st.from_regex(u'$'), bool)
def test_can_find_midnight(): find_any(times(), lambda x: x.hour == x.minute == x.second == 0)
def test_can_find_each_month(): for month in hrange(1, 13): find_any(datetimes(), lambda x: x.month == month)
def test_may_reuse_distinct_integers_if_asked(): find_any( arrays('uint64', 10, distinct_integers(), fill=distinct_integers()), lambda x: len(set(x)) < len(x))
def test_can_generate_naive_datetime(): find_any(datetimes(allow_naive=True), lambda d: d.tzinfo is None)
def test_issue_3080(): # Check for https://github.com/HypothesisWorks/hypothesis/issues/3080 s = st.from_type(typing.Union[list[int], int]) find_any(s, lambda x: isinstance(x, int)) find_any(s, lambda x: isinstance(x, list))
def test_different_instances_are_not_shared(): find_any( st.tuples(st.shared(st.integers()), st.shared(st.integers())), lambda x: x[0] != x[1] )
def test_literals_with_ignorecase(pattern): strategy = st.from_regex(pattern) find_any(strategy, lambda s: s == "a") find_any(strategy, lambda s: s == "A")
def test_native_unions(): s = st.from_type(int | list[str]) find_any(s, lambda x: isinstance(x, int)) find_any(s, lambda x: isinstance(x, list))
def test_can_find_off_the_minute(): find_any(times(), lambda x: x.second != 0)
def test_may_reuse_distinct_integers_if_asked(): find_any( arrays('uint64', 10, distinct_integers(), fill=distinct_integers()), lambda x: len(set(x)) < len(x) )
def test_bytestring_is_valid_sequence_of_int_and_parent_classes(type_): find_any( st.from_type(typing.Sequence[type_]), lambda val: isinstance(val, typing.ByteString), )