Skip to content

Commit 52cdedb

Browse files
committed
test: Fix for JVM 8
1 parent 10b4fe1 commit 52cdedb

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import de.marcphilipp.gradle.nexus.NexusRepository
12
import org.jetbrains.dokka.gradle.DokkaTask
23
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3-
import de.marcphilipp.gradle.nexus.NexusRepository
44

55
plugins {
66
kotlin("jvm").version("1.3.61")

src/test/kotlin/BaseNotationTest.kt

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package de.joshuagleitze.stringnotation
33
import ch.tutteli.atrium.api.fluent.en_GB.feature
44
import ch.tutteli.atrium.api.fluent.en_GB.toBe
55
import ch.tutteli.atrium.api.verbs.expect
6+
import org.junit.jupiter.api.Assumptions.assumeTrue
67
import org.junit.jupiter.api.TestInstance
78
import org.junit.jupiter.params.ParameterizedTest
89
import org.junit.jupiter.params.provider.Arguments
@@ -11,29 +12,32 @@ import org.junit.jupiter.params.provider.MethodSource
1112
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1213
abstract class BaseNotationTest(
1314
private val notation: StringNotation,
14-
private val unchangedWords: List<Pair<String, Word>>,
15-
private val parseOnlyWords: List<Pair<String, Word>> = emptyList(),
16-
private val printOnlyWords: List<Pair<Word, String>> = emptyList()
15+
private val unchangedWords: List<NotationTestData>,
16+
private val parseOnlyWords: List<NotationTestData> = emptyList(),
17+
private val printOnlyWords: List<NotationTestData> = emptyList()
1718
) {
18-
@ParameterizedTest(name = "\"{0}\" -> {1}")
19+
@ParameterizedTest(name = "\"{1}\" -> {2}")
1920
@MethodSource("parseWords")
20-
fun `parses words correctly`(input: String, expectedWord: Word) {
21+
fun `parses words correctly`(minimumJavaVersion: Int, input: String, expectedWord: Word) {
22+
assumeTrue(currentJavaVersion >= minimumJavaVersion, "Requires at least Java $minimumJavaVersion")
2123
expect(input.fromNotation(notation)) {
2224
feature(Word::partsList).toBe(expectedWord.partsList)
2325
}
2426
}
2527

26-
@ParameterizedTest(name = "{1} -> \"{0}\"")
28+
@ParameterizedTest(name = "{2} -> \"{1}\"")
2729
@MethodSource("printWords")
28-
fun `prints words correctly`(sourceWord: Word, expectedResult: String) {
30+
fun `prints words correctly`(minimumJavaVersion: Int, expectedResult: String, sourceWord: Word) {
31+
assumeTrue(currentJavaVersion >= minimumJavaVersion, "Requires at least Java $minimumJavaVersion")
2932
expect(sourceWord) {
3033
feature(Word::toNotation, notation).toBe(expectedResult)
3134
}
3235
}
3336

34-
@ParameterizedTest(name = "\"{0}\"")
37+
@ParameterizedTest(name = "\"{1}\"")
3538
@MethodSource("unchangedWords")
36-
fun `parsing and printing a word written in this notation does not change the word`(word: String) {
39+
fun `parsing and printing a word written in this notation does not change the word`(minimumJavaVersion: Int, word: String) {
40+
assumeTrue(currentJavaVersion >= minimumJavaVersion, "Requires at least Java $minimumJavaVersion")
3741
expect(word) {
3842
feature(String::fromNotation, notation) {
3943
feature(Word::toNotation, notation).toBe(word)
@@ -42,15 +46,27 @@ abstract class BaseNotationTest(
4246
}
4347

4448
private fun parseWords() = asArguments(unchangedWords + parseOnlyWords)
45-
private fun printWords() = asArguments(unchangedWords.map { it.swap() } + printOnlyWords)
49+
private fun printWords() = asArguments(unchangedWords + printOnlyWords)
4650
private fun unchangedWords() = asArguments(unchangedWords)
4751

48-
private fun asArguments(pairs: List<Pair<*, *>>) = pairs.map {
52+
private fun asArguments(pairs: List<NotationTestData>) = pairs.map {
4953
Arguments.arguments(
50-
it.first,
51-
it.second
54+
it.minimumJavaVersion,
55+
it.string,
56+
it.word
5257
)
5358
}
59+
}
60+
61+
data class NotationTestData(val word: Word, val string: String, var minimumJavaVersion: Int = 0)
62+
63+
infix fun Word.to(string: String) = NotationTestData(this, string)
64+
infix fun String.to(word: Word) = NotationTestData(word, this)
65+
infix fun NotationTestData.ifJvmVersionIsAtLeast(minimumJavaVersion: Int) = this.apply { this.minimumJavaVersion = minimumJavaVersion }
5466

55-
private fun <First, Second> Pair<First, Second>.swap() = Pair(this.second, this.first)
67+
val currentJavaVersion by lazy {
68+
System.getProperty("java.runtime.version")
69+
.split(".")
70+
.let { if (it[0] == "1") it.drop(1) else it }[0]
71+
.toInt()
5672
}

src/test/kotlin/JavaNotationsTest.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class JavaTypeNameTest: BaseNotationTest(
99
Word("removes", "upperCase") to "RemovesUppercase",
1010
Word("") to "__",
1111
Word("1") to "__",
12-
Word("8if") to "if_",
13-
Word("_") to "__"
12+
Word("8if") to "If",
13+
Word("enum") to "Enum",
14+
Word("_") to "__" ifJvmVersionIsAtLeast 9
1415
)
1516
)
1617

@@ -27,7 +28,8 @@ class JavaMemberNameTest: BaseNotationTest(
2728
Word("") to "__",
2829
Word("1") to "__",
2930
Word("8if") to "if_",
30-
Word("_") to "__"
31+
Word("enum") to "enum_",
32+
Word("_") to "__" ifJvmVersionIsAtLeast 9
3133
)
3234
)
3335

@@ -46,7 +48,8 @@ class JavaPackagePartTest: BaseNotationTest(
4648
Word("") to "__",
4749
Word("1") to "__",
4850
Word("8if") to "if_",
49-
Word("_") to "__"
51+
Word("enum") to "enum_",
52+
Word("_") to "__" ifJvmVersionIsAtLeast 9
5053
)
5154
)
5255

@@ -58,8 +61,9 @@ class JavaPackageNameTest: BaseNotationTest(
5861
"if.true" to Word("if", "true")
5962
),
6063
printOnlyWords = listOf(
64+
Word("enum") to "enum_",
6165
Word("if", "", "cApitAls") to "if_.__.capitals",
62-
Word("_") to "__"
66+
Word("_") to "__" ifJvmVersionIsAtLeast 9
6367
)
6468

6569
)
@@ -76,6 +80,7 @@ class JavaConstantNameTest: BaseNotationTest(
7680
Word("") to "__",
7781
Word("1") to "__",
7882
Word("8if") to "IF",
79-
Word("_") to "__"
83+
Word("enum") to "enum_",
84+
Word("_") to "__" ifJvmVersionIsAtLeast 9
8085
)
8186
)

0 commit comments

Comments
 (0)