Added more tests.

This commit is contained in:
Erik C. Thauvin 2021-07-10 02:31:54 -07:00
parent e8e7f65b2c
commit 352bdaeb0d
7 changed files with 102 additions and 34 deletions

View file

@ -78,6 +78,7 @@ detekt {
sonarqube { sonarqube {
properties { properties {
property("sonar.projectName", "semver-gradle")
property("sonar.projectKey", "ethauvin_semver-gradle") property("sonar.projectKey", "ethauvin_semver-gradle")
property("sonar.organization", "ethauvin-github") property("sonar.organization", "ethauvin-github")
property("sonar.host.url", "https://sonarcloud.io") property("sonar.host.url", "https://sonarcloud.io")

View file

@ -96,28 +96,29 @@ open class SemverConfig @Inject constructor(
val separator: String val separator: String
get() = semVersion.separator get() = semVersion.separator
override fun toString(): String { override fun toString(): String {
return "SemverConfig(" + return mapOf(
"properties='$properties', " + Pair("properties", properties),
"semverKey='$semverKey', " + Pair("semverKey", semverKey),
"majorKey='$majorKey', " + Pair("majorKey", majorKey),
"minorKey='$minorKey', " + Pair("minorKey", minorKey),
"patchKey='$patchKey', " + Pair("patchKey", patchKey),
"preReleaseKey='$preReleaseKey', " + Pair("preReleaseKey", preReleaseKey),
"preReleasePrefixKey='$preReleasePrefixKey', " + Pair("preReleasePrefixKey", preReleasePrefixKey),
"buildMetaKey='$buildMetaKey', " + Pair("buildMetaKey", buildMetaKey),
"buildMetaPrefixKey='$buildMetaPrefixKey', " + Pair("buildMetaPrefixKey", buildMetaPrefixKey),
"separator='$separatorKey', " + Pair("separator", separatorKey),
"keysPrefix='$keysPrefix', " + Pair("keysPrefix", keysPrefix),
"semver='$semver', " + Pair("semver", semver),
"major='$major', " + Pair("major", major),
"minor='$minor', " + Pair("minor", minor),
"patch='$patch', " + Pair("patch", patch),
"preRelease='$preRelease', " + Pair("preRelease", preRelease),
"buildMeta='$buildMeta', " + Pair("buildMeta", buildMeta),
"preReleasePrefix='$preReleasePrefix', " + Pair("preReleasePrefix", preReleasePrefix),
"buildMetaPrefix='$buildMetaPrefix', " + Pair("buildMetaPrefix", buildMetaPrefix),
"separator='$separator'" + Pair("separator", separator)
')' ).toString()
} }
} }

View file

@ -70,16 +70,5 @@ class Version {
if (isPatch) patch++ if (isPatch) patch++
} }
override fun toString(): String { override fun toString() = semver
return "Version(" +
"major='$major', " +
"minor='$minor', " +
"patch='$patch', " +
"preRelease='$preRelease', " +
"preReleasePrefix='$preReleasePrefix', " +
"buildMeta='$buildMeta', " +
"buildMetaPrefix='$buildMetaPrefix', " +
"separator='$separator', " +
')'
}
} }

View file

@ -69,6 +69,11 @@ class SemverConfigTest {
} }
assertEquals(config.properties, "version.properties", "config.properties should be version.properties") assertEquals(config.properties, "version.properties", "config.properties should be version.properties")
assertTrue(
config.toString().contains("properties=${SemverConfig.DEFAULT_PROPERTIES}"),
"toString contains default properties"
)
} }
@Test @Test

View file

@ -0,0 +1,58 @@
/*
* testSortedProperties.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import kotlin.test.Test
import kotlin.test.assertEquals
class SortedPropertiesTest {
@Test
fun testSortedProperties() {
val props = SortedProperties()
val fruits = setOf("Avocado", "Tomato", "apple", "banana", "cucumber", "zucchini")
fruits.reversed().forEach {
props[it] = "test"
}
val keys = props.keys().iterator()
fruits.forEach {
assertEquals(it, keys.next(), "$it key")
}
val entries = props.entries.iterator()
fruits.forEach {
assertEquals(it, entries.next().key, "$it entry")
}
}
}

View file

@ -134,6 +134,16 @@ class UtilsTest {
locked.delete() locked.delete()
} }
@Test
fun testLoadIntProperty() {
val props = SortedProperties()
props["foo"] = "bar"
assertFailsWith<GradleException> { Utils.loadIntProperty(props, "foo", 1) }
assertEquals(Utils.loadIntProperty(props, "none", 1), 1, "default int value")
}
@Test @Test
fun testPrefix() { fun testPrefix() {
version.preReleasePrefix = "." version.preReleasePrefix = "."
@ -198,6 +208,7 @@ class UtilsTest {
newPropsFile.delete() newPropsFile.delete()
System.getProperties().setProperty(config.semverKey, "3.2.2") System.getProperties().setProperty(config.semverKey, "3.2.2")
props["foo"] = "bar"
Utils.loadVersion(config, version, props) Utils.loadVersion(config, version, props)
assertEquals(version.semver, System.getProperty(config.semverKey), "versions should match") assertEquals(version.semver, System.getProperty(config.semverKey), "versions should match")
} }

View file

@ -49,6 +49,7 @@ class VersionTest {
assertEquals("+", version.buildMetaPrefix, "meta prefix should be +") assertEquals("+", version.buildMetaPrefix, "meta prefix should be +")
assertEquals(".", version.separator, "separator should be .") assertEquals(".", version.separator, "separator should be .")
assertEquals("1.0.0", version.semver, "version should be 1.0.0") assertEquals("1.0.0", version.semver, "version should be 1.0.0")
assertEquals(version.toString(), version.semver, "toString should be semver")
} }
@Test @Test
@ -102,5 +103,7 @@ class VersionTest {
version.separator = "-" version.separator = "-"
assertEquals("1-0-0--beta++007", version.semver, "should return 1-0-0--beta++007") assertEquals("1-0-0--beta++007", version.semver, "should return 1-0-0--beta++007")
assertEquals(version.toString(), version.semver, "toString() should return semver")
} }
} }