Skip to content

Commit bc1e84a

Browse files
Adjusting recognition of floats and doubles with regards to AssertJ assertions, given JavaTemplate.Matcher changes around matching X.0 style numbers. (#816)
1 parent f7b4bc2 commit bc1e84a

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed

src/main/java/org/openrewrite/java/testing/assertj/AssertJDoubleRules.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ static final class AbstractDoubleAssertIsEqualTo {
6464
@BeforeTemplate
6565
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert, double n) {
6666
return Refaster.anyOf(
67-
doubleAssert.isCloseTo(n, offset(0.0)), doubleAssert.isCloseTo(n, withPercentage(0.0)));
67+
doubleAssert.isCloseTo(n, offset(0.0)),
68+
doubleAssert.isCloseTo(n, offset(0d)),
69+
doubleAssert.isCloseTo(n, withPercentage(0)),
70+
doubleAssert.isCloseTo(n, withPercentage(0.0)),
71+
doubleAssert.isCloseTo(n, withPercentage(0d))
72+
);
6873
}
6974

7075
@AfterTemplate
@@ -82,7 +87,11 @@ static final class AbstractDoubleAssertIsNotEqualTo {
8287
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert, double n) {
8388
return Refaster.anyOf(
8489
doubleAssert.isNotCloseTo(n, offset(0.0)),
85-
doubleAssert.isNotCloseTo(n, withPercentage(0.0)));
90+
doubleAssert.isNotCloseTo(n, offset(0d)),
91+
doubleAssert.isNotCloseTo(n, withPercentage(0)),
92+
doubleAssert.isNotCloseTo(n, withPercentage(0.0)),
93+
doubleAssert.isNotCloseTo(n, withPercentage(0d))
94+
);
8695
}
8796

8897
@AfterTemplate
@@ -98,7 +107,11 @@ AbstractDoubleAssert<?> after(AbstractDoubleAssert<?> doubleAssert, double n) {
98107
static final class AbstractDoubleAssertIsZero {
99108
@BeforeTemplate
100109
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert) {
101-
return doubleAssert.isEqualTo(0);
110+
return Refaster.anyOf(
111+
doubleAssert.isEqualTo(0),
112+
doubleAssert.isEqualTo(0.0),
113+
doubleAssert.isEqualTo(0d)
114+
);
102115
}
103116

104117
@AfterTemplate
@@ -114,7 +127,11 @@ AbstractDoubleAssert<?> after(AbstractDoubleAssert<?> doubleAssert) {
114127
static final class AbstractDoubleAssertIsNotZero {
115128
@BeforeTemplate
116129
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert) {
117-
return doubleAssert.isNotEqualTo(0);
130+
return Refaster.anyOf(
131+
doubleAssert.isNotEqualTo(0),
132+
doubleAssert.isNotEqualTo(0.0),
133+
doubleAssert.isNotEqualTo(0d)
134+
);
118135
}
119136

120137
@AfterTemplate
@@ -130,7 +147,11 @@ AbstractDoubleAssert<?> after(AbstractDoubleAssert<?> doubleAssert) {
130147
static final class AbstractDoubleAssertIsOne {
131148
@BeforeTemplate
132149
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert) {
133-
return doubleAssert.isEqualTo(1);
150+
return Refaster.anyOf(
151+
doubleAssert.isEqualTo(1),
152+
doubleAssert.isEqualTo(1.0),
153+
doubleAssert.isEqualTo(1d)
154+
);
134155
}
135156

136157
@AfterTemplate

src/main/java/org/openrewrite/java/testing/assertj/AssertJFloatRules.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ static final class AbstractFloatAssertIsEqualTo {
6464
@BeforeTemplate
6565
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert, float n) {
6666
return Refaster.anyOf(
67-
floatAssert.isCloseTo(n, offset(0f)), floatAssert.isCloseTo(n, withPercentage(0)));
67+
floatAssert.isCloseTo(n, offset(0f)),
68+
floatAssert.isCloseTo(n, withPercentage(0)),
69+
floatAssert.isCloseTo(n, withPercentage(0.0)),
70+
floatAssert.isCloseTo(n, withPercentage(0d))
71+
);
6872
}
6973

7074
@AfterTemplate
@@ -81,7 +85,11 @@ static final class AbstractFloatAssertIsNotEqualTo {
8185
@BeforeTemplate
8286
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert, float n) {
8387
return Refaster.anyOf(
84-
floatAssert.isNotCloseTo(n, offset(0f)), floatAssert.isNotCloseTo(n, withPercentage(0)));
88+
floatAssert.isNotCloseTo(n, offset(0f)),
89+
floatAssert.isNotCloseTo(n, withPercentage(0)),
90+
floatAssert.isNotCloseTo(n, withPercentage(0.0)),
91+
floatAssert.isNotCloseTo(n, withPercentage(0d))
92+
);
8593
}
8694

8795
@AfterTemplate
@@ -97,7 +105,10 @@ AbstractFloatAssert<?> after(AbstractFloatAssert<?> floatAssert, float n) {
97105
static final class AbstractFloatAssertIsZero {
98106
@BeforeTemplate
99107
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert) {
100-
return floatAssert.isEqualTo(0);
108+
return Refaster.anyOf(
109+
floatAssert.isEqualTo(0),
110+
floatAssert.isEqualTo(0f)
111+
);
101112
}
102113

103114
@AfterTemplate
@@ -113,7 +124,10 @@ AbstractFloatAssert<?> after(AbstractFloatAssert<?> floatAssert) {
113124
static final class AbstractFloatAssertIsNotZero {
114125
@BeforeTemplate
115126
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert) {
116-
return floatAssert.isNotEqualTo(0);
127+
return Refaster.anyOf(
128+
floatAssert.isNotEqualTo(0),
129+
floatAssert.isNotEqualTo(0f)
130+
);
117131
}
118132

119133
@AfterTemplate
@@ -129,7 +143,10 @@ AbstractFloatAssert<?> after(AbstractFloatAssert<?> floatAssert) {
129143
static final class AbstractFloatAssertIsOne {
130144
@BeforeTemplate
131145
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert) {
132-
return floatAssert.isEqualTo(1);
146+
return Refaster.anyOf(
147+
floatAssert.isEqualTo(1),
148+
floatAssert.isEqualTo(1f)
149+
);
133150
}
134151

135152
@AfterTemplate

src/test/java/org/openrewrite/java/testing/assertj/AssertJDoubleRulesTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void isZero() {
4040
4141
class A {
4242
public void test(double d) {
43+
Assertions.assertThat(d).isEqualTo(0);
4344
Assertions.assertThat(d).isEqualTo(0.0);
4445
Assertions.assertThat(d).isEqualTo(0d);
4546
}
@@ -52,6 +53,7 @@ class A {
5253
public void test(double d) {
5354
Assertions.assertThat(d).isZero();
5455
Assertions.assertThat(d).isZero();
56+
Assertions.assertThat(d).isZero();
5557
}
5658
}
5759
"""
@@ -69,7 +71,9 @@ void isNotZero() {
6971
7072
class A {
7173
public void test(double d) {
74+
Assertions.assertThat(d).isNotEqualTo(0);
7275
Assertions.assertThat(d).isNotEqualTo(0.0);
76+
Assertions.assertThat(d).isNotEqualTo(0d);
7377
}
7478
}
7579
""",
@@ -79,6 +83,8 @@ public void test(double d) {
7983
class A {
8084
public void test(double d) {
8185
Assertions.assertThat(d).isNotZero();
86+
Assertions.assertThat(d).isNotZero();
87+
Assertions.assertThat(d).isNotZero();
8288
}
8389
}
8490
"""
@@ -99,6 +105,7 @@ void isCloseTo() {
99105
class A {
100106
public void test(double d, double expected) {
101107
Assertions.assertThat(d).isEqualTo(expected, offset(0.0));
108+
Assertions.assertThat(d).isEqualTo(expected, offset(0d));
102109
Assertions.assertThat(d).isEqualTo(expected, offset(1.0));
103110
}
104111
}
@@ -110,6 +117,7 @@ public void test(double d, double expected) {
110117
111118
class A {
112119
public void test(double d, double expected) {
120+
Assertions.assertThat(d).isEqualTo(expected);
113121
Assertions.assertThat(d).isEqualTo(expected);
114122
Assertions.assertThat(d).isCloseTo(expected, offset(1.0));
115123
}
@@ -133,7 +141,10 @@ void isEqualTo() {
133141
class A {
134142
public void test(double d, double expected) {
135143
Assertions.assertThat(d).isCloseTo(expected, offset(0.0));
144+
Assertions.assertThat(d).isCloseTo(expected, offset(0d));
136145
Assertions.assertThat(d).isCloseTo(expected, withPercentage(0));
146+
Assertions.assertThat(d).isCloseTo(expected, withPercentage(0.0));
147+
Assertions.assertThat(d).isCloseTo(expected, withPercentage(0d));
137148
}
138149
}
139150
""",
@@ -144,6 +155,9 @@ class A {
144155
public void test(double d, double expected) {
145156
Assertions.assertThat(d).isEqualTo(expected);
146157
Assertions.assertThat(d).isEqualTo(expected);
158+
Assertions.assertThat(d).isEqualTo(expected);
159+
Assertions.assertThat(d).isEqualTo(expected);
160+
Assertions.assertThat(d).isEqualTo(expected);
147161
}
148162
}
149163
"""
@@ -165,7 +179,10 @@ void isNotEqualTo() {
165179
class A {
166180
public void test(double d, double expected) {
167181
Assertions.assertThat(d).isNotCloseTo(expected, offset(0.0));
182+
Assertions.assertThat(d).isNotCloseTo(expected, offset(0d));
168183
Assertions.assertThat(d).isNotCloseTo(expected, withPercentage(0));
184+
Assertions.assertThat(d).isNotCloseTo(expected, withPercentage(0.0));
185+
Assertions.assertThat(d).isNotCloseTo(expected, withPercentage(0d));
169186
}
170187
}
171188
""",
@@ -176,6 +193,9 @@ class A {
176193
public void test(double d, double expected) {
177194
Assertions.assertThat(d).isNotEqualTo(expected);
178195
Assertions.assertThat(d).isNotEqualTo(expected);
196+
Assertions.assertThat(d).isNotEqualTo(expected);
197+
Assertions.assertThat(d).isNotEqualTo(expected);
198+
Assertions.assertThat(d).isNotEqualTo(expected);
179199
}
180200
}
181201
"""
@@ -193,7 +213,9 @@ void isOne() {
193213
194214
class A {
195215
public void test(double d) {
216+
Assertions.assertThat(d).isEqualTo(1);
196217
Assertions.assertThat(d).isEqualTo(1.0);
218+
Assertions.assertThat(d).isEqualTo(1d);
197219
}
198220
}
199221
""",
@@ -203,6 +225,8 @@ public void test(double d) {
203225
class A {
204226
public void test(double d) {
205227
Assertions.assertThat(d).isOne();
228+
Assertions.assertThat(d).isOne();
229+
Assertions.assertThat(d).isOne();
206230
}
207231
}
208232
"""

src/test/java/org/openrewrite/java/testing/assertj/AssertJFloatRulesTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ void isZero() {
4040
4141
class A {
4242
public void test(float f) {
43+
Assertions.assertThat(f).isEqualTo(0);
44+
Assertions.assertThat(f).isEqualTo(0f);
4345
Assertions.assertThat(f).isEqualTo(0.0f);
46+
Assertions.assertThat(f).isEqualTo(0F);
4447
Assertions.assertThat(f).isEqualTo(0.0F);
4548
}
4649
}
@@ -52,6 +55,9 @@ class A {
5255
public void test(float f) {
5356
Assertions.assertThat(f).isZero();
5457
Assertions.assertThat(f).isZero();
58+
Assertions.assertThat(f).isZero();
59+
Assertions.assertThat(f).isZero();
60+
Assertions.assertThat(f).isZero();
5561
}
5662
}
5763
"""
@@ -69,6 +75,8 @@ void isNotZero() {
6975
7076
class A {
7177
public void test(float f) {
78+
Assertions.assertThat(f).isNotEqualTo(0);
79+
Assertions.assertThat(f).isNotEqualTo(0f);
7280
Assertions.assertThat(f).isNotEqualTo(0.0f);
7381
}
7482
}
@@ -79,6 +87,8 @@ public void test(float f) {
7987
class A {
8088
public void test(float f) {
8189
Assertions.assertThat(f).isNotZero();
90+
Assertions.assertThat(f).isNotZero();
91+
Assertions.assertThat(f).isNotZero();
8292
}
8393
}
8494
"""
@@ -98,6 +108,7 @@ void isCloseTo() {
98108
99109
class A {
100110
public void test(float f, float expected) {
111+
Assertions.assertThat(f).isEqualTo(expected, offset(0f));
101112
Assertions.assertThat(f).isEqualTo(expected, offset(0.0f));
102113
Assertions.assertThat(f).isEqualTo(expected, offset(1.0f));
103114
}
@@ -110,6 +121,7 @@ public void test(float f, float expected) {
110121
111122
class A {
112123
public void test(float f, float expected) {
124+
Assertions.assertThat(f).isEqualTo(expected);
113125
Assertions.assertThat(f).isEqualTo(expected);
114126
Assertions.assertThat(f).isCloseTo(expected, offset(1.0f));
115127
}
@@ -166,6 +178,8 @@ class A {
166178
public void test(float f, float expected) {
167179
Assertions.assertThat(f).isNotCloseTo(expected, offset(0.0f));
168180
Assertions.assertThat(f).isNotCloseTo(expected, withPercentage(0));
181+
Assertions.assertThat(f).isNotCloseTo(expected, withPercentage(0.0));
182+
Assertions.assertThat(f).isNotCloseTo(expected, withPercentage(0d));
169183
}
170184
}
171185
""",
@@ -176,6 +190,8 @@ class A {
176190
public void test(float f, float expected) {
177191
Assertions.assertThat(f).isNotEqualTo(expected);
178192
Assertions.assertThat(f).isNotEqualTo(expected);
193+
Assertions.assertThat(f).isNotEqualTo(expected);
194+
Assertions.assertThat(f).isNotEqualTo(expected);
179195
}
180196
}
181197
"""
@@ -193,6 +209,8 @@ void isOne() {
193209
194210
class A {
195211
public void test(float f) {
212+
Assertions.assertThat(f).isEqualTo(1);
213+
Assertions.assertThat(f).isEqualTo(1f);
196214
Assertions.assertThat(f).isEqualTo(1.0f);
197215
}
198216
}
@@ -203,6 +221,8 @@ public void test(float f) {
203221
class A {
204222
public void test(float f) {
205223
Assertions.assertThat(f).isOne();
224+
Assertions.assertThat(f).isOne();
225+
Assertions.assertThat(f).isOne();
206226
}
207227
}
208228
"""

0 commit comments

Comments
 (0)