2016-01-04 00:26:46 +03:00
package models
import (
2016-01-31 19:16:29 +03:00
dmp "github.com/sergi/go-diff/diffmatchpatch"
"html/template"
"testing"
2016-01-04 00:26:46 +03:00
)
func assertEqual ( t * testing . T , s1 string , s2 template . HTML ) {
2016-01-31 19:16:29 +03:00
if s1 != string ( s2 ) {
t . Errorf ( "%s should be equal %s" , s2 , s1 )
}
2016-01-04 00:26:46 +03:00
}
2016-01-08 15:50:25 +03:00
func assertLineEqual ( t * testing . T , d1 * DiffLine , d2 * DiffLine ) {
2016-01-31 19:16:29 +03:00
if d1 != d2 {
t . Errorf ( "%v should be equal %v" , d1 , d2 )
}
2016-01-08 15:50:25 +03:00
}
2016-01-09 22:05:21 +03:00
func TestDiffToHTML ( t * testing . T ) {
2016-01-31 19:16:29 +03:00
assertEqual ( t , "foo <span class=\"added-code\">bar</span> biz" , diffToHTML ( [ ] dmp . Diff {
dmp . Diff { dmp . DiffEqual , "foo " } ,
dmp . Diff { dmp . DiffInsert , "bar" } ,
dmp . Diff { dmp . DiffDelete , " baz" } ,
dmp . Diff { dmp . DiffEqual , " biz" } ,
} , DIFF_LINE_ADD ) )
2016-01-04 00:26:46 +03:00
2016-01-31 19:16:29 +03:00
assertEqual ( t , "foo <span class=\"removed-code\">bar</span> biz" , diffToHTML ( [ ] dmp . Diff {
dmp . Diff { dmp . DiffEqual , "foo " } ,
dmp . Diff { dmp . DiffDelete , "bar" } ,
dmp . Diff { dmp . DiffInsert , " baz" } ,
dmp . Diff { dmp . DiffEqual , " biz" } ,
} , DIFF_LINE_DEL ) )
2016-01-04 00:26:46 +03:00
}
2016-01-08 15:50:25 +03:00
// test if GetLine is return the correct lines
func TestGetLine ( t * testing . T ) {
2016-01-31 19:16:29 +03:00
ds := DiffSection { Lines : [ ] * DiffLine {
& DiffLine { LeftIdx : 28 , RightIdx : 28 , Type : DIFF_LINE_PLAIN } ,
& DiffLine { LeftIdx : 29 , RightIdx : 29 , Type : DIFF_LINE_PLAIN } ,
& DiffLine { LeftIdx : 30 , RightIdx : 30 , Type : DIFF_LINE_PLAIN } ,
& DiffLine { LeftIdx : 31 , RightIdx : 0 , Type : DIFF_LINE_DEL } ,
& DiffLine { LeftIdx : 0 , RightIdx : 31 , Type : DIFF_LINE_ADD } ,
& DiffLine { LeftIdx : 0 , RightIdx : 32 , Type : DIFF_LINE_ADD } ,
& DiffLine { LeftIdx : 32 , RightIdx : 33 , Type : DIFF_LINE_PLAIN } ,
& DiffLine { LeftIdx : 33 , RightIdx : 0 , Type : DIFF_LINE_DEL } ,
& DiffLine { LeftIdx : 34 , RightIdx : 0 , Type : DIFF_LINE_DEL } ,
& DiffLine { LeftIdx : 35 , RightIdx : 0 , Type : DIFF_LINE_DEL } ,
& DiffLine { LeftIdx : 36 , RightIdx : 0 , Type : DIFF_LINE_DEL } ,
& DiffLine { LeftIdx : 0 , RightIdx : 34 , Type : DIFF_LINE_ADD } ,
& DiffLine { LeftIdx : 0 , RightIdx : 35 , Type : DIFF_LINE_ADD } ,
& DiffLine { LeftIdx : 0 , RightIdx : 36 , Type : DIFF_LINE_ADD } ,
& DiffLine { LeftIdx : 0 , RightIdx : 37 , Type : DIFF_LINE_ADD } ,
& DiffLine { LeftIdx : 37 , RightIdx : 38 , Type : DIFF_LINE_PLAIN } ,
& DiffLine { LeftIdx : 38 , RightIdx : 39 , Type : DIFF_LINE_PLAIN } ,
} }
2016-01-08 15:50:25 +03:00
2016-01-31 19:16:29 +03:00
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_ADD , 31 ) , ds . Lines [ 4 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_DEL , 31 ) , ds . Lines [ 3 ] )
2016-01-08 15:50:25 +03:00
2016-01-31 19:16:29 +03:00
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_ADD , 33 ) , ds . Lines [ 11 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_ADD , 34 ) , ds . Lines [ 12 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_ADD , 35 ) , ds . Lines [ 13 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_ADD , 36 ) , ds . Lines [ 14 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_DEL , 34 ) , ds . Lines [ 7 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_DEL , 35 ) , ds . Lines [ 8 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_DEL , 36 ) , ds . Lines [ 9 ] )
assertLineEqual ( t , ds . GetLine ( DIFF_LINE_DEL , 37 ) , ds . Lines [ 10 ] )
2016-01-08 15:50:25 +03:00
}