detect_test.go

  1package diffdetect
  2
  3import "testing"
  4
  5func TestInspect(t *testing.T) {
  6	t.Parallel()
  7
  8	tests := []struct {
  9		name    string
 10		content string
 11		want    Signal
 12	}{
 13		{
 14			name: "git unified diff",
 15			content: `diff --git a/main.go b/main.go
 16--- a/main.go
 17+++ b/main.go
 18@@ -1,2 +1,3 @@
 19 package main
 20+import "fmt"
 21`,
 22			want: Signal{HasHunk: true, HasFileHeader: true, HasGitHeader: true},
 23		},
 24		{
 25			name: "non-git unified diff",
 26			content: `--- old.c
 27+++ old.c
 28@@ -1 +1 @@
 29-old
 30+new
 31`,
 32			want: Signal{HasHunk: true, HasFileHeader: true, HasGitHeader: false},
 33		},
 34		{
 35			name:    "plain text",
 36			content: "hello world",
 37			want:    Signal{},
 38		},
 39		{
 40			name:    "hunk only",
 41			content: "@@ -1 +1 @@\n-old\n+new\n",
 42			want:    Signal{HasHunk: true},
 43		},
 44		{
 45			name:    "headers only",
 46			content: "--- a/file\n+++ b/file\n",
 47			want:    Signal{HasFileHeader: true},
 48		},
 49	}
 50
 51	for _, tt := range tests {
 52		t.Run(tt.name, func(t *testing.T) {
 53			t.Parallel()
 54			got := Inspect(tt.content)
 55			if got != tt.want {
 56				t.Errorf("Inspect() = %+v, want %+v", got, tt.want)
 57			}
 58		})
 59	}
 60}
 61
 62func TestIsUnifiedDiff(t *testing.T) {
 63	t.Parallel()
 64
 65	tests := []struct {
 66		name    string
 67		content string
 68		want    bool
 69	}{
 70		{
 71			name: "github-style multi-file diff",
 72			content: `diff --git a/one.txt b/one.txt
 73--- a/one.txt
 74+++ b/one.txt
 75@@ -1 +1 @@
 76-a
 77+b
 78diff --git a/two.txt b/two.txt
 79--- a/two.txt
 80+++ b/two.txt
 81@@ -1 +1 @@
 82-c
 83+d
 84`,
 85			want: true,
 86		},
 87		{
 88			name: "non-git unified patch",
 89			content: `--- a/old.c
 90+++ b/old.c
 91@@ -1,3 +1,4 @@
 92 #include <stdio.h>
 93-int main() {
 94+int main(int argc, char **argv) {
 95     return 0;
 96 }
 97`,
 98			want: true,
 99		},
100		{
101			name: "new file from dev null",
102			content: `--- /dev/null
103+++ newfile.txt
104@@ -0,0 +1,2 @@
105+hello
106+world
107`,
108			want: true,
109		},
110		{
111			name: "markdown false positive candidate",
112			content: `- Item one
113- Item two
114+ Bonus item
115- Item three
116`,
117			want: false,
118		},
119		{
120			name: "headers without hunk",
121			content: `--- a/somefile.txt
122+++ b/somefile.txt
123Just some content here
124No hunk markers at all
125`,
126			want: false,
127		},
128		{
129			name: "hunk without headers",
130			content: `@@ -1,3 +1,4 @@
131 some line
132-another line
133+changed line
134`,
135			want: false,
136		},
137		{
138			name:    "empty",
139			content: "",
140			want:    false,
141		},
142	}
143
144	for _, tt := range tests {
145		t.Run(tt.name, func(t *testing.T) {
146			t.Parallel()
147			if got := IsUnifiedDiff(tt.content); got != tt.want {
148				t.Errorf("IsUnifiedDiff() = %v, want %v", got, tt.want)
149			}
150		})
151	}
152}