|
| 1 | +module Elmish.TracersTests |
| 2 | + |
| 3 | +open Swensen.Unquote |
| 4 | +open NUnit.Framework |
| 5 | + |
| 6 | +type ChildMsg = |
| 7 | + | ChildMsgNoField |
| 8 | + | ChildMsgOneIntField of int |
| 9 | + | ChildMsgTwoStringAndIntField of childStr: string * childInt: int |
| 10 | + |
| 11 | +type RootMsg = |
| 12 | + | RootMsgNoField |
| 13 | + | RootMsgOneIntField of int |
| 14 | + | RootMsgTwoStringAndIntField of str: string * int: int |
| 15 | + | RootMsgOneChildField of ChildMsg |
| 16 | + | RootMsgOneIntOptionField of int option |
| 17 | + | RootMsgOneChildOptionField of ChildMsg option |
| 18 | + | RootMsgIntOptionOption of int option option |
| 19 | + |
| 20 | +let getTraceForMsg (msg: 'Msg) = |
| 21 | + let actualName, actualValues = Tracers.getMsgNameAndFields typeof<'Msg> msg |
| 22 | + let actualValues = |
| 23 | + actualValues :?> (string * obj) array |
| 24 | + |> Array.toList // So that we can compare the actual vs expected by value in the tests. |
| 25 | + actualName, actualValues |
| 26 | + |
| 27 | +[<Test>] |
| 28 | +let ``getMsgNameAndFields for RootMsg.RootMsgNoField`` () = |
| 29 | + getTraceForMsg (RootMsg.RootMsgNoField) |
| 30 | + =! (nameof (RootMsg.RootMsgNoField), |
| 31 | + []) |
| 32 | + |
| 33 | +[<Test>] |
| 34 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneIntField`` () = |
| 35 | + getTraceForMsg (RootMsg.RootMsgOneIntField 42) |
| 36 | + =! (nameof (RootMsg.RootMsgOneIntField), |
| 37 | + [ ("Item", 42) ]) |
| 38 | + |
| 39 | +[<Test>] |
| 40 | +let ``getMsgNameAndFields for RootMsg.RootMsgTwoStringAndIntField`` () = |
| 41 | + getTraceForMsg (RootMsg.RootMsgTwoStringAndIntField("test", 42)) |
| 42 | + =! (nameof (RootMsg.RootMsgTwoStringAndIntField), |
| 43 | + [ ("str", "test"); ("int", 42) ]) |
| 44 | + |
| 45 | +[<Test>] |
| 46 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildField, ChildMsg.ChildMsgNoField`` () = |
| 47 | + getTraceForMsg (RootMsg.RootMsgOneChildField(ChildMsg.ChildMsgNoField)) |
| 48 | + =! ($"{nameof ChildMsg.ChildMsgNoField}/{nameof (RootMsg.RootMsgOneChildField)}", |
| 49 | + []) |
| 50 | + |
| 51 | +[<Test>] |
| 52 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildField, ChildMsg.ChildMsgOneIntField`` () = |
| 53 | + getTraceForMsg (RootMsg.RootMsgOneChildField(ChildMsg.ChildMsgOneIntField(42))) |
| 54 | + =! ($"{nameof ChildMsg.ChildMsgOneIntField}/{nameof (RootMsg.RootMsgOneChildField)}", |
| 55 | + [ ("Item", 42) ]) |
| 56 | + |
| 57 | +[<Test>] |
| 58 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildField, ChildMsg.ChildMsgTwoStringAndIntField`` () = |
| 59 | + getTraceForMsg (RootMsg.RootMsgOneChildField(ChildMsg.ChildMsgTwoStringAndIntField("test", 42))) |
| 60 | + =! ($"{nameof ChildMsg.ChildMsgTwoStringAndIntField}/{nameof (RootMsg.RootMsgOneChildField)}", |
| 61 | + [ ("childStr", "test"); ("childInt", 42) ]) |
| 62 | + |
| 63 | +[<Test>] |
| 64 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneIntOptionField, None`` () = |
| 65 | + getTraceForMsg (RootMsg.RootMsgOneIntOptionField(None)) |
| 66 | + =! ($"None/{nameof (RootMsg.RootMsgOneIntOptionField)}", |
| 67 | + []) |
| 68 | + |
| 69 | +[<Test>] |
| 70 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneIntOptionField, Some`` () = |
| 71 | + getTraceForMsg (RootMsg.RootMsgOneIntOptionField(Some 42)) |
| 72 | + =! ($"Some/{nameof (RootMsg.RootMsgOneIntOptionField)}", |
| 73 | + [ ("Value", 42) ]) |
| 74 | + |
| 75 | +[<Test>] |
| 76 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, None`` () = |
| 77 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(None)) |
| 78 | + =! ($"None/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 79 | + []) |
| 80 | + |
| 81 | +[<Test>] |
| 82 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, Some ChildMsgNoField`` () = |
| 83 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(Some(ChildMsgNoField))) |
| 84 | + =! ($"{nameof (ChildMsg.ChildMsgNoField)}/Some/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 85 | + []) |
| 86 | + |
| 87 | +[<Test>] |
| 88 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, Some ChildMsgOneIntField`` () = |
| 89 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(Some(ChildMsgOneIntField 42))) |
| 90 | + =! ($"{nameof (ChildMsg.ChildMsgOneIntField)}/Some/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 91 | + [ ("Item", 42) ]) |
| 92 | + |
| 93 | +[<Test>] |
| 94 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, Some ChildMsgTwoStringAndIntField`` () = |
| 95 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(Some(ChildMsgTwoStringAndIntField("test", 42)))) |
| 96 | + =! ($"{nameof (ChildMsg.ChildMsgTwoStringAndIntField)}/Some/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 97 | + [ ("childStr", "test"); ("childInt", 42) ]) |
| 98 | + |
| 99 | +[<Test>] |
| 100 | +let ``getMsgNameAndFields for RootMsg.RootMsgIntOptionOption, Some, None`` () = |
| 101 | + getTraceForMsg (RootMsg.RootMsgIntOptionOption(Some(None))) |
| 102 | + =! ($"None/Some/{nameof (RootMsg.RootMsgIntOptionOption)}", |
| 103 | + []) |
| 104 | + |
| 105 | +[<Test>] |
| 106 | +let ``getMsgNameAndFields for RootMsg.RootMsgIntOptionOption, Some, Some`` () = |
| 107 | + getTraceForMsg (RootMsg.RootMsgIntOptionOption(Some(Some 42))) |
| 108 | + =! ($"Some/Some/{nameof (RootMsg.RootMsgIntOptionOption)}", |
| 109 | + [ ("Value", 42) ]) |
0 commit comments