2016-01-03 21:26:46 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2016-01-31 16:16:29 +00:00
|
|
|
"html/template"
|
2018-01-25 23:30:20 +00:00
|
|
|
"strings"
|
2016-01-31 16:16:29 +00:00
|
|
|
"testing"
|
2017-06-12 15:01:09 +00:00
|
|
|
|
2018-01-25 23:30:20 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
2017-06-12 15:01:09 +00:00
|
|
|
dmp "github.com/sergi/go-diff/diffmatchpatch"
|
2018-01-25 23:30:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-03 21:26:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
|
2016-01-31 16:16:29 +00:00
|
|
|
if s1 != string(s2) {
|
|
|
|
t.Errorf("%s should be equal %s", s2, s1)
|
|
|
|
}
|
2016-01-03 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 12:50:25 +00:00
|
|
|
func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) {
|
2016-01-31 16:16:29 +00:00
|
|
|
if d1 != d2 {
|
|
|
|
t.Errorf("%v should be equal %v", d1, d2)
|
|
|
|
}
|
2016-01-08 12:50:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 19:05:21 +00:00
|
|
|
func TestDiffToHTML(t *testing.T) {
|
2016-08-16 14:45:06 +00:00
|
|
|
assertEqual(t, "+foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
2018-03-29 23:21:27 +00:00
|
|
|
{Type: dmp.DiffEqual, Text: "foo "},
|
|
|
|
{Type: dmp.DiffInsert, Text: "bar"},
|
|
|
|
{Type: dmp.DiffDelete, Text: " baz"},
|
|
|
|
{Type: dmp.DiffEqual, Text: " biz"},
|
2016-11-07 16:24:59 +00:00
|
|
|
}, DiffLineAdd))
|
2016-01-03 21:26:46 +00:00
|
|
|
|
2016-08-16 14:45:06 +00:00
|
|
|
assertEqual(t, "-foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
2018-03-29 23:21:27 +00:00
|
|
|
{Type: dmp.DiffEqual, Text: "foo "},
|
|
|
|
{Type: dmp.DiffDelete, Text: "bar"},
|
|
|
|
{Type: dmp.DiffInsert, Text: " baz"},
|
|
|
|
{Type: dmp.DiffEqual, Text: " biz"},
|
2016-11-07 16:24:59 +00:00
|
|
|
}, DiffLineDel))
|
2016-01-03 21:26:46 +00:00
|
|
|
}
|
2018-01-25 23:30:20 +00:00
|
|
|
|
|
|
|
func benchParsePatch(b *testing.B, diffStr string) {
|
|
|
|
log.DelLogger("console")
|
|
|
|
log.DelLogger("file")
|
|
|
|
b.ResetTimer() //Disable logger for becnh
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ParsePatch(1000, 5000, 100, strings.NewReader(diffStr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkParsePatchSimple(b *testing.B) {
|
|
|
|
benchParsePatch(b, `diff --git a/integrations/api_issue_test.go b/integrations/api_issue_test.go
|
|
|
|
index 74436ffe9..ff316cec3 100644
|
|
|
|
--- a/integrations/api_issue_test.go
|
|
|
|
+++ b/integrations/api_issue_test.go
|
|
|
|
@@ -5,13 +5,13 @@
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
+ "fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
api "code.gitea.io/sdk/gitea"
|
|
|
|
|
|
|
|
- "fmt"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePatch(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
result error
|
|
|
|
files int
|
|
|
|
addition int
|
|
|
|
deletion int
|
|
|
|
diff string
|
|
|
|
}{
|
|
|
|
{nil, 1, 1, 1,
|
|
|
|
`diff --git a/integrations/api_issue_test.go b/integrations/api_issue_test.go
|
|
|
|
index 74436ffe9..ff316cec3 100644
|
|
|
|
--- a/integrations/api_issue_test.go
|
|
|
|
+++ b/integrations/api_issue_test.go
|
|
|
|
@@ -5,13 +5,13 @@
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
+ "fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
api "code.gitea.io/sdk/gitea"
|
|
|
|
|
|
|
|
- "fmt"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
`},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
diff, err := ParsePatch(1000, 5000, 100, strings.NewReader(tc.diff))
|
|
|
|
assert.Equal(t, tc.result, err)
|
|
|
|
assert.Equal(t, tc.files, diff.NumFiles())
|
|
|
|
assert.Equal(t, tc.addition, diff.TotalAddition)
|
|
|
|
assert.Equal(t, tc.deletion, diff.TotalDeletion)
|
|
|
|
}
|
|
|
|
}
|