Fishee/Tests/ParserTests.swift

103 lines
3.4 KiB
Swift
Raw Permalink Normal View History

2024-10-05 13:10:39 -07:00
//
// Copyright © 2024 Salar Rahmanian.
2025-02-09 10:34:15 -08:00
//
2024-10-05 13:10:39 -07:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2025-02-09 10:34:15 -08:00
//
2024-10-05 13:10:39 -07:00
// http://www.apache.org/licenses/LICENSE-2.0
2025-02-09 10:34:15 -08:00
//
2024-10-05 13:10:39 -07:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
2025-02-09 10:34:15 -08:00
2024-10-05 13:10:39 -07:00
import Foundation
import Testing
2025-02-09 10:34:15 -08:00
2024-10-05 13:10:39 -07:00
@testable import Fishee
@Suite(.serialized)
2024-10-05 13:10:39 -07:00
final class ParserTests {
2025-02-09 10:34:15 -08:00
let fishHistoryFile = Bundle.module.path(forResource: "fish_history_test", ofType: "txt")
let historyItem = FishHistoryEntry(
cmd: "cd Projects/Fishee/", when: 1_727_545_693, paths: ["Projects/Fishee/"])
let historyItem2 = FishHistoryEntry(
cmd: "swift package tools-version", when: 1_727_545_709, paths: [])
2025-02-09 10:42:18 -08:00
let filePathforWriteTest = FileManager.default.temporaryDirectory.appendingPathComponent(
2025-02-09 10:34:15 -08:00
"myfile.txt")
2025-02-09 10:42:18 -08:00
let filePathforFileBackupTest = FileManager.default.temporaryDirectory
2025-02-09 10:34:15 -08:00
.appendingPathComponent("myfile_copy.txt")
2024-10-05 13:10:39 -07:00
2025-02-09 10:34:15 -08:00
deinit {
if FileManager.default.fileExists(atPath: filePathforWriteTest.path) {
_ = try? FileManager.default.removeItem(at: filePathforWriteTest)
2024-10-05 13:10:39 -07:00
}
2025-02-09 10:34:15 -08:00
if FileManager.default.fileExists(atPath: filePathforFileBackupTest.path) {
_ = try? FileManager.default.removeItem(at: filePathforFileBackupTest)
2024-10-05 13:10:39 -07:00
}
2025-02-09 10:34:15 -08:00
}
2024-10-05 13:10:39 -07:00
2025-02-09 10:34:15 -08:00
@Test func parseFishHistoryTest() {
#expect(fishHistoryFile != nil)
let fishHistory = parseFishHistory(from: fishHistoryFile!)
#expect(fishHistory!.count > 0)
let expectedHistory = [historyItem, historyItem2]
#expect(fishHistory == expectedHistory)
}
@Test func writeFishHistoryTest() {
let written = writeFishHistory(
to: filePathforWriteTest.path,
history: [historyItem],
backup: false
)
#expect(written)
let fileContent = try? String(contentsOf: filePathforWriteTest, encoding: .utf8)
let expectedEntry = """
- cmd: cd Projects/Fishee/
when: 1727545693
paths:
- Projects/Fishee/
"""
#expect(fileContent == expectedEntry)
// confirm backup functionality is working
#expect(FileManager.default.fileExists(atPath: filePathforWriteTest.path))
let write_again = writeFishHistory(
to: filePathforWriteTest.path,
history: [historyItem],
backup: true
)
#expect(write_again)
#expect(FileManager.default.fileExists(atPath: filePathforFileBackupTest.path))
}
@Test func mergeFishHistoryTest() {
let merged = mergeFishHistory([historyItem], [historyItem2])
#expect(merged.count == 2)
#expect(merged.contains(historyItem))
#expect(merged.contains(historyItem2))
}
@Test func mergeFishHistoryWithDuplicateTest() {
let merged = mergeFishHistory([historyItem], [historyItem, historyItem2])
#expect(merged.count == 3)
#expect(merged.contains(historyItem))
#expect(merged.contains(historyItem2))
}
@Test func mergeFishHistoryRemoveDuplicateTest() {
let merged = mergeFishHistory(
[historyItem], [historyItem, historyItem2], removeDuplicates: true)
#expect(merged.count == 2)
#expect(merged.contains(historyItem))
#expect(merged.contains(historyItem2))
}
2024-10-05 13:10:39 -07:00
}