Files
helder/Tests/HelderCoreTests/Git/GitCommitLogTests.swift
T

58 lines
2.3 KiB
Swift

import Foundation
import HelderCore
import Testing
@Suite struct GitCommitLogTests {
@Test("each record splits into hash, subject, author and date, newest first")
func commits() {
let raw = "a1b2c3d\0fix the parser\0Jonathan van Rij\01767225600\u{1e}\n0123abc\0first\0Zoë\01700000000\u{1e}\n"
#expect(GitCommitLogParsing.commits(raw) == [
GitCommitSummary(hash: "a1b2c3d", subject: "fix the parser", author: "Jonathan van Rij",
date: Date(timeIntervalSince1970: 1_767_225_600)),
GitCommitSummary(hash: "0123abc", subject: "first", author: "Zoë", date: Date(timeIntervalSince1970: 1_700_000_000)),
])
}
@Test("a subject with quotes, tabs, unicode and separators stays whole")
func oddSubject() throws {
let subject = "feat: \"quotes\" · tab\there | pipe %s %x00 — émoji 🚀"
let commit = try #require(GitCommitLogParsing.commits("0123abc\0\(subject)\0Zoë\01700000000\u{1e}\n").first)
#expect(commit.subject == subject)
#expect(commit.author == "Zoë")
}
@Test("an empty subject still parses")
func emptySubject() {
#expect(GitCommitLogParsing.commits("0123abc\0\0Zoë\01700000000\u{1e}\n").first?.subject == "")
}
@Test("a malformed record is skipped, the rest stays")
func malformedRecord() {
let raw = "abc\0subject\0author\0soon\u{1e}\n0123abc\0ok\0Zoë\01700000000\u{1e}\n"
#expect(GitCommitLogParsing.commits(raw).map(\.hash) == ["0123abc"])
}
@Test("an empty repository prints nothing, which is no commits", arguments: ["", "\n", "garbage", "\u{1e}\n"])
func noCommits(raw: String) {
#expect(GitCommitLogParsing.commits(raw).isEmpty)
}
@Test("relative time steps from seconds to years", arguments: [
(0, "just now"),
(59, "just now"),
(60, "1 min ago"),
(3_540, "59 min ago"),
(10_800, "3 h ago"),
(86_400, "1 d ago"),
(518_400, "6 d ago"),
(1_209_600, "2 w ago"),
(5_184_000, "2 mo ago"),
(69_120_000, "2 y ago"),
(-120, "just now"),
] as [(TimeInterval, String)])
func relativeTime(age: TimeInterval, text: String) {
let now = Date(timeIntervalSince1970: 1_800_000_000)
#expect(RelativeTime.text(now.addingTimeInterval(-age), now: now) == text)
}
}