|
| 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 = actualValues :?> (string * obj) list |
| 23 | + actualName, actualValues |
| 24 | + |
| 25 | +[<Test>] |
| 26 | +let ``getMsgNameAndFields for RootMsg.RootMsgNoField`` () = |
| 27 | + getTraceForMsg (RootMsg.RootMsgNoField) |
| 28 | + =! (nameof (RootMsg.RootMsgNoField), |
| 29 | + []) |
| 30 | + |
| 31 | +[<Test>] |
| 32 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneIntField`` () = |
| 33 | + getTraceForMsg (RootMsg.RootMsgOneIntField 42) |
| 34 | + =! (nameof (RootMsg.RootMsgOneIntField), |
| 35 | + [ ("Item", 42) ]) |
| 36 | + |
| 37 | +[<Test>] |
| 38 | +let ``getMsgNameAndFields for RootMsg.RootMsgTwoStringAndIntField`` () = |
| 39 | + getTraceForMsg (RootMsg.RootMsgTwoStringAndIntField("test", 42)) |
| 40 | + =! (nameof (RootMsg.RootMsgTwoStringAndIntField), |
| 41 | + [ ("str", "test"); ("int", 42) ]) |
| 42 | + |
| 43 | +[<Test>] |
| 44 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildField, ChildMsg.ChildMsgNoField`` () = |
| 45 | + getTraceForMsg (RootMsg.RootMsgOneChildField(ChildMsg.ChildMsgNoField)) |
| 46 | + =! ($"{nameof ChildMsg.ChildMsgNoField}/{nameof (RootMsg.RootMsgOneChildField)}", |
| 47 | + []) |
| 48 | + |
| 49 | +[<Test>] |
| 50 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildField, ChildMsg.ChildMsgOneIntField`` () = |
| 51 | + getTraceForMsg (RootMsg.RootMsgOneChildField(ChildMsg.ChildMsgOneIntField(42))) |
| 52 | + =! ($"{nameof ChildMsg.ChildMsgOneIntField}/{nameof (RootMsg.RootMsgOneChildField)}", |
| 53 | + [ ("Item", 42) ]) |
| 54 | + |
| 55 | +[<Test>] |
| 56 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildField, ChildMsg.ChildMsgTwoStringAndIntField`` () = |
| 57 | + getTraceForMsg (RootMsg.RootMsgOneChildField(ChildMsg.ChildMsgTwoStringAndIntField("test", 42))) |
| 58 | + =! ($"{nameof ChildMsg.ChildMsgTwoStringAndIntField}/{nameof (RootMsg.RootMsgOneChildField)}", |
| 59 | + [ ("childStr", "test"); ("childInt", 42) ]) |
| 60 | + |
| 61 | +[<Test>] |
| 62 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneIntOptionField, None`` () = |
| 63 | + getTraceForMsg (RootMsg.RootMsgOneIntOptionField(None)) |
| 64 | + =! ($"None/{nameof (RootMsg.RootMsgOneIntOptionField)}", |
| 65 | + []) |
| 66 | + |
| 67 | +[<Test>] |
| 68 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneIntOptionField, Some`` () = |
| 69 | + getTraceForMsg (RootMsg.RootMsgOneIntOptionField(Some 42)) |
| 70 | + =! ($"Some/{nameof (RootMsg.RootMsgOneIntOptionField)}", |
| 71 | + [ ("Value", 42) ]) |
| 72 | + |
| 73 | +[<Test>] |
| 74 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, None`` () = |
| 75 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(None)) |
| 76 | + =! ($"None/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 77 | + []) |
| 78 | + |
| 79 | +[<Test>] |
| 80 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, Some ChildMsgNoField`` () = |
| 81 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(Some(ChildMsgNoField))) |
| 82 | + =! ($"{nameof (ChildMsg.ChildMsgNoField)}/Some/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 83 | + []) |
| 84 | + |
| 85 | +[<Test>] |
| 86 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, Some ChildMsgOneIntField`` () = |
| 87 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(Some(ChildMsgOneIntField 42))) |
| 88 | + =! ($"{nameof (ChildMsg.ChildMsgOneIntField)}/Some/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 89 | + [ ("Item", 42) ]) |
| 90 | + |
| 91 | +[<Test>] |
| 92 | +let ``getMsgNameAndFields for RootMsg.RootMsgOneChildOptionField, Some ChildMsgTwoStringAndIntField`` () = |
| 93 | + getTraceForMsg (RootMsg.RootMsgOneChildOptionField(Some(ChildMsgTwoStringAndIntField("test", 42)))) |
| 94 | + =! ($"{nameof (ChildMsg.ChildMsgTwoStringAndIntField)}/Some/{nameof (RootMsg.RootMsgOneChildOptionField)}", |
| 95 | + [ ("childStr", "test"); ("childInt", 42) ]) |
| 96 | + |
| 97 | +[<Test>] |
| 98 | +let ``getMsgNameAndFields for RootMsg.RootMsgIntOptionOption, Some, None`` () = |
| 99 | + getTraceForMsg (RootMsg.RootMsgIntOptionOption(Some(None))) |
| 100 | + =! ($"None/Some/{nameof (RootMsg.RootMsgIntOptionOption)}", |
| 101 | + []) |
| 102 | + |
| 103 | +[<Test>] |
| 104 | +let ``getMsgNameAndFields for RootMsg.RootMsgIntOptionOption, Some, Some`` () = |
| 105 | + getTraceForMsg (RootMsg.RootMsgIntOptionOption(Some(Some 42))) |
| 106 | + =! ($"Some/Some/{nameof (RootMsg.RootMsgIntOptionOption)}", |
| 107 | + [ ("Value", 42) ]) |
0 commit comments