Skip to content

Commit

Permalink
Add swiftlint to tests (#20)
Browse files Browse the repository at this point in the history
* check for existing links in checkText()

* Remove unnecessary closure parens

* Uses Set for text checking and adds tests

* Adds test case for html generated attributed strings

* Tweaks tests as per feedback from Chris

* Adds swiftlint to test sources and fixes linting errors

* fix linting issues broken from merge conflict

* Fix function duplication from merge conflict
  • Loading branch information
spadafiva authored and chansen22 committed Mar 29, 2019
1 parent 3eb79a7 commit aea75da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ disabled_rules:

included:
- Source
- Nantes/NantesTests

21 changes: 10 additions & 11 deletions Nantes/NantesTests/AttributedStringExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
// Copyright © 2019 Instacart. All rights reserved.
//

import XCTest
@testable import Nantes
import XCTest

final class AttributedStringExtensionTests: XCTestCase {

private let testUrl = URL(string: "https://www.swiftjoe.com")!

func testReturnsLinksIfFoundInAttributedString() {
let linkString = NSAttributedString(string: "Contains a link", attributes: [.link: testUrl])
let existingLinks = linkString.findExistingLinks()
XCTAssertFalse(existingLinks.isEmpty)
}

func testFindsLinksForAttributedStringsCreatedFromHTML() {
let htmlString = "<a href=\"http://www.google.com\">I can be clicked\""
guard let data = htmlString.data(using: .utf8) else {
XCTFail()
XCTFail("Could not get data detector")
return
}
do {
Expand All @@ -33,35 +32,35 @@ final class AttributedStringExtensionTests: XCTestCase {
let existingLinks = attributedString.findExistingLinks()
XCTAssert(existingLinks.count == 1)
} catch {
XCTFail()
XCTFail("Could not get data detector")
}
}

func testReturnsLinkIsEmptyIfNoAttributedLink() {
let linkString = NSAttributedString(string: "Contains no links at all")
let checkingResults = linkString.findExistingLinks()
XCTAssertTrue(checkingResults.isEmpty)
}

func testCheckingResultsIsEmptyIfAttributedStringContainsAttributedLink() {
do {
let detector = try NSDataDetector(types: NantesLabel().enabledTextCheckingTypes.rawValue)
let linkString = NSAttributedString(string: "Contains a link", attributes: [.link: testUrl])
let checkingResults = linkString.findCheckingResults(usingDetector: detector)
XCTAssertTrue(checkingResults.isEmpty)
} catch {
XCTFail()
XCTFail("Could not get data detector")
}
}

func testCheckingResultsContainsValueIfDataFoundInString() {
do {
let detector = try NSDataDetector(types: NantesLabel().enabledTextCheckingTypes.rawValue)
let linkString = NSAttributedString(string: "867-5309")
let checkingResults = linkString.findCheckingResults(usingDetector: detector)
XCTAssert(checkingResults.count == 1)
} catch {
XCTFail()
XCTFail("Could not get data detector")
}
}
}
19 changes: 11 additions & 8 deletions Nantes/NantesTests/NantesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
// Copyright © 2019 Instacart. All rights reserved.
//

import XCTest
@testable import Nantes
import XCTest

// swiftlint:disable force_cast
// swiftlint:disable force_try

final class NantesLabelTests: XCTestCase {
var label: NantesLabel = .init(frame: .zero)
Expand Down Expand Up @@ -215,35 +218,35 @@ final class NantesLabelTests: XCTestCase {
XCTAssertEqual(updatedParagraphStyle.maximumLineHeight, 42)
XCTAssertEqual(updatedKern.intValue, 32)
}

func testExistingAttributedLinksAreAddedAsNantesLinks() {
let testUrl = URL(string: "https://www.swiftjoe.com")!
let linkString = NSAttributedString(string: "Contains a link", attributes: [.link: testUrl])

let promise = expectation(description: "waiting for attributes to be set")
XCTAssertTrue(label.linkModels.isEmpty)
label.attributedText = linkString

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
promise.fulfill()
}

XCTWaiter().wait(for: [promise], timeout: 2)
XCTAssertFalse(label.linkModels.isEmpty)
}

func testMultipleLinksAreAdded() {
let testUrl = URL(string: "https://www.instacart.com")!
let linkString = NSMutableAttributedString(string: "Link 1", attributes: [.link: testUrl])
linkString.append(NSAttributedString(string: "https://www.swiftjoe.com"))
XCTAssertTrue(label.linkModels.isEmpty)
label.attributedText = linkString

let promise = expectation(description: "waiting for attributes to be set")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
promise.fulfill()
}

XCTWaiter().wait(for: [promise], timeout: 2)
XCTAssertTrue(label.linkModels.count == 2)
}
Expand Down

0 comments on commit aea75da

Please sign in to comment.