From 8500f6947944a7eec40121d39d5cd4eb833820f3 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sun, 7 Jan 2024 15:59:11 +0000 Subject: [PATCH] FSEntry name validation and duplicate names in folder tests --- src/main/kotlin/filesystem/FSCreator.kt | 1 + src/test/kotlin/filesystem/FSCreatorTest.kt | 32 ++++++- src/test/kotlin/filesystem/FSEntryTest.kt | 96 ++++++++++++++++++++- 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/filesystem/FSCreator.kt b/src/main/kotlin/filesystem/FSCreator.kt index 84b4487..cda4e58 100644 --- a/src/main/kotlin/filesystem/FSCreator.kt +++ b/src/main/kotlin/filesystem/FSCreator.kt @@ -53,4 +53,5 @@ class FSCreator { } class CyclicFolderException : Exception("Cyclic FSFolders are not supported") + class DuplicateEntryNameException : Exception("Folder contains entries with duplicate names") \ No newline at end of file diff --git a/src/test/kotlin/filesystem/FSCreatorTest.kt b/src/test/kotlin/filesystem/FSCreatorTest.kt index 77c20fd..9d19bca 100644 --- a/src/test/kotlin/filesystem/FSCreatorTest.kt +++ b/src/test/kotlin/filesystem/FSCreatorTest.kt @@ -104,7 +104,7 @@ class FSCreatorTest { "folder", listOf( FSFolder( - "folder", + "folder", // Repeated name should be fine here (not throw) listOf( FSFolder( "secrets", @@ -152,4 +152,34 @@ class FSCreatorTest { creator.create(folder4, "_tmp") } } + + @Test + fun `create throws on folder with duplicate names`() { + val folder = + FSFolder( + "folder", + listOf( + FSFile("README.md", "# Test File"), + FSFile("hello-world.txt", "Hello World!"), + FSFolder( + "src", + listOf( + FSFile("README.md", "# Source files"), + FSFolder( + "solution", + listOf( + FSFile("solution.py", "print('1 + 1 = 1')"), + FSFile("tmp", "A temporary file"), + FSFolder("tmp", listOf()), + ), + ), + ), + ), + FSFolder("tmp", listOf()), + ), + ) + assertThrows { + creator.create(folder, "_tmp") + } + } } \ No newline at end of file diff --git a/src/test/kotlin/filesystem/FSEntryTest.kt b/src/test/kotlin/filesystem/FSEntryTest.kt index b474ac2..86076bd 100644 --- a/src/test/kotlin/filesystem/FSEntryTest.kt +++ b/src/test/kotlin/filesystem/FSEntryTest.kt @@ -1,10 +1,34 @@ package filesystem -import org.junit.jupiter.api.Test +import org.junit.jupiter.api.* import kotlin.test.assertFalse import kotlin.test.assertTrue class FSEntryTest { + @Test + fun `valid name entries`() { + assertDoesNotThrow("should construct FSFile and FSFolder without throwing") { + FSFile("A file with a name.tar.xz", "Contents") + FSFolder(".a folder with a name", listOf()) + } + } + + @Test + fun `invalid name entries`() { + assertThrows { + FSFile("File/here", "Contents") + } + assertThrows { + FSFolder("Folder/here", listOf()) + } + assertThrows { + FSFolder(".", listOf()) + } + assertThrows { + FSFolder("/", listOf()) + } + } + @Test fun `non-cyclic folder`() { val folder = @@ -39,4 +63,74 @@ class FSEntryTest { assertTrue(folder3.isCyclic()) assertTrue(folder4.isCyclic()) } + + @Test + fun `no duplicate names folder`() { + val folder = + FSFolder( + "folder", + listOf( + FSFile("README.md", "# Test File"), + FSFile("hello-world.txt", "Hello World!"), + FSFolder( + "src", + listOf( + FSFile("README.md", "# Source files"), + FSFile("solution-1.py", "print('1 + 1 = 1')"), + FSFile("solution-2.py", "print('1 + 1 = 1')"), + ), + ), + FSFolder("tmp", listOf()), + ), + ) + assertFalse(folder.hasDuplicateNames()) + assertFalse(folder.deepHasDuplicateNames()) + } + + @Test + fun `shallow duplicate names folder`() { + val folder = + FSFolder( + "folder", + listOf( + FSFile("README.md", "# Test File"), + FSFile("hello-world.txt", "Hello World!"), + FSFolder( + "src", + listOf( + FSFile("README.md", "# Source files"), + FSFile("solution-1.py", "print('1 + 1 = 1')"), + FSFile("solution-2.py", "print('1 + 1 = 1')"), + ), + ), + FSFolder("tmp", listOf()), + FSFile("tmp", "A temporary file"), + ), + ) + assertTrue(folder.hasDuplicateNames()) + assertTrue(folder.deepHasDuplicateNames()) + } + + @Test + fun `deep duplicate names folder`() { + val folder = + FSFolder( + "folder", + listOf( + FSFile("README.md", "# Test File"), + FSFile("hello-world.txt", "Hello World!"), + FSFolder( + "src", + listOf( + FSFile("README.md", "# Source files"), + FSFile("solution-1.py", "print('1 + 1 = 1')"), + FSFile("solution-1.py", "print('1 + 1 = 1')"), + ), + ), + FSFolder("tmp", listOf()), + ), + ) + assertFalse(folder.hasDuplicateNames()) + assertTrue(folder.deepHasDuplicateNames()) + } } \ No newline at end of file