aboutsummaryrefslogtreecommitdiff
path: root/test
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 /test
parent7311f679f7b5def0b79a9c61ba506fa37dc14bb2 (diff)
downloadgleam_json-d91c49ac13e55b769d553a97b59727ebbc6ea071.tar.gz
gleam_json-d91c49ac13e55b769d553a97b59727ebbc6ea071.zip
add tests for runtime decode errors
Diffstat (limited to 'test')
-rw-r--r--test/gleam_json_js_ffi_test.gleam72
1 files changed, 72 insertions, 0 deletions
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"