From 573e78dba5850bf1dc3972f54e6e25fd71f59559 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 15 Nov 2021 17:06:51 +0000 Subject: [PATCH] fix(tests): only remove unquoted whitespace from json strings during comparison --- .../java/com/bugsnag/android/JsonUtils.kt | 48 +++++++++++++++++-- .../resources/app_data_serialization_0.json | 2 +- .../resources/app_data_serialization_1.json | 2 +- .../resources/breadcrumb_serialization_0.json | 2 +- .../breadcrumb_state_serialization_0.json | 2 +- .../test/resources/event_serialization_6.json | 4 +- 6 files changed, 51 insertions(+), 9 deletions(-) diff --git a/bugsnag-android-core/src/test/java/com/bugsnag/android/JsonUtils.kt b/bugsnag-android-core/src/test/java/com/bugsnag/android/JsonUtils.kt index 5d474a02f9..273ab21f46 100644 --- a/bugsnag-android-core/src/test/java/com/bugsnag/android/JsonUtils.kt +++ b/bugsnag-android-core/src/test/java/com/bugsnag/android/JsonUtils.kt @@ -19,11 +19,53 @@ internal fun verifyJsonMatches(map: Map, 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) } diff --git a/bugsnag-android-core/src/test/resources/app_data_serialization_0.json b/bugsnag-android-core/src/test/resources/app_data_serialization_0.json index 7be79b91fd..b917ef7159 100644 --- a/bugsnag-android-core/src/test/resources/app_data_serialization_0.json +++ b/bugsnag-android-core/src/test/resources/app_data_serialization_0.json @@ -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 } \ No newline at end of file diff --git a/bugsnag-android-core/src/test/resources/app_data_serialization_1.json b/bugsnag-android-core/src/test/resources/app_data_serialization_1.json index 998b1d533d..59db4bc4a7 100644 --- a/bugsnag-android-core/src/test/resources/app_data_serialization_1.json +++ b/bugsnag-android-core/src/test/resources/app_data_serialization_1.json @@ -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, diff --git a/bugsnag-android-core/src/test/resources/breadcrumb_serialization_0.json b/bugsnag-android-core/src/test/resources/breadcrumb_serialization_0.json index 54e639e08f..847d4b5cb5 100644 --- a/bugsnag-android-core/src/test/resources/breadcrumb_serialization_0.json +++ b/bugsnag-android-core/src/test/resources/breadcrumb_serialization_0.json @@ -1,6 +1,6 @@ { "timestamp": "1970-01-01T00:00:00.000Z", - "name": "helloworld", + "name": "hello world", "type": "manual", "metaData": {} } \ No newline at end of file diff --git a/bugsnag-android-core/src/test/resources/breadcrumb_state_serialization_0.json b/bugsnag-android-core/src/test/resources/breadcrumb_state_serialization_0.json index 6151953caa..5d127c8fbc 100644 --- a/bugsnag-android-core/src/test/resources/breadcrumb_state_serialization_0.json +++ b/bugsnag-android-core/src/test/resources/breadcrumb_state_serialization_0.json @@ -1,7 +1,7 @@ [ { "timestamp": "1970-01-01T00:00:00.000Z", - "name": "helloworld", + "name": "hello world", "type": "manual", "metaData": { "direction": "left" diff --git a/bugsnag-android-core/src/test/resources/event_serialization_6.json b/bugsnag-android-core/src/test/resources/event_serialization_6.json index 60ce3135a9..9908d60f6b 100644 --- a/bugsnag-android-core/src/test/resources/event_serialization_6.json +++ b/bugsnag-android-core/src/test/resources/event_serialization_6.json @@ -4,7 +4,7 @@ "foo": 55 }, "wham": { - "some_key": "Avalue" + "some_key": "A value" }, "device": { "bar": true @@ -53,7 +53,7 @@ "breadcrumbs": [ { "timestamp": "1970-01-01T00:00:00.000Z", - "name": "helloworld", + "name": "hello world", "type": "manual", "metaData": {} }