forked from go-openapi/testify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifflib_test.go
More file actions
91 lines (80 loc) · 2.06 KB
/
difflib_test.go
File metadata and controls
91 lines (80 loc) · 2.06 KB
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
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package difflib
import (
"bytes"
"strings"
"testing"
)
func TestSplitLines(t *testing.T) {
lines := SplitLines("a\nb\nc\n")
if len(lines) != 4 {
t.Fatalf("expected 4 lines, got %d: %q", len(lines), lines)
}
if lines[0] != "a\n" || lines[1] != "b\n" || lines[2] != "c\n" {
t.Errorf("unexpected split result: %q", lines)
}
}
func TestSplitLinesEmpty(t *testing.T) {
lines := SplitLines("")
if len(lines) != 1 {
t.Errorf("expected 1 line for empty string, got %d: %q", len(lines), lines)
}
}
func TestGetUnifiedDiffString(t *testing.T) {
diff := UnifiedDiff{
A: SplitLines("a\nb\nc\n"),
B: SplitLines("a\nB\nc\n"),
FromFile: "original",
ToFile: "modified",
Context: 1,
}
result, err := GetUnifiedDiffString(diff)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(result, "---") {
t.Errorf("expected unified diff header, got: %s", result)
}
if !strings.Contains(result, "-b") {
t.Errorf("expected removed line '-b', got: %s", result)
}
if !strings.Contains(result, "+B") {
t.Errorf("expected added line '+B', got: %s", result)
}
}
func TestGetUnifiedDiffStringIdentical(t *testing.T) {
lines := SplitLines("a\nb\nc\n")
diff := UnifiedDiff{
A: lines,
B: lines,
}
result, err := GetUnifiedDiffString(diff)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != "" {
t.Errorf("expected empty diff for identical inputs, got: %s", result)
}
}
func TestWriteUnifiedDiff(t *testing.T) {
diff := UnifiedDiff{
A: SplitLines("a\nb\nc\n"),
B: SplitLines("a\nB\nc\n"),
FromFile: "original",
ToFile: "modified",
Context: 1,
}
var buf bytes.Buffer
err := WriteUnifiedDiff(&buf, diff)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
result := buf.String()
if !strings.Contains(result, "-b") {
t.Errorf("expected removed line '-b', got: %s", result)
}
if !strings.Contains(result, "+B") {
t.Errorf("expected added line '+B', got: %s", result)
}
}