-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve LinkRule to accept titles and ignore internal markdown (#108)
* Add Title support to LinkRule * Adds support for links like: `[Google](http://www.google.com "custom title")` Co-authored-by: Edwin Veger <[email protected]>
- Loading branch information
1 parent
23eb0f5
commit 0c6f2c1
Showing
16 changed files
with
199 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// | ||
// Created by Maren Osnabrug on 10-05-16. | ||
// Copyright © 2016 M2mobi. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import markymark | ||
|
||
class LinkRuleTests: XCTestCase { | ||
|
||
var sut: LinkRule! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
sut = LinkRule() | ||
} | ||
|
||
func testRecognizesLines() { | ||
XCTAssertTrue(sut.recognizesLines(["[Alt text](image.png)"])) | ||
XCTAssertFalse((sut.recognizesLines(["![Alt text](image.png)"]))) | ||
XCTAssertTrue(sut.recognizesLines([#"[Alt text](image.png "some title")"#])) | ||
} | ||
|
||
func test_DoesNotRecognizeLines_When_PrefixedWithExclamationMark() { | ||
XCTAssertFalse((sut.recognizesLines(["![Alt text](image.png)"]))) | ||
XCTAssertFalse((sut.recognizesLines(["! [Alt text](image.png)"]))) | ||
XCTAssertFalse((sut.recognizesLines([#"![Alt text](image.png "some title")"#]))) | ||
} | ||
|
||
func test_GetAllMatchesReturnsZero_When_InvalidInput() { | ||
// Assert | ||
XCTAssert(sut.getAllMatches(["(https://www.google.com)"]).isEmpty) | ||
XCTAssert(sut.getAllMatches(["[Google]"]).isEmpty) | ||
XCTAssert(sut.getAllMatches(["![Google](https://www.google.com)"]).isEmpty) | ||
} | ||
|
||
func testCreateMarkDownItemWithLinesCreatesCorrectItem() { | ||
// Act | ||
let markDownItem = sut.createMarkDownItemWithLines(["[Google](http://www.google.com)"]) | ||
|
||
// Assert | ||
XCTAssert(markDownItem is LinkMarkDownItem) | ||
} | ||
|
||
func testCreateMarkDownItemContainsCorrectLink() { | ||
// Act | ||
let markDownItem = sut.createMarkDownItemWithLines(["[Google](http://www.google.com)"]) | ||
let markDownItem2 = sut.createMarkDownItemWithLines(["[Youtube](http://www.youtube.com)"]) | ||
|
||
// Assert | ||
XCTAssertEqual((markDownItem as! LinkMarkDownItem).content, "Google") | ||
XCTAssertEqual((markDownItem as! LinkMarkDownItem).url, "http://www.google.com") | ||
XCTAssertEqual((markDownItem2 as! LinkMarkDownItem).content, "Youtube") | ||
XCTAssertEqual((markDownItem2 as! LinkMarkDownItem).url, "http://www.youtube.com") | ||
} | ||
|
||
func testGetAllMatches() { | ||
// Arrange | ||
let expectedMatchesRange = NSRange(location: 0, length: 32) | ||
let expectedMatchesRange2 = NSRange(location: 38, length: 32) | ||
|
||
// Act + Assert | ||
XCTAssertEqual(sut.getAllMatches(["[Google]"]).count, 0) | ||
XCTAssertEqual(sut.getAllMatches(["(https://www.google.com)"]).count, 0) | ||
XCTAssertEqual(sut.getAllMatches(["[Google](https://www.google.com)"]), [expectedMatchesRange]) | ||
XCTAssertEqual(sut.getAllMatches(["![Google](https://www.google.com)"]).count, 0) | ||
XCTAssertEqual( | ||
sut.getAllMatches(["[Google](https://www.google.com) test [Google](https://www.google.com)"]), | ||
[expectedMatchesRange, expectedMatchesRange2] | ||
) | ||
|
||
XCTAssertEqual( | ||
sut.getAllMatches([#"[Google](https://www.google.com) test [Google](https://www.google.com "a11y title")"#]), | ||
[ | ||
NSRange(location: 0, length: 32), | ||
NSRange(location: 38, length: 45) | ||
] | ||
) | ||
} | ||
|
||
func test_ParsesItem_When_InputMatches() throws { | ||
// Arrange | ||
let cases: [(String, String, String, UInt)] = [ | ||
( | ||
#"[Google plain link](https://google.com)"#, | ||
"Google plain link", | ||
"https://google.com", | ||
#line | ||
), | ||
( | ||
#"[Google w/ title](http://www.google.com "with custom title")"#, | ||
"Google w/ title", | ||
"http://www.google.com", | ||
#line | ||
), | ||
( | ||
#"[Simple link](https://google.nl)"#, | ||
"Simple link", | ||
"https://google.nl", | ||
#line | ||
), | ||
( | ||
#"Inside [another link](http://m2mobi.com/) sentence"#, | ||
"another link", | ||
"http://m2mobi.com/", | ||
#line | ||
), | ||
( | ||
#"Inside [another link](http://m2mobi.com/ "with custom title") sentence"#, | ||
"another link", | ||
"http://m2mobi.com/", | ||
#line | ||
), | ||
( | ||
#"[Underscored link](https://google.nl/?param=a_b_c)"#, | ||
"Underscored link", | ||
"https://google.nl/?param=a_b_c", | ||
#line | ||
), | ||
( | ||
#"[Underscored link 2](https://google.nl/?param=a_b_c&d=e)"#, | ||
"Underscored link 2", | ||
"https://google.nl/?param=a_b_c&d=e", | ||
#line | ||
), | ||
( | ||
#"[Underscored link 4](https://google.nl/?param=a_b_c&d=e_f_g)"#, | ||
"Underscored link 4", | ||
"https://google.nl/?param=a_b_c&d=e_f_g", | ||
#line | ||
) | ||
] | ||
|
||
for (input, content, url, line) in cases { | ||
// Act | ||
let output = sut.createMarkDownItemWithLines([input]) | ||
|
||
// Assert | ||
let linkMarkDownItem = try XCTUnwrap(output as? LinkMarkDownItem) | ||
XCTAssertEqual(linkMarkDownItem.content, content, line: line) | ||
XCTAssertEqual(linkMarkDownItem.url, url, line: line) | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.