aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/gleam/dynamic/decode_test.gleam26
1 files changed, 26 insertions, 0 deletions
diff --git a/test/gleam/dynamic/decode_test.gleam b/test/gleam/dynamic/decode_test.gleam
index 582f2af..b176c70 100644
--- a/test/gleam/dynamic/decode_test.gleam
+++ b/test/gleam/dynamic/decode_test.gleam
@@ -927,3 +927,29 @@ pub fn js_map_test() {
|> should.be_ok
|> should.equal(dict.from_list([#("a", 10), #("b", 20), #("c", 30)]))
}
+
+type Nested {
+ Nested(List(Nested))
+ Value(String)
+}
+
+fn recursive_decoder() -> decode.Decoder(Nested) {
+ use <- decode.recursive()
+ decode.one_of(decode.string |> decode.map(Value), [
+ decode.list(recursive_decoder()) |> decode.map(Nested),
+ ])
+}
+
+pub fn recursive_test() {
+ let nested = [["one", "two"], ["three"], []]
+ let expected =
+ Nested([
+ Nested([Value("one"), Value("two")]),
+ Nested([Value("three")]),
+ Nested([]),
+ ])
+
+ decode.run(dynamic.from(nested), recursive_decoder())
+ |> should.be_ok
+ |> should.equal(expected)
+}