complete path expectation samples (#930)

This commit is contained in:
Bacho Kurtanidze
2021-06-04 13:17:31 +04:00
committed by GitHub
parent 9fe122f39f
commit 7c829ce7f9
3 changed files with 441 additions and 6 deletions

View File

@@ -97,6 +97,8 @@ fun <T : Path> Expect<T>.existsNot(): Expect<T> =
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.pathExtensionFeature
*
* @since 0.9.0
*/
val <T : Path> Expect<T>.extension: Expect<String>
@@ -111,6 +113,8 @@ val <T : Path> Expect<T>.extension: Expect<String>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.pathExtension
*
* @since 0.9.0
*/
fun <T : Path> Expect<T>.extension(assertionCreator: Expect<String>.() -> Unit): Expect<T> =
@@ -124,6 +128,8 @@ fun <T : Path> Expect<T>.extension(assertionCreator: Expect<String>.() -> Unit):
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.fileNameFeature
*
* @since 0.9.0
*/
val <T : Path> Expect<T>.fileName: Expect<String>
@@ -138,6 +144,8 @@ val <T : Path> Expect<T>.fileName: Expect<String>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.fileName
*
* @since 0.9.0
*/
fun <T : Path> Expect<T>.fileName(assertionCreator: Expect<String>.() -> Unit): Expect<T> =
@@ -151,6 +159,8 @@ fun <T : Path> Expect<T>.fileName(assertionCreator: Expect<String>.() -> Unit):
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.fileNameWithoutExtensionFeature
*
* @since 0.9.0
*/
val <T : Path> Expect<T>.fileNameWithoutExtension: Expect<String>
@@ -165,6 +175,8 @@ val <T : Path> Expect<T>.fileNameWithoutExtension: Expect<String>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.fileNameWithoutExtension
*
* @since 0.9.0
*/
fun <T : Path> Expect<T>.fileNameWithoutExtension(assertionCreator: Expect<String>.() -> Unit): Expect<T> =
@@ -177,6 +189,8 @@ fun <T : Path> Expect<T>.fileNameWithoutExtension(assertionCreator: Expect<Strin
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.parentFeature
*
* @since 0.9.0
*/
val <T : Path> Expect<T>.parent: Expect<Path>
@@ -189,6 +203,8 @@ val <T : Path> Expect<T>.parent: Expect<Path>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.parent
*
* @since 0.9.0
*/
fun <T : Path> Expect<T>.parent(assertionCreator: Expect<Path>.() -> Unit): Expect<T> =
@@ -201,6 +217,8 @@ fun <T : Path> Expect<T>.parent(assertionCreator: Expect<Path>.() -> Unit): Expe
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.resolveFeature
*
* @since 0.10.0
*/
fun <T : Path> Expect<T>.resolve(other: String): Expect<Path> =
@@ -213,6 +231,8 @@ fun <T : Path> Expect<T>.resolve(other: String): Expect<Path> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.resolve
*
* @since 0.10.0
*/
fun <T : Path> Expect<T>.resolve(other: String, assertionCreator: Expect<Path>.() -> Unit): Expect<T> =

View File

@@ -16,6 +16,8 @@ import java.nio.file.Path
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toStartWith
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toStartWith(expected: Path): Expect<T> =
@@ -26,6 +28,8 @@ fun <T : Path> Expect<T>.toStartWith(expected: Path): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.notToStartWith
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.notToStartWith(expected: Path): Expect<T> =
@@ -36,6 +40,8 @@ fun <T : Path> Expect<T>.notToStartWith(expected: Path): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toEndWith
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toEndWith(expected: Path): Expect<T> =
@@ -47,6 +53,8 @@ fun <T : Path> Expect<T>.toEndWith(expected: Path): Expect<T> =
* @param expected The [Path] provided to the assertion
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.notToEndWith
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.notToEndWith(expected: Path): Expect<T> =
@@ -61,6 +69,8 @@ fun <T : Path> Expect<T>.notToEndWith(expected: Path): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toExist
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toExist(): Expect<T> =
@@ -75,6 +85,8 @@ fun <T : Path> Expect<T>.toExist(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.notToExist
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.notToExist(): Expect<T> =
@@ -96,6 +108,8 @@ fun <T : Path> Expect<T>.notToExist(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeReadable
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeReadable(): Expect<T> =
@@ -112,6 +126,8 @@ fun <T : Path> Expect<T>.toBeReadable(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeWritable
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeWritable(): Expect<T> =
@@ -133,6 +149,8 @@ fun <T : Path> Expect<T>.toBeWritable(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeExecutable
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeExecutable(): Expect<T> =
@@ -152,6 +170,8 @@ fun <T : Path> Expect<T>.toBeExecutable(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeARegularFile
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeARegularFile(): Expect<T> =
@@ -171,6 +191,8 @@ fun <T : Path> Expect<T>.toBeARegularFile(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeADirectory
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeADirectory(): Expect<T> =
@@ -213,6 +235,8 @@ fun <T : Path> Expect<T>.toBeASymbolicLink(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeAbsolute
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeAbsolute(): Expect<T> =
@@ -224,6 +248,8 @@ fun <T : Path> Expect<T>.toBeAbsolute(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toBeRelative
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toBeRelative(): Expect<T> =
@@ -245,6 +271,8 @@ fun <T : Path> Expect<T>.toBeRelative(): Expect<T> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toHaveTheDirectoryEntries
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toHaveTheDirectoryEntries(entry: String, vararg otherEntries: String): Expect<T> =
@@ -260,6 +288,8 @@ fun <T : Path> Expect<T>.toHaveTheDirectoryEntries(entry: String, vararg otherEn
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toHaveTheSameTextualContentAs
*
* @since 0.17.0
*/
fun <T : Path> Expect<T>.toHaveTheSameTextualContentAs(
@@ -274,7 +304,10 @@ fun <T : Path> Expect<T>.toHaveTheSameTextualContentAs(
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.PathExpectationSamples.toHaveTheSameBinaryContentAs
*
* @since 0.17.0
*
*/
fun <T : Path> Expect<T>.toHaveTheSameBinaryContentAs(targetPath: Path): Expect<T> =
_logicAppend { hasSameBinaryContentAs(targetPath) }

View File

@@ -1,11 +1,13 @@
package ch.tutteli.atrium.api.fluent.en_GB.samples
import ch.tutteli.atrium.api.fluent.en_GB.toBeASymbolicLink
import ch.tutteli.atrium.api.fluent.en_GB.toBeAnEmptyDirectory
import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.api.verbs.internal.expect
import ch.tutteli.niok.newDirectory
import ch.tutteli.niok.newFile
import ch.tutteli.niok.*
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import javax.swing.filechooser.FileSystemView
import kotlin.test.Test
class PathExpectationSamples {
@@ -29,12 +31,392 @@ class PathExpectationSamples {
@Test
fun toBeAnEmptyDirectory() {
val dir = tempDir.newDirectory("dir")
val dir = tempDir.newDirectory("test_dir")
expect(dir).toBeAnEmptyDirectory()
dir.newFile("test.txt")
dir.newFile("test_file.txt")
fails {
expect(dir).toBeAnEmptyDirectory()
}
}
@Test
fun toStartWith() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).toStartWith(dir.parent)
fails {
expect(dir).toStartWith(Paths.get("invalid_dir"))
}
}
@Test
fun notToStartWith() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).notToStartWith(Paths.get("invalid_dir"))
fails {
expect(dir).notToStartWith(dir.parent)
}
}
@Test
fun toEndWith() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).toEndWith(Paths.get("test_dir"))
fails {
expect(dir).toEndWith(Paths.get("invalid_dir"))
}
}
@Test
fun notToEndWith() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).notToEndWith(Paths.get("invalid_dir"))
fails {
expect(dir).notToEndWith(Paths.get("test_dir"))
}
}
@Test
fun toExist() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).toExist()
fails {
expect(Paths.get("invalid_dir")).toExist()
}
}
@Test
fun notToExist() {
val dir = tempDir.newDirectory("test_dir")
expect(Paths.get("invalid_dir")).notToExist()
fails {
expect(dir).notToExist()
}
}
@Test
fun toBeReadable() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).toBeReadable()
fails {
expect(Paths.get("invalid_dir")).toBeReadable()
}
}
@Test
fun toBeWritable() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).toBeWritable()
fails {
expect(Paths.get("invalid_dir")).toBeWritable()
}
}
@Test
fun toBeExecutable() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).toBeExecutable()
fails {
expect(Paths.get("invalid_dir")).toBeExecutable()
}
}
@Test
fun toBeARegularFile() {
val file = tempDir.newFile("test_file")
val dir = tempDir.newDirectory("test_dir")
expect(file).toBeARegularFile()
fails {
expect(dir).toBeARegularFile()
}
}
@Test
fun toBeADirectory() {
val file = tempDir.newFile("test_file")
val dir = tempDir.newDirectory("test_dir")
expect(dir).toBeADirectory()
fails {
expect(file).toBeADirectory()
}
}
@Test
fun toBeAbsolute() {
val s = FileSystems.getDefault().separator
val prefix = if (s == "\\") "C:" else "" // if (s == "\\") => true current os is windows
expect(Paths.get("$prefix${s}absolute${s}path")).toBeAbsolute()
fails {
expect(Paths.get("relative/path")).toBeAbsolute()
}
}
@Test
fun toBeRelative() {
expect(Paths.get("relative/path")).toBeRelative()
fails {
val s = FileSystems.getDefault().separator
val prefix = if (s == "\\") "C:" else "" // if (s == "\\") => true current os is windows
expect(Paths.get("$prefix${s}absolute${s}path")).toBeRelative()
}
}
@Test
fun toHaveTheDirectoryEntries() {
val dir = tempDir.newDirectory("test_dir")
val file1 = dir.newFile("test_file1")
val file2 = dir.newFile("test_file2")
expect(dir).toHaveTheDirectoryEntries("test_file1", "test_file2")
file1.delete()
file2.delete()
fails {
expect(dir).toHaveTheDirectoryEntries("test_file1", "test_file2")
}
}
@Test
fun toHaveTheSameTextualContentAs() {
val writtenTextInFile = "test_test"
val notEmptyFilePath = tempDir.newFile("test_file_1")
notEmptyFilePath.writeText(writtenTextInFile)
val expectedFilePath = tempDir.newFile("test_file_2")
expectedFilePath.writeText(writtenTextInFile)
val emptyFilePath = tempDir.newFile("test_file_3")
expect(notEmptyFilePath).toHaveTheSameTextualContentAs(expectedFilePath)
fails { // because nothing is written inside of `emptyFilePath`
expect(emptyFilePath).toHaveTheSameTextualContentAs(expectedFilePath)
}
}
@Test
fun toHaveTheSameBinaryContentAs() {
val notEmptyFilePath = tempDir.newFile("test_file_1")
notEmptyFilePath.writeBytes(byteArrayOf(1, 2, 3))
val expectedFilePath = tempDir.newFile("test_file_2")
expectedFilePath.writeBytes(byteArrayOf(1, 2, 3))
val emptyFilePath = tempDir.newFile("test_file_3")
expect(notEmptyFilePath).toHaveTheSameBinaryContentAs(expectedFilePath)
fails { // because nothing is written inside of `emptyFilePath`
expect(emptyFilePath).toHaveTheSameBinaryContentAs(expectedFilePath)
}
}
@Test
fun extensionFeature() {
val extension = "txt"
val dir = tempDir.newFile("test_file.$extension")
expect(dir).extension // subject is now of type String (actually "txt")
.notToBeEmpty()
.toEqual(extension)
.notToEndWith("jpg")
fails {
expect(dir).extension // subject is now of type String (actually "txt")
.toEqual("txtt") // fails because it doesn't equal to "txtt"
.toEndWith("jpg") // not reported
// use `.extension` if you want that all expectations are evaluated
}
}
@Test
fun extension() {
val extension = "txt"
val dir = tempDir.newDirectory("test.$extension")
expect(dir).extension { // subject is now of type String (actually "txt")
notToBeEmpty()
toEqual(extension)
notToEndWith("jpg")
}
fails {
expect(dir).extension { // subject is now of type String (actually "txt")
toEqual("txtt") // fails because it doesn't equal to "txtt"
toEndWith("jpg") // fails because it doesn't end with "jpg"
// use `.extension` if you want fail fast behaviour
}
}
}
@Test
fun fileNameFeature() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).fileName // subject is now of type String (actually "test_dir")
.toEndWith("dir")
.toStartWith("test")
.notToBeBlank()
fails {
expect(dir).fileName // subject is now of type String (actually "test_dir")
.toEndWith("foo") // fails because it does not end with "foo"
.toStartWith("invalid") // not reported
// use `fileName {...}` if you want that all expectations are evaluated
}
}
@Test
fun fileName() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).fileName { // subject is now of type String (actually "test_dir")
toEndWith("dir")
toStartWith("test")
notToBeBlank()
}
fails {
expect(dir).fileName { // subject is now of type String (actually "test_dir")
toEndWith("foo") // fails because it does not end with "foo"
toStartWith("invalid") // still evaluated even though `toEndWith("foo")` already fails
// use `.fileName` if you want fail fast behaviour
}
}
}
@Test
fun fileNameWithoutExtensionFeature() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).fileNameWithoutExtension.notToBeEmpty().toEqual("test_dir")
fails {
expect(dir).fileNameWithoutExtension // subject is now of type String (actually "test_dir")
.toBeEmpty() // fails because string is not empty
.notToEqual("test_dir") // not reported toBeEmpty already fails
// use `.fileNameWithoutExtension { ... }` if you want that all expectations are evaluated
}
}
@Test
fun fileNameWithoutExtension() {
val dir = tempDir.newDirectory("test_dir")
expect(dir).fileNameWithoutExtension { // subject is now of type String (actually "test_dir")
notToBeEmpty()
toEqual("test_dir")
}
fails { // because fileNameWithoutExtension equals `test_dir`
expect(dir).fileNameWithoutExtension { // subject is now of type String (actually "test_dir")
toBeEmpty() // fails because string is not empty
notToEqual("test_dir") // still evaluated even though `toBeEmpty()` already fails
// use `.fileNameWithoutExtension` if you want a fail fast behaviour
}
}
}
@Test
fun parentFeature() {
val dir = tempDir.newDirectory("test_dir_1")
val dir2 = tempDir.newDirectory("test_dir_2")
expect(dir).parent.toEqual(dir2.parent).toExist()
fails {
val dir3 = dir2.newDirectory("test_dir")
expect(dir).parent
.toEqual(dir3) // fails because dir3 and dir does not have same parents
.notToExist() // not reported `toEqual(dir3)` already fails
// use `.parent { ... }` if you want that all expectations are evaluated
}
}
@Test
fun parent() {
val dir = tempDir.newDirectory("test_dir_1")
val dir2 = tempDir.newDirectory("test_dir_2")
expect(dir).parent {
toEqual(dir2.parent)
toExist()
}
fails {
val dir3 = dir2.newDirectory("test_dir")
expect(dir).parent {
toEqual(dir3) // fails because dir3 and dir does not have same parents
notToExist() // still evaluated even though `toEqual(dir3)` already fails
// use `.parent` if you want a fail fast behaviour
}
}
}
@Test
fun resolveFeature() {
val dir = tempDir.newDirectory("test_dir")
val fileInDir = dir.newFile("test_file.txt")
expect(dir).resolve("test_file.txt")
.toEqual(fileInDir)
.toEndWith(Paths.get("test_file.txt"))
fails {
expect(dir).resolve("test_file.ttt")
.toEqual(fileInDir) // fails because resolve returns *test_file.ttt and fileInDir equals *test_file.txt
.toEndWith(Paths.get("ttt")) // not reported `toEqual(fileInDir)` already fails
// use `.resolve(other) { ... }` if you want that all expectations are evaluated
}
}
@Test
fun resolve() {
val dir = tempDir.newDirectory("test_dir")
val fileInDir = dir.newFile("test_file.txt")
expect(dir).resolve("test_file.txt") {
toEqual(fileInDir)
toExist()
}
fails {
expect(dir).resolve("test_file.ttt") {
toEqual(fileInDir) // fails
toExist() // still evaluated even though `toEqual(fileInDir)` already fails
// use `.resolve(other).` if you want a fail fast behaviour
}
}
}
}