Skip to content

Commit

Permalink
Update Sorter tests for 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Sep 4, 2024
1 parent 1760b2b commit 5ae8b77
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions MaccyTests/SorterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Defaults

class SorterTests: XCTestCase {
let savedPinTo = Defaults[.pinTo]
let sorter = Sorter()

var item1: HistoryItem!
var item2: HistoryItem!
var item3: HistoryItem!

@MainActor
override func setUp() {
CoreDataManager.inMemory = true
super.setUp()
item1 = historyItem(value: "foo", firstCopiedAt: -300, lastCopiedAt: -100, numberOfCopies: 3)
item2 = historyItem(value: "bar", firstCopiedAt: -400, lastCopiedAt: -300, numberOfCopies: 2)
Expand All @@ -19,47 +20,48 @@ class SorterTests: XCTestCase {

override func tearDown() {
super.tearDown()
CoreDataManager.inMemory = false
Defaults[.pinTo] = savedPinTo
}

func testSortByLastCopiedAt() {
let sorter = Sorter(by: .lastCopiedAt)
XCTAssertEqual(sorter.sort([item1, item2, item3]), [item1, item3, item2])
XCTAssertEqual(sorter.sort([item1, item2, item3], by: .lastCopiedAt), [item1, item3, item2])
}

func testSortByFirstCopiedAt() {
let sorter = Sorter(by: .firstCopiedAt)
XCTAssertEqual(sorter.sort([item1, item2, item3]), [item3, item1, item2])
XCTAssertEqual(sorter.sort([item1, item2, item3], by: .firstCopiedAt), [item3, item1, item2])
}

func testSortByNumberOfCopies() {
let sorter = Sorter(by: .numberOfCopies)
XCTAssertEqual(sorter.sort([item1, item2, item3]), [item1, item2, item3])
XCTAssertEqual(sorter.sort([item1, item2, item3], by: .numberOfCopies), [item1, item2, item3])
}

func testSortByPinToTop() {
Defaults[.pinTo] = .top

item1.pin = "a"
item3.pin = "b"
let sorter = Sorter(by: .lastCopiedAt)
XCTAssertEqual(sorter.sort([item1, item2, item3]), [item1, item3, item2])
XCTAssertEqual(sorter.sort([item1, item2, item3], by: .lastCopiedAt), [item1, item3, item2])
}

func testSortByPinToBottom() {
Defaults[.pinTo] = .bottom

item1.pin = "a"
item3.pin = "b"
let sorter = Sorter(by: .lastCopiedAt)
XCTAssertEqual(sorter.sort([item1, item2, item3]), [item2, item1, item3])
XCTAssertEqual(sorter.sort([item1, item2, item3], by: .lastCopiedAt), [item2, item1, item3])
}

private func historyItem(value: String, firstCopiedAt: Int,
lastCopiedAt: Int, numberOfCopies: Int) -> HistoryItem {
let content = HistoryItemContent(type: "", value: value.data(using: .utf8)!)
let item = HistoryItem(contents: [content])
@MainActor
private func historyItem(
value: String,
firstCopiedAt: Int,
lastCopiedAt: Int,
numberOfCopies: Int
) -> HistoryItem {
let contents = [HistoryItemContent(type: "", value: value.data(using: .utf8)!)]
let item = HistoryItem()
Storage.shared.context.insert(item)
item.contents = contents
item.firstCopiedAt = Date(timeIntervalSinceNow: TimeInterval(firstCopiedAt))
item.lastCopiedAt = Date(timeIntervalSinceNow: TimeInterval(lastCopiedAt))
item.numberOfCopies = numberOfCopies
Expand Down

0 comments on commit 5ae8b77

Please sign in to comment.