-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some low-effort AssertJ refasters #851
Changes from 9 commits
35535f1
f41a861
30edf49
504a8e4
2aa27de
a658b08
a8048c3
4f624b5
afa93f5
6f6b6b2
c5497a2
4ac72f2
37517d6
3f3b2cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Collection; | ||
|
||
public final class AssertjCollectionHasSizeExactly<T> { | ||
|
||
@BeforeTemplate | ||
void bad1(Collection<T> things, int size) { | ||
assertThat(things.size() == size).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad2(Collection<T> things, int size) { | ||
assertThat(things.size()).isEqualTo(size); | ||
} | ||
|
||
@AfterTemplate | ||
void after(Collection<T> things, int size) { | ||
assertThat(things).hasSize(size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any way we can make this work with assertions that have descriptions without duplicating each refaster rule? Example: assertThat(things.size()).describedAs("size was not updated").isEqualTo(size); I would like that to be updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what we want to do in all of these cases is to match against the return type of
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Collection; | ||
|
||
public final class AssertjCollectionHasSizeGreaterThan<T> { | ||
|
||
@BeforeTemplate | ||
void before(Collection<T> things, int size) { | ||
assertThat(things.size() > size).isTrue(); | ||
} | ||
|
||
@AfterTemplate | ||
void after(Collection<T> things, int size) { | ||
assertThat(things).hasSizeGreaterThan(size); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public final class AssertjCollectionIsEmpty<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be cleaner to make all these inner classes of an AssertJ refaster class. Keep the verbose but related stuff together There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh, all the files have the same prefix, I think they're pretty easy to navigate! |
||
|
||
@BeforeTemplate | ||
void bad1(Collection<T> things) { | ||
assertThat(things.size() == 0).isTrue(); | ||
iamdanfox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@BeforeTemplate | ||
void bad2(Collection<T> things) { | ||
assertThat(things.size()).isEqualTo(0); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad3(Collection<T> things) { | ||
assertThat(things).isEqualTo(Collections.emptyList()); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad4(Collection<T> things) { | ||
assertThat(things).isEqualTo(Collections.emptySet()); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad5(Collection<T> things) { | ||
assertThat(things).isEqualTo(ImmutableList.of()); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad6(Collection<T> things) { | ||
assertThat(things).isEqualTo(ImmutableSet.of()); | ||
} | ||
|
||
@AfterTemplate | ||
void after(Collection<T> things) { | ||
assertThat(things).isEmpty(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Collection; | ||
|
||
public final class AssertjCollectionIsNotEmpty<T> { | ||
|
||
@BeforeTemplate | ||
void bad1(Collection<T> things) { | ||
assertThat(things.size() != 0).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad2(Collection<T> things) { | ||
assertThat(things.size() == 0).isFalse(); | ||
} | ||
|
||
@BeforeTemplate | ||
void bad3(Collection<T> things) { | ||
assertThat(things.size()).isNotEqualTo(0); | ||
iamdanfox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@AfterTemplate | ||
void after(Collection<T> things) { | ||
assertThat(things).isNotEmpty(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Optional; | ||
|
||
public final class AssertjOptionalIsPresent<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add equality checks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually find the verbosity of the |
||
|
||
@BeforeTemplate | ||
void before(Optional<T> thing) { | ||
assertThat(thing.isPresent()).isTrue(); | ||
} | ||
|
||
@AfterTemplate | ||
void after(Optional<T> thing) { | ||
assertThat(thing).isPresent(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: improvement | ||
improvement: | ||
description: Some AssertJ assertions can now be automatically replaced with more | ||
idiomatic ones using refaster. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/851 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this play with https://github.com/palantir/gradle-baseline/blob/develop/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/CollectionsIsEmpty.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if this one refasters it to hasSize(0), the other one will get it on the next pass, so it'll be eventually consistent!