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

Add assertj refaster rules for map size asserts #919

Merged
merged 2 commits into from
Oct 2, 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
@@ -0,0 +1,44 @@
/*
* (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.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.Map;

public final class AssertjMapHasSizeExactly<K, V> {

@BeforeTemplate
void before1(Map<K, V> things, int size) {
assertThat(things.size() == size).isTrue();
}

@BeforeTemplate
void before2(Map<K, V> things, int size) {
assertThat(things.size()).isEqualTo(size);
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, int size) {
assertThat(things).hasSize(size);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* (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.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.Map;

public final class AssertjMapHasSizeExactlyWithDescription<K, V> {

@BeforeTemplate
void before1(Map<K, V> things, int size, String description) {
assertThat(things.size() == size).describedAs(description).isTrue();
}

@BeforeTemplate
void before2(Map<K, V> things, int size, String description) {
assertThat(things.size()).describedAs(description).isEqualTo(size);
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, int size, String description) {
assertThat(things).describedAs(description).hasSize(size);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* (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.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.Map;

public final class AssertjMapIsEmpty<K, V> {

@BeforeTemplate
void before1(Map<K, V> things) {
assertThat(things.size() == 0).isTrue();
}

@BeforeTemplate
void before2(Map<K, V> things) {
assertThat(things.isEmpty()).isTrue();
}

@BeforeTemplate
void before3(Map<K, V> things) {
assertThat(things.size()).isZero();
}

@BeforeTemplate
void before4(Map<K, V> things) {
assertThat(things.size()).isEqualTo(0);
}

@BeforeTemplate
void before5(Map<K, V> things) {
assertThat(things).hasSize(0);
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things) {
assertThat(things).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* (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.ImmutableMap;
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.MapAssert;

public final class AssertjMapIsEmpty2<A extends MapAssert<K, V>, K, V> {

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

@BeforeTemplate
void before2(A in) {
in.isEqualTo(Refaster.anyOf(
ImmutableMap.of(),
Collections.emptyMap()));
}

@AfterTemplate
void after(A in) {
in.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* (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.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.Map;

public final class AssertjMapIsEmptyWithDescription<K, V> {

@BeforeTemplate
void before1(Map<K, V> things, String description) {
assertThat(things.size() == 0).describedAs(description).isTrue();
}

@BeforeTemplate
void before2(Map<K, V> things, String description) {
assertThat(things.isEmpty()).describedAs(description).isTrue();
}

@BeforeTemplate
void before3(Map<K, V> things, String description) {
assertThat(things.size()).describedAs(description).isZero();
}

@BeforeTemplate
void before4(Map<K, V> things, String description) {
assertThat(things.size()).describedAs(description).isEqualTo(0);
}

@BeforeTemplate
void before5(Map<K, V> things, String description) {
assertThat(things).describedAs(description).hasSize(0);
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, String description) {
assertThat(things).describedAs(description).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* (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 org.junit.Test;

public class AssertjMapHasSizeTest {

@Test
public void exactSize_simple() {
RefasterTestHelper
.forRefactoring(AssertjMapHasSizeExactly.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" assertThat(in.size() == 2).isTrue();",
" assertThat(in.size()).isEqualTo(2);",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" assertThat(in).hasSize(2);",
" assertThat(in).hasSize(2);",
" }",
"}");
}

@Test
public void exactSize_description() {
RefasterTestHelper
.forRefactoring(AssertjMapHasSizeExactlyWithDescription.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import com.google.common.collect.ImmutableList;",
"import java.util.Collections;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" assertThat(in.size() == 2).describedAs(\"desc\").isTrue();",
" assertThat(in.size()).describedAs(\"desc\").isEqualTo(2);",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import com.google.common.collect.ImmutableList;",
"import java.util.Collections;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" assertThat(in).describedAs(\"desc\").hasSize(2);",
" assertThat(in).describedAs(\"desc\").hasSize(2);",
" }",
"}");
}
}
Loading