aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrkutly <mark.sauer.utley@gmail.com>2022-06-09 21:45:48 -0400
committerLouis Pilfold <louis@lpil.uk>2022-06-11 18:39:10 +0100
commitd91c49ac13e55b769d553a97b59727ebbc6ea071 (patch)
treea103ba30e13fc259c52f17ed586d6819f83c79dc
parent7311f679f7b5def0b79a9c61ba506fa37dc14bb2 (diff)
downloadgleam_json-d91c49ac13e55b769d553a97b59727ebbc6ea071.tar.gz
gleam_json-d91c49ac13e55b769d553a97b59727ebbc6ea071.zip
add tests for runtime decode errors
-rw-r--r--src/gleam_json_ffi.mjs8
-rw-r--r--test/gleam_json_js_ffi_test.gleam72
2 files changed, 76 insertions, 4 deletions
diff --git a/src/gleam_json_ffi.mjs b/src/gleam_json_ffi.mjs
index 115c503..b60d848 100644
--- a/src/gleam_json_ffi.mjs
+++ b/src/gleam_json_ffi.mjs
@@ -36,7 +36,7 @@ export function decode(string) {
}
}
-function getJsonDecodeError(stdErr, json) {
+export function getJsonDecodeError(stdErr, json) {
if (isUnexpectedEndOfInput(stdErr)) return new UnexpectedEndOfInput()
const unexpectedByteRuntime = getUnexpectedByteRuntime(stdErr)
if (unexpectedByteRuntime) return toUnexpectedByteError(unexpectedByteRuntime, stdErr, json)
@@ -88,7 +88,7 @@ const chromiumUnexpectedByteRegex = /unexpected token (.) in JSON at position (\
*
* JavascriptCore only reports what the character is and not its position.
*/
-const jsCoreUnexpectedByteRegex = /unexpected identifier "(.)"/i
+const jsCoreUnexpectedByteRegex = /unexpected (identifier|token) "(.)"/i
/**
* Matches unexpected byte messages in:
@@ -96,7 +96,7 @@ const jsCoreUnexpectedByteRegex = /unexpected identifier "(.)"/i
*
* Matches the position in a 2d grid only and not the character.
*/
-const spidermonkeyUnexpectedByteRegex = /(unexpected character|expected double-quoted property name) at line (\d+) column (\d+)/i
+const spidermonkeyUnexpectedByteRegex = /(unexpected character|expected .*) at line (\d+) column (\d+)/i
function getUnexpectedByteRuntime(err) {
if (chromiumUnexpectedByteRegex.test(err.message)) return Runtime.chromium
@@ -151,7 +151,7 @@ function toSpidermonkeyUnexpectedByteError(err, json) {
function toJsCoreUnexpectedByteError(err) {
const match = jsCoreUnexpectedByteRegex.exec(err.message)
- const byte = toHex(match[1])
+ const byte = toHex(match[2])
return new UnexpectedByte(byte, 0)
}
diff --git a/test/gleam_json_js_ffi_test.gleam b/test/gleam_json_js_ffi_test.gleam
new file mode 100644
index 0000000..74d1867
--- /dev/null
+++ b/test/gleam_json_js_ffi_test.gleam
@@ -0,0 +1,72 @@
+import gleeunit
+import gleam/json.{DecodeError, UnexpectedByte, UnexpectedEndOfInput}
+import gleeunit/should
+
+if javascript {
+ pub fn main() {
+ gleeunit.main()
+ }
+}
+
+type StandardError {
+ StandardError(message: String)
+}
+
+// === End of input tests === //
+pub fn chromium_end_of_input_test() {
+ "Unexpected end of JSON input"
+ |> StandardError
+ |> get_json_decode_error("")
+ |> should.equal(UnexpectedEndOfInput)
+}
+
+pub fn spidermonkey_end_of_input_test() {
+ "JSON.parse: unexpected end of data at line 1 column 1 of the JSON data"
+ |> StandardError
+ |> get_json_decode_error("")
+ |> should.equal(UnexpectedEndOfInput)
+}
+
+pub fn javascript_core_end_of_input_test() {
+ "JSON Parse error: Unexpected EOF"
+ |> StandardError
+ |> get_json_decode_error("")
+ |> should.equal(UnexpectedEndOfInput)
+}
+
+// === Unexpected byte tests === //
+pub fn chromium_unexpected_byte_test() {
+ "Unexpected token a in JSON at position 5"
+ |> StandardError
+ |> get_json_decode_error("{\"b\":a}")
+ |> should.equal(UnexpectedByte(byte: "0x61", position: 5))
+}
+
+pub fn spidermonkey_unexpected_byte_test() {
+ "JSON.parse: expected property name or '}' at line 1 column 6 of the JSON data"
+ |> StandardError
+ |> get_json_decode_error("{\"b\":a}")
+ |> should.equal(UnexpectedByte(byte: "0x61", position: 5))
+}
+
+pub fn javascript_core_unexpected_byte_test() {
+ "JSON Parse error: Unexpected identifier \"a\""
+ |> StandardError
+ |> get_json_decode_error("{\"b\":a}")
+ |> should.equal(UnexpectedByte(byte: "0x61", position: 0))
+}
+
+pub fn spidermonkey_multiline_unexpected_byte_test() {
+ "JSON.parse: expected property name or '}' at line 2 column 6 of the JSON data"
+ |> StandardError
+ |> get_json_decode_error("{\n\"b\": a\n}")
+ |> should.equal(UnexpectedByte(byte: "0x61", position: 7))
+
+ "JSON.parse: expected double-quoted property name at line 3 column 1 of the JSON data"
+ |> StandardError
+ |> get_json_decode_error("{\n\"b\": \"x\",\na\n}")
+ |> should.equal(UnexpectedByte(byte: "0x61", position: 12))
+}
+
+external fn get_json_decode_error(StandardError, String) -> DecodeError =
+ "./gleam_json_ffi.mjs" "getJsonDecodeError"