Skip to content

Commit 3c9a950

Browse files
david-allisonmikehardy
authored andcommitted
test: fix isJsonEqual
fixes: `1` and `1L` in the JSONObject are output to the same string improves: * add JSON syntax highlighting * fix call: no need to call JSONObject( ) at the call site
1 parent e4f60ca commit 3c9a950

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

AnkiDroid/src/test/java/com/ichi2/anki/dialogs/CustomStudyDialogTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import org.hamcrest.CoreMatchers.equalTo
4848
import org.hamcrest.CoreMatchers.not
4949
import org.hamcrest.MatcherAssert.assertThat
5050
import org.intellij.lang.annotations.Language
51-
import org.json.JSONObject
5251
import org.junit.Ignore
5352
import org.junit.Test
5453
import org.junit.runner.RunWith
@@ -104,7 +103,7 @@ class CustomStudyDialogTest : RobolectricTest() {
104103
"usn": -1
105104
}
106105
""".trimIndent()
107-
assertThat(customStudy, isJsonEqual(JSONObject(expected)))
106+
assertThat(customStudy, isJsonEqual(expected))
108107
}
109108

110109
@Test

AnkiDroid/src/test/java/com/ichi2/testutils/JsonUtils.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ package com.ichi2.testutils
1919
import com.ichi2.anki.common.json.JSONObjectHolder
2020
import org.hamcrest.BaseMatcher
2121
import org.hamcrest.Description
22+
import org.intellij.lang.annotations.Language
2223
import org.json.JSONObject
2324

2425
fun isJsonEqual(value: JSONObject) = IsJsonEqual(value)
2526

26-
fun isJsonEqual(value: String) = IsJsonEqual(JSONObject(value))
27+
fun isJsonEqual(
28+
@Language("JSON") value: String,
29+
) = IsJsonEqual(JSONObject(value))
2730

2831
private fun matchesJsonValue(
2932
expectedValue: JSONObject,
@@ -35,7 +38,7 @@ private fun matchesJsonValue(
3538
}
3639
// And that each key have the same associated values in both object.
3740
for (key in expectedValue.keys()) {
38-
if (expectedValue[key] != actualValue[key]) {
41+
if (!areJsonEquivalent(expectedValue[key], actualValue[key])) {
3942
return false
4043
}
4144
}
@@ -78,3 +81,24 @@ private fun jsonObjectOf(vararg pairs: Pair<String, Any>): JSONObject =
7881
put(key, value)
7982
}
8083
}
84+
85+
/**
86+
* Returns whether [a] and [b] produce the same JSON output as a string
87+
*
88+
* [JSONObject] handles Int and Long differently, but they are equivalent in the JSON output
89+
*/
90+
private fun areJsonEquivalent(
91+
a: Any,
92+
b: Any,
93+
): Boolean {
94+
if (a == b) return true
95+
96+
// In the string output, 1L and 1 are the same
97+
fun isIntOrLong(n: Any) = n is Int || n is Long
98+
if (isIntOrLong(a) && isIntOrLong(b)) {
99+
return (a as Number).toLong() == (b as Number).toLong()
100+
}
101+
102+
// Double & Long are not equivalent: '1.0' and '1' are different textual outputs
103+
return false
104+
}

0 commit comments

Comments
 (0)