From 3e079a48edaf5669f5e7d8ebd26a7815b03ac437 Mon Sep 17 00:00:00 2001 From: Robert Stoll Date: Thu, 13 Feb 2020 22:36:59 +0100 Subject: [PATCH] Add path resolve shortcut --- .../api/fluent/en_GB/jdk8/pathAssertions.kt | 23 +++++++++ .../en_GB/jdk8/PathFeatureAssertionsSpec.kt | 9 ++-- .../api/infix/en_GB/jdk8/pathAssertions.kt | 11 +++++ .../en_GB/jdk8/PathFeatureAssertionsSpec.kt | 11 +++-- .../atrium/domain/creating/PathAssertions.kt | 2 + .../creating/PathAssertionsBuilder.kt | 3 ++ .../robstoll/lib/creating/pathAssertions.kt | 3 ++ .../robstoll/creating/PathAssertionsImpl.kt | 1 + .../integration/PathFeatureAssertionsSpec.kt | 47 +++++++++++++++++++ 9 files changed, 104 insertions(+), 6 deletions(-) diff --git a/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/pathAssertions.kt b/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/pathAssertions.kt index 4a9ad4ba4..54938c60f 100644 --- a/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/pathAssertions.kt +++ b/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/pathAssertions.kt @@ -156,6 +156,29 @@ val Expect.parent: Expect fun Expect.parent(assertionCreator: Expect.() -> Unit): Expect = ExpectImpl.path.parent(this).addToInitial(assertionCreator) +/** + * Expects that [other] resolve against this [Path] and creates an [Expect] for the resolved [Path] + * so that further fluent calls are assertions about it. + * + * @return The newly created [Expect]. + * @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct. + * + * @since 0.10.0 + */ +fun Expect.resolve(other: String): Expect = + ExpectImpl.path.resolve(this, other).getExpectOfFeature() + +/** + * Expect that [other] resolve against this [Path] and that the resolved [Path] holds all assertions the + * given [assertionCreator] creates for it and returns this assertion container. + * + * @return This assertion container to support a fluent API. + * @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct. + * + * @since 0.10.0 + */ +fun Expect.resolve(other: String, assertionCreator: Expect.() -> Unit): Expect = + ExpectImpl.path.resolve(this, other).addToInitial(assertionCreator) /** * Expects that the subject of the assertion (a [Path]) is readable; diff --git a/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/PathFeatureAssertionsSpec.kt b/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/PathFeatureAssertionsSpec.kt index 39b569a06..25d7a07a5 100644 --- a/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/PathFeatureAssertionsSpec.kt +++ b/apis/fluent-en_GB/extensions/jdk8/atrium-api-fluent-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/jdk8/PathFeatureAssertionsSpec.kt @@ -1,14 +1,14 @@ package ch.tutteli.atrium.api.fluent.en_GB.jdk8 import ch.tutteli.atrium.creating.Expect -import ch.tutteli.atrium.specs.fun1 -import ch.tutteli.atrium.specs.notImplemented -import ch.tutteli.atrium.specs.property +import ch.tutteli.atrium.specs.* import java.nio.file.Path class PathFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.PathFeatureAssertionsSpec( property(Expect::parent), fun1.() -> Unit>(Expect::parent), + feature1(Expect::resolve), + fun2.() -> Unit>(Expect::resolve), property(Expect::fileName), fun1.() -> Unit>(Expect::fileName), property(Expect::fileNameWithoutExtension), @@ -31,5 +31,8 @@ class PathFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.PathFeatur a1.extension a1 = a1.extension { } + + a1.resolve("test") + a1.resolve("test", {}) } } diff --git a/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/pathAssertions.kt b/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/pathAssertions.kt index b75ffcd0c..80fcf8094 100644 --- a/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/pathAssertions.kt +++ b/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/pathAssertions.kt @@ -158,6 +158,17 @@ val Expect.parent: Expect infix fun Expect.parent(assertionCreator: Expect.() -> Unit): Expect = ExpectImpl.path.parent(this).addToInitial(assertionCreator) +/** + * Expects that [other] resolve against this [Path] and creates an [Expect] for the resolved [Path] + * so that further fluent calls are assertions about it. + * + * @return The newly created [Expect]. + * @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct. + * + * @since 0.10.0 + */ +infix fun Expect.resolve(other: String): Expect = + ExpectImpl.path.resolve(this, other).getExpectOfFeature() /** * Expects that the subject of the assertion (a [Path]) is readable; diff --git a/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/PathFeatureAssertionsSpec.kt b/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/PathFeatureAssertionsSpec.kt index 76f0bf16b..60d08f755 100644 --- a/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/PathFeatureAssertionsSpec.kt +++ b/apis/infix-en_GB/extensions/jdk8/atrium-api-infix-en_GB-jdk8-jvm/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/jdk8/PathFeatureAssertionsSpec.kt @@ -1,15 +1,15 @@ package ch.tutteli.atrium.api.infix.en_GB.jdk8 import ch.tutteli.atrium.creating.Expect -import ch.tutteli.atrium.specs.fun1 -import ch.tutteli.atrium.specs.notImplemented -import ch.tutteli.atrium.specs.property +import ch.tutteli.atrium.specs.* import ch.tutteli.atrium.specs.testutils.WithAsciiReporter import java.nio.file.Path class PathFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.PathFeatureAssertionsSpec( property(Expect::parent), fun1.() -> Unit>(Expect::parent), + feature1(Expect::resolve), + fun2.() -> Unit>(Expect::resolve), //resolve with assertionCreator not implemented in this API property(Expect::fileName), fun1.() -> Unit>(Expect::fileName), property(Expect::fileNameWithoutExtension), @@ -34,5 +34,10 @@ class PathFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.PathFeatur a1.parent a1 parent {} + + a1 resolve "test" } } + +private fun Expect.resolve(other: String, assertionCreator: Expect.() -> Unit): Expect = + (this resolve other).addAssertionsCreatedBy(assertionCreator) diff --git a/domain/api/atrium-domain-api-jvm/src/main/kotlin/ch/tutteli/atrium/domain/creating/PathAssertions.kt b/domain/api/atrium-domain-api-jvm/src/main/kotlin/ch/tutteli/atrium/domain/creating/PathAssertions.kt index 485f5ebf0..5e4a09e54 100644 --- a/domain/api/atrium-domain-api-jvm/src/main/kotlin/ch/tutteli/atrium/domain/creating/PathAssertions.kt +++ b/domain/api/atrium-domain-api-jvm/src/main/kotlin/ch/tutteli/atrium/domain/creating/PathAssertions.kt @@ -24,6 +24,7 @@ interface PathAssertions { fun extension(expect: Expect): ExtractedFeaturePostStep fun fileNameWithoutExtension(expect: Expect): ExtractedFeaturePostStep fun parent(expect: Expect): ExtractedFeaturePostStep + fun resolve(expect: Expect, other: String): ExtractedFeaturePostStep fun startsWith(expect: Expect, expected: Path): Assertion fun startsNotWith(expect: Expect, expected: Path): Assertion @@ -37,4 +38,5 @@ interface PathAssertions { fun isWritable(expect: Expect): Assertion fun isRegularFile(expect: Expect): Assertion fun isDirectory(expect: Expect): Assertion + } diff --git a/domain/builders/atrium-domain-builders-jvm/src/main/kotlin/ch/tutteli/atrium/domain/builders/creating/PathAssertionsBuilder.kt b/domain/builders/atrium-domain-builders-jvm/src/main/kotlin/ch/tutteli/atrium/domain/builders/creating/PathAssertionsBuilder.kt index 6a092915b..d4cf5188c 100644 --- a/domain/builders/atrium-domain-builders-jvm/src/main/kotlin/ch/tutteli/atrium/domain/builders/creating/PathAssertionsBuilder.kt +++ b/domain/builders/atrium-domain-builders-jvm/src/main/kotlin/ch/tutteli/atrium/domain/builders/creating/PathAssertionsBuilder.kt @@ -48,6 +48,9 @@ object PathAssertionsBuilder : PathAssertions { override inline fun parent(expect: Expect) = pathAssertions.parent(expect) + override inline fun resolve(expect: Expect, other: String) = + pathAssertions.resolve(expect, other) + override inline fun isReadable(expect: Expect) = pathAssertions.isReadable(expect) diff --git a/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/pathAssertions.kt b/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/pathAssertions.kt index 0b63f5670..7522a8e53 100644 --- a/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/pathAssertions.kt +++ b/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/pathAssertions.kt @@ -395,5 +395,8 @@ fun _parent(expect: Expect): ExtractedFeaturePostStep = .withoutOptions() .build() +fun _resolve(expect: Expect, other: String): ExtractedFeaturePostStep = + ExpectImpl.feature.f1(expect, Path::resolve, other) + fun _extension(expect: Expect): ExtractedFeaturePostStep = ExpectImpl.feature.manualFeature(expect, EXTENSION) { extension } diff --git a/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/PathAssertionsImpl.kt b/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/PathAssertionsImpl.kt index 5c4009279..6cbb9bcc0 100644 --- a/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/PathAssertionsImpl.kt +++ b/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/PathAssertionsImpl.kt @@ -30,6 +30,7 @@ class PathAssertionsImpl : PathAssertions { _fileNameWithoutExtension(expect) override fun parent(expect: Expect) = _parent(expect) + override fun resolve(expect: Expect, other: String) = _resolve(expect, other) override fun isReadable(expect: Expect) = _isReadable(expect) override fun isWritable(expect: Expect) = _isWritable(expect) diff --git a/misc/specs/atrium-specs-jvm/src/main/kotlin/ch/tutteli/atrium/specs/integration/PathFeatureAssertionsSpec.kt b/misc/specs/atrium-specs-jvm/src/main/kotlin/ch/tutteli/atrium/specs/integration/PathFeatureAssertionsSpec.kt index b3ab36942..5b60c07ee 100644 --- a/misc/specs/atrium-specs-jvm/src/main/kotlin/ch/tutteli/atrium/specs/integration/PathFeatureAssertionsSpec.kt +++ b/misc/specs/atrium-specs-jvm/src/main/kotlin/ch/tutteli/atrium/specs/integration/PathFeatureAssertionsSpec.kt @@ -16,6 +16,8 @@ import java.nio.file.Paths abstract class PathFeatureAssertionsSpec( parentFeature: Feature0, parent: Fun1.() -> Unit>, + resolveFeature: Feature1, + resolve: Fun2.() -> Unit>, fileNameFeature: Feature0, fileName: Fun1.() -> Unit>, fileNameWithoutExtensionFeature: Feature0, @@ -30,6 +32,8 @@ abstract class PathFeatureAssertionsSpec( include(object : SubjectLessSpec(describePrefix, parentFeature.forSubjectLess().adjustName { "$it feature" }, parent.forSubjectLess { }, + resolveFeature.forSubjectLess("test").adjustName { "$it feature" }, + resolve.forSubjectLess("test") { toBe(Paths.get("a/my.txt")) }, fileNameFeature.forSubjectLess().adjustName { "$it feature" }, fileName.forSubjectLess { }, fileNameWithoutExtensionFeature.forSubjectLess().adjustName { "$it feature" }, @@ -105,6 +109,49 @@ abstract class PathFeatureAssertionsSpec( } } + describeFun("val ${resolveFeature.name}") { + val resolveVal = resolveFeature.lambda + + context("Folder resolved from root") { + it("toBe(folder.resolve) holds") { + val resolvedFolder = tempFolder.newDirectory("resolve") + val rootFolder = resolvedFolder.parent + expect(rootFolder).resolveVal("resolve").toBe(resolvedFolder) + } + it("toBe(folder) fails") { + expect { + val resolvedFolder = tempFolder.newDirectory("resolve") + val rootFolder = resolvedFolder.parent + expect(rootFolder).resolveVal("notResolved").toBe(resolvedFolder) + }.toThrow { + messageContains("notResolved") + } + } + } + } + + describeFun("fun ${resolve.name}") { + val resolveFun = resolve.lambda + + context("Folder resolved from root") { + it("toBe(folder.resolve) holds") { + val resolvedFolder = tempFolder.newDirectory("resolve") + val rootFolder = resolvedFolder.parent + expect(rootFolder).resolveFun("resolve") { toBe(resolvedFolder) } + } + it("toBe(folder) fails") { + expect { + val resolvedFolder = tempFolder.newDirectory("resolve") + val rootFolder = resolvedFolder.parent + expect(rootFolder).resolveFun("notResolved"){ toBe(resolvedFolder) } + }.toThrow { + messageContains("notResolved") + } + } + } + } + + describeFun("val ${fileNameFeature.name}") { val fileNameVal = fileNameFeature.lambda