Skip to content

Instantly share code, notes, and snippets.

@tateisu
Last active January 14, 2022 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/e1297aa9ae022a91f7cded21a01e674a to your computer and use it in GitHub Desktop.
Save tateisu/e1297aa9ae022a91f7cded21a01e674a to your computer and use it in GitHub Desktop.
package jp.juggler.myapplication
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Test
class JsonInstrumentedTest {
@Test
fun test1() {
val expected = """[{"mute":"on"}]"""
// A
run {
assertEquals(expected, JSONArray(expected).toString())
}
// B
run {
// .apply{} で組み立てる
val json = JSONArray().apply {
put(JSONObject().apply {
put("mute", "on")
})
}
assertEquals(expected, json.toString())
}
// C
run {
// 気の利いたコンストラクタがなければ作る
fun jsonObject(vararg pairs: Pair<String, Any?>) =
JSONObject().apply {
pairs.forEach { putOpt(it.first, it.second) }
}
fun jsonArray(vararg elements: Any?) =
JSONArray().apply {
elements.forEach { put(it) }
}
val json = jsonArray(jsonObject("mute" to "on"))
assertEquals(expected, json.toString())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment