1 // Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
2 // https://github.com/sergi/go-diff
3 // See the included LICENSE file for license details.
5 // go-diff is a Go implementation of Google's Diff, Match, and Patch library
6 // Original library is Copyright (c) 2006 Google Inc.
7 // http://code.google.com/p/google-diff-match-patch/
15 "github.com/stretchr/testify/assert"
18 func TestMatchAlphabet(t *testing.T) {
19 type TestCase struct {
27 for i, tc := range []TestCase{
31 Expected: map[byte]int{
40 Expected: map[byte]int{
47 actual := dmp.MatchAlphabet(tc.Pattern)
48 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
52 func TestMatchBitap(t *testing.T) {
53 type TestCase struct {
64 dmp.MatchDistance = 100
65 dmp.MatchThreshold = 0.5
67 for i, tc := range []TestCase{
68 {"Exact match #1", "abcdefghijk", "fgh", 5, 5},
69 {"Exact match #2", "abcdefghijk", "fgh", 0, 5},
70 {"Fuzzy match #1", "abcdefghijk", "efxhi", 0, 4},
71 {"Fuzzy match #2", "abcdefghijk", "cdefxyhijk", 5, 2},
72 {"Fuzzy match #3", "abcdefghijk", "bxy", 1, -1},
73 {"Overflow", "123456789xx0", "3456789x0", 2, 2},
74 {"Before start match", "abcdef", "xxabc", 4, 0},
75 {"Beyond end match", "abcdef", "defyy", 4, 3},
76 {"Oversized pattern", "abcdef", "xabcdefy", 0, 0},
78 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
79 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
82 dmp.MatchThreshold = 0.4
84 for i, tc := range []TestCase{
85 {"Threshold #1", "abcdefghijk", "efxyhi", 1, 4},
87 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
88 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
91 dmp.MatchThreshold = 0.3
93 for i, tc := range []TestCase{
94 {"Threshold #2", "abcdefghijk", "efxyhi", 1, -1},
96 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
97 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
100 dmp.MatchThreshold = 0.0
102 for i, tc := range []TestCase{
103 {"Threshold #3", "abcdefghijk", "bcdef", 1, 1},
105 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
106 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
109 dmp.MatchThreshold = 0.5
111 for i, tc := range []TestCase{
112 {"Multiple select #1", "abcdexyzabcde", "abccde", 3, 0},
113 {"Multiple select #2", "abcdexyzabcde", "abccde", 5, 8},
115 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
116 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
120 dmp.MatchDistance = 10
122 for i, tc := range []TestCase{
123 {"Distance test #1", "abcdefghijklmnopqrstuvwxyz", "abcdefg", 24, -1},
124 {"Distance test #2", "abcdefghijklmnopqrstuvwxyz", "abcdxxefg", 1, 0},
126 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
127 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
131 dmp.MatchDistance = 1000
133 for i, tc := range []TestCase{
134 {"Distance test #3", "abcdefghijklmnopqrstuvwxyz", "abcdefg", 24, 0},
136 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
137 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
141 func TestMatchMain(t *testing.T) {
142 type TestCase struct {
154 for i, tc := range []TestCase{
155 {"Equality", "abcdef", "abcdef", 1000, 0},
156 {"Null text", "", "abcdef", 1, -1},
157 {"Null pattern", "abcdef", "", 3, 3},
158 {"Exact match", "abcdef", "de", 3, 3},
159 {"Beyond end match", "abcdef", "defy", 4, 3},
160 {"Oversized pattern", "abcdef", "abcdefy", 0, 0},
162 actual := dmp.MatchMain(tc.Text1, tc.Text2, tc.Location)
163 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
166 dmp.MatchThreshold = 0.7
168 for i, tc := range []TestCase{
169 {"Complex match", "I am the very model of a modern major general.", " that berry ", 5, 4},
171 actual := dmp.MatchMain(tc.Text1, tc.Text2, tc.Location)
172 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))