forked from vtourraine/AcknowList
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AcknowParserTests.swift
125 lines (109 loc) · 4.61 KB
/
AcknowParserTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// AcknowParserTests.swift
// AcknowExampleTests
//
// Created by Vincent Tourraine on 15/08/15.
// Copyright © 2015-2020 Vincent Tourraine. All rights reserved.
//
import UIKit
import XCTest
import AcknowList
class AcknowParserTests: XCTestCase {
func testHeaderFooter() {
let bundle = Bundle(for: AcknowParserTests.self)
let path = bundle.path(forResource: "Pods-acknowledgements", ofType: "plist")
if let path = path {
let parser = AcknowParser(plistPath: path)
XCTAssertNotNil(parser)
let headerFooter = parser.parseHeaderAndFooter()
if let header = headerFooter.header,
let footer = headerFooter.footer {
XCTAssertEqual(header, "This application makes use of the following third party libraries:")
XCTAssertEqual(footer, "Generated by CocoaPods - https://cocoapods.org")
}
else {
XCTAssert(false, "Header/footer not found")
}
}
else {
XCTAssert(false, "Plist not found")
}
}
func testAcknowledgements() {
let bundle = Bundle(for: AcknowParserTests.self)
let path = bundle.path(forResource: "Pods-acknowledgements", ofType: "plist")
if let path = path {
let parser = AcknowParser(plistPath: path)
XCTAssertNotNil(parser)
let acknowledgements = parser.parseAcknowledgements()
XCTAssertEqual(acknowledgements.count, 1)
let acknow = acknowledgements.first
if let acknow = acknow {
XCTAssertEqual(acknow.title, "AcknowList")
XCTAssertTrue(acknow.text.hasPrefix("Copyright (c) 2015-2019 Vincent Tourraine (http://www.vtourraine.net)"))
}
else {
XCTAssert(false, "Acknowledgement not found")
}
}
else {
XCTAssert(false, "Plist not found")
}
}
// To test the somewhat complicated extraneous-newline-removing regex, I have:
//
// (1) Made a temporary project and installed the 5 most popular pods that
// had no dependencies (loosely based on https://trendingcocoapods.github.io -
// scroll down to the "Top CocoaPods" section). I skipped pods with duplicate
// licenses.
//
// Ultimately, I installed: TYPFontAwesome (SIL OFL 1.1), pop (BSD),
// Alamofire (MIT), Charts (Apache 2), and TPKeyboardAvoiding (zLib)
//
// (2) Copied the acknowledgements file over to Pods-acknowledgements-RegexTesting.plist
//
// (3) Created this test, which parses the plist and applies the regex, then
// verifies that the generated strings are correct verus a manually edited
// "ground truth" text file.
func testFilterOutPrematureLineBreaks() {
let bundle = Bundle(for: AcknowParserTests.self)
let plistPath = bundle.path(forResource: "Pods-acknowledgements-RegexTesting", ofType: "plist")
if let plistPath = plistPath {
let parser = AcknowParser(plistPath: plistPath)
XCTAssertNotNil(parser)
let acknowledgements = parser.parseAcknowledgements()
XCTAssertEqual(acknowledgements.count, 5)
// For each acknowledgement, load the ground truth and compare...
for acknowledgement in acknowledgements {
let groundTruthPath = bundle.url(forResource: "RegexTesting-GroundTruth-\(acknowledgement.title)", withExtension: "txt")
do {
let groundTruth = try String(contentsOf: groundTruthPath!, encoding: .utf8)
XCTAssertEqual(acknowledgement.text, groundTruth)
}
catch {
XCTFail("Cannot load ground truth")
}
}
}
else {
XCTAssert(false, "Plist not found")
}
}
func testGeneralPerformance() {
let bundle = Bundle(for: AcknowParserTests.self)
let path = bundle.path(forResource: "Pods-AcknowExampleTests-acknowledgements", ofType: "plist")
self.measure() {
if let path = path {
let parser = AcknowParser(plistPath: path)
_ = parser.parseHeaderAndFooter()
_ = parser.parseAcknowledgements()
}
}
}
func testParseNonExistentFile() {
let parser = AcknowParser(plistPath: "/404")
XCTAssertNil(parser.parseHeaderAndFooter().header)
XCTAssertNil(parser.parseHeaderAndFooter().footer)
XCTAssertTrue(parser.parseAcknowledgements().isEmpty)
}
}