@@ -88,6 +88,14 @@ module MyDiffModule =
8888
8989 let differ < 'T > = CustomDiffer() .GetDiffer< 'T>()
9090
91+ module MyDiffWithCombinators =
92+
93+ let customDiffer = CustomDiffer< CustomDiffable>. Build( fun factory ->
94+ let stringDiffer = factory.GetDiffer< string>()
95+ fun x1 x2 -> stringDiffer.Diff( x1.x, x2.x))
96+
97+ let differ < 'T > = customDiffer.GetDiffer< 'T>()
98+
9199type MyDiffer ( differFactory : IDifferFactory ) =
92100 let stringDiffer = differFactory.GetDiffer< string>()
93101
@@ -105,61 +113,62 @@ type MyCustomDiffer() =
105113type MyDiffType < 'T >() =
106114 static member val Differ = MyCustomDiffer() .GetDiffer< 'T>()
107115
116+ type MyDiffTypeWithCombinators < 'T >() =
117+ static let customDiffer = CustomDiffer< CustomDiffable>. Build( fun factory ->
118+ let stringDiffer = factory.GetDiffer< string>()
119+ fun x1 x2 -> stringDiffer.Diff( x1.x, x2.x))
120+
121+ static member val Differ = customDiffer.GetDiffer< 'T>()
122+
108123[<Fact>]
109124let ``Custom differ`` () =
110125 Assert.Equal( " x Expect = \" a\"\n Actual = \" b\"\n " ,
111126 Differ.ToString({ x = " a" }, { x = " b" }))
112127 Assert.Equal( " Expect = \" a\"\n Actual = \" b\"\n " ,
113128 Differ.ToString({ x = " a" }, { x = " b" }, MyDiffModule.differ))
129+ Assert.Equal( " Expect = \" a\"\n Actual = \" b\"\n " ,
130+ Differ.ToString({ x = " a" }, { x = " b" }, MyDiffWithCombinators.differ))
114131 Assert.Equal( " Expect = \" a\"\n Actual = \" b\"\n " ,
115132 Differ.ToString({ x = " a" }, { x = " b" }, MyDiffType.Differ))
133+ Assert.Equal( " Expect = \" a\"\n Actual = \" b\"\n " ,
134+ Differ.ToString({ x = " a" }, { x = " b" }, MyDiffTypeWithCombinators.Differ))
116135
117136module ` `Custom differ with custom diff output`` =
118137
119- type MyCustomDiffer () =
120- interface ICustomDiffer with
121- member this.GetCustomDiffer < 'T >( _ , shape ) =
122- if shape.Type = typeof< CustomDiffable> then
123- { new IDiffer< CustomDiffable> with
124- member _.Diff ( x1 , x2 ) =
125- if x1.x = x2.x then
126- None
127- else
128- Diff.MakeCustom( fun writer param indent path recur ->
129- if param.ensureFirstLineIsAligned then writer.WriteLine()
130- let indentLike str = String.replicate ( String.length str) " "
131- let dpath = if path = " " then " " else path + " "
132- writer.WriteLine( $" {indent}{dpath}{param.x1Name} __is__ {x1.x}" )
133- writer.WriteLine( $" {indent}{indentLike dpath}{param.x2Name} __is__ {x2.x}" ))
134- |> Some }
135- |> unbox< IDiffer< 'T>>
136- |> Some
137- else
138- None
138+ let myCustomDiffer = CustomDiffer< CustomDiffable>. Build( fun x1 x2 ->
139+ if x1.x = x2.x then
140+ None
141+ else
142+ Diff.MakeCustom( fun writer param indent path recur ->
143+ if param.ensureFirstLineIsAligned then writer.WriteLine()
144+ let indentLike str = String.replicate ( String.length str) " "
145+ let dpath = if path = " " then " " else path + " "
146+ writer.WriteLine( $" {indent}{dpath}{param.x1Name} __is__ {x1.x}" )
147+ writer.WriteLine( $" {indent}{indentLike dpath}{param.x2Name} __is__ {x2.x}" ))
148+ |> Some)
139149
140- type MyDiffType < 'T >() =
141- static member val Differ = MyCustomDiffer() .GetDiffer< 'T>()
150+ let differ < 'T > = myCustomDiffer.GetDiffer< 'T>()
142151
143152 [<Fact>]
144153 let ``Assert with immediate value adds newline`` () =
145154 let ex = Assert.Throws< AssertionFailedException>( fun () ->
146- Differ.Assert({ x = " a" }, { x = " b" }, MyDiffType.Differ ))
155+ Differ.Assert({ x = " a" }, { x = " b" }, differ ))
147156 Assert.Equal( " \n Expect __is__ a\n Actual __is__ b\n " , ex.Message)
148157
149158 [<Fact>]
150159 let ``Assert with nested value doesn 't add newline`` () =
151160 let ex = Assert.Throws< AssertionFailedException>( fun () ->
152- Differ.Assert({| i = { x = " a" } |}, {| i = { x = " b" } |}, MyDiffType.Differ ))
161+ Differ.Assert({| i = { x = " a" } |}, {| i = { x = " b" } |}, differ ))
153162 Assert.Equal( " i Expect __is__ a\n Actual __is__ b\n " , ex.Message)
154163
155164 [<Fact>]
156165 let ``ToString with immediate value doesn 't add newline`` () =
157- let diff = Differ.ToString({ x = " a" }, { x = " b" }, MyDiffType.Differ )
166+ let diff = Differ.ToString({ x = " a" }, { x = " b" }, differ )
158167 Assert.Equal( " Expect __is__ a\n Actual __is__ b\n " , diff)
159168
160169 [<Fact>]
161170 let ``ToString with nested value doesn 't add newline`` () =
162- let diff = Differ.ToString({| i = { x = " a" } |}, {| i = { x = " b" } |}, MyDiffType.Differ )
171+ let diff = Differ.ToString({| i = { x = " a" } |}, {| i = { x = " b" } |}, differ )
163172 Assert.Equal( " i Expect __is__ a\n Actual __is__ b\n " , diff)
164173
165174type Rec = { xRec: Rec option }
0 commit comments