-
Notifications
You must be signed in to change notification settings - Fork 284
/
InterlinkResultsControllerTests.swift
135 lines (105 loc) · 4.29 KB
/
InterlinkResultsControllerTests.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
126
127
128
129
130
131
132
133
134
135
import XCTest
@testable import Simplenote
// MARK: - InterlinkResultsController Tests
//
class InterlinkResultsControllerTests: XCTestCase {
/// InMemory Storage!
///
private let storage = MockupStorageManager()
/// InterlinkResultsController
///
private var resultsController: InterlinkResultsController!
// MARK: - Overridden Methods
override func setUp() {
super.setUp()
resultsController = InterlinkResultsController(viewContext: storage.viewContext)
}
override func tearDown() {
super.tearDown()
storage.reset()
}
// MARK: - Tests: Filtering
/// Verifies that only Notes with matching keywords in their title are returned by `searchNotes:byTitleKeyword:`
///
func testSearchNotesByKeywordsOnlyReturnsNotesThatContainTheSpecifiedKeywordInTheirTitle() {
let (matching, _) = insertSampleEntities()
guard let results = resultsController.searchNotes(byTitleKeyword: Settings.sampleMatchingKeyword, excluding: nil) else {
XCTFail()
return
}
XCTAssertEqual(matching, Set(results))
}
/// Verifies that `searchNotes:byTitleKeyword:` limits the output count to the value defined by `maximumNumberOfResults`
///
func testSearchNotesByKeywordsRespectsTheMaximumNumberOfResultsLimit() {
let (_, _) = insertSampleEntities()
resultsController.maximumNumberOfResults = 1
guard let results = resultsController.searchNotes(byTitleKeyword: Settings.sampleMatchingKeyword, excluding: nil) else {
XCTFail()
return
}
XCTAssertEqual(results.count, resultsController.maximumNumberOfResults)
}
/// Verifies that `searchNotes:byTitleKeyword:` excludes the specified entity
///
func testSearchNotesByKeywordsExcludesTheSpecifiedObjectID() {
let (matching, _) = insertSampleEntities()
let excluded = matching.randomElement()!
guard let results = resultsController.searchNotes(byTitleKeyword: Settings.sampleMatchingKeyword, excluding: excluded.objectID) else {
XCTFail()
return
}
XCTAssertFalse(results.contains(excluded))
XCTAssertEqual(results.count, matching.count - 1)
}
/// Verifies that `searchNotes:byTitleKeyword:` is diacritic insensitive with regards of the Note Content
///
func testSearchNotesByKeywordsIsDicriticAndCaseInsensitive() {
let matching = [
storage.insertSampleNote(contents: "DíäcrìtIcs"),
]
storage.save()
guard let results = resultsController.searchNotes(byTitleKeyword: "diacritics", excluding: nil) else {
XCTFail()
return
}
XCTAssertEqual(results.count, matching.count)
}
/// Verifies that `searchNotes:byTitleKeyword:` is diacritic insensitive with a special keyword
///
func testSearchNotesByKeywordsIsDicriticAndCaseInsensitiveWithKeywordUsingSpecialCharacters() {
let matching = [
storage.insertSampleNote(contents: "diacritics"),
]
storage.save()
guard let results = resultsController.searchNotes(byTitleKeyword: "DíäcrìtIcs", excluding: nil) else {
XCTFail()
return
}
XCTAssertEqual(results.count, matching.count)
}
}
// MARK: - Private
//
private extension InterlinkResultsControllerTests {
/// Inserts a collection of sample entities.
///
func insertSampleEntities() -> (matching: Set<Note>, irrelevant: Set<Note>) {
let notesWithKeyword = Set(arrayLiteral:
storage.insertSampleNote(contents: Settings.sampleMatchingKeyword.uppercased() + "Match \n nope"),
storage.insertSampleNote(contents: "Another " + Settings.sampleMatchingKeyword + " \n nope"),
storage.insertSampleNote(contents: "Yet Another " + Settings.sampleMatchingKeyword + " \n nope")
)
let notesWithoutKeyword = Set(arrayLiteral:
storage.insertSampleNote(contents: "nope"),
storage.insertSampleNote(contents: "neither this one \n " + Settings.sampleMatchingKeyword)
)
storage.save()
return (notesWithKeyword, notesWithoutKeyword)
}
}
// MARK: - Constants
//
private enum Settings {
static let sampleMatchingKeyword: String = "match"
}