@@ -998,8 +998,42 @@ func TestDiffText(t *testing.T) {
998
998
}
999
999
1000
1000
func TestDiffDelta (t * testing.T ) {
1001
+ type TestCase struct {
1002
+ Name string
1003
+
1004
+ Text string
1005
+ Delta string
1006
+
1007
+ ErrorMessagePrefix string
1008
+ }
1009
+
1001
1010
dmp := New ()
1002
1011
1012
+ for i , tc := range []TestCase {
1013
+ {"Delta shorter than text" , "jumps over the lazyx" , "=4\t -1\t +ed\t =6\t -3\t +a\t =5\t +old dog" , "Delta length (19) is different from source text length (20)" },
1014
+ {"Delta longer than text" , "umps over the lazy" , "=4\t -1\t +ed\t =6\t -3\t +a\t =5\t +old dog" , "Delta length (19) is different from source text length (18)" },
1015
+ {"Invalid URL escaping" , "" , "+%c3%xy" , "invalid URL escape \" %xy\" " },
1016
+ {"Invalid UTF-8 sequence" , "" , "+%c3xy" , "invalid UTF-8 token: \" \\ xc3xy\" " },
1017
+ {"Invalid diff operation" , "" , "a" , "Invalid diff operation in DiffFromDelta: a" },
1018
+ {"Invalid diff syntax" , "" , "-" , "strconv.ParseInt: parsing \" \" : invalid syntax" },
1019
+ {"Negative number in delta" , "" , "--1" , "Negative number in DiffFromDelta: -1" },
1020
+ {"Empty case" , "" , "" , "" },
1021
+ } {
1022
+ diffs , err := dmp .DiffFromDelta (tc .Text , tc .Delta )
1023
+ msg := fmt .Sprintf ("Test case #%d, %s" , i , tc .Name )
1024
+ if tc .ErrorMessagePrefix == "" {
1025
+ assert .Nil (t , err , msg )
1026
+ assert .Nil (t , diffs , msg )
1027
+ } else {
1028
+ e := err .Error ()
1029
+ if strings .HasPrefix (e , tc .ErrorMessagePrefix ) {
1030
+ e = tc .ErrorMessagePrefix
1031
+ }
1032
+ assert .Nil (t , diffs , msg )
1033
+ assert .Equal (t , tc .ErrorMessagePrefix , e , msg )
1034
+ }
1035
+ }
1036
+
1003
1037
// Convert a diff into delta string.
1004
1038
diffs := []Diff {
1005
1039
Diff {DiffEqual , "jump" },
@@ -1021,30 +1055,6 @@ func TestDiffDelta(t *testing.T) {
1021
1055
deltaDiffs , err := dmp .DiffFromDelta (text1 , delta )
1022
1056
assert .Equal (t , diffs , deltaDiffs )
1023
1057
1024
- // Generates error (19 < 20).
1025
- _ , err = dmp .DiffFromDelta (text1 + "x" , delta )
1026
- if err == nil {
1027
- t .Fatal ("Too long." )
1028
- }
1029
-
1030
- // Generates error (19 > 18).
1031
- _ , err = dmp .DiffFromDelta (text1 [1 :], delta )
1032
- if err == nil {
1033
- t .Fatal ("Too short." )
1034
- }
1035
-
1036
- // Generates error (%xy invalid URL escape).
1037
- _ , err = dmp .DiffFromDelta ("" , "+%c3%xy" )
1038
- if err == nil {
1039
- assert .Fail (t , "expected Invalid URL escape." )
1040
- }
1041
-
1042
- // Generates error (invalid utf8).
1043
- _ , err = dmp .DiffFromDelta ("" , "+%c3xy" )
1044
- if err == nil {
1045
- assert .Fail (t , "expected Invalid utf8." )
1046
- }
1047
-
1048
1058
// Test deltas with special characters.
1049
1059
diffs = []Diff {
1050
1060
Diff {DiffEqual , "\u0680 \x00 \t %" },
@@ -1074,29 +1084,6 @@ func TestDiffDelta(t *testing.T) {
1074
1084
deltaDiffs , err = dmp .DiffFromDelta ("" , delta )
1075
1085
assert .Equal (t , diffs , deltaDiffs )
1076
1086
assert .Nil (t , err )
1077
-
1078
- // Test blank tokens.
1079
- _ , err = dmp .DiffFromDelta ("" , "" )
1080
- assert .Nil (t , err )
1081
-
1082
- // Test invalid diff operation "a"
1083
- _ , err = dmp .DiffFromDelta ("" , "a" )
1084
- if err == nil {
1085
- assert .Fail (t , "expected Invalid diff operation." )
1086
- }
1087
-
1088
- // Test non-numeric parameter
1089
- _ , err = dmp .DiffFromDelta ("" , "-" )
1090
- if err == nil {
1091
- assert .Fail (t , "expected Invalid syntax." )
1092
- }
1093
-
1094
- // Test negative parameter
1095
- _ , err = dmp .DiffFromDelta ("" , "--1" )
1096
- if err == nil {
1097
- assert .Fail (t , "expected Negative number." )
1098
- }
1099
-
1100
1087
}
1101
1088
1102
1089
func TestDiffXIndex (t * testing.T ) {
0 commit comments