Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File assertions: exists, isDir, isFile; updated Gradle to 4.0 #48

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Fri Mar 17 11:18:59 CET 2017
#Fri Jun 23 18:28:02 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/org/amshove/kluent/File.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.amshove.kluent

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import java.io.File

fun File.shouldExist() = assertTrue("The file does not exist", this.exists())
fun File.shouldNotExist() = assertFalse("The file exists", this.exists())


fun File.shouldBeDir() = assertTrue("The file is not a directory", this.isDirectory)
fun File.shouldNotBeDir() = assertFalse("The file is a directory", this.isDirectory)


fun File.shouldBeFile() = assertTrue("The file is not a file", this.isFile)
fun File.shouldNotBeFile() = assertFalse("The file is a file", this.isFile)


infix fun File.shouldHaveExtension(other: String) = this.extension shouldEqualTo other
infix fun File.shouldNotHaveExtension(other: String) = this.extension shouldNotEqualTo other
infix fun File.`should have extension`(other: String) = this shouldHaveExtension other
infix fun File.`should not have extension`(other: String) = this shouldNotHaveExtension other

infix fun File.shouldHaveName(other: String) = this.name shouldEqualTo other
infix fun File.shouldNotHaveName(other: String) = this.name shouldNotEqualTo other
infix fun File.`should have name`(other: String) = this shouldHaveName other
infix fun File.`should not have name`(other: String) = this shouldNotHaveName other

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.amshove.kluent.tests.assertions.file

import java.io.File

/**
* Constructs a directory or a plain file with no content and deletes it after the body has run
*/
fun File.useFile(isDir: Boolean = false, body: (file: File) -> Unit) = try {
if (isDir) this.mkdir() else this.writeText("")
body(this)
} finally {
if (!this.delete()) throw Exception("Could not delete file")
}

fun File.useDir(body: (file: File) -> Unit) = this.useFile(true, body)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.amshove.kluent.tests.assertions.file

import org.amshove.kluent.shouldBeDir
import org.amshove.kluent.shouldNotBeDir
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldBeDirTests : Spek({
val dir = File("testDir")
val file = File("test")
given("the shouldBeDir method") {
on("passing a File that is a directory") {
it("should pass") {
dir.useDir { it.shouldBeDir() }
}
}

on("passing a File that is not a directory") {
it("should fail") {
file.useFile { assertFails { it.shouldBeDir() } }
}
}
}

given("the shouldNotBeDir method") {
on("passing a File that is a directory") {
it("should fail") {
dir.useDir { assertFails { it.shouldNotBeDir() } }
}
}
on("passing a File that is not a directory") {
it("should pass") {
file.useFile { it.shouldNotBeDir() }
}
}
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.amshove.kluent.tests.assertions.file

import org.amshove.kluent.shouldBeFile
import org.amshove.kluent.shouldNotBeFile
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldBeFileTests : Spek({
val file = File("isFile")
val dir = File("isDir")
given("the shouldBeFile method") {
on("passing a File that is a file") {
it("should pass") {
file.useFile { it.shouldBeFile() }
}
}
on("passing a File that is a directory") {
it("should fail") {
dir.useDir { assertFails { it.shouldBeFile() } }
}
}
}

given("the shouldNotBeFile method") {
on("passing a File that is a file") {
it("should fail") {
file.useFile { assertFails { it.shouldNotBeFile() } }
}
}
on("passing a File that is a directory") {
it("should pass") {
dir.useDir { it.shouldNotBeFile() }
}
}
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.amshove.kluent.tests.assertions.file

import org.amshove.kluent.shouldExist
import org.amshove.kluent.shouldNotExist
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldExistTests : Spek({
val file = File("test")
given("the shouldExist method") {
on("passing a File that exists") {
it("should pass") {
file.useFile { it.shouldExist() }
}
}
on("passing a File that doesn't exist") {
it("should fail") {
assertFails { file.shouldExist() }
}
}
}

given("the shouldNotExist method") {
on("passing a File that exists") {
it("should fail") {
file.useFile { assertFails { it.shouldNotExist() } }
}
}
on("passing a File that doesn't exist") {
it("should pass") {
file.shouldNotExist()
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.amshove.kluent.tests.assertions.file

import org.amshove.kluent.shouldHaveExtension
import org.amshove.kluent.shouldNotHaveExtension
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldHaveExtensionTests : Spek({
val txtFile = File("test.txt")
val wavFile = File("test.wav")
given("the shouldHaveExtension method") {

on("passing txt on a .txt File") {
it("should pass") {
txtFile.useFile { it shouldHaveExtension "txt" }
}
}

on("passing txt on a File that is not .txt") {
it("should fail") {
wavFile.useFile { assertFails { it shouldHaveExtension "txt" } }
}
}

}

given("the shouldNotHaveExtension method") {
on("passing txt on a .txt File") {
it("should fail") {
txtFile.useFile { assertFails { it shouldNotHaveExtension "txt" } }
}
}
on("passing txt on a File that is not .txt") {
it("should pass") {
wavFile.useFile { it shouldNotHaveExtension "txt" }
}
}
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.amshove.kluent.tests.assertions.file

import org.amshove.kluent.shouldHaveName
import org.amshove.kluent.shouldNotHaveName
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldHaveNameTests : Spek({
val name = "thisfilenameisweird.csv"
val file = File(name)
given("the shouldHaveName method") {
on("passing the correct name of the File") {
it("should pass") {
file.useFile { it shouldHaveName name }
}
}
on("passing the incorrect name of the File") {
it("should fail") {
file.useFile { assertFails { it shouldHaveName "someothername" } }
}
}
}

given("the shouldNotHaveName method") {
on("passing the correct name of the File") {
it("should fail") {
file.useFile { assertFails { it shouldNotHaveName name } }
}
}
on("passing the incorrect name of the File") {
it("should pass") {
file.useFile { it shouldNotHaveName "someothername" }
}
}
}


})
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.amshove.kluent.tests.backtickassertions.file

import org.amshove.kluent.`should have extension`
import org.amshove.kluent.`should not have extension`
import org.amshove.kluent.tests.assertions.file.useFile
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldHaveExtensionTests : Spek({
val txtFile = File("test.txt")
val wavFile = File("test.wav")
given("the shouldHaveExtension method") {

on("passing txt on a .txt File") {
it("should pass") {
txtFile.useFile { it `should have extension` "txt" }
}
}

on("passing txt on a File that is not .txt") {
it("should fail") {
wavFile.useFile { assertFails { it `should have extension` "txt" } }
}
}

}

given("the shouldNotHaveExtension method") {
on("passing txt on a .txt File") {
it("should fail") {
txtFile.useFile { assertFails { it `should not have extension` "txt" } }
}
}
on("passing txt on a File that is not .txt") {
it("should pass") {
wavFile.useFile { it `should not have extension` "txt" }
}
}
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.amshove.kluent.tests.backtickassertions.file

import org.amshove.kluent.`should have name`
import org.amshove.kluent.`should not have name`
import org.amshove.kluent.tests.assertions.file.useFile
import org.jetbrains.spek.api.Spek
import java.io.File
import kotlin.test.assertFails

class ShouldHaveNameTests : Spek({
val name = "thisfilenameisweird.csv"
val file = File(name)
given("the shouldHaveName method") {
on("passing the correct name of the File") {
it("should pass") {
file.useFile { it `should have name` name }
}
}
on("passing the incorrect name of the File") {
it("should fail") {
file.useFile { assertFails { it `should have name` "someothername" } }
}
}
}

given("the shouldNotHaveName method") {
on("passing the correct name of the File") {
it("should fail") {
file.useFile { assertFails { it `should not have name` name } }
}
}
on("passing the incorrect name of the File") {
it("should pass") {
file.useFile { it `should not have name` "someothername" }
}
}
}

})