Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only remove unquoted whitespace from JSON strings for comparison #1512

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,53 @@ internal fun verifyJsonMatches(map: Map<String, Any>, resourceName: String) {
validateJson(resourceName, json)
}

/**
* To help comparing JSON we remove any whitespace that hasn't been quoted. So:
* ```
* {
* "some key": "Some Value"
* }
* ```
*
* Becomes:
* ```
* {"some key":"Some Value"}
* ```
*/
private fun removeUnquotedWhitespace(json: String): String {
val builder = StringBuilder(json.length)
var quoted = false
var index = 0

while (index < json.length) {
val ch = json[index++]

if (quoted) {
when (ch) {
'\"' -> quoted = false
'\\' -> {
builder.append('\\')
builder.append(json[index++])
}
}

builder.append(ch)
} else if (!ch.isWhitespace()) {
builder.append(ch)

if (ch == '\"') {
quoted = true
}
}
}

return builder.toString()
}

internal fun validateJson(resourceName: String, json: String) {
val whitespace = "\\s".toRegex()
val rawJson = JsonParser().read(resourceName)
val expectedJson = rawJson.replace(whitespace, "")
val generatedJson = json.replace(whitespace, "")
val expectedJson = removeUnquotedWhitespace(rawJson)
val generatedJson = removeUnquotedWhitespace(json)
Assert.assertEquals(expectedJson, generatedJson)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"codeBundleId": "foo-99",
"id": "com.example.foo",
"releaseStage": "test-stage",
"type": "ReactNative",
"type": "React Native",
"version": "1.2.3",
"versionCode": 55
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"codeBundleId": "foo-99",
"id": "com.example.foo",
"releaseStage": "test-stage",
"type": "ReactNative",
"type": "React Native",
"version": "1.2.3",
"versionCode": 55,
"duration": 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "helloworld",
"name": "hello world",
"type": "manual",
"metaData": {}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "helloworld",
"name": "hello world",
"type": "manual",
"metaData": {
"direction": "left"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"foo": 55
},
"wham": {
"some_key": "Avalue"
"some_key": "A value"
},
"device": {
"bar": true
Expand Down Expand Up @@ -53,7 +53,7 @@
"breadcrumbs": [
{
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "helloworld",
"name": "hello world",
"type": "manual",
"metaData": {}
}
Expand Down