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

Refaster for AssertJ isZero/isNotZero/isOne and collections #881

Merged
merged 3 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

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.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Collection;
import java.util.Collections;

public final class AssertjCollectionIsEmpty<T> {

Expand All @@ -45,32 +42,7 @@ void bad3(Collection<T> things) {
}

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

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

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

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

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

@BeforeTemplate
void bad9(Collection<T> things) {
void bad4(Collection<T> things) {
assertThat(things.size()).isEqualTo(0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* (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 com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.util.Collections;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractIterableAssert;

public final class AssertjCollectionIsEmpty2<A extends AbstractIterableAssert<A, I, T, E>,
I extends Iterable<? extends T>, T, E extends AbstractAssert<E, T>> {

@BeforeTemplate
void before1(A in) {
in.hasSize(0);
}

@BeforeTemplate
void before2(A in) {
in.isEqualTo(Refaster.anyOf(
ImmutableList.of(),
ImmutableSet.of(),
Collections.emptySet(),
Collections.emptyList()));
}

@AfterTemplate
void after(A in) {
in.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

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.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Collection;
import java.util.Collections;

public final class AssertjCollectionIsEmptyWithDescription<T> {

Expand All @@ -45,32 +42,7 @@ void bad3(Collection<T> things, String description) {
}

@BeforeTemplate
void bad4(Iterable<T> things, String description) {
assertThat(things).describedAs(description).isEqualTo(Collections.emptyList());
}

@BeforeTemplate
void bad5(Iterable<T> things, String description) {
assertThat(things).describedAs(description).isEqualTo(Collections.emptySet());
}

@BeforeTemplate
void bad6(Iterable<T> things, String description) {
assertThat(things).describedAs(description).isEqualTo(ImmutableList.of());
}

@BeforeTemplate
void bad7(Iterable<T> things, String description) {
assertThat(things).describedAs(description).isEqualTo(ImmutableSet.of());
}

@BeforeTemplate
void bad8(Iterable<T> things, String description) {
assertThat(things).describedAs(description).hasSize(0);
}

@BeforeTemplate
void bad9(Collection<T> things, String description) {
void bad4(Collection<T> things, String description) {
assertThat(things.size()).describedAs(description).isEqualTo(0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* (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 com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.assertj.core.api.AbstractDoubleAssert;
import org.assertj.core.api.AbstractFloatAssert;
import org.assertj.core.api.AbstractIntegerAssert;
import org.assertj.core.api.AbstractLongAssert;
import org.assertj.core.api.NumberAssert;

public final class AssertjIsNotZero {

@BeforeTemplate
public AbstractIntegerAssert<?> before(AbstractIntegerAssert<?> input) {
return input.isNotEqualTo(0);
}

@BeforeTemplate
public AbstractLongAssert<?> before(AbstractLongAssert<?> input) {
return input.isNotEqualTo(0L);
}

@BeforeTemplate
public AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> input) {
return input.isNotEqualTo(0D);
}

@BeforeTemplate
public AbstractFloatAssert<?> before(AbstractFloatAssert<?> input) {
return input.isNotEqualTo(0f);
}

@BeforeTemplate
public AbstractDoubleAssert<?> beforeWithDecimal(AbstractDoubleAssert<?> input) {
return input.isNotEqualTo(0.0D);
}

@BeforeTemplate
public AbstractFloatAssert<?> beforeWithDecimal(AbstractFloatAssert<?> input) {
return input.isNotEqualTo(0.0);
}

@AfterTemplate
public NumberAssert<?, ?> after(NumberAssert<?, ?> input) {
return input.isNotZero();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* (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 com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.assertj.core.api.AbstractDoubleAssert;
import org.assertj.core.api.AbstractFloatAssert;
import org.assertj.core.api.AbstractIntegerAssert;
import org.assertj.core.api.AbstractLongAssert;
import org.assertj.core.api.NumberAssert;

public final class AssertjIsOne {

@BeforeTemplate
public AbstractIntegerAssert<?> before(AbstractIntegerAssert<?> input) {
return input.isEqualTo(1);
}

@BeforeTemplate
public AbstractLongAssert<?> before(AbstractLongAssert<?> input) {
return input.isEqualTo(1L);
}

@BeforeTemplate
public AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> input) {
return input.isEqualTo(1D);
}

@BeforeTemplate
public AbstractFloatAssert<?> before(AbstractFloatAssert<?> input) {
return input.isEqualTo(1f);
}

@BeforeTemplate
public AbstractDoubleAssert<?> beforeWithDecimal(AbstractDoubleAssert<?> input) {
return input.isEqualTo(1.0D);
}

@BeforeTemplate
public AbstractFloatAssert<?> beforeWithDecimal(AbstractFloatAssert<?> input) {
return input.isEqualTo(1.0);
}

@AfterTemplate
public NumberAssert<?, ?> after(NumberAssert<?, ?> input) {
return input.isOne();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* (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 com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.assertj.core.api.AbstractDoubleAssert;
import org.assertj.core.api.AbstractFloatAssert;
import org.assertj.core.api.AbstractIntegerAssert;
import org.assertj.core.api.AbstractLongAssert;
import org.assertj.core.api.NumberAssert;

public final class AssertjIsZero {

@BeforeTemplate
public AbstractIntegerAssert<?> before(AbstractIntegerAssert<?> input) {
return input.isEqualTo(0);
}

@BeforeTemplate
public AbstractLongAssert<?> before(AbstractLongAssert<?> input) {
return input.isEqualTo(0L);
}

@BeforeTemplate
public AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> input) {
return input.isEqualTo(0D);
}

@BeforeTemplate
public AbstractFloatAssert<?> before(AbstractFloatAssert<?> input) {
return input.isEqualTo(0f);
}

@BeforeTemplate
public AbstractDoubleAssert<?> beforeWithDecimal(AbstractDoubleAssert<?> input) {
return input.isEqualTo(0.0D);
}

@BeforeTemplate
public AbstractFloatAssert<?> beforeWithDecimal(AbstractFloatAssert<?> input) {
return input.isEqualTo(0.0);
}

@AfterTemplate
public NumberAssert<?, ?> after(NumberAssert<?, ?> input) {
return input.isZero();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.palantir.baseline.refaster;

import static org.assertj.core.api.Assumptions.assumeThat;

import org.junit.Test;

public class AssertjCollectionIsEmptyTest {
Expand Down Expand Up @@ -78,4 +80,44 @@ public void description() {
"}");
}

@Test
public void test2() {
assumeThat(System.getProperty("java.specification.version"))
.describedAs("Refaster does not currently support fluent refactors on java 11")
.isEqualTo("1.8");
RefasterTestHelper
.forRefactoring(AssertjCollectionIsEmpty2.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import com.google.common.collect.ImmutableList;",
"import java.util.Collections;",
"import java.util.List;",
"public class Test {",
" void f(List<String> in) {",
" assertThat(in).hasSize(0);",
" assertThat(in).isEqualTo(ImmutableList.of());",
" assertThat(in).isEqualTo(Collections.emptyList());",
" assertThat(in).describedAs(\"desc\").hasSize(0);",
" assertThat(in).describedAs(\"desc\").isEqualTo(ImmutableList.of());",
" assertThat(in).describedAs(\"desc\").isEqualTo(Collections.emptyList());",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import com.google.common.collect.ImmutableList;",
"import java.util.Collections;",
"import java.util.List;",
"public class Test {",
" void f(List<String> in) {",
" assertThat(in).isEmpty();",
" assertThat(in).isEmpty();",
" assertThat(in).isEmpty();",
" assertThat(in).describedAs(\"desc\").isEmpty();",
" assertThat(in).describedAs(\"desc\").isEmpty();",
" assertThat(in).describedAs(\"desc\").isEmpty();",
" }",
"}");
}

}
Loading