@@ -4,6 +4,7 @@ import Code.BranchRef as BranchRef
44import Code.Definition.Reference as Reference
55import Code.FullyQualifiedName as FQN
66import Code.Hash as Hash
7+ import Code.Syntax as Syntax
78import Code.Syntax.SyntaxSegment as SyntaxSegment
89import Expect
910import List.Nonempty as NEL
@@ -162,3 +163,95 @@ diff =
162163 }
163164 in
164165 DefinitionDiff . Diff diffDetails
166+
167+
168+ sortWithDocs : Test
169+ sortWithDocs =
170+ describe " BranchDiff.sortWithDocs"
171+ [ test " pairs definitions with their .doc changes (doc before definition) regardless of input order" <|
172+ \ _ ->
173+ let
174+ -- Definitions with docs in various orders
175+ aDoc =
176+ addedChangeLine ( FQN . fromString " a.doc" ) ( FQN . fromString " a.doc" ) DefinitionType . Doc
177+
178+ a =
179+ addedChangeLine ( FQN . fromString " a" ) ( FQN . fromString " a" ) DefinitionType . Term
180+
181+ b =
182+ addedChangeLine ( FQN . fromString " b" ) ( FQN . fromString " b" ) DefinitionType . Term
183+
184+ bDoc =
185+ addedChangeLine ( FQN . fromString " b.doc" ) ( FQN . fromString " b.doc" ) DefinitionType . Doc
186+
187+ -- Definition without doc
188+ c =
189+ addedChangeLine ( FQN . fromString " c" ) ( FQN . fromString " c" ) DefinitionType . Term
190+
191+ -- Standalone doc (no corresponding definition)
192+ dDoc =
193+ addedChangeLine ( FQN . fromString " d.doc" ) ( FQN . fromString " d.doc" ) DefinitionType . Doc
194+
195+ -- Input: doc first, def later, def first, doc later, def without doc, standalone doc
196+ input =
197+ [ aDoc, b, c, a, dDoc, bDoc ]
198+
199+ -- Expected: pairs together with doc first (paired when encountered)
200+ expected =
201+ [ aDoc, a, bDoc, b, c, dDoc ]
202+
203+ result =
204+ BranchDiff . sortWithDocs input
205+ in
206+ Expect . equal expected result
207+ , test " recursively sorts within namespaces" <|
208+ \ _ ->
209+ let
210+ innerDef =
211+ addedChangeLine ( FQN . fromString " data.List.map" ) ( FQN . fromString " List.map" ) DefinitionType . Term
212+
213+ innerDoc =
214+ addedChangeLine ( FQN . fromString " data.List.map.doc" ) ( FQN . fromString " List.map.doc" ) DefinitionType . Doc
215+
216+ innerOther =
217+ addedChangeLine ( FQN . fromString " data.List.filter" ) ( FQN . fromString " List.filter" ) DefinitionType . Term
218+
219+ namespace =
220+ ChangeLine . Namespace
221+ { name = FQN . fromString " data.List"
222+ , lines = [ innerDef, innerOther, innerDoc ]
223+ }
224+
225+ expectedNamespace =
226+ ChangeLine . Namespace
227+ { name = FQN . fromString " data.List"
228+ , lines = [ innerDoc, innerDef, innerOther ]
229+ }
230+
231+ input =
232+ [ namespace ]
233+
234+ expected =
235+ [ expectedNamespace ]
236+
237+ result =
238+ BranchDiff . sortWithDocs input
239+ in
240+ Expect . equal expected result
241+ ]
242+
243+
244+ addedChangeLine : FQN .FQN -> FQN .FQN -> DefinitionType .DefinitionType -> ChangeLine .ChangeLine
245+ addedChangeLine fullName shortName type_ =
246+ let
247+ source =
248+ Syntax . fromNEL ( NEL . singleton ( SyntaxSegment . SyntaxSegment SyntaxSegment . TextLiteral " test" ))
249+ in
250+ ChangeLine . Added
251+ type_
252+ { hash = Hash . unsafeFromString " #testHash"
253+ , shortName = shortName
254+ , fullName = fullName
255+ , ref = Reference . fromFQN ( DefinitionType . toReferenceConstructor type_) fullName
256+ , source = source
257+ }
0 commit comments