File: gitdiff_test.go

package info (click to toggle)
golang-github-gitleaks-go-gitdiff 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 800 kB
  • sloc: makefile: 2; sh: 1
file content (161 lines) | stat: -rw-r--r-- 3,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package gitdiff

import (
	"strings"
	"testing"
)

func TestTextFragmentValidate(t *testing.T) {
	tests := map[string]struct {
		Fragment TextFragment
		Err      string
	}{
		"oldLines": {
			Fragment: TextFragment{
				OldPosition:     1,
				OldLines:        3,
				NewPosition:     1,
				NewLines:        2,
				LeadingContext:  1,
				TrailingContext: 0,
				LinesAdded:      1,
				LinesDeleted:    1,
				Lines: []Line{
					{Op: OpContext, Line: "line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line 2\n"},
				},
			},
			Err: "2 old lines",
		},
		"newLines": {
			Fragment: TextFragment{
				OldPosition:     1,
				OldLines:        2,
				NewPosition:     1,
				NewLines:        3,
				LeadingContext:  1,
				TrailingContext: 0,
				LinesAdded:      1,
				LinesDeleted:    1,
				Lines: []Line{
					{Op: OpContext, Line: "line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line 2\n"},
				},
			},
			Err: "2 new lines",
		},
		"leadingContext": {
			Fragment: TextFragment{
				OldPosition:     1,
				OldLines:        2,
				NewPosition:     1,
				NewLines:        2,
				LeadingContext:  0,
				TrailingContext: 0,
				LinesAdded:      1,
				LinesDeleted:    1,
				Lines: []Line{
					{Op: OpContext, Line: "line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line 2\n"},
				},
			},
			Err: "1 leading context lines",
		},
		"trailingContext": {
			Fragment: TextFragment{
				OldPosition:     1,
				OldLines:        4,
				NewPosition:     1,
				NewLines:        3,
				LeadingContext:  1,
				TrailingContext: 1,
				LinesAdded:      1,
				LinesDeleted:    2,
				Lines: []Line{
					{Op: OpContext, Line: "line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line 2\n"},
					{Op: OpContext, Line: "line 3\n"},
					{Op: OpDelete, Line: "old line 4\n"},
				},
			},
			Err: "0 trailing context lines",
		},
		"linesAdded": {
			Fragment: TextFragment{
				OldPosition:     1,
				OldLines:        4,
				NewPosition:     1,
				NewLines:        3,
				LeadingContext:  1,
				TrailingContext: 0,
				LinesAdded:      2,
				LinesDeleted:    2,
				Lines: []Line{
					{Op: OpContext, Line: "line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line 2\n"},
					{Op: OpContext, Line: "line 3\n"},
					{Op: OpDelete, Line: "old line 4\n"},
				},
			},
			Err: "1 added lines",
		},
		"linesDeleted": {
			Fragment: TextFragment{
				OldPosition:     1,
				OldLines:        4,
				NewPosition:     1,
				NewLines:        3,
				LeadingContext:  1,
				TrailingContext: 0,
				LinesAdded:      1,
				LinesDeleted:    1,
				Lines: []Line{
					{Op: OpContext, Line: "line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line 2\n"},
					{Op: OpContext, Line: "line 3\n"},
					{Op: OpDelete, Line: "old line 4\n"},
				},
			},
			Err: "2 deleted lines",
		},
		"fileCreation": {
			Fragment: TextFragment{
				OldPosition:     0,
				OldLines:        2,
				NewPosition:     1,
				NewLines:        1,
				LeadingContext:  0,
				TrailingContext: 0,
				LinesAdded:      1,
				LinesDeleted:    2,
				Lines: []Line{
					{Op: OpDelete, Line: "old line 1\n"},
					{Op: OpDelete, Line: "old line 2\n"},
					{Op: OpAdd, Line: "new line\n"},
				},
			},
			Err: "creation fragment",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			err := test.Fragment.Validate()
			if test.Err == "" && err != nil {
				t.Fatalf("unexpected validation error: %v", err)
			}
			if test.Err != "" && err == nil {
				t.Fatal("expected validation error, but got nil")
			}
			if !strings.Contains(err.Error(), test.Err) {
				t.Fatalf("incorrect validation error: %q is not in %q", test.Err, err.Error())
			}
		})
	}
}