@@ -283,4 +283,93 @@ final class MarkupTests: XCTestCase {
283283 ) !. debugDescription ( )
284284 )
285285 }
286+
287+ func testChildAtPositionHasCorrectType( ) throws {
288+ let source = " This is a [*link*](github.com). And some **bold** and *italic* text. "
289+
290+ /*
291+ Document
292+ └─ Paragraph
293+ */
294+ let document = Document ( parsing: source)
295+ let paragraph = try XCTUnwrap ( document. child ( at: 0 ) )
296+ assertEqualType ( paragraph, Paragraph . self)
297+
298+ /*
299+ ├─ Text "This is a "
300+ ├─ Link destination: "github.com"
301+ │ └─ Emphasis
302+ │ └─ Text "link"
303+
304+ */
305+ assertEqualType ( paragraph. child ( at: 0 ) , Text . self)
306+ assertEqualType ( paragraph. child ( at: 1 ) , Link . self)
307+ assertEqualType ( paragraph. child ( at: 1 ) ? . child ( at: 0 ) , Emphasis . self)
308+ assertEqualType ( paragraph. child ( at: 1 ) ? . child ( at: 0 ) ? . child ( at: 0 ) , Text . self)
309+
310+ /*
311+ ├─ Text ". And some "
312+ ├─ Strong
313+ │ └─ Text "bold"
314+ */
315+ assertEqualType ( paragraph. child ( at: 2 ) , Text . self)
316+ assertEqualType ( paragraph. child ( at: 3 ) , Strong . self)
317+ assertEqualType ( paragraph. child ( at: 3 ) ? . child ( at: 0 ) , Text . self)
318+
319+ /*
320+ ├─ Text " and "
321+ ├─ Emphasis
322+ │ └─ Text "italic"
323+ └─ Text " text."
324+ */
325+ assertEqualType ( paragraph. child ( at: 4 ) , Text . self)
326+ assertEqualType ( paragraph. child ( at: 5 ) , Emphasis . self)
327+ assertEqualType ( paragraph. child ( at: 5 ) ? . child ( at: 0 ) , Text . self)
328+ assertEqualType ( paragraph. child ( at: 6 ) , Text . self)
329+
330+ XCTAssertNil ( paragraph. child ( at: 7 ) )
331+ }
332+
333+ func testChildAtPositionHasCorrectMetadata( ) throws {
334+ let source = " This is a [*link*](github.com). And some **bold** and *italic* text. "
335+
336+ let document = Document ( parsing: source)
337+ let paragraph = try XCTUnwrap ( document. child ( at: 0 ) as? Paragraph )
338+
339+ for (index, sequencedChild) in paragraph. children. enumerated ( ) {
340+ let indexedChild = try XCTUnwrap ( paragraph. child ( at: index) )
341+
342+ let indexedChildMetadata = indexedChild. raw. metadata
343+ let sequencedChildMetadata = sequencedChild. raw. metadata
344+
345+ XCTAssertEqual ( indexedChildMetadata. id, sequencedChildMetadata. id)
346+ XCTAssertEqual ( indexedChildMetadata. indexInParent, sequencedChildMetadata. indexInParent)
347+ XCTAssertEqual ( indexedChildMetadata. indexInParent, index)
348+ }
349+ }
350+
351+ func testChildAtPositionHasCorrectDataID( ) throws {
352+ let source = " This is a [*link*](github.com). And some **bold** and *italic* text. "
353+
354+ let document = Document ( parsing: source)
355+ let paragraph = try XCTUnwrap ( document. child ( at: 0 ) as? Paragraph )
356+
357+ for (index, sequencedChild) in paragraph. children. enumerated ( ) {
358+ let indexedChild = try XCTUnwrap ( paragraph. child ( at: index) )
359+
360+ XCTAssertEqual ( indexedChild. _data. id, sequencedChild. _data. id)
361+ }
362+ }
363+
364+ func assertEqualType< FirstType, SecondType> (
365+ _ first: FirstType ,
366+ _ second: SecondType . Type ,
367+ file: StaticString = #file,
368+ line: UInt = #line
369+ ) {
370+ guard first is SecondType else {
371+ XCTFail ( " ' \( type ( of: first) ) ' is not expected type ' \( second) ' " , file: file, line: line)
372+ return
373+ }
374+ }
286375}
0 commit comments