@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
dmp "github.com/sergi/go-diff/diffmatchpatch"
dmp "github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
@ -23,7 +24,7 @@ import (
func assertEqual ( t * testing . T , s1 string , s2 template . HTML ) {
func assertEqual ( t * testing . T , s1 string , s2 template . HTML ) {
if s1 != string ( s2 ) {
if s1 != string ( s2 ) {
t . Errorf ( "%s should be equal %s" , s2 , s1 )
t . Errorf ( "Did not receive expected results:\nExpected: %s\nActual: %s" , s1 , s2 )
}
}
}
}
@ -61,7 +62,7 @@ func TestDiffToHTML(t *testing.T) {
{ Type : dmp . DiffEqual , Text : "</span><span class=\"p\">)</span>" } ,
{ Type : dmp . DiffEqual , Text : "</span><span class=\"p\">)</span>" } ,
} , DiffLineDel ) )
} , DiffLineDel ) )
assertEqual ( t , "<span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nf\">WrapperRenderer</span><span class=\"p\">(</span><span class=\"nx\">w</span><span class=\"p\">,</span> <span class=\"removed-code\"><span class=\"nx\">language</span></span><span class=\"removed-code\">< span class=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs</span></span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" , diffToHTML ( "" , [ ] dmp . Diff {
assertEqual ( t , "<span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nf\">WrapperRenderer</span><span class=\"p\">(</span><span class=\"nx\">w</span><span class=\"p\">,</span> <span class=\"removed-code\"><span class=\"nx\">language</span><span class=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs</span></span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" , diffToHTML ( "" , [ ] dmp . Diff {
{ Type : dmp . DiffEqual , Text : "<span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nf\">WrapperRenderer</span><span class=\"p\">(</span><span class=\"nx\">w</span><span class=\"p\">,</span> <span class=\"nx\">" } ,
{ Type : dmp . DiffEqual , Text : "<span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nf\">WrapperRenderer</span><span class=\"p\">(</span><span class=\"nx\">w</span><span class=\"p\">,</span> <span class=\"nx\">" } ,
{ Type : dmp . DiffDelete , Text : "language</span><span " } ,
{ Type : dmp . DiffDelete , Text : "language</span><span " } ,
{ Type : dmp . DiffEqual , Text : "c" } ,
{ Type : dmp . DiffEqual , Text : "c" } ,
@ -69,14 +70,14 @@ func TestDiffToHTML(t *testing.T) {
{ Type : dmp . DiffEqual , Text : "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" } ,
{ Type : dmp . DiffEqual , Text : "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" } ,
} , DiffLineDel ) )
} , DiffLineDel ) )
assertEqual ( t , "<span class=\"added-code\">language</span></span><span class=\"added-code\">< span class=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs</span></span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" , diffToHTML ( "" , [ ] dmp . Diff {
assertEqual ( t , "<span class=\"added-code\">language</span><span class=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs</span></span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" , diffToHTML ( "" , [ ] dmp . Diff {
{ Type : dmp . DiffInsert , Text : "language</span><span " } ,
{ Type : dmp . DiffInsert , Text : "language</span><span " } ,
{ Type : dmp . DiffEqual , Text : "c" } ,
{ Type : dmp . DiffEqual , Text : "c" } ,
{ Type : dmp . DiffInsert , Text : "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs" } ,
{ Type : dmp . DiffInsert , Text : "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs" } ,
{ Type : dmp . DiffEqual , Text : "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" } ,
{ Type : dmp . DiffEqual , Text : "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>" } ,
} , DiffLineAdd ) )
} , DiffLineAdd ) )
assertEqual ( t , "<span class=\"k\">print</span><span class=\"added-code\"></span><span class=\"added-code\">< span class=\"p\">(</span></span><span class=\"sa\"></span><span class=\"s2\">"</span><span class=\"s2\">// </span><span class=\"s2\">"</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span><span class=\"added-code\"><span class=\"p\">)</span></span>" , diffToHTML ( "" , [ ] dmp . Diff {
assertEqual ( t , "<span class=\"k\">print</span><span class=\"added-code\"><span class=\"p\">(</span></span><span class=\"sa\"></span><span class=\"s2\">"</span><span class=\"s2\">// </span><span class=\"s2\">"</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span><span class=\"added-code\"><span class=\"p\">)</span></span>" , diffToHTML ( "" , [ ] dmp . Diff {
{ Type : dmp . DiffEqual , Text : "<span class=\"k\">print</span>" } ,
{ Type : dmp . DiffEqual , Text : "<span class=\"k\">print</span>" } ,
{ Type : dmp . DiffInsert , Text : "<span" } ,
{ Type : dmp . DiffInsert , Text : "<span" } ,
{ Type : dmp . DiffEqual , Text : " " } ,
{ Type : dmp . DiffEqual , Text : " " } ,
@ -85,14 +86,14 @@ func TestDiffToHTML(t *testing.T) {
{ Type : dmp . DiffInsert , Text : "<span class=\"p\">)</span>" } ,
{ Type : dmp . DiffInsert , Text : "<span class=\"p\">)</span>" } ,
} , DiffLineAdd ) )
} , DiffLineAdd ) )
assertEqual ( t , "sh <span class=\"added-code\">'useradd -u $(stat -c "%u" .gitignore) jenkins< /span>' " , diffToHTML ( "" , [ ] dmp . Diff {
assertEqual ( t , "sh <span class=\"added-code\">'useradd -u $(stat -c "%u" .gitignore) jenkins'< /span> " , diffToHTML ( "" , [ ] dmp . Diff {
{ Type : dmp . DiffEqual , Text : "sh " } ,
{ Type : dmp . DiffEqual , Text : "sh " } ,
{ Type : dmp . DiffDelete , Text : "4;useradd -u 111 jenkins"" } ,
{ Type : dmp . DiffDelete , Text : "4;useradd -u 111 jenkins"" } ,
{ Type : dmp . DiffInsert , Text : "9;useradd -u $(stat -c "%u" .gitignore) jenkins'" } ,
{ Type : dmp . DiffInsert , Text : "9;useradd -u $(stat -c "%u" .gitignore) jenkins'" } ,
{ Type : dmp . DiffEqual , Text : ";" } ,
{ Type : dmp . DiffEqual , Text : ";" } ,
} , DiffLineAdd ) )
} , DiffLineAdd ) )
assertEqual ( t , "<span class=\"x\"> <h<span class=\"added-code\">4 class=</span><span class=\"added-code\">& #34;release-list-title df ac"</span>></span>" , diffToHTML ( "" , [ ] dmp . Diff {
assertEqual ( t , "<span class=\"x\"> <h<span class=\"added-code\">4 class=& #34;release-list-title df ac"</span>></span>" , diffToHTML ( "" , [ ] dmp . Diff {
{ Type : dmp . DiffEqual , Text : "<span class=\"x\"> <h" } ,
{ Type : dmp . DiffEqual , Text : "<span class=\"x\"> <h" } ,
{ Type : dmp . DiffInsert , Text : "4 class=&#" } ,
{ Type : dmp . DiffInsert , Text : "4 class=&#" } ,
{ Type : dmp . DiffEqual , Text : "3" } ,
{ Type : dmp . DiffEqual , Text : "3" } ,
@ -462,3 +463,14 @@ func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
}
}
}
}
}
}
func TestDiffToHTML_14231 ( t * testing . T ) {
setting . Cfg = ini . Empty ( )
diffRecord := diffMatchPatch . DiffMain ( highlight . Code ( "main.v" , " run()\n" ) , highlight . Code ( "main.v" , " run(db)\n" ) , true )
diffRecord = diffMatchPatch . DiffCleanupEfficiency ( diffRecord )
expected := ` <span class="n">run</span><span class="added-code"><span class="o">(</span><span class="n">db</span></span><span class="o">)</span> `
output := diffToHTML ( "main.v" , diffRecord , DiffLineAdd )
assertEqual ( t , expected , output )
}