Skip to content

Commit 42ee3be

Browse files
committed
Allow explicit construction of FeatureFlags
1 parent 02e4a30 commit 42ee3be

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

hypothesis-python/src/hypothesis/searchstrategy/featureflags.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ class FeatureFlags(object):
4040
required disabled features.
4141
"""
4242

43-
def __init__(self, data):
43+
def __init__(self, data=None, enabled=(), disabled=()):
4444
self.__data = data
4545
self.__decisions = {}
4646

47+
for f in enabled:
48+
self.__decisions[f] = 0
49+
50+
for f in disabled:
51+
self.__decisions[f] = 255
52+
4753
# In the original swarm testing paper they turn features on or off
4854
# uniformly at random. Instead we decide the probability with which to
4955
# enable features up front. This can allow for scenarios where all or
@@ -54,12 +60,17 @@ def __init__(self, data):
5460
# score >= that value. In particular when self.__baseline is 0, all
5561
# features will be enabled. This is so that we shrink in the direction
5662
# of more features being enabled.
57-
self.__baseline = data.draw_bits(8)
63+
if self.__data is not None:
64+
self.__baseline = data.draw_bits(8)
65+
else:
66+
# If data is None we're in example mode so all that matters is the
67+
# enabled/disabled lists above. We set this up so that
68+
self.__baseline = 1
5869

5970
def is_enabled(self, name):
6071
"""Tests whether the feature named ``name`` should be enabled on this
6172
test run."""
62-
if self.__data.frozen:
73+
if self.__data is None or self.__data.frozen:
6374
# Feature set objects might hang around after data generation has
6475
# finished. If this happens then we just report all new features as
6576
# enabled, because that's our shrinking direction and they have no

hypothesis-python/tests/cover/test_feature_flags.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
from __future__ import absolute_import, division, print_function
1919

20+
from hypothesis import given, strategies as st
2021
from hypothesis.internal.compat import hrange
21-
from hypothesis.searchstrategy.featureflags import FeatureStrategy
22+
from hypothesis.searchstrategy.featureflags import FeatureFlags, FeatureStrategy
2223
from tests.common.debug import find_any, minimal
2324

2425
STRAT = FeatureStrategy()
@@ -55,3 +56,29 @@ def test_marks_unknown_features_as_enabled():
5556
x = find_any(STRAT, lambda v: True)
5657

5758
assert x.is_enabled("fish")
59+
60+
61+
def test_by_default_all_enabled():
62+
f = FeatureFlags()
63+
64+
assert f.is_enabled("foo")
65+
66+
67+
@given(st.data())
68+
def test_repr_can_be_evalled(data):
69+
flags = data.draw(STRAT)
70+
71+
features = data.draw(st.lists(st.text(), unique=True))
72+
73+
for f in features:
74+
flags.is_enabled(f)
75+
76+
flags2 = eval(repr(flags))
77+
78+
for f in features:
79+
assert flags2.is_enabled(f) == flags.is_enabled(f)
80+
81+
more_features = data.draw(st.lists(st.text().filter(lambda s: s not in features)))
82+
83+
for f in more_features:
84+
assert flags2.is_enabled(f)

0 commit comments

Comments
 (0)