diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/gleam_json_js_ffi_test.gleam | 123 | ||||
-rw-r--r-- | test/gleam_json_test.gleam | 14 |
2 files changed, 72 insertions, 65 deletions
diff --git a/test/gleam_json_js_ffi_test.gleam b/test/gleam_json_js_ffi_test.gleam index f746ce2..df1b763 100644 --- a/test/gleam_json_js_ffi_test.gleam +++ b/test/gleam_json_js_ffi_test.gleam @@ -1,67 +1,76 @@ -if javascript { - import gleam/json.{DecodeError, UnexpectedByte, UnexpectedEndOfInput} - import gleeunit/should +@target(javascript) +import gleam/json.{DecodeError, UnexpectedByte, UnexpectedEndOfInput} +@target(javascript) +import gleeunit/should - 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) - } +@target(javascript) +type StandardError { + StandardError(message: String) +} - 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) - } +// === End of input tests === // +@target(javascript) +pub fn chromium_end_of_input_test() { + "Unexpected end of JSON input" + |> 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) - } +@target(javascript) +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) +} - // === 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)) - } +@target(javascript) +pub fn javascript_core_end_of_input_test() { + "JSON Parse error: Unexpected EOF" + |> StandardError + |> get_json_decode_error("") + |> should.equal(UnexpectedEndOfInput) +} - 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)) - } +// === Unexpected byte tests === // +@target(javascript) +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 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)) - } +@target(javascript) +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 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)) +@target(javascript) +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)) +} - "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)) - } +@target(javascript) +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)) - external fn get_json_decode_error(StandardError, String) -> DecodeError = - "./gleam_json_ffi.mjs" "getJsonDecodeError" + "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)) } + +@target(javascript) +@external(javascript, "./gleam_json_ffi.mjs", "getJsonDecodeError") +fn get_json_decode_error(a: StandardError, b: String) -> DecodeError diff --git a/test/gleam_json_test.gleam b/test/gleam_json_test.gleam index 69c5550..f27edce 100644 --- a/test/gleam_json_test.gleam +++ b/test/gleam_json_test.gleam @@ -130,14 +130,12 @@ fn should_encode(data: Json, expected: String) { |> should.equal(json.to_string(data)) } -if erlang { - fn empty_list_decode_error() -> dynamic.DecodeError { - dynamic.DecodeError(expected: "Int", found: "List", path: []) - } +@target(erlang) +fn empty_list_decode_error() -> dynamic.DecodeError { + dynamic.DecodeError(expected: "Int", found: "List", path: []) } -if javascript { - fn empty_list_decode_error() { - dynamic.DecodeError(expected: "Int", found: "Tuple of 0 elements", path: []) - } +@target(javascript) +fn empty_list_decode_error() { + dynamic.DecodeError(expected: "Int", found: "Tuple of 0 elements", path: []) } |