-
-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hasNoExtension to Path assertions
- Loading branch information
1 parent
cd2ce82
commit 931b592
Showing
6 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/org/assertj/core/error/ShouldHaveNoExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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. | ||
* | ||
* Copyright 2012-2021 the original author or authors. | ||
*/ | ||
package org.assertj.core.error; | ||
|
||
import java.nio.file.Path; | ||
|
||
/** | ||
* Creates an error message indicating that a {@code Path} should have no extension. | ||
*/ | ||
public class ShouldHaveNoExtension extends BasicErrorMessageFactory { | ||
|
||
private static final String PATH_HAS_EXTENSION = "%nExpected actual path:%n %s%n not to have an extension, but extension was: %s"; | ||
|
||
public static ShouldHaveNoExtension shouldHaveNoExtension(Path actual, String extension) { | ||
return new ShouldHaveNoExtension(actual, extension); | ||
} | ||
|
||
private ShouldHaveNoExtension(Path actual, String extension) { | ||
super(PATH_HAS_EXTENSION, actual, extension); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/org/assertj/core/api/path/PathAssert_hasNoExtension_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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. | ||
* | ||
* Copyright 2012-2021 the original author or authors. | ||
*/ | ||
package org.assertj.core.api.path; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
import org.assertj.core.api.PathAssert; | ||
import org.assertj.core.api.PathAssertBaseTest; | ||
|
||
class PathAssert_hasNoExtension_Test extends PathAssertBaseTest { | ||
|
||
@Override | ||
protected PathAssert invoke_api_method() { | ||
return assertions.hasNoExtension(); | ||
} | ||
|
||
@Override | ||
protected void verify_internal_effects() { | ||
verify(paths).assertHasNoExtension(getInfo(assertions), getActual(assertions)); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/assertj/core/error/ShouldHaveNoExtension_create_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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. | ||
* | ||
* Copyright 2012-2021 the original author or authors. | ||
*/ | ||
package org.assertj.core.error; | ||
|
||
import static java.lang.String.format; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension; | ||
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.nio.file.Path; | ||
|
||
import org.assertj.core.internal.TestDescription; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ShouldHaveNoExtension_create_Test { | ||
|
||
private static final TestDescription TEST_DESCRIPTION = new TestDescription("Test"); | ||
|
||
@Test | ||
void should_create_error_message() { | ||
// GIVEN | ||
final Path path = mock(Path.class); | ||
// WHEN | ||
String actualMessage = shouldHaveNoExtension(path, "java").create(TEST_DESCRIPTION, STANDARD_REPRESENTATION); | ||
// THEN | ||
then(actualMessage).isEqualTo(format("[Test] %n" + | ||
"Expected actual path:%n" + | ||
" %s%n" + | ||
" not to have an extension, but extension was: \"java\"", path)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoExtension_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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. | ||
* | ||
* Copyright 2012-2021 the original author or authors. | ||
*/ | ||
package org.assertj.core.internal.paths; | ||
|
||
import static java.nio.file.Files.createDirectory; | ||
import static java.nio.file.Files.createFile; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; | ||
import static org.assertj.core.error.ShouldExist.shouldExist; | ||
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
import org.assertj.core.internal.PathsBaseTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class Paths_assertHasNoExtension_Test extends PathsBaseTest { | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
Path actual = null; | ||
// WHEN | ||
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual)); | ||
// THEN | ||
then(error).hasMessage(actualIsNull()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_does_not_exist() { | ||
// GIVEN | ||
Path actual = tempDir.resolve("non-existent"); | ||
// WHEN | ||
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual)); | ||
// THEN | ||
then(error).hasMessage(shouldExist(actual).create()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_is_not_a_regular_file() throws IOException { | ||
// GIVEN | ||
Path actual = createDirectory(tempDir.resolve("directory")); | ||
// WHEN | ||
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual)); | ||
// THEN | ||
then(error).hasMessage(shouldBeRegularFile(actual).create()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_has_extension() throws IOException { | ||
// GIVEN | ||
Path actual = createFile(tempDir.resolve("file.txt")); | ||
// WHEN | ||
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual)); | ||
// THEN | ||
then(error).hasMessage(shouldHaveNoExtension(actual, "txt").create()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { "file", "file." }) | ||
void should_pass_if_actual_has_no_extension(String filename) throws IOException { | ||
// GIVEN | ||
Path actual = createFile(tempDir.resolve(filename)); | ||
// WHEN/THEN | ||
paths.assertHasNoExtension(info, actual); | ||
} | ||
|
||
} |