aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-12-10 13:45:08 +0000
committerLouis Pilfold <louis@lpil.uk>2024-12-22 10:56:21 +0000
commitd452f95871e847b656b48b654c6d3e499f8452b3 (patch)
tree8b86e5aa031e569f797822692afb6ecb456426b6 /test
parentf5585adfb4a969d9167817041af0e8c4334e84ec (diff)
downloadgleam_stdlib-d452f95871e847b656b48b654c6d3e499f8452b3.tar.gz
gleam_stdlib-d452f95871e847b656b48b654c6d3e499f8452b3.zip
Decode JS objects, maps, weakmaps
Diffstat (limited to 'test')
-rw-r--r--test/gleam/dynamic/decode_test.gleam26
-rwxr-xr-xtest/gleam_stdlib_test_ffi.mjs15
2 files changed, 40 insertions, 1 deletions
diff --git a/test/gleam/dynamic/decode_test.gleam b/test/gleam/dynamic/decode_test.gleam
index cff3d46..582f2af 100644
--- a/test/gleam/dynamic/decode_test.gleam
+++ b/test/gleam/dynamic/decode_test.gleam
@@ -1,5 +1,5 @@
import gleam/dict
-import gleam/dynamic.{DecodeError}
+import gleam/dynamic.{type Dynamic, DecodeError}
import gleam/dynamic/decode
import gleam/float
import gleam/int
@@ -903,3 +903,27 @@ pub fn optionally_at_no_path_error_test() {
|> should.be_ok
|> should.equal(100)
}
+
+@external(erlang, "maps", "from_list")
+@external(javascript, "../../gleam_stdlib_test_ffi.mjs", "object")
+fn make_object(items: List(#(String, t))) -> Dynamic
+
+@external(erlang, "maps", "from_list")
+@external(javascript, "../../gleam_stdlib_test_ffi.mjs", "map")
+fn make_map(items: List(#(String, t))) -> Dynamic
+
+pub fn js_object_test() {
+ [#("a", 10), #("b", 20), #("c", 30)]
+ |> make_object
+ |> decode.run(decode.dict(decode.string, decode.int))
+ |> should.be_ok
+ |> should.equal(dict.from_list([#("a", 10), #("b", 20), #("c", 30)]))
+}
+
+pub fn js_map_test() {
+ [#("a", 10), #("b", 20), #("c", 30)]
+ |> make_map
+ |> decode.run(decode.dict(decode.string, decode.int))
+ |> should.be_ok
+ |> should.equal(dict.from_list([#("a", 10), #("b", 20), #("c", 30)]))
+}
diff --git a/test/gleam_stdlib_test_ffi.mjs b/test/gleam_stdlib_test_ffi.mjs
index c859093..986258d 100755
--- a/test/gleam_stdlib_test_ffi.mjs
+++ b/test/gleam_stdlib_test_ffi.mjs
@@ -10,3 +10,18 @@ export function uint8array(list) {
export function get_null() {
return null;
}
+
+export function object(items) {
+ const object = {};
+ for (const [k, v] of items) {
+ object[k] = v;
+ }
+ return object;
+}
+export function map(items) {
+ const object = new Map();
+ for (const [k, v] of items) {
+ object.set(k, v);
+ }
+ return object;
+}