@@ -63,42 +63,75 @@ final class DefaultMarkdownFormatter_Tests: XCTestCase {
63
63
let expectedCodeAttributedSubstring = " let property: Double = 10.0 "
64
64
let expectedLinkAttributedSubstring = " this link "
65
65
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 " ]
67
67
68
68
// WHEN
69
69
let attributedString = sut. format ( stringWithMarkdown, attributes: [ : ] )
70
70
71
71
// 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
76
77
let fontAttribute = attributes [ . font] as? UIFont
77
78
79
+ // Heading 1 check
78
80
if let headerAttribute = fontAttribute,
79
81
headerAttribute. fontDescriptor. pointSize == UIFont . preferredFont ( forTextStyle: . title1) . pointSize {
80
- XCTAssertEqual ( expectedHeading1AttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
82
+ XCTAssertEqual ( expectedHeading1AttributedSubstring, substring)
83
+
84
+ // Strikethrough check
81
85
} else if let strikethroughAttribute = attributes [ . strikethroughStyle] as? NSNumber ,
82
86
strikethroughAttribute == 1 {
83
- XCTAssertEqual ( expectedStrikethroughAttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
87
+ XCTAssertEqual ( expectedStrikethroughAttributedSubstring, substring)
88
+
89
+ // Bold check
84
90
} else if let boldAttribute = fontAttribute,
85
91
boldAttribute. fontDescriptor. symbolicTraits. contains ( . traitBold) {
86
- XCTAssertEqual ( expectedBoldAttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
92
+ XCTAssertEqual ( expectedBoldAttributedSubstring, substring)
93
+
94
+ // Code font check
87
95
} else if let fontAttribute = fontAttribute,
88
96
let fontNameAttribute = fontAttribute. fontDescriptor. fontAttributes [ . name] as? String ,
89
97
fontNameAttribute == DefaultMarkdownFormatter ( ) . styles. codeFont. name {
90
- XCTAssertEqual ( expectedCodeAttributedSubstring, attributedString. attributedSubstring ( from: range) . string)
98
+ XCTAssertEqual ( expectedCodeAttributedSubstring, substring)
99
+
100
+ // Link check
91
101
} else if let linkAttribute = attributes [ . link] as? NSURL ,
92
102
let url = linkAttribute. absoluteString {
93
- XCTAssertEqual ( expectedLinkAttributedSubstring, attributedString . attributedSubstring ( from : range ) . string )
103
+ XCTAssertEqual ( expectedLinkAttributedSubstring, substring )
94
104
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
- )
101
105
}
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) } )
102
135
}
103
136
}
104
137
0 commit comments