Skip to content

Commit

Permalink
Merge pull request #648 from jenspav/string-normalize
Browse files Browse the repository at this point in the history
Normalize file names even more
  • Loading branch information
jenspav authored Dec 12, 2024
2 parents d335af7 + 3681e44 commit c95de70
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package nl.avisi.structurizr.site.generatr

fun String.normalize(): String = lowercase().replace("\\s+".toRegex(), "-")
// based on https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
private const val reservedChars = "|\\?*<\":>+[]/'"
private val reservedNames = setOf(
"CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
)

fun String.normalize(): String =
lowercase()
.replace("\\s+".toRegex(), "-")
.filterNot { reservedChars.contains(it) }
.trim()
.let {
if (reservedNames.contains(it.uppercase()))
"${it}-"
else
it
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nl.avisi.structurizr.site.generatr.site

import assertk.assertThat
import assertk.assertions.isEqualTo
import nl.avisi.structurizr.site.generatr.normalize
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory

class StringUtilitiesTest {

@TestFactory
fun `normalize strips invalid chars`() = listOf(
listOf("doc", "doc"),
listOf("d c", "d-c"),
listOf("doc:", "doc"),
listOf(" doc ", "-doc-"),
listOf("aux", "aux-"),
).map { (actual, expected) ->
DynamicTest.dynamicTest("normalize replaces $actual with $expected") {
assertThat(actual.normalize()).isEqualTo(expected)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package nl.avisi.structurizr.site.generatr.site

import assertk.assertThat
import assertk.assertions.isEqualTo
import com.structurizr.Workspace
import nl.avisi.structurizr.site.generatr.includedProperties
import kotlin.test.Test
import kotlin.test.assertEquals

class StructurizrUtilitiesTest {

protected val svgFactory = { _: String, _: String -> "" }
private val svgFactory = { _: String, _: String -> "" }

protected fun generatorContext(
private fun generatorContext(
workspaceName: String = "Workspace name",
branches: List<String> = listOf("main"),
currentBranch: String = "main",
Expand All @@ -27,8 +28,8 @@ class StructurizrUtilitiesTest {

val includedProperties = element.includedProperties

assertEquals(3, includedProperties.size)
assertEquals(includedProperties.keys, setOf("structurizrnotquite.key", "generatrbut.not.key", "other.key"))
assertThat(includedProperties.size).isEqualTo(3)
assertThat(includedProperties.keys).isEqualTo(setOf("structurizrnotquite.key", "generatrbut.not.key", "other.key"))
}

@Test
Expand All @@ -39,15 +40,15 @@ class StructurizrUtilitiesTest {

val includedProperties = element.includedProperties

assertEquals(2, includedProperties.size)
assertEquals("key1", includedProperties.keys.first())
assertEquals("key2", includedProperties.keys.last())
assertThat(includedProperties.size).isEqualTo(2)
assertThat(includedProperties.keys.first()).isEqualTo("key1")
assertThat(includedProperties.keys.last()).isEqualTo("key2")
}

@Test
fun `includedProperties returns empty map when no properties are set`() {
val element = generatorContext().workspace.model.addSoftwareSystem("System 1")
val includedProperties = element.includedProperties
assertEquals(0, includedProperties.size)
assertThat(includedProperties.size).isEqualTo(0)
}
}

0 comments on commit c95de70

Please sign in to comment.