From 6ffc86da4b72b137d6a4d9ad2c7efedfb7049782 Mon Sep 17 00:00:00 2001 From: goreRatzete Date: Sun, 25 Jun 2017 14:41:25 +0200 Subject: [PATCH 1/3] File assertions: exists, isDir, isFile; updated Gradle to 4.0 --- gradle/wrapper/gradle-wrapper.properties | 4 +- src/main/kotlin/org/amshove/kluent/File.kt | 16 ++++++++ .../tests/assertions/file/FileTestHelper.kt | 15 +++++++ .../tests/assertions/file/ShouldBeDirTests.kt | 39 +++++++++++++++++++ .../assertions/file/ShouldBeFileTests.kt | 38 ++++++++++++++++++ .../tests/assertions/file/ShouldExistTests.kt | 36 +++++++++++++++++ 6 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/org/amshove/kluent/File.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/assertions/file/FileTestHelper.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeDirTests.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeFileTests.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldExistTests.kt diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 22543614..6dacc39e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#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 zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.0-bin.zip diff --git a/src/main/kotlin/org/amshove/kluent/File.kt b/src/main/kotlin/org/amshove/kluent/File.kt new file mode 100644 index 00000000..a60f3deb --- /dev/null +++ b/src/main/kotlin/org/amshove/kluent/File.kt @@ -0,0 +1,16 @@ +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) diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/file/FileTestHelper.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/FileTestHelper.kt new file mode 100644 index 00000000..9dc270a4 --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/FileTestHelper.kt @@ -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) \ No newline at end of file diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeDirTests.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeDirTests.kt new file mode 100644 index 00000000..f94b9dbd --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeDirTests.kt @@ -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() } + } + } + } + +}) \ No newline at end of file diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeFileTests.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeFileTests.kt new file mode 100644 index 00000000..0dbc893a --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldBeFileTests.kt @@ -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() } + } + } + } + +}) \ No newline at end of file diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldExistTests.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldExistTests.kt new file mode 100644 index 00000000..a198d68e --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldExistTests.kt @@ -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() + } + } + } +}) \ No newline at end of file From 59dac1d84d77ffa930acf37c5ec426a94f54d676 Mon Sep 17 00:00:00 2001 From: goreRatzete Date: Sun, 25 Jun 2017 14:51:45 +0200 Subject: [PATCH 2/3] revert gradle version --- gradle/wrapper/gradle-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 6dacc39e..48b91da6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip From 46d66f3ffa8de3161d28483584d2db78dd839c37 Mon Sep 17 00:00:00 2001 From: goreRatzete Date: Mon, 26 Jun 2017 20:51:06 +0200 Subject: [PATCH 3/3] added File shouldHaveName and shouldHaveExtension --- src/main/kotlin/org/amshove/kluent/File.kt | 12 ++++++ .../file/ShouldHaveExtensionTests.kt | 41 ++++++++++++++++++ .../assertions/file/ShouldHaveNameTests.kt | 39 +++++++++++++++++ .../file/ShouldHaveExtensionTests.kt | 42 +++++++++++++++++++ .../file/ShouldHaveNameTests.kt | 39 +++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveExtensionTests.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveNameTests.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveExtensionTests.kt create mode 100644 src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveNameTests.kt diff --git a/src/main/kotlin/org/amshove/kluent/File.kt b/src/main/kotlin/org/amshove/kluent/File.kt index a60f3deb..11654949 100644 --- a/src/main/kotlin/org/amshove/kluent/File.kt +++ b/src/main/kotlin/org/amshove/kluent/File.kt @@ -14,3 +14,15 @@ fun File.shouldNotBeDir() = assertFalse("The file is a directory", this.isDirect 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 + diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveExtensionTests.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveExtensionTests.kt new file mode 100644 index 00000000..dd59434c --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveExtensionTests.kt @@ -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" } + } + } + } + +}) \ No newline at end of file diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveNameTests.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveNameTests.kt new file mode 100644 index 00000000..54e255e3 --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/file/ShouldHaveNameTests.kt @@ -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" } + } + } + } + + +}) \ No newline at end of file diff --git a/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveExtensionTests.kt b/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveExtensionTests.kt new file mode 100644 index 00000000..b5e9a156 --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveExtensionTests.kt @@ -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" } + } + } + } + +}) \ No newline at end of file diff --git a/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveNameTests.kt b/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveNameTests.kt new file mode 100644 index 00000000..5563c5ee --- /dev/null +++ b/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/file/ShouldHaveNameTests.kt @@ -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" } + } + } + } + +}) \ No newline at end of file