-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_fix_float_equality.py
More file actions
132 lines (103 loc) · 3.24 KB
/
Copy pathtest_fix_float_equality.py
File metadata and controls
132 lines (103 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from codemodder.codemods.test import BaseCodemodTest
from core_codemods.fix_float_equality import FixFloatEquality
class TestNumpyNanEquality(BaseCodemodTest):
codemod = FixFloatEquality
def test_name(self):
assert self.codemod.name == "fix-float-equality"
def test_change(self, tmpdir):
input_code = """
0.2 == 2 - 0.1
0.01 + 1 == 1
a = 1.0
b = 3.111
a != b
"""
expected = """
import math
math.isclose(0.2, 2 - 0.1, rel_tol=1e-09, abs_tol=0.0)
math.isclose(0.01 + 1, 1, rel_tol=1e-09, abs_tol=0.0)
a = 1.0
b = 3.111
not math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0)
"""
self.run_and_assert(tmpdir, input_code, expected, num_changes=3)
def test_change_resolve_float(self, tmpdir):
input_code = """
def foo(a, b):
return a == b + 0.1
def foo(b):
a = 1.02
return a != b + 2
"""
expected = """
import math
def foo(a, b):
return math.isclose(a, b + 0.1, rel_tol=1e-09, abs_tol=0.0)
def foo(b):
a = 1.02
return not math.isclose(a, b + 2, rel_tol=1e-09, abs_tol=0.0)
"""
self.run_and_assert(tmpdir, input_code, expected, num_changes=2)
def test_change_if_asserts(self, tmpdir):
input_code = """
a = 1.0
b = 3.111
if a != b:
pass
assert a == b and 1 + a != b
assert a != b
"""
expected = """
import math
a = 1.0
b = 3.111
if not math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
pass
assert math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0) and not math.isclose(1 + a, b, rel_tol=1e-09, abs_tol=0.0)
assert not math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0)
"""
self.run_and_assert(tmpdir, input_code, expected, num_changes=4)
def test_change_math_already_imported(self, tmpdir):
input_code = """
import math
math.e != 3 - 0.1
"""
expected = """
import math
not math.isclose(math.e, 3 - 0.1, rel_tol=1e-09, abs_tol=0.0)
"""
self.run_and_assert(tmpdir, input_code, expected, num_changes=1)
def test_no_change(self, tmpdir):
input_code = """
2 == 1 + 1
a = 3
b = 3
if a != b:
pass
a is b - 0.11
"""
self.run_and_assert(tmpdir, input_code, input_code)
def test_no_change_unknown_type(self, tmpdir):
input_code = """
def foo(a, b):
return a == b
def foo(a, b):
return a - 10 == b
def foo(b):
a = 1
return a == b + 1
"""
self.run_and_assert(tmpdir, input_code, input_code)
def test_exclude_line(self, tmpdir):
input_code = (
expected
) = """
0.2 == 2 - 0.1
"""
lines_to_exclude = [2]
self.run_and_assert(
tmpdir,
input_code,
expected,
lines_to_exclude=lines_to_exclude,
)