-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a263b46
commit fa1b557
Showing
1 changed file
with
38 additions
and
10 deletions.
There are no files selected for viewing
48 changes: 38 additions & 10 deletions
48
src/jvmTest/kotlin/io/github/detekt/sarif4k/PropertyBagTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |