-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathformat_test.go
139 lines (121 loc) · 3.82 KB
/
format_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package sqlcmd
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFitToScreen(t *testing.T) {
type fitTest struct {
width int64
raw string
fit string
}
tests := []fitTest{
{0, "this is a string", "this is a string"},
{9, "12345678", "12345678"},
{9, "123456789", "123456789"},
{9, "123456789A", "123456789" + SqlcmdEol + "A"},
{9, "123456789" + SqlcmdEol, "123456789" + SqlcmdEol},
{9, "12345678" + SqlcmdEol + "9A", "12345678" + SqlcmdEol + "9A"},
{9, "123456789\rA", "123456789" + SqlcmdEol + "\rA"},
}
for _, test := range tests {
line := new(strings.Builder)
line.WriteString(test.raw)
t.Log(test.raw)
f := fitToScreen(line, test.width).String()
assert.Equal(t, test.fit, f, "Mismatched fit for raw string: '%s'", test.raw)
}
}
func TestCalcColumnDetails(t *testing.T) {
type colTest struct {
fixed int64
variable int64
query string
details []columnDetail
max int
}
tests := []colTest{
{8, 8,
"select 100 as '123456789ABC', getdate() as '987654321', 'string' as col1",
[]columnDetail{
{leftJustify: false, displayWidth: 12},
{leftJustify: false, displayWidth: 23},
{leftJustify: true, displayWidth: 6},
},
12,
},
}
db, err := ConnectDb(t)
if assert.NoError(t, err, "ConnectDB failed") {
defer db.Close()
for x, test := range tests {
rows, err := db.Query(test.query)
if assert.NoError(t, err, "Query failed: %s", test.query) {
defer rows.Close()
cols, err := rows.ColumnTypes()
if assert.NoError(t, err, "ColumnTypes failed:%s", test.query) {
actual, max := calcColumnDetails(cols, test.fixed, test.variable)
for i, a := range actual {
if test.details[i].displayWidth != a.displayWidth ||
test.details[i].leftJustify != a.leftJustify ||
test.details[i].zeroesAfterDecimal != a.zeroesAfterDecimal {
assert.Failf(t, "", "[%d] Incorrect test details for column [%s] in query '%s':%+v", x, cols[i].Name(), test.query, a)
}
assert.Equal(t, test.max, max, "[%d] Max column name length incorrect", x)
}
}
}
}
}
}
func TestControlCharacterBehavior(t *testing.T) {
type ccbTest struct {
raw string
replaced string
removed string
consecutivereplaced string
}
tests := []ccbTest{
{"no control", "no control", "no control", "no control"},
{string(rune(1)) + "tabs\t\treturns\r\n\r\n", " tabs returns ", "tabsreturns", " tabs returns "},
}
for _, test := range tests {
s := applyControlCharacterBehavior(test.raw, ControlReplace)
assert.Equalf(t, test.replaced, s, "Incorrect Replaced for '%s'", test.raw)
s = applyControlCharacterBehavior(test.raw, ControlRemove)
assert.Equalf(t, test.removed, s, "Incorrect Remove for '%s'", test.raw)
s = applyControlCharacterBehavior(test.raw, ControlReplaceConsecutive)
assert.Equalf(t, test.consecutivereplaced, s, "Incorrect ReplaceConsecutive for '%s'", test.raw)
}
}
func TestDecodeBinary(t *testing.T) {
type decodeTest struct {
b []byte
s string
}
tests := []decodeTest{
{[]byte("123456ABCDEF"), "313233343536414243444546"},
{[]byte{0x12, 0x34, 0x56}, "123456"},
}
for _, test := range tests {
a := decodeBinary(test.b)
assert.Equalf(t, test.s, a, "Incorrect decoded binary string for %v", test.b)
}
}
func BenchmarkDecodeBinary(b *testing.B) {
b.ReportAllocs()
bytes := make([]byte, 10000)
for i := 0; i < 10000; i++ {
bytes[i] = byte(i % 0xff)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := decodeBinary(bytes)
if len(s) != 20000 {
b.Fatalf("Incorrect length of returned string. Should be 20k, was %d", len(s))
}
}
}