Merge pull request #661 from jakubriegel/#629_path_is_absolute

#629 add Path.isAbsolute method
This commit is contained in:
Robert Stoll
2020-10-22 15:06:56 +02:00
committed by GitHub
10 changed files with 63 additions and 0 deletions

View File

@@ -286,6 +286,18 @@ fun <T : Path> Expect<T>.isRegularFile(): Expect<T> =
fun <T : Path> Expect<T>.isDirectory(): Expect<T> =
_logicAppend { isDirectory() }
/**
* Expects that the subject of the assertion (a [Path]) is an absolute path;
* meaning that the [Path] specified in this instance starts at the file system root.
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.14.0
*/
fun <T : Path> Expect<T>.isAbsolute(): Expect<T> =
_logicAppend { isAbsolute() }
/**
* Expects that the subject of the assertion (a [Path]) is a relative path;
* meaning that the [Path] specified in this instance does not start at the file system root.

View File

@@ -20,6 +20,7 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
fun0(Expect<Path>::isExecutable),
fun0(Expect<Path>::isRegularFile),
fun0(Expect<Path>::isDirectory),
fun0(Expect<Path>::isAbsolute),
fun0(Expect<Path>::isRelative),
fun1(Expect<Path>::hasSameBinaryContentAs),
fun3(Expect<Path>::hasSameTextualContentAs),

View File

@@ -22,6 +22,14 @@ object aRegularFile : Keyword
*/
object aDirectory : Keyword
/**
* A helper construct to allow expressing assertions about a path being absolute.
* It can be used for a parameterless function so that it has one parameter and thus can be used as infix function.
*
* @since 0.14.0
*/
object absolute : Keyword
/**
* A helper construct to allow expressing assertions about a path being relative.
* It can be used for a parameterless function so that it has one parameter and thus can be used as infix function.

View File

@@ -296,6 +296,18 @@ infix fun <T : Path> Expect<T>.toBe(@Suppress("UNUSED_PARAMETER") aRegularFile:
infix fun <T : Path> Expect<T>.toBe(@Suppress("UNUSED_PARAMETER") aDirectory: aDirectory): Expect<T> =
_logicAppend { isDirectory() }
/**
* Expects that the subject of the assertion (a [Path]) is an absolute path;
* meaning that the [Path] specified in this instance starts at the file system root.
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.14.0
*/
infix fun <T : Path> Expect<T>.toBe(@Suppress("UNUSED_PARAMETER") absolute: absolute): Expect<T> =
_logicAppend { isAbsolute() }
/**
* Expects that the subject of the assertion (a [Path]) is a relative path;
* meaning that the [Path] specified in this instance does not start at the file system root.

View File

@@ -21,6 +21,7 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
"toBe ${executable::class.simpleName}" to Companion::isExecutable,
"toBe ${aRegularFile::class.simpleName}" to Companion::isRegularFile,
"toBe ${aDirectory::class.simpleName}" to Companion::isDirectory,
"toBe ${relative::class.simpleName}" to Companion::isAbsolute,
"toBe ${relative::class.simpleName}" to Companion::isRelative,
fun1(Expect<Path>::hasSameBinaryContentAs),
fun3(Companion::hasSameTextualContentAs),
@@ -35,6 +36,7 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
private fun isExecutable(expect: Expect<Path>) = expect toBe executable
private fun isRegularFile(expect: Expect<Path>) = expect toBe aRegularFile
private fun isDirectory(expect: Expect<Path>) = expect toBe aDirectory
private fun isAbsolute(expect: Expect<Path>) = expect toBe absolute
private fun isRelative(expect: Expect<Path>) = expect toBe relative
private fun hasSameTextualContentAs(
@@ -64,6 +66,7 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
a1 toBe writable
a1 toBe aRegularFile
a1 toBe aDirectory
a1 toBe absolute
a1 toBe relative
a1 hasSameTextualContentAs withEncoding(Paths.get("a"))
a1 hasSameTextualContentAs Paths.get("a")

View File

@@ -32,6 +32,7 @@ fun <T : Path> AssertionContainer<T>.isWritable(): Assertion = impl.isWritable(t
fun <T : Path> AssertionContainer<T>.isExecutable(): Assertion = impl.isExecutable(this)
fun <T : Path> AssertionContainer<T>.isRegularFile(): Assertion = impl.isRegularFile(this)
fun <T : Path> AssertionContainer<T>.isDirectory(): Assertion = impl.isDirectory(this)
fun <T : Path> AssertionContainer<T>.isAbsolute(): Assertion = impl.isAbsolute(this)
fun <T : Path> AssertionContainer<T>.isRelative(): Assertion = impl.isRelative(this)
fun <T : Path> AssertionContainer<T>.hasSameTextualContentAs(targetPath: Path, sourceCharset: Charset, targetCharset: Charset): Assertion =

View File

@@ -28,6 +28,7 @@ interface PathAssertions {
fun <T : Path> isExecutable(container: AssertionContainer<T>): Assertion
fun <T : Path> isRegularFile(container: AssertionContainer<T>): Assertion
fun <T : Path> isDirectory(container: AssertionContainer<T>): Assertion
fun <T : Path> isAbsolute(container: AssertionContainer<T>): Assertion
fun <T : Path> isRelative(container: AssertionContainer<T>): Assertion
fun <T : Path> hasSameTextualContentAs(

View File

@@ -109,6 +109,9 @@ class DefaultPathAssertions : PathAssertions {
override fun <T : Path> isDirectory(container: AssertionContainer<T>): Assertion =
fileTypeAssertion(container, A_DIRECTORY) { it.isDirectory }
override fun <T : Path> isAbsolute(container: AssertionContainer<T>): Assertion =
container.createDescriptiveAssertion(DescriptionBasic.IS, ABSOLUTE_PATH) { it.isAbsolute }
override fun <T : Path> isRelative(container: AssertionContainer<T>): Assertion =
container.createDescriptiveAssertion(DescriptionBasic.IS, RELATIVE_PATH) { !it.isAbsolute }

View File

@@ -3,6 +3,7 @@
package ch.tutteli.atrium.api.fluent.en_GB.jdk8
import ch.tutteli.atrium.api.fluent.en_GB.isAbsolute
import ch.tutteli.atrium.api.fluent.en_GB.isExecutable
import ch.tutteli.atrium.api.fluent.en_GB.isRelative
import ch.tutteli.atrium.creating.Expect
@@ -25,6 +26,7 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
fun0(Expect<Path>::isExecutable), // checks the new function from fluent-jvm because it is not implemented in fluent-jkd8
fun0(Expect<Path>::isRegularFile),
fun0(Expect<Path>::isDirectory),
fun0(Expect<Path>::isAbsolute), // checks the new function from fluent-jvm because it is not implemented in fluent-jkd8
fun0(Expect<Path>::isRelative), // checks the new function from fluent-jvm because it is not implemented in fluent-jkd8
fun1(Expect<Path>::hasSameBinaryContentAs),
fun3(Expect<Path>::hasSameTextualContentAs),

View File

@@ -43,6 +43,7 @@ abstract class PathAssertionsSpec(
isExecutable: Fun0<Path>,
isRegularFile: Fun0<Path>,
isDirectory: Fun0<Path>,
isAbsolute: Fun0<Path>,
isRelative: Fun0<Path>,
hasSameBinaryContentAs: Fun1<Path, Path>,
hasSameTextualContentAs: Fun3<Path, Path, Charset, Charset>,
@@ -63,6 +64,7 @@ abstract class PathAssertionsSpec(
isExecutable.forSubjectLess(),
isRegularFile.forSubjectLess(),
isDirectory.forSubjectLess(),
isAbsolute.forSubjectLess(),
isRelative.forSubjectLess(),
hasSameBinaryContentAs.forSubjectLess(Paths.get("a")),
hasSameTextualContentAs.forSubjectLess(Paths.get("a"), Charsets.ISO_8859_1, Charsets.ISO_8859_1),
@@ -830,6 +832,24 @@ abstract class PathAssertionsSpec(
}
}
describeFun(isAbsolute) {
val isAbsoluteFun = isAbsolute.lambda
it("throws an AssertionError for relative path") {
val path = Paths.get("test/bla.txt")
expect {
expect(path).isAbsoluteFun()
}.toThrow<AssertionError> {
messageContains("$isDescr: ${ABSOLUTE_PATH.getDefault()}")
}
}
it("does not throw for absolute path") {
val path = tempFolder.newFile("test")
expect(path).isAbsoluteFun()
}
}
describeFun(isRelative) {
val isRelativeFun = isRelative.lambda