| 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 libcst.codemod import CodemodTest |
| 12 |
|
| 13 |
from hypothesis.extra import codemods |
| 14 |
|
| 15 |
|
| 16 |
def test_refactor_function_is_idempotent(): |
| 17 |
before = ( |
| 18 |
"from hypothesis.strategies import complex_numbers\n\n" |
| 19 |
"complex_numbers(None)\n" |
| 20 |
) |
| 21 |
after = codemods.refactor(before) |
| 22 |
assert before.replace("None", "min_magnitude=0") == after |
| 23 |
assert codemods.refactor(after) == after |
| 24 |
|
| 25 |
|
| 26 |
class TestFixComplexMinMagnitude(CodemodTest): |
| 27 |
TRANSFORM = codemods.HypothesisFixComplexMinMagnitude |
| 28 |
|
| 29 |
def test_noop(self) -> None: |
| 30 |
before = """ |
| 31 |
from hypothesis.strategies import complex_numbers, complex_numbers as cn |
| 32 |
|
| 33 |
complex_numbers(min_magnitude=1) # value OK |
| 34 |
complex_numbers(max_magnitude=None) # different argument |
| 35 |
|
| 36 |
class Foo: |
| 37 |
def complex_numbers(self, **kw): pass |
| 38 |
|
| 39 |
complex_numbers(min_magnitude=None) # defined in a different scope |
| 40 |
""" |
| 41 |
self.assertCodemod(before=before, after=before) |
| 42 |
|
| 43 |
def test_substitution(self) -> None: |
| 44 |
before = """ |
| 45 |
from hypothesis.strategies import complex_numbers, complex_numbers as cn |
| 46 |
|
| 47 |
complex_numbers(min_magnitude=None) # simple call to fix |
| 48 |
complex_numbers(min_magnitude=None, max_magnitude=1) # plus arg after |
| 49 |
complex_numbers(allow_infinity=False, min_magnitude=None) # plus arg before |
| 50 |
cn(min_magnitude=None) # imported as alias |
| 51 |
""" |
| 52 |
self.assertCodemod(before=before, after=before.replace("None", "0")) |
| 53 |
|
| 54 |
|
| 55 |
class TestFixPositionalKeywonlyArgs(CodemodTest): |
| 56 |
TRANSFORM = codemods.HypothesisFixPositionalKeywonlyArgs |
| 57 |
|
| 58 |
def test_substitution(self) -> None: |
| 59 |
before = """ |
| 60 |
import hypothesis.strategies as st |
| 61 |
|
| 62 |
st.floats(0, 1, False, False, 32) |
| 63 |
st.fractions(0, 1, 9) |
| 64 |
""" |
| 65 |
after = """ |
| 66 |
import hypothesis.strategies as st |
| 67 |
|
| 68 |
st.floats(0, 1, allow_nan=False, allow_infinity=False, width=32) |
| 69 |
st.fractions(0, 1, max_denominator=9) |
| 70 |
""" |
| 71 |
self.assertCodemod(before=before, after=after) |
| 72 |
|
| 73 |
def test_noop_with_new_floats_kw(self) -> None: |
| 74 |
before = """ |
| 75 |
import hypothesis.strategies as st |
| 76 |
|
| 77 |
st.floats(0, 1, False, False, True, 32, False, False) # allow_subnormal=True |
| 78 |
""" |
| 79 |
self.assertCodemod(before=before, after=before) |
| 80 |
|
| 81 |
def test_noop_if_unsure(self) -> None: |
| 82 |
before = """ |
| 83 |
import random |
| 84 |
|
| 85 |
if random.getrandbits(1): |
| 86 |
from hypothesis import target |
| 87 |
from hypothesis.strategies import lists as sets |
| 88 |
|
| 89 |
def fractions(*args): |
| 90 |
pass |
| 91 |
|
| 92 |
else: |
| 93 |
from hypothesis import target |
| 94 |
from hypothesis.strategies import fractions, sets |
| 95 |
|
| 96 |
fractions(0, 1, 9) |
| 97 |
sets(None, 1) |
| 98 |
target(0, 'label') |
| 99 |
""" |
| 100 |
after = before.replace("'label'", "label='label'") |
| 101 |
self.assertCodemod(before=before, after=after) |
| 102 |
|
| 103 |
def test_stateful_rule_noop(self): |
| 104 |
# `rule()(lambda self: None)` is a call with a positional argument, and |
| 105 |
# so we need an additional check that the "func" node is a Name rather than |
| 106 |
# itself being a Call, lest we rewrite the outer instead of the inner. |
| 107 |
# (this may be an upstream bug in metadata processing) |
| 108 |
before = """ |
| 109 |
from hypothesis.stateful import RuleBasedStateMachine, rule |
| 110 |
|
| 111 |
class MultipleRulesSameFuncMachine(RuleBasedStateMachine): |
| 112 |
rule1 = rule()(lambda self: None) |
| 113 |
""" |
| 114 |
self.assertCodemod(before=before, after=before) |
| 115 |
|
| 116 |
def test_kwargs_noop(self): |
| 117 |
before = """ |
| 118 |
from hypothesis import target |
| 119 |
|
| 120 |
kwargs = {"observation": 1, "label": "foobar"} |
| 121 |
target(**kwargs) |
| 122 |
""" |
| 123 |
self.assertCodemod(before=before, after=before) |
| 124 |
|
| 125 |
def test_noop_with_too_many_arguments_passed(self) -> None: |
| 126 |
# If there are too many arguments, we should leave this alone to raise |
| 127 |
# TypeError on older versions instead of deleting the additional args. |
| 128 |
before = """ |
| 129 |
import hypothesis.strategies as st |
| 130 |
|
| 131 |
st.sets(st.integers(), 0, 1, True) |
| 132 |
""" |
| 133 |
self.assertCodemod(before=before, after=before) |
| 134 |
|
| 135 |
|
| 136 |
class TestHealthCheckAll(CodemodTest): |
| 137 |
TRANSFORM = codemods.HypothesisFixHealthCheckAll |
| 138 |
|
| 139 |
def test_noop_other_attributes(self): |
| 140 |
# Test that calls to other attributes of HealthCheck are not modified |
| 141 |
before = "result = HealthCheck.data_too_large" |
| 142 |
self.assertCodemod(before=before, after=before) |
| 143 |
|
| 144 |
def test_substitution(self) -> None: |
| 145 |
# Test that HealthCheck.all() is replaced with list(HealthCheck) |
| 146 |
before = "result = HealthCheck.all()" |
| 147 |
after = "result = list(HealthCheck)" |
| 148 |
# self.assertEqual(run_codemod(input_code), expected_code) |
| 149 |
self.assertCodemod(before=before, after=after) |
| 150 |
|
| 151 |
|
| 152 |
class TestFixCharactersArguments(CodemodTest): |
| 153 |
TRANSFORM = codemods.HypothesisFixCharactersArguments |
| 154 |
|
| 155 |
def test_substitution(self) -> None: |
| 156 |
for in_, out in codemods.HypothesisFixCharactersArguments._replacements.items(): |
| 157 |
before = f""" |
| 158 |
import hypothesis.strategies as st |
| 159 |
st.characters({in_}=...) |
| 160 |
""" |
| 161 |
self.assertCodemod(before=before, after=before.replace(in_, out)) |
| 162 |
|
| 163 |
def test_remove_redundant_exclude_categories(self) -> None: |
| 164 |
args = "blacklist_categories=OUT, whitelist_categories=IN" |
| 165 |
before = f""" |
| 166 |
import hypothesis.strategies as st |
| 167 |
st.characters({args}) |
| 168 |
""" |
| 169 |
self.assertCodemod(before=before, after=before.replace(args, "categories=IN")) |