@@ -63,42 +63,75 @@ final class DefaultMarkdownFormatter_Tests: XCTestCase {
6363 let expectedCodeAttributedSubstring = " let property: Double = 10.0 "
6464 let expectedLinkAttributedSubstring = " this link "
6565 let expectedLinkURL = " https://docs.swift.org/swift-book/ "
66- let expectedUnorderedListedSubstrings = [ " \u{2022} class" , " \u{2022} struct" , " \u{2022} enum" , " \u{2022} actor" ]
66+ let expectedListItems = [ " class " , " struct " , " enum " , " actor " ]
6767
6868 // WHEN
6969 let attributedString = sut. format ( stringWithMarkdown, attributes: [ : ] )
7070
7171 // THEN
72- attributedString. enumerateAttributes ( in: NSRange (
73- location: 0 ,
74- length: attributedString. length
75- ) ) { attributes, range, _ in
72+ var listItemBuffer = " "
73+ var currentHeadIndent : CGFloat ?
74+
75+ attributedString. enumerateAttributes ( in: NSRange ( location: 0 , length: attributedString. length) ) { attributes, range, _ in
76+ let substring = attributedString. attributedSubstring ( from: range) . string
7677 let fontAttribute = attributes [ . font] as? UIFont
7778
79+ // Heading 1 check
7880 if let headerAttribute = fontAttribute,
7981 headerAttribute. fontDescriptor. pointSize == UIFont . preferredFont ( forTextStyle: . title1) . pointSize {
80- XCTAssertEqual ( expectedHeading1AttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
82+ XCTAssertEqual ( expectedHeading1AttributedSubstring, substring)
83+
84+ // Strikethrough check
8185 } else if let strikethroughAttribute = attributes [ . strikethroughStyle] as? NSNumber ,
8286 strikethroughAttribute == 1 {
83- XCTAssertEqual ( expectedStrikethroughAttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
87+ XCTAssertEqual ( expectedStrikethroughAttributedSubstring, substring)
88+
89+ // Bold check
8490 } else if let boldAttribute = fontAttribute,
8591 boldAttribute. fontDescriptor. symbolicTraits. contains ( . traitBold) {
86- XCTAssertEqual ( expectedBoldAttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
92+ XCTAssertEqual ( expectedBoldAttributedSubstring, substring)
93+
94+ // Code font check
8795 } else if let fontAttribute = fontAttribute,
8896 let fontNameAttribute = fontAttribute. fontDescriptor. fontAttributes [ . name] as? String ,
8997 fontNameAttribute == DefaultMarkdownFormatter ( ) . styles. codeFont. name {
90- XCTAssertEqual ( expectedCodeAttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
98+ XCTAssertEqual ( expectedCodeAttributedSubstring, substring)
99+
100+ // Link check
91101 } else if let linkAttribute = attributes [ . link] as? NSURL ,
92102 let url = linkAttribute. absoluteString {
93- XCTAssertEqual ( expectedLinkAttributedSubstring, attributedString . attributedSubstring ( from : range ) . string )
103+ XCTAssertEqual ( expectedLinkAttributedSubstring, substring )
94104 XCTAssertEqual ( expectedLinkURL, url)
95- } else if let paragraphStyleAttribute = attributes [ . paragraphStyle] as? NSParagraphStyle ,
96- paragraphStyleAttribute. headIndent > 0 {
97- XCTAssertEqual (
98- true ,
99- expectedUnorderedListedSubstrings. contains ( attributedString. attributedSubstring ( from: range) . string)
100- )
101105 }
106+
107+ // List item handling
108+ if let paragraphStyle = attributes [ . paragraphStyle] as? NSParagraphStyle ,
109+ paragraphStyle. headIndent > 0 {
110+ // same list item as previous?
111+ if currentHeadIndent == paragraphStyle. headIndent {
112+ listItemBuffer += substring
113+ } else {
114+ // process previous buffer
115+ if !listItemBuffer. isEmpty {
116+ XCTAssertTrue ( expectedListItems. contains { listItemBuffer. contains ( $0) } )
117+ }
118+ // start new buffer
119+ listItemBuffer = substring
120+ currentHeadIndent = paragraphStyle. headIndent
121+ }
122+ } else {
123+ // process any leftover buffer
124+ if !listItemBuffer. isEmpty {
125+ XCTAssertTrue ( expectedListItems. contains { listItemBuffer. contains ( $0) } )
126+ listItemBuffer = " "
127+ currentHeadIndent = nil
128+ }
129+ }
130+ }
131+
132+ // process last buffer
133+ if !listItemBuffer. isEmpty {
134+ XCTAssertTrue ( expectedListItems. contains { listItemBuffer. contains ( $0) } )
102135 }
103136 }
104137
0 commit comments