swift format

This commit is contained in:
Salar Rahmanian 2025-02-09 10:34:15 -08:00
parent f69cec36ac
commit 54edbfc36c
8 changed files with 407 additions and 395 deletions

View file

@ -1,42 +1,44 @@
//
// Copyright © 2024 Salar Rahmanian.
//
//
// 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
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// 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.
//
import Foundation
import Testing
@testable import Fishee
@Suite
final class AlgebraTests {
let historyItem = FishHistoryEntry(cmd: "cd Projects/Fishee/", when: 1727545693, paths: ["Projects/Fishee/"])
let historyItem = FishHistoryEntry(
cmd: "cd Projects/Fishee/", when: 1_727_545_693, paths: ["Projects/Fishee/"])
@Test func dateFromHistoryTest() {
let gotDate = historyItem.getDate()
#expect(gotDate == Date(timeIntervalSince1970: 1_727_545_693))
}
@Test func writeEntryTest() {
let entry = historyItem.writeEntry()
#expect(entry.count > 0)
let expectedEntry = """
- cmd: cd Projects/Fishee/
when: 1727545693
paths:
- Projects/Fishee/
"""
#expect(entry.joined(separator: "\n") == expectedEntry)
}
@Test func dateFromHistoryTest() {
let gotDate = historyItem.getDate()
#expect(gotDate == Date(timeIntervalSince1970: 1727545693))
}
@Test func writeEntryTest() {
let entry = historyItem.writeEntry()
#expect(entry.count > 0)
let expectedEntry = """
- cmd: cd Projects/Fishee/
when: 1727545693
paths:
- Projects/Fishee/
"""
#expect(entry.joined(separator: "\n") == expectedEntry)
}
}

View file

@ -1,48 +1,50 @@
//
// Copyright © 2024 Salar Rahmanian.
//
//
// 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
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// 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.
//
import Foundation
import Testing
@testable import Fishee
@testable import Fishee
@Suite(.serialized)
final class FileHelpersTests {
let filePath = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("myfile.txt")
init() {
try? "this is a test".write(
to: filePath,
atomically: true,
encoding: .utf8
)
}
deinit {
try? FileManager.default.removeItem(at: filePath)
}
@Test(arguments: [
"$HOME/myfile.txt",
"~/myfile.txt",
"\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt"
])
func getPathTest(testPath: String) {
let path = getPath(testPath)
let expected = URL(fileURLWithPath: "\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt")
#expect(path == expected)
}
let filePath = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(
"myfile.txt")
init() {
try? "this is a test".write(
to: filePath,
atomically: true,
encoding: .utf8
)
}
deinit {
try? FileManager.default.removeItem(at: filePath)
}
@Test(arguments: [
"$HOME/myfile.txt",
"~/myfile.txt",
"\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt",
])
func getPathTest(testPath: String) {
let path = getPath(testPath)
let expected = URL(
fileURLWithPath: "\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt")
#expect(path == expected)
}
}

View file

@ -1,77 +1,77 @@
//
// Copyright © 2024 Salar Rahmanian.
//
//
// 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
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// 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.
//
import Foundation
import Testing
@testable import Fishee
@Suite
final class FisheeTests {
@Test func DryRunTest() {
do {
let help = try #require(Fishee.parse(["--dry-run"]) as Fishee)
#expect(help.dryRun)
} catch {
Issue.record("Test failed! \(error)")
}
}
@Test func HistoryFileTest() {
do {
let help = try #require(Fishee.parse(["--history-file", "/tmp/fishtest.txt"]) as Fishee)
#expect(help.fishHistoryLocationStr == "/tmp/fishtest.txt")
} catch {
Issue.record("Test failed! \(error)")
}
@Test func DryRunTest() {
do {
let help = try #require(Fishee.parse(["--dry-run"]) as Fishee)
#expect(help.dryRun)
} catch {
Issue.record("Test failed! \(error)")
}
}
@Test func OutputFileTest() {
do {
let help = try #require(Fishee.parse(["--output-file", "/tmp/fishtest.txt"]) as Fishee)
#expect(help.writeFileStr == "/tmp/fishtest.txt")
} catch {
Issue.record("Test failed! \(error)")
}
@Test func HistoryFileTest() {
do {
let help = try #require(Fishee.parse(["--history-file", "/tmp/fishtest.txt"]) as Fishee)
#expect(help.fishHistoryLocationStr == "/tmp/fishtest.txt")
} catch {
Issue.record("Test failed! \(error)")
}
}
@Test func RemoveDuplicatesTest() {
do {
let help = try #require(Fishee.parse(["--remove-duplicates"]) as Fishee)
#expect(help.removeDuplicates)
} catch {
Issue.record("Test failed! \(error)")
}
@Test func OutputFileTest() {
do {
let help = try #require(Fishee.parse(["--output-file", "/tmp/fishtest.txt"]) as Fishee)
#expect(help.writeFileStr == "/tmp/fishtest.txt")
} catch {
Issue.record("Test failed! \(error)")
}
}
@Test func BackupTest() {
do {
let help = try #require(Fishee.parse(["--backup"]) as Fishee)
#expect(help.backup)
} catch {
Issue.record("Test failed! \(error)")
}
@Test func RemoveDuplicatesTest() {
do {
let help = try #require(Fishee.parse(["--remove-duplicates"]) as Fishee)
#expect(help.removeDuplicates)
} catch {
Issue.record("Test failed! \(error)")
}
}
@Test func NoBackupTest() {
do {
let help = try #require(Fishee.parse(["--no-backup"]) as Fishee)
#expect(!help.backup)
} catch {
Issue.record("Test failed! \(error)")
}
@Test func BackupTest() {
do {
let help = try #require(Fishee.parse(["--backup"]) as Fishee)
#expect(help.backup)
} catch {
Issue.record("Test failed! \(error)")
}
}
@Test func NoBackupTest() {
do {
let help = try #require(Fishee.parse(["--no-backup"]) as Fishee)
#expect(!help.backup)
} catch {
Issue.record("Test failed! \(error)")
}
}
}

View file

@ -1,96 +1,102 @@
//
// Copyright © 2024 Salar Rahmanian.
//
//
// 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
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// 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.
//
import Foundation
import Testing
@testable import Fishee
@Suite(.serialized)
final class ParserTests {
let fishHistoryFile = Bundle.module.path(forResource: "fish_history_test", ofType: "txt")
let historyItem = FishHistoryEntry(cmd: "cd Projects/Fishee/", when: 1727545693, paths: ["Projects/Fishee/"])
let historyItem2 = FishHistoryEntry(cmd: "swift package tools-version", when: 1727545709, paths: [])
let filePathforWriteTest = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("myfile.txt")
let filePathforFileBackupTest = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("myfile_copy.txt")
deinit {
if FileManager.default.fileExists(atPath: filePathforWriteTest.path) {
_ = try? FileManager.default.removeItem(at: filePathforWriteTest)
}
if FileManager.default.fileExists(atPath: filePathforFileBackupTest.path) {
_ = try? FileManager.default.removeItem(at: filePathforFileBackupTest)
}
}
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: [])
let filePathforWriteTest = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(
"myfile.txt")
let filePathforFileBackupTest = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("myfile_copy.txt")
@Test func parseFishHistoryTest() {
#expect(fishHistoryFile != nil)
let fishHistory = parseFishHistory(from: fishHistoryFile!)
#expect(fishHistory!.count > 0)
let expectedHistory = [historyItem, historyItem2]
#expect(fishHistory == expectedHistory)
deinit {
if FileManager.default.fileExists(atPath: filePathforWriteTest.path) {
_ = try? FileManager.default.removeItem(at: filePathforWriteTest)
}
@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))
if FileManager.default.fileExists(atPath: filePathforFileBackupTest.path) {
_ = try? FileManager.default.removeItem(at: filePathforFileBackupTest)
}
}
@Test func mergeFishHistoryRemoveDuplicateTest() {
let merged = mergeFishHistory([historyItem], [historyItem, historyItem2], removeDuplicates: true)
#expect(merged.count == 2)
#expect(merged.contains(historyItem))
#expect(merged.contains(historyItem2))
}
@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))
}
}