Skip to content

Commit

Permalink
Merge pull request #557 from robstoll/#507-Path.hasSameTextualContent…
Browse files Browse the repository at this point in the history
…-infix

#507 Path.hasSameTextualContent for the infix API
  • Loading branch information
robstoll authored Aug 19, 2020
2 parents f20a4d8 + 951b0e1 commit cf330c8
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fun <T : Path> Expect<T>.extension(assertionCreator: Expect<String>.() -> Unit):

/**
* Expects that the subject of the assertion (a [Path]) has the same textual content
* as [targetPath].
* as [targetPath] taking the given encodings into account (UTF-8 if none given).
*
* @param sourceCharset source file encoding - UTF-8 per default.
* @param targetCharset target file encoding - UTF-8 per default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
fun0(Expect<Path>::isRegularFile),
fun0(Expect<Path>::isDirectory),
fun1(Expect<Path>::hasSameBinaryContentAs),
fun3(Expect<Path>::hasSameTextualContentAs)
fun3(Expect<Path>::hasSameTextualContentAs),
fun1(Companion::hasSameTextualContentAsDefaultArgs)
) {

companion object {
private fun hasSameTextualContentAsDefaultArgs(expect: Expect<Path>, targetPath: Path): Expect<Path> =
expect.hasSameTextualContentAs(targetPath)
}

@Suppress("unused", "UNUSED_VALUE")
private fun ambiguityTest() {
val a1: Expect<DummyPath> = notImplemented()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package ch.tutteli.atrium.api.infix.en_GB.creating.path
import ch.tutteli.atrium.creating.Expect

/**
* Parameter object which combines an [path] (as [String]) with an [assertionCreator] which defines assertions for
* Parameter object which combines a [path] (as [String]) with an [assertionCreator] which defines assertions for
* a resulting feature of type [E].
*
* Use the function `path(String) { ... }` to create this representation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.tutteli.atrium.api.infix.en_GB.creating.path

import java.nio.charset.Charset
import java.nio.file.Path

/**
* Parameter object which combines [path] (as [Path]) with a [sourceCharset] and [targetCharset].
*
* Use the function `withEncoding(Path, Charset, Charset)` to create this representation.
*
* @since 0.13.0
*/
data class PathWithEncoding internal constructor(
val path: Path,
val sourceCharset: Charset,
val targetCharset: Charset
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

package ch.tutteli.atrium.api.infix.en_GB

import ch.tutteli.atrium.api.infix.en_GB.creating.path.PathWithEncoding
import ch.tutteli.atrium.api.infix.en_GB.creating.path.PathWithCreator
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.*
import java.nio.charset.Charset
import java.nio.file.Path

/**
Expand Down Expand Up @@ -298,3 +300,59 @@ val <T : Path> Expect<T>.extension: Expect<String>
*/
infix fun <T : Path> Expect<T>.extension(assertionCreator: Expect<String>.() -> Unit): Expect<T> =
_logic.extension().addToInitial(assertionCreator)

/**
* Expects that the subject of the assertion (a [Path]) has the same textual content
* as [targetPath] (using UTF-8 for encoding)
*
* @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.13.0
*/
infix fun <T : Path> Expect<T>.hasSameTextualContentAs(
targetPath: Path
): Expect<T> = hasSameTextualContentAs(withEncoding(targetPath))

/**
* Helper function to create a [PathWithEncoding] based on the given [path] and the [sourceCharset] and [targetCharset]
* where UTF-8 is used as default if one encoding is missing.
*/
fun withEncoding(
path: Path,
sourceCharset: Charset = Charsets.UTF_8,
targetCharset: Charset = Charsets.UTF_8
): PathWithEncoding =
PathWithEncoding(
path,
sourceCharset,
targetCharset
)

/**
* Expects that the subject of the assertion (a [Path]) has the same textual content
* as [PathWithEncoding.path] in the given [pathWithEncoding] with the specified encodings.
*
* Use the function `withEncoding(Path, Charset, Charset)` to create a [PathWithEncoding].
*
* @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.13.0
*/
infix fun <T : Path> Expect<T>.hasSameTextualContentAs(pathWithEncoding: PathWithEncoding): Expect<T> =
_logicAppend {
hasSameTextualContentAs(pathWithEncoding.path, pathWithEncoding.sourceCharset, pathWithEncoding.targetCharset)
}

/**
* Expects that the subject of the assertion (a [Path]) has the same binary content
* as [targetPath].
*
* @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.13.0
*/
infix fun <T : Path> Expect<T>.hasSameBinaryContentAs(targetPath: Path): Expect<T> =
_logicAppend { hasSameBinaryContentAs(targetPath) }
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ch.tutteli.atrium.api.infix.en_GB

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.domain.builders.ExpectImpl
import ch.tutteli.atrium.domain.builders.path
import ch.tutteli.atrium.specs.fun1
import ch.tutteli.atrium.specs.fun3
import ch.tutteli.atrium.specs.notImplemented
import ch.tutteli.atrium.specs.testutils.WithAsciiReporter
import java.nio.charset.Charset
Expand All @@ -21,8 +20,9 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
"toBe ${writable::class.simpleName}" to Companion::isWritable,
"toBe ${aRegularFile::class.simpleName}" to Companion::isRegularFile,
"toBe ${aDirectory::class.simpleName}" to Companion::isDirectory,
"not supported in this API - hasSameBinaryContentAs" to Companion::hasSameBinaryContentAs,
"not supported in this API - hasSameTextualContentAs" to Companion::hasSameTextualContentAs
fun1(Expect<Path>::hasSameBinaryContentAs),
fun3(Companion::hasSameTextualContentAs),
fun1(Companion::hasSameTextualContentAsDefaultArgs)
) {
companion object : WithAsciiReporter() {

Expand All @@ -33,25 +33,17 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
private fun isRegularFile(expect: Expect<Path>) = expect toBe aRegularFile
private fun isDirectory(expect: Expect<Path>) = expect toBe aDirectory

@Suppress(/* TODO remove with the introduction of this functionality in infix */ "DEPRECATION")
private fun hasSameTextualContentAs(
expect: Expect<Path>,
targetPath: Path,
sourceCharset: Charset,
targetCharset: Charset
): Expect<Path> =
expect.addAssertion(
ExpectImpl.path.hasSameTextualContentAs(
expect,
targetPath,
sourceCharset,
targetCharset
)
)
): Expect<Path> = expect hasSameTextualContentAs withEncoding(targetPath, sourceCharset, targetCharset)

@Suppress(/* TODO remove with the introduction of this functionality in infix */ "DEPRECATION")
private fun hasSameBinaryContentAs(expect: Expect<Path>, targetPath: Path): Expect<Path> =
expect.addAssertion(ExpectImpl.path.hasSameBinaryContentAs(expect, targetPath))
private fun hasSameTextualContentAsDefaultArgs(
expect: Expect<Path>,
targetPath: Path
): Expect<Path> = expect hasSameTextualContentAs targetPath
}

@Suppress("unused", "UNUSED_VALUE")
Expand All @@ -68,6 +60,8 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
a1 toBe writable
a1 toBe aRegularFile
a1 toBe aDirectory
a1 hasSameTextualContentAs withEncoding(Paths.get("a"))
a1 hasSameTextualContentAs Paths.get("a")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ class PathAssertionsSpec : ch.tutteli.atrium.specs.integration.PathAssertionsSpe
fun0(Expect<Path>::isRegularFile),
fun0(Expect<Path>::isDirectory),
fun1(Expect<Path>::hasSameBinaryContentAs),
fun3(Expect<Path>::hasSameTextualContentAs)
fun3(Expect<Path>::hasSameTextualContentAs),
fun1(Companion::hasSameTextualContentAsDefaultArgs)
) {

companion object {
private fun hasSameTextualContentAsDefaultArgs(expect: Expect<Path>, targetPath: Path): Expect<Path> =
expect.hasSameTextualContentAs(targetPath)
}

@Suppress("unused", "UNUSED_VALUE")
private fun ambiguityTest() {
val a1: Expect<DummyPath> = notImplemented()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ abstract class PathAssertionsSpec(
isDirectory: Fun0<Path>,
hasSameBinaryContentAs: Fun1<Path, Path>,
hasSameTextualContentAs: Fun3<Path, Path, Charset, Charset>,
hasSameTextualContentAsDefaultArgs: Fun1<Path, Path>,
describePrefix: String = "[Atrium] "
) : Spek({

Expand All @@ -75,7 +76,8 @@ abstract class PathAssertionsSpec(
isRegularFile.forSubjectLess(),
isDirectory.forSubjectLess(),
hasSameBinaryContentAs.forSubjectLess(Paths.get("a")),
hasSameTextualContentAs.forSubjectLess(Paths.get("a"), Charsets.ISO_8859_1, Charsets.ISO_8859_1)
hasSameTextualContentAs.forSubjectLess(Paths.get("a"), Charsets.ISO_8859_1, Charsets.ISO_8859_1) ,
hasSameTextualContentAsDefaultArgs.forSubjectLess(Paths.get("a"))
) {})

val tempFolder by memoizedTempFolder()
Expand Down Expand Up @@ -748,9 +750,10 @@ abstract class PathAssertionsSpec(
}
}

describeFun(hasSameBinaryContentAs, hasSameTextualContentAs) {
describeFun(hasSameBinaryContentAs, hasSameTextualContentAs, hasSameTextualContentAsDefaultArgs) {
val hasSameBinaryContentAsFun = hasSameBinaryContentAs.lambda
val hasSameTextualContentAsFun = hasSameTextualContentAs.lambda
val hasSameTextualContentAsDefaultArgsAsFun = hasSameTextualContentAsDefaultArgs.lambda

fun errorHasSameTextualContentAs(sourceEncoding: Charset, targetEncoding: Charset) =
TranslatableWithArgs(HAS_SAME_TEXTUAL_CONTENT, sourceEncoding, targetEncoding).getDefault()
Expand Down Expand Up @@ -781,10 +784,16 @@ abstract class PathAssertionsSpec(
val (sourcePath, targetPath) = createFiles(maybeLink)
expect(sourcePath).hasSameTextualContentAsFun(targetPath, Charsets.UTF_8, Charsets.UTF_16)
}

it("${hasSameTextualContentAs.name} - does not throw if UTF-16, ISO_8859_1 is used") withAndWithoutSymlink { maybeLink ->
val (sourcePath, targetPath) = createFiles(maybeLink)
expect(sourcePath).hasSameTextualContentAsFun(targetPath, Charsets.UTF_16, Charsets.ISO_8859_1)
}

it("${hasSameTextualContentAsDefaultArgs.name} - does not throw") withAndWithoutSymlink { maybeLink ->
val (sourcePath, targetPath) = createFiles(maybeLink)
expect(sourcePath).hasSameTextualContentAsDefaultArgsAsFun(targetPath)
}
}

context("has same binary content") {
Expand Down Expand Up @@ -828,6 +837,11 @@ abstract class PathAssertionsSpec(
contains(errorHasSameTextualContentAs(Charsets.UTF_16, Charsets.ISO_8859_1))
}
}

it("${hasSameTextualContentAsDefaultArgs.name} - does not throw if UTF-8, UTF-8 is used") withAndWithoutSymlink { maybeLink ->
val (sourcePath, targetPath) = createFiles(maybeLink)
expect(sourcePath).hasSameTextualContentAsDefaultArgsAsFun(targetPath)
}
}

context("has same textual content in UTF-8 and UTF-16") {
Expand Down Expand Up @@ -870,6 +884,15 @@ abstract class PathAssertionsSpec(
contains(errorHasSameTextualContentAs(Charsets.UTF_16, Charsets.UTF_8))
}
}

it("${hasSameTextualContentAsDefaultArgs.name} - throws AssertionError if UTF-8, UTF-8 is used") withAndWithoutSymlink { maybeLink ->
val (sourcePath, targetPath) = createFiles(maybeLink)
expect {
expect(sourcePath).hasSameTextualContentAsDefaultArgsAsFun(targetPath)
}.toThrow<AssertionError>().message {
contains(errorHasSameTextualContentAs(Charsets.UTF_8, Charsets.UTF_8))
}
}
}

context("has different textual content") {
Expand Down Expand Up @@ -907,6 +930,15 @@ abstract class PathAssertionsSpec(
contains(errorHasSameTextualContentAs(Charsets.UTF_16, Charsets.UTF_16))
}
}

it("${hasSameTextualContentAsDefaultArgs.name} - throws AssertionError if UTF-8, UTF-8 is used") withAndWithoutSymlink { maybeLink ->
val (sourcePath, targetPath) = createFiles(maybeLink)
expect {
expect(sourcePath).hasSameTextualContentAsDefaultArgsAsFun(targetPath)
}.toThrow<AssertionError>().message {
contains(errorHasSameTextualContentAs(Charsets.UTF_8, Charsets.UTF_8))
}
}
}
}
})
Expand Down

0 comments on commit cf330c8

Please sign in to comment.