| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from hypothesis import given, strategies as st |
| 12 |
from hypothesis.strategies._internal.featureflags import FeatureFlags, FeatureStrategy |
| 13 |
|
| 14 |
from tests.common.debug import find_any, minimal |
| 15 |
|
| 16 |
STRAT = FeatureStrategy() |
| 17 |
|
| 18 |
|
| 19 |
def test_can_all_be_enabled(): |
| 20 |
find_any(STRAT, lambda x: all(x.is_enabled(i) for i in range(100))) |
| 21 |
|
| 22 |
|
| 23 |
def test_minimizes_open(): |
| 24 |
features = range(10) |
| 25 |
|
| 26 |
flags = minimal(STRAT, lambda x: [x.is_enabled(i) for i in features]) |
| 27 |
|
| 28 |
assert all(flags.is_enabled(i) for i in features) |
| 29 |
|
| 30 |
|
| 31 |
def test_minimizes_individual_features_to_open(): |
| 32 |
features = list(range(10)) |
| 33 |
|
| 34 |
flags = minimal( |
| 35 |
STRAT, lambda x: sum(x.is_enabled(i) for i in features) < len(features) |
| 36 |
) |
| 37 |
|
| 38 |
assert all(flags.is_enabled(i) for i in features[:-1]) |
| 39 |
assert not flags.is_enabled(features[-1]) |
| 40 |
|
| 41 |
|
| 42 |
def test_marks_unknown_features_as_enabled(): |
| 43 |
x = find_any(STRAT, lambda v: True) |
| 44 |
|
| 45 |
assert x.is_enabled("fish") |
| 46 |
|
| 47 |
|
| 48 |
def test_by_default_all_enabled(): |
| 49 |
f = FeatureFlags() |
| 50 |
|
| 51 |
assert f.is_enabled("foo") |
| 52 |
|
| 53 |
|
| 54 |
def test_eval_featureflags_repr(): |
| 55 |
flags = FeatureFlags(enabled=["on"], disabled=["off"]) |
| 56 |
assert flags.is_enabled("on") |
| 57 |
assert not flags.is_enabled("off") |
| 58 |
flags2 = eval(repr(flags)) |
| 59 |
assert flags2.is_enabled("on") |
| 60 |
assert not flags2.is_enabled("off") |
| 61 |
|
| 62 |
|
| 63 |
@given(st.data()) |
| 64 |
def test_repr_can_be_evalled(data): |
| 65 |
flags = data.draw(STRAT) |
| 66 |
|
| 67 |
features = data.draw(st.lists(st.text(), unique=True)) |
| 68 |
|
| 69 |
for f in features: |
| 70 |
flags.is_enabled(f) |
| 71 |
|
| 72 |
flags2 = eval(repr(flags)) |
| 73 |
|
| 74 |
for f in features: |
| 75 |
assert flags2.is_enabled(f) == flags.is_enabled(f) |
| 76 |
|
| 77 |
more_features = data.draw(st.lists(st.text().filter(lambda s: s not in features))) |
| 78 |
|
| 79 |
for f in more_features: |
| 80 |
assert flags2.is_enabled(f) |
| 81 |
|
| 82 |
|
| 83 |
@given(FeatureStrategy(at_least_one_of={"a", "b", "c"})) |
| 84 |
def test_can_avoid_disabling_every_flag(flags): |
| 85 |
assert any(flags.is_enabled(k) for k in {"a", "b", "c"}) |