Skip to content

Commit

Permalink
feat: camel case to sentence for test names
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmcdowall committed May 24, 2023
1 parent fac9c5d commit dfd2423
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/main/kotlin/io/lsdconsulting/junit5/LsdExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.lang.reflect.Method
import java.util.*
import java.util.function.Consumer
import java.util.function.Predicate
import java.util.regex.Pattern

/**
* Junit 5 extension to create LSD reports based on test results.
Expand Down Expand Up @@ -74,7 +75,7 @@ class LsdExtension : TestWatcher, AfterTestExecutionCallback, AfterAllCallback {
if (parent.isPresent) {
val parentDisplayName = prefixParentDisplayName(parent.get())
val separator = if (StringUtils.isBlank(parentDisplayName)) "" else ": "
return parentDisplayName + separator + context.displayName
return parentDisplayName + separator + context.displayName.deCamelCase()
}
return ""
}
Expand Down Expand Up @@ -126,4 +127,13 @@ class LsdExtension : TestWatcher, AfterTestExecutionCallback, AfterAllCallback {
}
}
}
}

fun String.deCamelCase(): String {
return replace(Pattern.compile("([a-z])([A-Z])").toRegex(), "$1 $2")
.replace(Pattern.compile("([A-Z])([a-z])").toRegex(), " $1$2")
.replace(" ", " ")
.replace(Pattern.compile("[()]").toRegex(), "")
.trim()
.lowercase()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="logo">LSD</h1>
<h1>Aborted Test Report</h1><article id="2">
<span class="articleStart"></span>
<h2 class="warn">The test name</h2><section class="description warn">
<h2 class="warn">a test name</h2><section class="description warn">
<h3 class="warn">Description</h3>
<p><h4 class="error">Test Aborted!</h4> <a href="#1"><pre>Aborted for testing reasons</pre></a>
<div id="1" class="overlay" onclick="location.href='#!';">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="logo">LSD</h1>
<h1>Disabled Test Report</h1><article id="1">
<span class="articleStart"></span>
<h2 class="warn">The test name</h2><section class="description warn">
<h2 class="warn">a test name</h2><section class="description warn">
<h3 class="warn">Description</h3>
<p><h4 class="warn">Test Disabled</h4></p>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="logo">LSD</h1>
<h1>Failure Test Report</h1><article id="2">
<span class="articleStart"></span>
<h2 class="error">The test name</h2><section class="description error">
<h2 class="error">a test name</h2><section class="description error">
<h3 class="error">Description</h3>
<p><h4 class="error">&#10060; Failed!</h4> <a href="#1"><pre>Expected &lt;true&gt; but was &lt;false&gt;</pre></a>
<div id="1" class="overlay" onclick="location.href='#!';">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="logo">LSD</h1>
<h1>Successful Test Report</h1><article id="1">
<span class="articleStart"></span>
<h2 class="success">The test name</h2><section class="description success">
<h2 class="success">a test name</h2><section class="description success">
<h3 class="success">Description</h3>
<p><h4 class="success">&#10003; Test Passed</h4></p>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import com.lsd.core.LsdContext
import com.lsd.core.properties.LsdProperties.OUTPUT_DIR
import com.lsd.core.properties.LsdProperties.get
import io.lsdconsulting.junit5.LsdExtension
import io.lsdconsulting.junit5.deCamelCase
import org.approvaltests.Approvals
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.util.*
import kotlin.io.path.copyTo

internal class LsdExtensionTest {
private val mockTestContext = mock(ExtensionContext::class.java)
Expand Down Expand Up @@ -63,7 +65,7 @@ internal class LsdExtensionTest {
`when`(mockTestContext.parent).thenReturn(Optional.empty())
`when`(mockTestContext.displayName).thenReturn("a display name")
lsdExtension.afterAll(mockTestContext)
Assertions.assertThat(indexFile).exists()
assertThat(indexFile).exists()
}

@Test
Expand All @@ -72,12 +74,23 @@ internal class LsdExtensionTest {
`when`(mockTestContext.parent).thenReturn(Optional.of(mockTestContext))
`when`(mockTestContext.displayName).thenReturn("a nested display name")
lsdExtension.afterAll(mockTestContext)
Assertions.assertThat(indexFile).doesNotExist()
assertThat(indexFile).doesNotExist()
}

@ParameterizedTest
@CsvSource(
"aCamelCaseExample,a camel case example",
"CamelCase,camel case",
"anITSolution,an it solution",
"myClassUnderTestShouldDoSomethingNice(),my class under test should do something nice",
)
fun deCamelCase(input: String, expected: String) {
assertThat(input.deCamelCase()).isEqualTo(expected)
}

private fun configureMockContextToReturnDisplayNames() {
`when`(mockTestContext.requiredTestInstance).thenReturn(this)
`when`(mockTestContext.displayName).thenReturn("The test name")
`when`(mockTestContext.displayName).thenReturn("aTestName()")
`when`(mockTestContext.parent)
.thenReturn(Optional.of(mockTestContext))
.thenReturn(Optional.empty())
Expand Down

0 comments on commit dfd2423

Please sign in to comment.