| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package batch |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestBatchSplit(t *testing.T) { |
| 13 |
type testItem struct { |
| 14 |
Sql string |
| 15 |
Expect []string |
| 16 |
} |
| 17 |
|
| 18 |
list := []testItem{ |
| 19 |
testItem{ |
| 20 |
Sql: `use DB |
| 21 |
go |
| 22 |
select 1 |
| 23 |
go |
| 24 |
select 2 |
| 25 |
`, |
| 26 |
Expect: []string{`use DB |
| 27 |
`, ` |
| 28 |
select 1 |
| 29 |
`, ` |
| 30 |
select 2 |
| 31 |
`, |
| 32 |
}, |
| 33 |
}, |
| 34 |
testItem{ |
| 35 |
Sql: `go |
| 36 |
use DB go |
| 37 |
`, |
| 38 |
Expect: []string{` |
| 39 |
use DB go |
| 40 |
`, |
| 41 |
}, |
| 42 |
}, |
| 43 |
testItem{ |
| 44 |
Sql: `select 'It''s go time' |
| 45 |
go |
| 46 |
select top 1 1`, |
| 47 |
Expect: []string{`select 'It''s go time' |
| 48 |
`, ` |
| 49 |
select top 1 1`, |
| 50 |
}, |
| 51 |
}, |
| 52 |
testItem{ |
| 53 |
Sql: `select 1 /* go */ |
| 54 |
go |
| 55 |
select top 1 1`, |
| 56 |
Expect: []string{`select 1 /* go */ |
| 57 |
`, ` |
| 58 |
select top 1 1`, |
| 59 |
}, |
| 60 |
}, |
| 61 |
testItem{ |
| 62 |
Sql: `select 1 -- go |
| 63 |
go |
| 64 |
select top 1 1`, |
| 65 |
Expect: []string{`select 1 -- go |
| 66 |
`, ` |
| 67 |
select top 1 1`, |
| 68 |
}, |
| 69 |
}, |
| 70 |
testItem{Sql: `"0'"`, Expect: []string{`"0'"`}}, |
| 71 |
testItem{Sql: "0'", Expect: []string{"0'"}}, |
| 72 |
testItem{Sql: "--", Expect: []string{"--"}}, |
| 73 |
testItem{Sql: "GO", Expect: nil}, |
| 74 |
testItem{Sql: "/*", Expect: []string{"/*"}}, |
| 75 |
testItem{Sql: "gO\x01\x00O550655490663051008\n", Expect: []string{"\n"}}, |
| 76 |
testItem{Sql: "select 1;\nGO 2\nselect 2;", Expect: []string{"select 1;\n", "select 1;\n", "\nselect 2;"}}, |
| 77 |
testItem{Sql: "select 'hi\\\n-hello';", Expect: []string{"select 'hi-hello';"}}, |
| 78 |
testItem{Sql: "select 'hi\\\r\n-hello';", Expect: []string{"select 'hi-hello';"}}, |
| 79 |
testItem{Sql: "select 'hi\\\r-hello';", Expect: []string{"select 'hi-hello';"}}, |
| 80 |
testItem{Sql: "select 'hi\\\n\nhello';", Expect: []string{"select 'hi\nhello';"}}, |
| 81 |
} |
| 82 |
|
| 83 |
index := -1 |
| 84 |
|
| 85 |
for i := range list { |
| 86 |
if index >= 0 && index != i { |
| 87 |
continue |
| 88 |
} |
| 89 |
sqltext := list[i].Sql |
| 90 |
t.Run(fmt.Sprintf("index-%d", i), func(t *testing.T) { |
| 91 |
ss := Split(sqltext, "go") |
| 92 |
if len(ss) != len(list[i].Expect) { |
| 93 |
t.Errorf("Test Item index %d; expect %d items, got %d %q", i, len(list[i].Expect), len(ss), ss) |
| 94 |
return |
| 95 |
} |
| 96 |
for j := 0; j < len(ss); j++ { |
| 97 |
if ss[j] != list[i].Expect[j] { |
| 98 |
t.Errorf("Test Item index %d, batch index %d; expect <%s>, got <%s>", i, j, list[i].Expect[j], ss[j]) |
| 99 |
} |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestHasPrefixFold(t *testing.T) { |
| 106 |
list := []struct { |
| 107 |
s, pre string |
| 108 |
is bool |
| 109 |
}{ |
| 110 |
{"h", "H", true}, |
| 111 |
{"h", "K", false}, |
| 112 |
{"go 5\n", "go", true}, |
| 113 |
} |
| 114 |
for _, item := range list { |
| 115 |
is := hasPrefixFold(item.s, item.pre) |
| 116 |
if is != item.is { |
| 117 |
t.Error("want (%q, %q)=%t got %t", item.s, item.pre, item.is, is) |
| 118 |
} |
| 119 |
} |
| 120 |
} |