Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add refaster rule to simplify empty optional asserts
  • Loading branch information
Carter Kozak committed Sep 30, 2019
commit ce3c89bcb39d6396825beb4dc2d003f70216fafc
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.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.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public final class AssertjOptionalIsNotPresent<T> {

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

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

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Optional<T> thing) {
assertThat(thing).isNotPresent();
}
}
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.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.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public final class AssertjOptionalIsNotPresentWithDescription<T> {

@BeforeTemplate
void before1(Optional<T> thing, String description) {
assertThat(thing.isPresent()).describedAs(description).isFalse();
}

@BeforeTemplate
void before2(Optional<T> thing, String description) {
assertThat(!thing.isPresent()).describedAs(description).isTrue();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Optional<T> thing, String description) {
assertThat(thing).describedAs(description).isNotPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,54 @@ public void isPresent_description() {
" }",
"}");
}

@Test
public void isNotPresent_simple() {
RefasterTestHelper
.forRefactoring(AssertjOptionalIsNotPresent.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Optional;",
"public class Test {",
" void f(Optional<String> in) {",
" assertThat(in.isPresent()).isFalse();",
" assertThat(!in.isPresent()).isTrue();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Optional;",
"public class Test {",
" void f(Optional<String> in) {",
" assertThat(in).isNotPresent();",
" assertThat(in).isNotPresent();",
" }",
"}");
}

@Test
public void isNotPresent_description() {
RefasterTestHelper
.forRefactoring(AssertjOptionalIsNotPresentWithDescription.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Optional;",
"public class Test {",
" void f(Optional<String> in) {",
" assertThat(in.isPresent()).describedAs(\"desc\").isFalse();",
" assertThat(!in.isPresent()).describedAs(\"desc\").isTrue();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Optional;",
"public class Test {",
" void f(Optional<String> in) {",
" assertThat(in).describedAs(\"desc\").isNotPresent();",
" assertThat(in).describedAs(\"desc\").isNotPresent();",
" }",
"}");
}
}