Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Feb 22, 2024
1 parent a263b46 commit fa1b557
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/jvmTest/kotlin/io/github/detekt/sarif4k/PropertyBagTest.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
package io.github.detekt.sarif4k

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import kotlin.test.Test
import kotlin.test.assertEquals

class PropertyBagTest {

@ParameterizedTest
@MethodSource("data")
fun encode(propertyBag: PropertyBag, json: String) {
assertEquals(json, Json.encodeToString(propertyBag))
fun encode(value: Map<String, Any?>, json: String) {
assertEquals(json, Json.encodeToString(PropertyBag.serializer(), PropertyBag(value)))
}

@ParameterizedTest
@MethodSource("data")
fun decode(propertyBag: PropertyBag, json: String) {
assertEquals(propertyBag, Json.decodeFromString<PropertyBag>(json))
fun decode(value: Map<String, Any?>, json: String) {
assertEquals(PropertyBag(value), Json.decodeFromString(PropertyBag.serializer(), json))
}

@Test
fun noTags() {
assertEquals(null, PropertyBag(mapOf()).tags)
}

@Test
fun nullTags() {
assertEquals(null, PropertyBag(mapOf("tags" to null)).tags)
}

@Test
fun emptyTags() {
assertEquals(emptyList(), PropertyBag(mapOf("tags" to emptyList<Nothing>())).tags)
}

@Test
fun tags() {
assertEquals(listOf("foo", "bar"), PropertyBag(mapOf("tags" to listOf("foo", "bar"))).tags)
}

@Test
fun incorrectTags() {
assertThrows<ClassCastException> {
PropertyBag(mapOf("tags" to listOf(1))).tags
}
}

companion object {
@JvmStatic
fun data(): List<Arguments> {
return listOf(
PropertyBag("foo" to null) to """{"foo":null}""",
PropertyBag("tags" to null) to """{"tags":null}""",
mapOf<String, Any?>() to """{}""",
mapOf("foo" to null) to """{"foo":null}""",
mapOf("foo" to mapOf("bar" to listOf("1", 2L, false, 3.5))) to """{"foo":{"bar":["1",2,false,3.5]}}""",
mapOf("tags" to null) to """{"tags":null}""",
mapOf("tags" to emptyList<Nothing>()) to """{"tags":[]}""",
).map { (first, second) -> Arguments.of(first, second) }
}
}
}

private fun PropertyBag(vararg values: Pair<String, Any?>) = PropertyBag(mapOf(*values))

0 comments on commit fa1b557

Please sign in to comment.