Skip to content

Commit

Permalink
fix VariantTest and Variant.toJson()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Yiu authored and Tim Yiu committed Sep 15, 2023
1 parent 7456554 commit e54024c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sdk/src/main/java/com/amplitude/experiment/util/Variant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal fun Variant.toJson(): String {
if (expKey != null) {
jsonObject.put("expKey", expKey)
}
if (metadata != null) {
jsonObject.put("metadata", metadata)
}
} catch (e: JSONException) {
Logger.w("Error converting Variant to json string", e)
}
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/test/java/com/amplitude/experiment/VariantTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ class VariantTest {
jsonObject.put("value", "value")
jsonObject.put("payload", "payload")
jsonObject.put("expKey", "expKey")
jsonObject.put("metadata", emptyMap<Any,Any>())
val variant = jsonObject.toVariant()
Assert.assertNotNull(variant)
Assert.assertEquals("key", variant!!.key)
Assert.assertEquals("value", variant.value)
Assert.assertEquals("payload", variant.payload)
Assert.assertEquals("expKey", variant.expKey)
Assert.assertEquals(emptyMap<Any,Any>(), variant.metadata)
}

@Test
fun `variant to json object`() {
run {
val variant = Variant("value", null, "expKey", "key")
val variant = Variant("value", null, "expKey", "key", emptyMap())
val jsonObject = JSONObject()
jsonObject.put("key", "key")
jsonObject.put("value", "value")
jsonObject.put("expKey", "expKey")
jsonObject.put("metadata", emptyMap<Any,Any>())
Assert.assertEquals(jsonObject.toString(), variant.toJson())
}
}
Expand Down

0 comments on commit e54024c

Please sign in to comment.