~wegtam/smederee

Showing details for patch 88d48a1b68ad90b96135d8fa9b245eddc044443d.
2024-10-21 (Mon), 11:13 AM - Jens Grassel - 88d48a1b68ad90b96135d8fa9b245eddc044443d

Add test for DarcsHash

Summary of changes
1 files added
  • modules/darcs/src/test/scala/de/smederee/darcs/DarcsHashTest.scala
1 files modified with 5 lines added and 0 lines removed
  • modules/darcs/src/main/scala/de/smederee/darcs/DarcsCommands.scala with 5 added and 0 removed lines
diff -rN -u old-smederee/modules/darcs/src/main/scala/de/smederee/darcs/DarcsCommands.scala new-smederee/modules/darcs/src/main/scala/de/smederee/darcs/DarcsCommands.scala
--- old-smederee/modules/darcs/src/main/scala/de/smederee/darcs/DarcsCommands.scala	2024-11-22 05:37:26.670463085 +0000
+++ new-smederee/modules/darcs/src/main/scala/de/smederee/darcs/DarcsCommands.scala	2024-11-22 05:37:26.670463085 +0000
@@ -16,6 +16,11 @@
 
 import scala.util.matching.Regex
 
+/** A type representing a "darcs patch hash" which according to documentation is: a hash (SHA1) of patch info (name,
+  * author name, timestamp, log, random seed) and is used as an identifier for patches independently of their context
+  * and also shown in the darcs log output. This hash never changes, even if the patch commutes. But it changes if the
+  * patch is amended to (random seed is changed).
+  */
 opaque type DarcsHash = String
 object DarcsHash {
     val Format: Regex = "^[a-f0-9]{40}$".r
diff -rN -u old-smederee/modules/darcs/src/test/scala/de/smederee/darcs/DarcsHashTest.scala new-smederee/modules/darcs/src/test/scala/de/smederee/darcs/DarcsHashTest.scala
--- old-smederee/modules/darcs/src/test/scala/de/smederee/darcs/DarcsHashTest.scala	1970-01-01 00:00:00.000000000 +0000
+++ new-smederee/modules/darcs/src/test/scala/de/smederee/darcs/DarcsHashTest.scala	2024-11-22 05:37:26.670463085 +0000
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022 Contributors as noted in the AUTHORS.md file
+ *
+ * Licensed under the EUPL
+ */
+
+package de.smederee.darcs
+
+import java.nio.charset.StandardCharsets
+import java.security.MessageDigest
+
+import munit.*
+
+import org.scalacheck.*
+import org.scalacheck.Prop.*
+
+final class DarcsHashTest extends ScalaCheckSuite {
+    private val hasher = MessageDigest.getInstance("SHA1")
+
+    private val genSha1String: Gen[String] =
+        Gen.alphaNumStr
+            .map(string =>
+                hasher.digest(string.getBytes(StandardCharsets.UTF_8)).map(byte => "%02x".format(byte)).mkString("")
+            )
+
+    property("from must work for correctly formatted hash strings") {
+        forAll(genSha1String) { (hashedString: String) =>
+            DarcsHash.from(hashedString) match {
+                case None       => fail(s"No darcs hash generated from given SHA1 hash: $hashedString!")
+                case Some(hash) => assertEquals(hash, DarcsHash(hashedString))
+            }
+        }
+    }
+}