mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
Add final tests for git header parsing functions
This gets 100% test coverage for the functions involved in parsing Git-style file headers. Whether that actually makes it correct remains to be seen.
This commit is contained in:
+1
-1
@@ -252,7 +252,7 @@ func parseGitHeaderName(header string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if header[n] == ' ' || header[n] == '\t' {
|
||||
if n < len(header) && (header[n] == ' ' || header[n] == '\t') {
|
||||
n++
|
||||
}
|
||||
|
||||
|
||||
@@ -288,6 +288,10 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
Line: "--- a/dir/file.txt\n",
|
||||
Err: true,
|
||||
},
|
||||
"oldFileNameMissing": {
|
||||
Line: "--- \n",
|
||||
Err: true,
|
||||
},
|
||||
"newFileName": {
|
||||
Line: "+++ b/dir/file.txt\n",
|
||||
OutputFile: &File{
|
||||
@@ -325,6 +329,10 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
Line: "+++ b/dir/file.txt\n",
|
||||
Err: true,
|
||||
},
|
||||
"newFileNameMissing": {
|
||||
Line: "+++ \n",
|
||||
Err: true,
|
||||
},
|
||||
"oldMode": {
|
||||
Line: "old mode 100644\n",
|
||||
OutputFile: &File{
|
||||
@@ -403,6 +411,10 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
Score: 0,
|
||||
},
|
||||
},
|
||||
"similarityIndexInvalid": {
|
||||
Line: "similarity index 12ab\n",
|
||||
Err: true,
|
||||
},
|
||||
"indexFullSHA1AndMode": {
|
||||
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14..04fab916d8f938173cbb8b93469855f0e838f098 100644\n",
|
||||
OutputFile: &File{
|
||||
@@ -426,6 +438,10 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
OldMode: os.FileMode(0100644),
|
||||
},
|
||||
},
|
||||
"indexInvalid": {
|
||||
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14\n",
|
||||
Err: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, test := range tests {
|
||||
@@ -455,3 +471,47 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGitHeaderName(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
Input string
|
||||
Output string
|
||||
Err bool
|
||||
}{
|
||||
"twoMatchingNames": {
|
||||
Input: "a/dir/file.txt b/dir/file.txt",
|
||||
Output: "dir/file.txt",
|
||||
},
|
||||
"twoDifferentNames": {
|
||||
Input: "a/dir/foo.txt b/dir/bar.txt",
|
||||
Output: "",
|
||||
},
|
||||
"missingSecondName": {
|
||||
Input: "a/dir/foo.txt",
|
||||
Err: true,
|
||||
},
|
||||
"invalidName": {
|
||||
Input: `"a/dir/file.txt b/dir/file.txt`,
|
||||
Err: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, test := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
output, err := parseGitHeaderName(test.Input)
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatalf("expected error parsing header name, but got nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error parsing header name: %v", err)
|
||||
}
|
||||
|
||||
if output != test.Output {
|
||||
t.Errorf("incorrect output: expected %q, actual %q", test.Output, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user