Last active
January 14, 2022 10:10
-
-
Save tateisu/e1297aa9ae022a91f7cded21a01e674a to your computer and use it in GitHub Desktop.
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
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