From 0e1afa6dc8594530b739ee148fc6ca4ef363e636 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 2 Dec 2023 02:50:08 +0000 Subject: [PATCH] Implement toString() for objects (#4) > Search for specific commit by hash or metadata and print its content. To satisfy this final requirement, we need to have `toString()` implemented for version manager `Object`s which will allow them to be easily (and nicely) printed if needed (includes tests). Reviewed-on: https://git.koval.net/cyclane/teamcity-gitea-test-task/pulls/4 --- src/main/kotlin/tinyvm/Tree.kt | 15 ++++++- src/test/kotlin/tinyvm/RepositoryTest.kt | 51 ++++++++++++++++++++---- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/tinyvm/Tree.kt b/src/main/kotlin/tinyvm/Tree.kt index 1500f48..404aa96 100644 --- a/src/main/kotlin/tinyvm/Tree.kt +++ b/src/main/kotlin/tinyvm/Tree.kt @@ -15,9 +15,22 @@ class Tree(val nodes: Map) : Node("tree") { nodes.map { (name, node) -> "${node.type} $name\u0000${node.hash()}" }.sorted().joinToString() + + override fun toString(): String = + "tree ${hash()}\n" + + nodes.map { (name, node) -> + when (node) { + is Tree -> "+$name/\n" + is Blob -> "+$name\n" + } + node.toString().leftMargin() + }.sorted().joinToString("\n") } /** * A blob is a data container. */ -class Blob(override val data: String) : Node("blob") \ No newline at end of file +class Blob(override val data: String) : Node("blob") { + override fun toString(): String = "blob ${hash()}\n${data.leftMargin()}" +} + +private fun String.leftMargin(): String = split('\n').joinToString("\n") { "| $it" } \ No newline at end of file diff --git a/src/test/kotlin/tinyvm/RepositoryTest.kt b/src/test/kotlin/tinyvm/RepositoryTest.kt index 172886a..93cb2eb 100644 --- a/src/test/kotlin/tinyvm/RepositoryTest.kt +++ b/src/test/kotlin/tinyvm/RepositoryTest.kt @@ -18,22 +18,22 @@ internal class RepositoryTest { tree = Tree( mapOf( + "dir2" to + Tree( + mapOf( + "test1.txt" to Blob("This is a second file"), + ), + ), "dir1" to Tree( mapOf( "test1.txt" to Blob("Hello World!"), ), ), - "dir2" to - Tree( - mapOf( - "test2.txt" to Blob("This is a second file"), - ), - ), ), ), author = Author("Gleb Koval", "gleb@koval.net"), - message = "Move test1.txt and add dir2/test2.txt", + message = "Move test1.txt to dir1 and add dir2/test1.txt", timestamp = Instant.ofEpochSecond(50), ), Commit( @@ -49,7 +49,7 @@ internal class RepositoryTest { "dir2" to Tree( mapOf( - "test2.txt" to Blob("This is a second file"), + "test1.txt" to Blob("This is a second file"), ), ), "README.md" to Blob("# This is a test repo!"), @@ -117,4 +117,39 @@ internal class RepositoryTest { assertEquals(committed, repository.findCommit { it.message.matches("Move.*".toRegex()) }) assertEquals(null, repository.getCommit("00000000000000000000")) } + + @Test + fun `can display commit`() { + assertEquals( + """ + commit 804c6f0c66da0ea0eab8ac29f23a627e03a962b2 + tree aaebbb258bce30749fc302cbd161b78462252a32 + author Gleb Koval + timestamp 0 + + Add test1.txt + """.trimIndent(), + commits[0].toString(), + ) + } + + @Test + fun `can display tree`() { + assertEquals( + """ + tree 048b8e267dd7a6c5d4849d59f6ff82e6ae948f13 + +dir1/ + | tree aaebbb258bce30749fc302cbd161b78462252a32 + | +test1.txt + | | blob c57eff55ebc0c54973903af5f72bac72762cf4f4 + | | | Hello World! + +dir2/ + | tree 0e14cbdba02924cd63a58c2492fc3dd05dc682bd + | +test1.txt + | | blob da3fcc31cbc16fcbb7526d68771f77e7e7f02fb1 + | | | This is a second file + """.trimIndent(), + commits[1].tree.toString(), + ) + } } \ No newline at end of file