Skip to content

Commit

Permalink
Add samples for fileAssertions (#944) (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
iljakorneckis authored Oct 21, 2021
1 parent 3883719 commit 5f52211
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import java.nio.file.Path
*
* @return The newly created [Expect] for the transformed subject.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.FileExpectationSamples.asPathFeature
*
* @since 0.9.0
*/
fun <T : File> Expect<T>.asPath(): Expect<Path> =
Expand All @@ -34,6 +36,8 @@ fun <T : File> Expect<T>.asPath(): Expect<Path> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.FileExpectationSamples.asPath
*
* @since 0.9.0
*/
fun <T : File> Expect<T>.asPath(assertionCreator: Expect<Path>.() -> Unit): Expect<T> =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ch.tutteli.atrium.api.fluent.en_GB.samples

import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.api.verbs.internal.expect
import ch.tutteli.niok.newFile
import java.nio.file.Files
import kotlin.test.Test

class FileExpectationSamples {

private val tempDir = Files.createTempDirectory("FileAssertionSamples")

@Test
fun asPathFeature() {
val file = tempDir.newFile("target").toFile()

expect(file)
.asPath() // subject is now of type Path
.toBeARegularFile()

fails {
expect(file)
.asPath()
.toBeADirectory() //fails
}
}

@Test
fun asPath() {
val file = tempDir.newFile("target").toFile()

expect(file).asPath { // subject within this block is of type Path
toBeARegularFile()
toStartWith(tempDir)
} // subject here is back to type File

fails {
// all assertions are evaluated inside an assertion group block; for more details:
// https://github.com/robstoll/atrium#define-single-assertions-or-assertion-groups

expect(file).asPath {
toBeADirectory() // fails
notToStartWith(tempDir) // still evaluated, use `.asPath().` if you want a fail fast behaviour
}
}
}
}

0 comments on commit 5f52211

Please sign in to comment.