Files
helder/Tests/HelderCoreTests/Git/CommitMessagePromptTests.swift

74 lines
3.2 KiB
Swift

import Foundation
import HelderCore
import Testing
@Suite struct CommitMessagePromptTests {
private let diff = StagedDiff(stat: " src/a.php | 2 +-\n 1 file changed\n", patch: "diff --git a/src/a.php b/src/a.php\n-old\n+new\n")
@Test("the prompt holds the rules, the three examples verbatim, the stat and the diff")
func promptContents() {
let prompt = CommitMessagePrompt.make(diff)
#expect(prompt.contains("Write ONE git commit message"))
#expect(prompt.contains("\" - \""))
#expect(prompt.contains("Adds a new button on the header to navigate to profile"))
#expect(prompt.contains("Fixed security issue that made it possible to hack a file upload"))
#expect(prompt.contains("Makes sure the register form does not accept R&B fans - Enforces password rules on login form (two things changed)"))
#expect(prompt.contains(" src/a.php | 2 +-\n 1 file changed"))
#expect(prompt.hasSuffix("+new"))
#expect(!prompt.contains("is cut"))
}
@Test("a long diff is cut at the limit and the prompt says so; the stat stays whole")
func cutDiff() {
let stat = (0..<50).map { " file\($0).txt | 1 +" }.joined(separator: "\n")
let long = StagedDiff(stat: stat, patch: String(repeating: "a", count: 90) + "TAIL")
let prompt = CommitMessagePrompt.make(long, limit: 90)
#expect(prompt.contains("cut after 90 characters"))
#expect(prompt.contains(stat))
#expect(prompt.contains(String(repeating: "a", count: 90)))
#expect(!prompt.contains("TAIL"))
}
@Test("a diff exactly at the limit is not cut")
func diffAtLimit() {
let prompt = CommitMessagePrompt.make(StagedDiff(stat: "s", patch: String(repeating: "b", count: 10)), limit: 10)
#expect(!prompt.contains("is cut"))
}
@Test("cleaning takes the first non-empty line and trims it")
func firstLine() {
#expect(CommitMessagePrompt.clean("\n\n Adds a button \nSecond line\n") == "Adds a button")
}
@Test("cleaning strips surrounding quotes and backticks, nested too")
func wrappers() {
#expect(CommitMessagePrompt.clean("\"Fixed the parser\"") == "Fixed the parser")
#expect(CommitMessagePrompt.clean("`'Fixed the parser'`") == "Fixed the parser")
#expect(CommitMessagePrompt.clean("“Fixed the parser”") == "Fixed the parser")
#expect(CommitMessagePrompt.clean("Fixed the \"parser\" bug") == "Fixed the \"parser\" bug")
}
@Test("cleaning skips Markdown fence lines")
func fences() {
#expect(CommitMessagePrompt.clean("```text\nAdds a button\n```") == "Adds a button")
}
@Test("cleaning collapses runs of whitespace")
func whitespace() {
#expect(CommitMessagePrompt.clean("Adds\ta button") == "Adds a button")
}
@Test("nothing usable cleans to nil")
func empty() {
#expect(CommitMessagePrompt.clean("") == nil)
#expect(CommitMessagePrompt.clean(" \n\t\n") == nil)
#expect(CommitMessagePrompt.clean("\"\"") == nil)
}
@Test("an empty patch is an empty diff")
func isEmpty() {
#expect(StagedDiff(stat: "", patch: " \n").isEmpty)
#expect(!diff.isEmpty)
}
}