@@ -20,6 +20,7 @@ class LinkRuleTests: XCTestCase {
20
20
XCTAssertFalse ( ( sut. recognizesLines ( [ "  " ] ) ) )
21
21
XCTAssertTrue ( sut. recognizesLines ( [ #"[Alt text](image.png "some title")"# ] ) )
22
22
XCTAssertTrue ( sut. recognizesLines ( [ #"[Alt text](https://www.website.com/ "some-test")"# ] ) )
23
+ XCTAssertTrue ( sut. recognizesLines ( [ #"[Alt text](https://www.website.com/ "some"test"with"quotation"marks")"# ] ) )
23
24
}
24
25
25
26
func test_DoesNotRecognizeLines_When_PrefixedWithExclamationMark( ) {
@@ -35,15 +36,7 @@ class LinkRuleTests: XCTestCase {
35
36
XCTAssert ( sut. getAllMatches ( [ "  " ] ) . isEmpty)
36
37
}
37
38
38
- func testCreateMarkDownItemWithLinesCreatesCorrectItem( ) {
39
- // Act
40
- let markDownItem = sut. createMarkDownItemWithLines ( [ " [Google](http://www.google.com) " ] )
41
-
42
- // Assert
43
- XCTAssert ( markDownItem is LinkMarkDownItem )
44
- }
45
-
46
- func testCreateMarkDownItemContainsCorrectLink( ) {
39
+ func test_MarkDownItemContainsCorrectLink_When_CreatingMarkDownItemWithLines( ) {
47
40
// Act
48
41
let markDownItem = sut. createMarkDownItemWithLines ( [ " [Google](http://www.google.com) " ] )
49
42
let markDownItem2 = sut. createMarkDownItemWithLines ( [ " [Youtube](http://www.youtube.com) " ] )
@@ -55,19 +48,21 @@ class LinkRuleTests: XCTestCase {
55
48
XCTAssertEqual ( ( markDownItem2 as! LinkMarkDownItem ) . url, " http://www.youtube.com " )
56
49
}
57
50
58
- func testCreateMarkDownItemContainsCorrectLinkWhenUsingAriaLabel ( ) {
51
+ func test_MarkDownItemContainsCorrectLink_When_CreatingMarkdownItemWithLinesUsingLinkTitle ( ) {
59
52
// Act
60
53
let markDownItem = sut. createMarkDownItemWithLines ( [ #"[Google](http://www.google.com "Google")"# ] )
61
54
let markDownItem2 = sut. createMarkDownItemWithLines ( [ #"[Youtube](http://www.youtube.com "You-tube")"# ] )
62
55
63
56
// Assert
64
57
XCTAssertEqual ( ( markDownItem as! LinkMarkDownItem ) . content, " Google " )
58
+ XCTAssertEqual ( ( markDownItem as! LinkMarkDownItem ) . title, " Google " )
65
59
XCTAssertEqual ( ( markDownItem as! LinkMarkDownItem ) . url, " http://www.google.com " )
66
60
XCTAssertEqual ( ( markDownItem2 as! LinkMarkDownItem ) . content, " Youtube " )
61
+ XCTAssertEqual ( ( markDownItem2 as! LinkMarkDownItem ) . title, " You-tube " )
67
62
XCTAssertEqual ( ( markDownItem2 as! LinkMarkDownItem ) . url, " http://www.youtube.com " )
68
63
}
69
64
70
- func testGetAllMatches ( ) {
65
+ func test_GetsAllMatches_When_ProvidingLinks ( ) {
71
66
// Arrange
72
67
let expectedMatchesRange = NSRange ( location: 0 , length: 32 )
73
68
let expectedMatchesRange2 = NSRange ( location: 38 , length: 32 )
@@ -81,16 +76,96 @@ class LinkRuleTests: XCTestCase {
81
76
sut. getAllMatches ( [ " [Google](https://www.google.com) test [Google](https://www.google.com) " ] ) ,
82
77
[ expectedMatchesRange, expectedMatchesRange2]
83
78
)
79
+ }
80
+
81
+ func test_GetsAllMatches_When_ProvidingLinksWithAdditionalTitleValues( ) {
82
+ // Act + Assert
83
+ XCTAssertEqual (
84
+ sut. getAllMatches ( [ #"[http://www.google.com](http://www.google.com "title"with"lots"of"quotationmarks")"# ] ) ,
85
+ [
86
+ NSRange ( location: 0 , length: 82 )
87
+ ]
88
+ )
89
+
90
+ XCTAssertEqual (
91
+ sut. getAllMatches ( [ #"[Google](https://www.google.com "great-url-title") test [Google](https://www.google.com "a11y title")"# ] ) ,
92
+ [
93
+ NSRange ( location: 0 , length: 50 ) ,
94
+ NSRange ( location: 56 , length: 45 )
95
+ ]
96
+ )
84
97
85
98
XCTAssertEqual (
86
- sut. getAllMatches ( [ #"[Google](https://www.google.com) test [Google](https://www.google.com "a11y title")"# ] ) ,
99
+ sut. getAllMatches ( [ #"[Google](https://www.google.com "great-url-title" ) test [Google](https://www.google.com "a11y title") and even more [https://www.apple.com](https://www.apple.com "Apple-aria-label") test "# ] ) ,
87
100
[
88
- NSRange ( location: 0 , length: 32 ) ,
89
- NSRange ( location: 38 , length: 45 )
101
+ NSRange ( location: 0 , length: 50 ) ,
102
+ NSRange ( location: 56 , length: 45 ) ,
103
+ NSRange ( location: 116 , length: 65 )
90
104
]
91
105
)
92
106
}
93
107
108
+ func test_FailsToMatch_When_ProvidingLinksWithIncorrectSyntax( ) {
109
+ // Act + Assert
110
+ XCTAssertTrue ( sut. getAllMatches ( [ #"[Google](https://www.google.com great-url-title")"# ] ) . isEmpty)
111
+ XCTAssertTrue ( sut. getAllMatches ( [ #"[Google](https://www.google.com great url title")"# ] ) . isEmpty)
112
+ }
113
+
114
+ func test_OnlyMatchesFirstLink_When_ProvidingOneCorrectLinkAndOneFaulty( ) {
115
+ // Act + Assert
116
+ XCTAssertEqual (
117
+ sut. getAllMatches ( [ #"[Google](https://www.google.com "great-url-title") test [Google](https://www.google.com a11y title")"# ] ) ,
118
+ [
119
+ NSRange ( location: 0 , length: 50 )
120
+ ]
121
+ )
122
+ }
123
+
124
+ func test_ParsesAdditionalTitleItems_When_InputMatches( ) throws {
125
+ // Arrange
126
+ let cases : [ ( String , String ? , UInt ) ] = [
127
+ (
128
+ #"[Google w/ title](http://www.google.com "with custom title")"# ,
129
+ " with custom title " ,
130
+ #line
131
+ ) ,
132
+ (
133
+ #"[Google w/ title](http://www.google.com "with-custom-title")"# ,
134
+ " with-custom-title " ,
135
+ #line
136
+ ) ,
137
+ (
138
+ #"[http://www.google.com](http://www.google.com "http://www.google.com")"# ,
139
+ " http://www.google.com " ,
140
+ #line
141
+ ) ,
142
+ (
143
+ #"[plain link](http://www.google.com "1234567890!@#$%^&*()")"# ,
144
+ " 1234567890!@#$%^&*() " ,
145
+ #line
146
+ ) ,
147
+ (
148
+ #"[http://www.google.com](http://www.google.com "title"with"lots"of"quotationmarks")"# ,
149
+ #"title"with"lots"of"quotationmarks"# ,
150
+ #line
151
+ ) ,
152
+ (
153
+ #"[plain link](http://www.google.com)"# ,
154
+ nil ,
155
+ #line
156
+ )
157
+ ]
158
+
159
+ for (input, title, line) in cases {
160
+ // Act
161
+ let output = sut. createMarkDownItemWithLines ( [ input] )
162
+
163
+ // Assert
164
+ let linkMarkDownItem = try XCTUnwrap ( output as? LinkMarkDownItem )
165
+ XCTAssertEqual ( linkMarkDownItem. title, title, line: line)
166
+ }
167
+ }
168
+
94
169
func test_ParsesItem_When_InputMatches( ) throws {
95
170
// Arrange
96
171
let cases : [ ( String , String , String , UInt ) ] = [
@@ -154,4 +229,31 @@ class LinkRuleTests: XCTestCase {
154
229
XCTAssertEqual ( linkMarkDownItem. url, url, line: line)
155
230
}
156
231
}
232
+
233
+ func test_LinkItemsAreCorrect_When_CreatingMarkDownItemsWithContent( ) throws {
234
+ // Arrange
235
+ let input = #"[Google](https://www.google.com "great-url-title") test [Google](https://www.google.com "a11y title") and even more [https://www.apple.com](https://www.apple.com "Apple-title") test"#
236
+ let markyMark = MarkyMark ( build: {
237
+ $0. setFlavor ( ContentfulFlavor ( ) )
238
+ } )
239
+
240
+ // Act
241
+ let paragraphItem = markyMark. parseMarkDown ( input) . first
242
+
243
+ // Assert
244
+ let linkMarkDownItem1 = try XCTUnwrap ( paragraphItem? . markDownItems ? [ 0 ] as? LinkMarkDownItem )
245
+ XCTAssertEqual ( linkMarkDownItem1. content, " Google " )
246
+ XCTAssertEqual ( linkMarkDownItem1. url, " https://www.google.com " )
247
+ XCTAssertEqual ( linkMarkDownItem1. title, " great-url-title " )
248
+
249
+ let linkMarkDownItem2 = try XCTUnwrap ( paragraphItem? . markDownItems ? [ 2 ] as? LinkMarkDownItem )
250
+ XCTAssertEqual ( linkMarkDownItem2. content, " Google " )
251
+ XCTAssertEqual ( linkMarkDownItem2. url, " https://www.google.com " )
252
+ XCTAssertEqual ( linkMarkDownItem2. title, " a11y title " )
253
+
254
+ let linkMarkDownItem3 = try XCTUnwrap ( paragraphItem? . markDownItems ? [ 4 ] as? LinkMarkDownItem )
255
+ XCTAssertEqual ( linkMarkDownItem3. content, " https://www.apple.com " )
256
+ XCTAssertEqual ( linkMarkDownItem3. url, " https://www.apple.com " )
257
+ XCTAssertEqual ( linkMarkDownItem3. title, " Apple-title " )
258
+ }
157
259
}
0 commit comments