本文整理汇总了Python中hypothesis.strategies.recursive方法的典型用法代码示例。如果您正苦于以下问题:Python strategies.recursive方法的具体用法?Python strategies.recursive怎么用?Python strategies.recursive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hypothesis.strategies
的用法示例。
在下文中一共展示了strategies.recursive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_json_dumps
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def test_json_dumps(value):
"""Checks that value is serialisable as JSON."""
# We expect this test to always pass - the point of this exercise is
# to define a recursive strategy, and then investigate the values it
# generates for a *passing* test.
hypothesis.note("type: {}".format(type(value)))
hypothesis.event("type: {}".format(type(value)))
json.dumps(value)
# Takeaway: you've seen and played with a few ways to see what a
# passing test is doing, without having to inject a failure.
##############################################################################
# `@st.composite` exercise
# This goal of this exercise is to play with a contrived data dependency,
# using a composite strategy to generate inputs. You can use the same tricks
# as above to check what's being generated, so try to keep the test passing!
示例2: action_structures
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def action_structures(draw):
"""
A Hypothesis strategy that creates a tree of L{ActionStructure} and
L{unicode}.
"""
tree = draw(st.recursive(labels, st.lists, max_leaves=20))
def to_structure(tree_or_message):
if isinstance(tree_or_message, list):
return ActionStructure(
type=draw(labels),
failed=draw(st.booleans()),
children=[to_structure(o) for o in tree_or_message],
)
else:
return tree_or_message
return to_structure(tree)
示例3: assert_roundtrip
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def assert_roundtrip(obj):
"""Assert that we can successfully round-trip the given object."""
buf = pencode(obj)
assert isinstance(buf, bytes)
obj2 = pdecode(buf)
assert type(obj) == type(obj2)
# By definition NaN does not equal anything, even itself
if isinstance(obj2, float) and math.isnan(obj2):
return obj2
try:
assert obj == obj2
except RuntimeError as e:
if 'maximum recursion depth exceeded' not in e.args[0]:
raise
# If we hit a RecursionError, we correctly decoded a recursive
# structure, so test passes :)
except RecursionError:
pass
return obj2
示例4: _create_hyp_nested_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def _create_hyp_nested_strategy(simple_class_strategy):
"""
Create a recursive attrs class.
Given a strategy for building (simpler) classes, create and return
a strategy for building classes that have as an attribute:
* just the simpler class
* a list of simpler classes
* a dict mapping the string "cls" to a simpler class.
"""
# A strategy producing tuples of the form ([list of attributes], <given
# class strategy>).
attrs_and_classes = st.tuples(
lists_of_attrs(defaults=True), simple_class_strategy
)
return (
attrs_and_classes.flatmap(just_class)
| attrs_and_classes.flatmap(just_class_with_type)
| attrs_and_classes.flatmap(list_of_class)
| attrs_and_classes.flatmap(list_of_class_with_type)
| attrs_and_classes.flatmap(dict_of_class)
)
示例5: regex_patterns
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def regex_patterns(draw: Any) -> str:
"""Return a recursive strategy for simple regular expression patterns."""
fragments = st.one_of(
st.just("."),
st.from_regex(r"\[\^?[A-Za-z0-9]+\]"),
REGEX_PATTERNS.map("{}+".format),
REGEX_PATTERNS.map("{}?".format),
REGEX_PATTERNS.map("{}*".format),
)
result = draw(st.lists(fragments, min_size=1, max_size=3).map("".join))
assert isinstance(result, str)
try:
re.compile(result)
except re.error:
assume(False)
return result
示例6: add_bools
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def add_bools(list_of_lists):
"""
Given recursive list that can contain other lists, return tuple of that plus
a booleans strategy for each list.
"""
l = []
def count(recursive):
l.append(1)
for child in recursive:
if isinstance(child, list):
count(child)
count(list_of_lists)
return st.tuples(st.just(list_of_lists), st.tuples(*[st.sampled_from([True, False]) for i in l]))
示例7: from_json
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def from_json(cls, string):
"""Decodes a JSON-string back into a `Record` instance
This is a *bad* method. This needs to be fixed
"""
value = string
return cls(value)
# We can define recursive strategies like so:
示例8: test_map_odd_numbers
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def test_map_odd_numbers(x):
assert x[-1] in "13579"
# Takeaway
# --------
# `.map` permits us to extend Hypothesis' core strategies in powerful
# ways. See that it can be used to affect the individual values being
# produced by a strategy (e.g. mapping integers to even-valued
# integers), as well as to cast the values to a different type (e.g.
# mapping an integer to a string.
#
# If it seems like a data-type is missing from Hypothesis'
# strategies, then it is likely that a simple application of `.map`
# will suffice. E.g. suppose you want a strategy that generate deques,
# then
# `deque_strat = st.lists(...).map(deque)`
# will serve nicely - we don't even need a lambda!
##############################################################################
# Defining recursive data.
# There are a couple of ways to define recursive data with Hypothesis,
# leaning on the fact that strategies are lazily instantiated.
#
# `st.recursive` takes a base strategy, and a function that takes a strategy
# and returns an extended strategy. All good if we want that structure!
# If you want mutual recursion though, or have a complicated kind of data
# (or just limited time in a tutorial), `st.deferred` is the way to go.
#
# The `Record` exercise in pbt-101.py defined JSON using `st.recursive`,
# if you want to compare them, and has some extension problems that you
# could write as tests here instead.
# JSON values are defined as one of null, false, true, a finite number,
# a string, an array of json values, or a dict of string to json values.
示例9: hashable_containers
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def hashable_containers(primitives):
def extend(base):
return st.one_of(
st.frozensets(base, max_size=50),
st.lists(base, max_size=50).map(tuple),
)
return st.recursive(primitives, extend)
示例10: containers
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def containers(primitives):
def extend(base):
return st.one_of(
st.lists(base, max_size=50),
st.lists(base, max_size=50).map(tuple),
st.dictionaries(
keys=hashable_containers(primitives),
values=base,
max_size=10
),
)
return st.recursive(primitives, extend, max_leaves=50)
示例11: nested_dictionaries
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def nested_dictionaries():
simple_strings_alphabet = 'abcdefghijklmnopqrstuvwxyz\'"\r\n '
simple_text = st.text(alphabet=simple_strings_alphabet, min_size=5)
def extend(base):
return st.one_of(
st.lists(base, min_size=5),
st.dictionaries(keys=simple_text, values=base, min_size=1)
)
return st.recursive(simple_text, extend, max_leaves=50)
示例12: json
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def json(value_limit=5):
"""Hypothesis strategy for generating values that can be passed to
`json.dumps` to produce valid JSON data.
:param value_limit: A limit on the number of values in the JSON data -
setting this too high can cause value generation to
time out.
:type value_limit: int
"""
return hy_st.recursive(
hy_st.floats() | hy_st.booleans() | hy_st.text() | hy_st.none(),
lambda children: hy_st.dictionaries(hy_st.text(), children),
max_leaves=value_limit)
示例13: simple_classes
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def simple_classes(defaults=None, min_attrs=0):
"""
Return a strategy that yields tuples of simple classes and values to
instantiate them.
"""
return lists_of_attrs(defaults, min_size=min_attrs).flatmap(
_create_hyp_class
)
# Ok, so st.recursive works by taking a base strategy (in this case,
# simple_classes) and a special function. This function receives a strategy,
# and returns another strategy (building on top of the base strategy).
示例14: test_nestedInteractions
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def test_nestedInteractions(self, values):
"""
Nested interactions operate independently of parent interactions.
:param values: a two-tuple composed of:
- a recursive list of unicode and other recursive lists - list start
means begin interaction, string means node resolve, list end means
finish interaction.
- list of False/True; True means failed interaction
"""
requested_interactions, failures = values
failures = iter(failures)
assume(not isinstance(requested_interactions, unicode))
self.init()
ws_actor = self.connector.expectSocket()
self.connector.connect(ws_actor)
failures = iter(failures)
created_services = {}
expected_success_nodes = Counter()
expected_failed_nodes = Counter()
def run_interaction(children):
should_fail = next(failures)
failed = []
succeeded = []
self.session.start_interaction()
for child in children:
if isinstance(child, unicode):
# Make sure disco knows about the node:
if child in created_services:
node = created_services[child]
else:
node = create_node(child, child)
created_services[child] = node
self.disco.onMessage(None, NodeActive(node))
# Make sure the child Node is resolved in the interaction
self.session.resolve(node.service, "1.0")
if should_fail:
expected_failed_nodes[node] += 1
failed.append(node)
else:
expected_success_nodes[node] += 1
succeeded.append(node)
else:
run_interaction(child)
if should_fail:
self.session.fail_interaction("OHNO")
self.session.finish_interaction()
self.connector.advance_time(5.0) # Make sure interaction is sent
ws_actor.swallowLogMessages()
self.connector.expectInteraction(
self, ws_actor, self.session, failed, succeeded)
run_interaction(requested_interactions)
for node in set(expected_failed_nodes) | set(expected_success_nodes):
policy = self.disco.failurePolicy(node)
self.assertEqual((policy.successes, policy.failures),
(expected_success_nodes[node],
expected_failed_nodes[node]))
示例15: _create_hyp_nested_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import recursive [as 别名]
def _create_hyp_nested_strategy(simple_class_strategy):
"""
Create a recursive attrs class.
Given a strategy for building (simpler) classes, create and return
a strategy for building classes that have as an attribute: either just
the simpler class, a list of simpler classes, a tuple of simpler classes,
an ordered dict or a dict mapping the string "cls" to a simpler class.
"""
# Use a tuple strategy to combine simple attributes and an attr class.
def just_class(tup):
combined_attrs = list(tup[0])
combined_attrs.append(attr.ib(default=attr.Factory(tup[1])))
return _create_hyp_class(combined_attrs)
def list_of_class(tup):
default = attr.Factory(lambda: [tup[1]()])
combined_attrs = list(tup[0])
combined_attrs.append(attr.ib(default=default))
return _create_hyp_class(combined_attrs)
def tuple_of_class(tup):
default = attr.Factory(lambda: (tup[1](),))
combined_attrs = list(tup[0])
combined_attrs.append(attr.ib(default=default))
return _create_hyp_class(combined_attrs)
def dict_of_class(tup):
default = attr.Factory(lambda: {"cls": tup[1]()})
combined_attrs = list(tup[0])
combined_attrs.append(attr.ib(default=default))
return _create_hyp_class(combined_attrs)
def ordereddict_of_class(tup):
default = attr.Factory(lambda: OrderedDict([("cls", tup[1]())]))
combined_attrs = list(tup[0])
combined_attrs.append(attr.ib(default=default))
return _create_hyp_class(combined_attrs)
# A strategy producing tuples of the form ([list of attributes], <given
# class strategy>).
attrs_and_classes = st.tuples(list_of_attrs, simple_class_strategy)
return st.one_of(
attrs_and_classes.map(just_class),
attrs_and_classes.map(list_of_class),
attrs_and_classes.map(tuple_of_class),
attrs_and_classes.map(dict_of_class),
attrs_and_classes.map(ordereddict_of_class),
)