aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-05-23 19:02:55 +0100
committerLouis Pilfold <louis@lpil.uk>2024-05-23 19:32:48 +0100
commit1979919b792910e6c75f3e28596f017d980b45ee (patch)
treee59fae108e195b6edc22bfc13181b2e2dcd7cd59 /test
parentefe70ef04ae57bca78344cd13ccf4e40eac3ed21 (diff)
downloadgleam_json-1979919b792910e6c75f3e28596f017d980b45ee.tar.gz
gleam_json-1979919b792910e6c75f3e28596f017d980b45ee.zip
Decoding
Diffstat (limited to 'test')
-rw-r--r--test/gleam_json_js_ffi_test.gleam10
-rw-r--r--test/gleam_json_test.gleam21
2 files changed, 18 insertions, 13 deletions
diff --git a/test/gleam_json_js_ffi_test.gleam b/test/gleam_json_js_ffi_test.gleam
index cecfe20..b1ddb3b 100644
--- a/test/gleam_json_js_ffi_test.gleam
+++ b/test/gleam_json_js_ffi_test.gleam
@@ -39,7 +39,7 @@ 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))
+ |> should.equal(UnexpectedByte("0x61"))
}
@target(javascript)
@@ -47,7 +47,7 @@ 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))
+ |> should.equal(UnexpectedByte("0x61"))
}
@target(javascript)
@@ -55,7 +55,7 @@ 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))
+ |> should.equal(UnexpectedByte("0x61"))
}
@target(javascript)
@@ -63,12 +63,12 @@ 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))
+ |> should.equal(UnexpectedByte("0x61"))
"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))
+ |> should.equal(UnexpectedByte("0x61"))
}
@target(javascript)
diff --git a/test/gleam_json_test.gleam b/test/gleam_json_test.gleam
index 3e22f00..fcc4a62 100644
--- a/test/gleam_json_test.gleam
+++ b/test/gleam_json_test.gleam
@@ -1,6 +1,7 @@
import gleam/dynamic
import gleam/json.{type Json}
import gleam/option.{None, Some}
+import gleam/string
import gleam/string_builder
import gleeunit
import gleeunit/should
@@ -21,11 +22,8 @@ pub fn decode_empty_test() {
pub fn decode_unexpected_byte_test() {
let assert Error(error) = json.decode(from: "[}", using: dynamic.int)
- let assert json.UnexpectedByte(byte, index) = error
+ let assert json.UnexpectedByte(byte) = error
let assert "0x7D" = byte
-
- // V8 does not report the position of the unexpected byte any more.
- let assert True = index == 1 || index == -1
}
pub fn decode_unexpected_format_test() {
@@ -45,11 +43,8 @@ pub fn decode_bits_empty_test() {
pub fn decode_bits_unexpected_byte_test() {
let assert Error(error) = json.decode(from: "[}", using: dynamic.int)
- let assert json.UnexpectedByte(byte, index) = error
+ let assert json.UnexpectedByte(byte) = error
let assert "0x7D" = byte
-
- // V8 does not report the position of the unexpected byte any more.
- let assert True = index == 1 || index == -1
}
pub fn decode_bits_unexpected_format_test() {
@@ -57,6 +52,16 @@ pub fn decode_bits_unexpected_format_test() {
|> should.equal(Error(json.UnexpectedFormat([empty_list_decode_error()])))
}
+pub fn decode_unexpected_sequence_test() {
+ let assert Error(error) =
+ json.decode(from: "\"\\uxxxx\"", using: dynamic.float)
+ case error {
+ json.UnexpectedSequence("\\uxxxx") -> Nil
+ json.UnexpectedByte("0x78") -> Nil
+ _ -> panic as { "unexpected error: " <> string.inspect(error) }
+ }
+}
+
pub fn encode_string_test() {
json.string("hello")
|> should_encode("\"hello\"")