-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff2html_test.go
64 lines (55 loc) · 1.14 KB
/
diff2html_test.go
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
package diff2html
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pmezard/go-difflib/difflib"
"testing"
)
func Test_GetPrettyHTML(t *testing.T) {
a, _ := json.Marshal(struct {
A string `json:"a"`
B int `json:"b"`
}{
A: "cat",
B: 100,
})
var ao bytes.Buffer
json.Indent(&ao, a, "", " ")
b, _ := json.Marshal(struct {
A string `json:"a"`
B int `json:"b"`
}{
A: "doc",
B: 10,
})
var bo bytes.Buffer
json.Indent(&bo, b, "", " ")
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(ao.String()),
B: difflib.SplitLines(bo.String()),
FromFile: "sample.json",
ToFile: "sample.json",
Context: 3,
}
input, _ := difflib.GetUnifiedDiffString(diff)
fmt.Println(input)
html, err := GetPrettyHTML(input)
fmt.Println(html)
fmt.Println(err)
}
func BenchmarkGetPrettyHTML(b *testing.B) {
diff := "diff --git a/sample b/sample\n" +
"index 0000001..0ddf2ba\n" +
"--- a/sample\n" +
"+++ b/sample\n" +
"@@ -1 +1 @@\n" +
"-test\n" +
"+test1r\n"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
GetPrettyHTML(diff)
}
}
// 5000 233375 ns/op 268966 B/op 758 allocs/op