Improve tests & validate FSEntry names #4
@@ -53,4 +53,5 @@ class FSCreator {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CyclicFolderException : Exception("Cyclic FSFolders are not supported")
 | 
					class CyclicFolderException : Exception("Cyclic FSFolders are not supported")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class DuplicateEntryNameException : Exception("Folder contains entries with duplicate names")
 | 
					class DuplicateEntryNameException : Exception("Folder contains entries with duplicate names")
 | 
				
			||||||
@@ -104,7 +104,7 @@ class FSCreatorTest {
 | 
				
			|||||||
                    "folder",
 | 
					                    "folder",
 | 
				
			||||||
                    listOf(
 | 
					                    listOf(
 | 
				
			||||||
                        FSFolder(
 | 
					                        FSFolder(
 | 
				
			||||||
                            "folder",
 | 
					                            "folder", // Repeated name should be fine here (not throw)
 | 
				
			||||||
                            listOf(
 | 
					                            listOf(
 | 
				
			||||||
                                FSFolder(
 | 
					                                FSFolder(
 | 
				
			||||||
                                    "secrets",
 | 
					                                    "secrets",
 | 
				
			||||||
@@ -152,4 +152,34 @@ class FSCreatorTest {
 | 
				
			|||||||
            creator.create(folder4, "_tmp")
 | 
					            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<DuplicateEntryNameException> {
 | 
				
			||||||
 | 
					            creator.create(folder, "_tmp")
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,10 +1,34 @@
 | 
				
			|||||||
package filesystem
 | 
					package filesystem
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.junit.jupiter.api.Test
 | 
					import org.junit.jupiter.api.*
 | 
				
			||||||
import kotlin.test.assertFalse
 | 
					import kotlin.test.assertFalse
 | 
				
			||||||
import kotlin.test.assertTrue
 | 
					import kotlin.test.assertTrue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class FSEntryTest {
 | 
					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<InvalidEntryNameException> {
 | 
				
			||||||
 | 
					            FSFile("File/here", "Contents")
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        assertThrows<InvalidEntryNameException> {
 | 
				
			||||||
 | 
					            FSFolder("Folder/here", listOf())
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        assertThrows<InvalidEntryNameException> {
 | 
				
			||||||
 | 
					            FSFolder(".", listOf())
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        assertThrows<InvalidEntryNameException> {
 | 
				
			||||||
 | 
					            FSFolder("/", listOf())
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun `non-cyclic folder`() {
 | 
					    fun `non-cyclic folder`() {
 | 
				
			||||||
        val folder =
 | 
					        val folder =
 | 
				
			||||||
@@ -39,4 +63,74 @@ class FSEntryTest {
 | 
				
			|||||||
        assertTrue(folder3.isCyclic())
 | 
					        assertTrue(folder3.isCyclic())
 | 
				
			||||||
        assertTrue(folder4.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())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user