Skip to content

Commit

Permalink
Add detailed negative cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Mar 8, 2020
1 parent 6f2e0d1 commit ec133cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/FsCodec.SystemTextJson/TypeSafeEnumConverter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module TypeSafeEnum =

let tryParseT (t : Type) (str : string) =
match Union.tryGetUnion t with
| None -> invalidArg "t" "Type must be a Union."
| None -> invalidArg "t" "Type must be a FSharpUnion."
| Some u ->
u.cases
|> Array.tryFindIndex (fun case -> case.Name = str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module FsCodec.SystemTextJson.Tests.TypeSafeEnumConverterTests
open FsCodec.SystemTextJson
open System
open System.Collections.Generic
open System.Text.Json
open Swensen.Unquote
open Xunit

Expand All @@ -15,6 +16,12 @@ let [<Fact>] happy () =
test <@ None = TypeSafeEnum.tryParse<Outcome> "Wat" @>
raises<KeyNotFoundException> <@ TypeSafeEnum.parse<Outcome> "Wat" @>

let optionsWithOutcomeConverter = Options.Create(TypeSafeEnumConverter<Outcome>())
test <@ Joy = Serdes.Deserialize("\"Joy\"", optionsWithOutcomeConverter) @>
test <@ Some Joy = Serdes.Deserialize("\"Joy\"", optionsWithOutcomeConverter) @>
raises<KeyNotFoundException> <@ Serdes.Deserialize<Outcome>("\"Confusion\"", optionsWithOutcomeConverter) @>
raises<JsonException> <@ Serdes.Deserialize<Outcome> "1" @>

let [<Fact>] sad () =
raises<ArgumentException> <@ TypeSafeEnum.tryParse<string> "Wat" @>
raises<ArgumentException> <@ TypeSafeEnum.toString "Wat" @>
Expand All @@ -35,4 +42,5 @@ let [<Fact>] fallBackExample () =
test <@ Joy = Serdes.Deserialize<OutcomeWithOther> "\"Joy\"" @>
test <@ Some Other = Serdes.Deserialize<OutcomeWithOther option> "\"Wat\"" @>
test <@ Other = Serdes.Deserialize<OutcomeWithOther> "\"Wat\"" @>
raises<JsonException> <@ Serdes.Deserialize<OutcomeWithOther> "1" @>
test <@ Seq.forall (fun (x,y) -> x = y) <| Seq.zip [Joy; Other] (Serdes.Deserialize "[\"Joy\", \"Wat\"]") @>

0 comments on commit ec133cd

Please sign in to comment.