Skip to content

Commit

Permalink
tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz committed Oct 19, 2022
1 parent c06f7fd commit b259908
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 128 deletions.
8 changes: 8 additions & 0 deletions src/test/kotlin/org/jetbrains/changelog/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import org.gradle.kotlin.dsl.getByType
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.intellij.lang.annotations.Language
import java.io.File
import java.nio.file.Files.createTempDirectory
import kotlin.test.BeforeTest
import kotlin.test.assertEquals

open class BaseTest {

Expand All @@ -26,6 +28,7 @@ open class BaseTest {
protected val changelogFile
get() = File(extension.path.get())

@Language("Markdown")
protected var changelog: String = ""
set(value) {
field = value
Expand All @@ -42,6 +45,7 @@ open class BaseTest {
project.version = value
}

@Language("Groovy")
protected var buildFile = ""
set(value) {
field = value
Expand Down Expand Up @@ -85,4 +89,8 @@ open class BaseTest {

protected fun runFailingTask(taskName: String, vararg arguments: String): BuildResult =
prepareTask(taskName, *arguments).buildAndFail()

protected fun assertMarkdown(@Language("Markdown") expected: String, @Language("Markdown") actual: String) {
assertEquals(expected.trim(), actual.trim())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@BeforeTest
fun localSetUp() {
changelog = //language=markdown
changelog =
"""
# Changelog
Project description.
Expand Down Expand Up @@ -52,8 +52,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
extension.get(version).apply {
assertEquals(project.version, version)

//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
First release.
Expand All @@ -65,8 +64,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
toText()
)

//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
First release.
Expand All @@ -78,8 +76,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
toString()
)

//language=markdown
assertEquals(
assertMarkdown(
"""
<h2>[1.0.0]</h2>
<p>First release.</p>
Expand All @@ -106,8 +103,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
fun `getUnreleased() returns Unreleased section`() {
extension.getUnreleased().apply {
assertEquals("[Unreleased]", version)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [Unreleased]
Not yet released version.
Expand All @@ -127,8 +123,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
extension.unreleasedTerm.set("[Upcoming]")
extension.getUnreleased().withSummary(true).apply {
assertEquals("[Upcoming]", version)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [Upcoming]
Not yet released version.
Expand All @@ -145,7 +140,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
@Test
@Suppress("LongMethod", "MaxLineLength")
fun `parses changelog into structured sections`() {
changelog = //language=markdown
changelog =
"""
# Changelog
My project description.
Expand Down Expand Up @@ -174,8 +169,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
extension.get(version).apply {
assertEquals(this@ChangelogPluginExtensionTest.version, version)
assertEquals("## [1.0.0]", header)
//language=markdown
assertEquals(
assertMarkdown(
"""
First release.
Expand All @@ -193,8 +187,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
assertTrue(containsKey("Removed"))
assertEquals(1, get("Removed")?.size)
}
//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
First release.
Expand All @@ -219,7 +212,6 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent(),
toText()
)
//language=html
assertEquals(
"""
<h2>[1.0.0]</h2>
Expand All @@ -239,8 +231,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent(),
toHTML()
)
//language=markdown
assertEquals(
assertMarkdown(
"""
[1.0.0]
First release.
Expand Down Expand Up @@ -270,7 +261,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `filters out entries from the change notes for the given version`() {
changelog = //language=markdown
changelog =
"""
# Changelog
Expand Down Expand Up @@ -306,8 +297,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
assertFalse(containsKey("Removed"))
}

//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
Expand All @@ -323,7 +313,6 @@ class ChangelogPluginExtensionTest : BaseTest() {
toText()
)

//language=html
assertEquals(
"""
<h2>[1.0.0]</h2>
Expand All @@ -346,8 +335,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
extension.getLatest().apply {
assertEquals("[Unreleased]", version)
assertEquals("## [Unreleased]", header)
//language=markdown
assertEquals(
assertMarkdown(
"""
Not yet released version.
Expand All @@ -366,7 +354,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `parses header with custom format containing version and date`() {
changelog = //language=markdown
changelog =
"""
# Changelog
## NEW VERSION
Expand All @@ -384,7 +372,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `returns change notes without group sections if not present`() {
changelog = //language=markdown
changelog =
"""
# Changelog
## [1.0.0]
Expand All @@ -399,16 +387,14 @@ class ChangelogPluginExtensionTest : BaseTest() {
assertTrue(containsKey(""))
assertEquals(1, get("")?.size)
}
//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
- Foo
""".trimIndent(),
toText()
)
//language=html
assertEquals(
"""
<h2>[1.0.0]</h2>
Expand All @@ -422,7 +408,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `splits change notes into a list by the given itemPrefix`() {
changelog = //language=markdown
changelog =
"""
# Changelog
## [1.0.0]
Expand All @@ -436,8 +422,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
assertEquals(1, getSections().keys.size)
getSections().values.first().apply {
assertEquals(2, size)
//language=markdown
assertEquals(
assertMarkdown(
"""
- Foo - bar
* Foo2
Expand All @@ -458,8 +443,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
assertEquals("1.0.0", keys.last())
assertEquals("## [Unreleased]", values.first().header)
assertEquals("## [1.0.0]", values.last().header)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [Unreleased]
Not yet released version.
Expand All @@ -470,8 +454,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent(),
values.first().toText()
)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [Unreleased]
Not yet released version.
Expand All @@ -482,8 +465,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent(),
values.first().toText()
)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
First release.
Expand All @@ -494,8 +476,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent(),
values.last().toText()
)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
First release.
Expand All @@ -513,7 +494,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
fun `returns Changelog items for change note without category`() {
extension.itemPrefix.set("*")
extension.unreleasedTerm.set("Unreleased")
changelog = //language=markdown
changelog =
"""
# My Changelog
Expand All @@ -524,7 +505,6 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent()

assertNotNull(extension.getLatest())
//language=html
assertEquals(
"""
<h2>Unreleased</h2>
Expand All @@ -537,7 +517,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `allows to customize the header parser regex to match version in different format than semver`() {
changelog = //language=markdown
changelog =
"""
# My Changelog
Expand All @@ -563,7 +543,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `return null for non-existent version`() {
changelog = //language=markdown
changelog =
"""
# My Changelog
Expand All @@ -577,7 +557,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `return change notes for version with custom headerParserRegex`() {
changelog = //language=markdown
changelog =
"""
# My Changelog
Expand All @@ -594,7 +574,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `return change notes without summary`() {
changelog = //language=markdown
changelog =
"""
# My Changelog
Expand All @@ -607,8 +587,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

extension.get("1.0.0").apply {
assertEquals("1.0.0", version)
//language=markdown
assertEquals(
assertMarkdown(
"""
## [1.0.0]
First release.
Expand All @@ -623,8 +602,7 @@ class ChangelogPluginExtensionTest : BaseTest() {

@Test
fun `returns changelog description`() {
//language=markdown
assertEquals(
assertMarkdown(
"""
Project description.
Multiline description:
Expand Down
Loading

0 comments on commit b259908

Please sign in to comment.