Skip to content
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

Merged
merged 14 commits into from
Sep 20, 2019
3 changes: 3 additions & 0 deletions .baseline/checkstyle/custom-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
<suppressions>
<!-- Do not lint class which is mostly copied -->
<suppress files=".*StrictUnusedVariable.java" checks="." />

<!-- We have a special whitelist for AssertJ's assertThat method in test code, but we need it in prod code -->
<suppress files="src/main/java/com/palantir/baseline/refaster" checks="AvoidStaticImport" />
</suppressions>
3 changes: 2 additions & 1 deletion baseline-refaster-rules/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ apply plugin: 'java-library'
apply from: "${rootDir}/gradle/publish-jar.gradle"

dependencies {
compile 'com.google.errorprone:error_prone_refaster'
implementation 'com.google.errorprone:error_prone_refaster'
implementation 'org.assertj:assertj-core'

testCompile 'junit:junit'
testCompile project(':baseline-refaster-testing')
Expand Down
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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!

}

@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);
Copy link
Contributor

@carterkozak carterkozak Sep 18, 2019

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 assertThat(T) instead so that chained calls are also fixed.
i.e.

@AfterTemplate
void after(ListAssert<T> assert, int size) {
	assert.hasSize(size);
}

}
}
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,74 @@
/*
* (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> {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
}

@BeforeTemplate
void bad2(Collection<T> things) {
assertThat(things.isEmpty()).isTrue();
}

@BeforeTemplate
void bad3(Collection<T> things) {
assertThat(things.size() == 0).isTrue();
}

@BeforeTemplate
void bad4(Collection<T> things) {
assertThat(things).isEqualTo(Collections.emptyList());
}

@BeforeTemplate
void bad5(Collection<T> things) {
assertThat(things).isEqualTo(Collections.emptySet());
}

@BeforeTemplate
void bad6(Collection<T> things) {
assertThat(things).isEqualTo(ImmutableList.of());
}

@BeforeTemplate
void bad7(Collection<T> things) {
assertThat(things).isEqualTo(ImmutableSet.of());
}

@BeforeTemplate
void bad8(Collection<T> things) {
assertThat(things).hasSize(0);
}

@AfterTemplate
void after(Collection<T> things) {
assertThat(things).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* (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 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);
}

@BeforeTemplate
void bad4(Collection<T> things) {
assertThat(things.isEmpty()).isFalse();
}

@BeforeTemplate
void bad5(Collection<T> things) {
assertThat(!things.isEmpty()).isTrue();
}

@BeforeTemplate
void bad6(Collection<T> things) {
assertThat(things).isNotEqualTo(Collections.emptyList());
}

@BeforeTemplate
void bad7(Collection<T> things) {
assertThat(things).isNotEqualTo(Collections.emptySet());
}

@BeforeTemplate
void bad8(Collection<T> things) {
assertThat(things).isNotEqualTo(ImmutableList.of());
}

@BeforeTemplate
void bad9(Collection<T> things) {
assertThat(things).isNotEqualTo(ImmutableSet.of());
}

@AfterTemplate
void after(Collection<T> things) {
assertThat(things).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* (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.io.Files;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public final class AssertjFileContent<T> {

@BeforeTemplate
void before(File file, String expected) throws IOException {
assertThat(new String(Files.toByteArray(file), StandardCharsets.UTF_8)).isEqualTo(expected);
}

@BeforeTemplate
@SuppressWarnings("deprecation") // we're migrating people off a deprecated method
void before2(File file, String expected) throws IOException {
assertThat(Files.toString(file, StandardCharsets.UTF_8)).isEqualTo(expected);
}

@AfterTemplate
void after(File file, String expected) {
assertThat(file).hasContent(expected);
}
}
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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add equality checks? assertThat(foo).isEqualTo(Optional.of("foo") -> asserThat(foo).hasValue("foo")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually find the verbosity of the .isEqualTo(Optional.of("foo")) kinda reassuring... I think I could easily misinterpret what hasValue actually means...


@BeforeTemplate
void before(Optional<T> thing) {
assertThat(thing.isPresent()).isTrue();
}

@AfterTemplate
void after(Optional<T> thing) {
assertThat(thing).isPresent();
}
}
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-851.v2.yml
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