Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtfeldman committed May 4, 2024
1 parent 5ad2a0e commit 764f70f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 45 deletions.
4 changes: 2 additions & 2 deletions examples/simple1.roc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ app [main] {

import cli.Task
import cli.Stdout
import json.Core
import json.Json

main =
requestBody = Str.toUtf8 "{\"Image\":{\"Animated\":false,\"Height\":600,\"Ids\":[116,943,234,38793],\"Thumbnail\":{\"Height\":125,\"Url\":\"http:\\/\\/www.example.com\\/image\\/481989943\",\"Width\":100},\"Title\":\"View from 15th Floor\",\"Width\":800}}"

decoder = Core.jsonWithOptions { fieldNameMapping: PascalCase }
decoder = Json.utf8With { fieldNameMapping: PascalCase }

decoded : Decode.DecodeResult ImageRequest
decoded = Decode.fromBytesPartial requestBody decoder
Expand Down
4 changes: 2 additions & 2 deletions examples/simple2.roc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ app [main] {

import cli.Stdout
import cli.Task
import json.Core
import json.Json
import "data.json" as requestBody : List U8

main =
decoder = Core.jsonWithOptions {}
decoder = Json.utf8With {}

decoded : Decode.DecodeResult (List DataRequest)
decoded = Decode.fromBytesPartial requestBody decoder
Expand Down
4 changes: 2 additions & 2 deletions examples/tuple.roc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ app [main] {

import cli.Task
import cli.Stdout
import json.Core
import json.Json

main =
bytes = Str.toUtf8 "[ [ 123,\n\"apples\" ], [ 456, \"oranges\" ]]"

decoded : Decode.DecodeResult (List FruitCount)
decoded = Decode.fromBytesPartial bytes Core.json
decoded = Decode.fromBytesPartial bytes Json.utf8

when decoded.result is
Ok tuple -> Stdout.line! "Successfully decoded tuple, got $(toStr tuple)"
Expand Down
32 changes: 16 additions & 16 deletions package/Json.roc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
module [
Json,
utf8,
jsonWithOptions,
utf8With,
encodeAsNullOption,
]

Expand Down Expand Up @@ -99,8 +99,8 @@ utf8 = @Json { fieldNameMapping: Default, skipMissingProperties: Bool.true, null
## json. If `False` when an encoder returns `[]` the record field, or list/tuple element, will be ommitted.
## eg: `{email:@Option None, name:"bob"}` encodes to `{"email":null, "name":"bob"}` instead of `{"name":"bob"}` (Default: `True`)

jsonWithOptions : { fieldNameMapping ? FieldNameMapping, skipMissingProperties ? Bool, nullDecodeAsEmpty ? Bool, emptyEncodeAsNull ? EncodeAsNull } -> Json
jsonWithOptions = \{ fieldNameMapping ? Default, skipMissingProperties ? Bool.true, nullDecodeAsEmpty ? Bool.true, emptyEncodeAsNull ? defaultEncodeAsNull } ->
utf8With : { fieldNameMapping ? FieldNameMapping, skipMissingProperties ? Bool, nullDecodeAsEmpty ? Bool, emptyEncodeAsNull ? EncodeAsNull } -> Json
utf8With = \{ fieldNameMapping ? Default, skipMissingProperties ? Bool.true, nullDecodeAsEmpty ? Bool.true, emptyEncodeAsNull ? defaultEncodeAsNull } ->
@Json { fieldNameMapping, skipMissingProperties, nullDecodeAsEmpty, emptyEncodeAsNull }

EncodeAsNull : {
Expand Down Expand Up @@ -378,7 +378,7 @@ encodeRecord = \fields ->
# Test encode for a record with two strings ignoring whitespace
expect
input = { fruitCount: 2, ownerName: "Farmer Joe" }
encoder = jsonWithOptions { fieldNameMapping: PascalCase }
encoder = utf8With { fieldNameMapping: PascalCase }
actual = Encode.toBytes input encoder
expected = Str.toUtf8 "{\"FruitCount\":2,\"OwnerName\":\"Farmer Joe\"}"

Expand All @@ -387,7 +387,7 @@ expect
# Test encode of record with an array of strings and a boolean field
expect
input = { fruitFlavours: ["Apples", "Bananas", "Pears"], isFresh: Bool.true }
encoder = jsonWithOptions { fieldNameMapping: KebabCase }
encoder = utf8With { fieldNameMapping: KebabCase }
actual = Encode.toBytes input encoder
expected = Str.toUtf8 "{\"fruit-flavours\":[\"Apples\",\"Bananas\",\"Pears\"],\"is-fresh\":true}"

Expand All @@ -396,7 +396,7 @@ expect
# Test encode of record with a string and number field
expect
input = { firstSegment: "ab", secondSegment: 10u8 }
encoder = jsonWithOptions { fieldNameMapping: SnakeCase }
encoder = utf8With { fieldNameMapping: SnakeCase }
actual = Encode.toBytes input encoder
expected = Str.toUtf8 "{\"first_segment\":\"ab\",\"second_segment\":10}"

Expand All @@ -405,7 +405,7 @@ expect
# Test encode of record of a record
expect
input = { outer: { inner: "a" }, other: { one: "b", two: 10u8 } }
encoder = jsonWithOptions { fieldNameMapping: Custom toYellingCase }
encoder = utf8With { fieldNameMapping: Custom toYellingCase }
actual = Encode.toBytes input encoder
expected = Str.toUtf8 "{\"OTHER\":{\"ONE\":\"b\",\"TWO\":10},\"OUTER\":{\"INNER\":\"a\"}}"

Expand Down Expand Up @@ -482,7 +482,7 @@ encodeTag = \name, payload ->
# Test encode of tag
expect
input = TheAnswer "is" 42
encoder = jsonWithOptions { fieldNameMapping: KebabCase }
encoder = utf8With { fieldNameMapping: KebabCase }
actual = Encode.toBytes input encoder
expected = Str.toUtf8 "{\"TheAnswer\":[\"is\",42]}"

Expand Down Expand Up @@ -1390,7 +1390,7 @@ expect
expect
input = Str.toUtf8 "[{\"field_name\":1}]"

decoder = jsonWithOptions { fieldNameMapping: SnakeCase }
decoder = utf8With { fieldNameMapping: SnakeCase }

actual : DecodeResult (List { fieldName : U64 })
actual = Decode.fromBytesPartial input decoder
Expand All @@ -1403,7 +1403,7 @@ expect
expect
input = Str.toUtf8 "[{\"extraField\":2,\"fieldName\":1}]"

decoder = jsonWithOptions { skipMissingProperties: Bool.false }
decoder = utf8With { skipMissingProperties: Bool.false }

actual : DecodeResult (List { fieldName : U64 })
actual = Decode.fromBytesPartial input decoder
Expand Down Expand Up @@ -1765,7 +1765,7 @@ ObjectState : [
# Test decode of record with two strings ignoring whitespace
expect
input = Str.toUtf8 " {\n\"FruitCount\"\t:2\n, \"OwnerName\": \"Farmer Joe\" } "
decoder = jsonWithOptions { fieldNameMapping: PascalCase }
decoder = utf8With { fieldNameMapping: PascalCase }
actual = Decode.fromBytesPartial input decoder
expected = Ok { fruitCount: 2, ownerName: "Farmer Joe" }

Expand All @@ -1774,7 +1774,7 @@ expect
# Test decode of record with an array of strings and a boolean field
expect
input = Str.toUtf8 "{\"fruit-flavours\": [\"Apples\",\"Bananas\",\"Pears\"], \"is-fresh\": true }"
decoder = jsonWithOptions { fieldNameMapping: KebabCase }
decoder = utf8With { fieldNameMapping: KebabCase }
actual = Decode.fromBytesPartial input decoder
expected = Ok { fruitFlavours: ["Apples", "Bananas", "Pears"], isFresh: Bool.true }

Expand All @@ -1783,7 +1783,7 @@ expect
# Test decode of record with a string and number field
expect
input = Str.toUtf8 "{\"first_segment\":\"ab\",\"second_segment\":10}"
decoder = jsonWithOptions { fieldNameMapping: SnakeCase }
decoder = utf8With { fieldNameMapping: SnakeCase }
actual = Decode.fromBytesPartial input decoder
expected = Ok { firstSegment: "ab", secondSegment: 10u8 }

Expand All @@ -1792,7 +1792,7 @@ expect
# Test decode of record of a record
expect
input = Str.toUtf8 "{\"OUTER\":{\"INNER\":\"a\"},\"OTHER\":{\"ONE\":\"b\",\"TWO\":10}}"
decoder = jsonWithOptions { fieldNameMapping: Custom fromYellingCase }
decoder = utf8With { fieldNameMapping: Custom fromYellingCase }
actual = Decode.fromBytesPartial input decoder
expected = Ok { outer: { inner: "a" }, other: { one: "b", two: 10u8 } }

Expand Down Expand Up @@ -1826,7 +1826,7 @@ complexExampleRecord = {
# Test decode of Complex Example
expect
input = complexExampleJson
decoder = jsonWithOptions { fieldNameMapping: PascalCase }
decoder = utf8With { fieldNameMapping: PascalCase }
actual = Decode.fromBytes input decoder
expected = Ok complexExampleRecord

Expand All @@ -1835,7 +1835,7 @@ expect
# Test encode of Complex Example
expect
input = complexExampleRecord
encoder = jsonWithOptions { fieldNameMapping: PascalCase }
encoder = utf8With { fieldNameMapping: PascalCase }
actual = Encode.toBytes input encoder
expected = complexExampleJson

Expand Down
11 changes: 5 additions & 6 deletions package/Option.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## If you need to distinguish between a missing field and a `null` field you should use `OptionOrNull`
module [none, some, get, getResult, from, fromResult]

import Core
import Json

Option val := [Some val, None]
implements [
Expand Down Expand Up @@ -55,7 +55,7 @@ expect
"""
|> json
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8

expected = Ok ({ name: "hi", lastName: none {}, age: some 1u8 })
expected == decoded
Expand All @@ -68,7 +68,7 @@ expect
"""
|> json
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8

expected = Ok ({ name: "hi", lastName: none {}, age: some 1u8 })
expected == decoded
Expand All @@ -80,7 +80,7 @@ expect
# { name: "hi", lastName: none {}, age: some 1u8 }
# encoded =
# toEncode
# |> Encode.toBytes Core.json
# |> Encode.toBytes Json.utf8
# |> Str.fromUtf8

# expected =
Expand All @@ -97,7 +97,7 @@ expect
# { name: "hi", lastName: none {}, age: some 1u8 }
# encoded =
# toEncode
# |> Encode.toBytes (Core.jsonWithOptions { emptyEncodeAsNull: Core.encodeAsNullOption { record: Bool.false } })
# |> Encode.toBytes (Json.utf8With { emptyEncodeAsNull: Json.encodeAsNullOption { record: Bool.false } })
# |> Str.fromUtf8

# expected =
Expand All @@ -107,4 +107,3 @@ expect
# |> json
# |> Ok
# expected == encoded

7 changes: 3 additions & 4 deletions package/OptionOrNull.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## eg: `core.jsonwithoptions { emptyencodeasnull: bool.false, nullasundefined: bool.false }`
module [none, null, some, get, getResult, from]

import Core
import Json

OptionOrNull val := [Some val, None, Null]
implements [
Expand Down Expand Up @@ -68,7 +68,7 @@ expect
"""
|> json
|> Str.toUtf8
|> Decode.fromBytes (Core.jsonWithOptions { emptyEncodeAsNull: Core.encodeAsNullOption { record: Bool.false }, nullDecodeAsEmpty: Bool.false })
|> Decode.fromBytes (Json.utf8With { emptyEncodeAsNull: Json.encodeAsNullOption { record: Bool.false }, nullDecodeAsEmpty: Bool.false })

expected = Ok ({ age: 1u8, name: null {}, lastName: none {} })
expected == decoded
Expand All @@ -79,7 +79,7 @@ expect
dat : OptionTest
dat = { lastName: none {}, name: null {}, age: 1 }
dat
|> Encode.toBytes (Core.jsonWithOptions { emptyEncodeAsNull: Core.encodeAsNullOption { record: Bool.false } })
|> Encode.toBytes (Json.utf8With { emptyEncodeAsNull: Json.encodeAsNullOption { record: Bool.false } })
|> Str.fromUtf8

expected =
Expand All @@ -89,4 +89,3 @@ expect
|> json
|> Ok
expected == encoded

26 changes: 13 additions & 13 deletions package/Testing.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module []

import Core
import Json

Option val := [None, Some val]
implements [
Expand All @@ -26,7 +26,7 @@ expect
encoded =
dat : Option U8
dat = @Option None
Encode.toBytes dat Core.json
Encode.toBytes dat Json.utf8
|> Str.fromUtf8

expected = Ok ""
Expand All @@ -36,7 +36,7 @@ expect
encoded =
dat : { maybe : Option U8, other : Str }
dat = { maybe: none {}, other: "hi" }
Encode.toBytes dat (Core.jsonWithOptions { emptyEncodeAsNull: Core.encodeAsNullOption { record: Bool.false } })
Encode.toBytes dat (Json.utf8With { emptyEncodeAsNull: Json.encodeAsNullOption { record: Bool.false } })
|> Str.fromUtf8

expected = Ok
Expand All @@ -48,7 +48,7 @@ expect
expect
encoded =
{ maybe: some 10 }
|> Encode.toBytes Core.json
|> Encode.toBytes Json.utf8
|> Str.fromUtf8

expected = Ok
Expand All @@ -60,7 +60,7 @@ expect
expect
encoded =
dat = [some 1, none {}, some 2, some 3]
Encode.toBytes dat Core.json
Encode.toBytes dat Json.utf8
|> Str.fromUtf8

expected = Ok "[1,2,3]"
Expand All @@ -71,7 +71,7 @@ expect
encoded =
dat : { maybe : Option U8, other : Str }
dat = { maybe: none {}, other: "hi" }
Encode.toBytes dat Core.json
Encode.toBytes dat Json.utf8
|> Str.fromUtf8

expected = Ok
Expand All @@ -84,7 +84,7 @@ expect
encoded =
dat : (U8, Option U8, Option Str, Str)
dat = (10, none {}, some "opt", "hi")
Encode.toBytes dat Core.json
Encode.toBytes dat Json.utf8
|> Str.fromUtf8

expected = Ok
Expand All @@ -96,7 +96,7 @@ expect
expect
encoded =
dat = [some 1, none {}, some 2, some 3]
Encode.toBytes dat (Core.jsonWithOptions { emptyEncodeAsNull: Core.encodeAsNullOption { list: Bool.true } })
Encode.toBytes dat (Json.utf8With { emptyEncodeAsNull: Json.encodeAsNullOption { list: Bool.true } })
|> Str.fromUtf8

expected = Ok "[1,null,2,3]"
Expand All @@ -119,7 +119,7 @@ expect
{"y":1}
"""
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8

expected = Ok ({ y: 1u8, maybe: none {} })
isGood =
Expand All @@ -138,7 +138,7 @@ expect
{"maybe":1}
"""
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8

expected = Ok ({ maybe: some 1u8 })
expected == decoded
Expand All @@ -147,7 +147,7 @@ expect
decoded =
"[1,2,3]"
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8

expected = Ok [some 1, some 2, some 3]
expected == decoded
Expand All @@ -158,7 +158,7 @@ expect
decoded =
"[1,null,2]"
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8
expected = Ok (some 1, none {}, some 2)
expected == decoded

Expand All @@ -170,7 +170,7 @@ expect
{"y":1,"maybe":null}
"""
|> Str.toUtf8
|> Decode.fromBytes Core.json
|> Decode.fromBytes Json.utf8

expected = Ok ({ y: 1u8, maybe: none {} })
isGood =
Expand Down

0 comments on commit 764f70f

Please sign in to comment.