Skip to content

Commit 9cfd040

Browse files
authored
test: Add tests for go/ioutil and refactor existing (#15885)
Signed-off-by: Noble Mittal <noblemittal@outlook.com>
1 parent 61061cb commit 9cfd040

File tree

5 files changed

+190
-15
lines changed

5 files changed

+190
-15
lines changed

go/hack/hack_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,30 @@ package hack
2020

2121
import (
2222
"testing"
23+
24+
"github.com/stretchr/testify/assert"
2325
)
2426

2527
func TestByteToString(t *testing.T) {
2628
v1 := []byte("1234")
27-
if s := String(v1); s != "1234" {
28-
t.Errorf("String(\"1234\"): %q, want 1234", s)
29-
}
29+
s := String(v1)
30+
assert.Equal(t, "1234", s)
3031

3132
v1 = []byte("")
32-
if s := String(v1); s != "" {
33-
t.Errorf("String(\"\"): %q, want empty", s)
34-
}
33+
s = String(v1)
34+
assert.Equal(t, "", s)
3535

3636
v1 = nil
37-
if s := String(v1); s != "" {
38-
t.Errorf("String(\"\"): %q, want empty", s)
39-
}
37+
s = String(v1)
38+
assert.Equal(t, "", s)
39+
}
40+
41+
func TestStringToByte(t *testing.T) {
42+
s := "1234"
43+
b := StringBytes(s)
44+
assert.Equal(t, []byte("1234"), b)
45+
46+
s = ""
47+
b = StringBytes(s)
48+
assert.Nil(t, b)
4049
}

go/ioutil/meter_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2024 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ioutil
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
var (
27+
calledBytes int
28+
calledDuration time.Duration
29+
)
30+
31+
func testfn(b int, d time.Duration) {
32+
calledBytes = b
33+
calledDuration = d
34+
}
35+
36+
func TestMeter(t *testing.T) {
37+
tm := meter{
38+
fs: []func(b int, d time.Duration){testfn},
39+
bytes: 123,
40+
duration: time.Second,
41+
}
42+
43+
assert.Equal(t, int64(123), tm.Bytes())
44+
assert.Equal(t, time.Second, tm.Duration())
45+
46+
tf := func(p []byte) (int, error) {
47+
return 1, nil
48+
}
49+
50+
b, err := tm.measure(tf, []byte(""))
51+
wantDuration := time.Second + calledDuration
52+
wantBytes := int64(123) + int64(calledBytes)
53+
54+
assert.NoError(t, err)
55+
assert.Equal(t, 1, b)
56+
assert.Equal(t, wantDuration, tm.duration)
57+
assert.Equal(t, wantBytes, tm.bytes)
58+
}

go/ioutil/reader_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2024 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ioutil
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
type mockReadCloser struct{}
26+
27+
func (*mockReadCloser) Read([]byte) (int, error) {
28+
return 2, nil
29+
}
30+
31+
func (*mockReadCloser) Close() error {
32+
return nil
33+
}
34+
35+
type mockRead struct{}
36+
37+
func (*mockRead) Read([]byte) (int, error) {
38+
return 3, nil
39+
}
40+
41+
func TestMeteredReader(t *testing.T) {
42+
mrc := NewMeteredReadCloser(&mockReadCloser{}, testfn)
43+
n, err := mrc.Read([]byte(""))
44+
assert.NoError(t, err)
45+
assert.Equal(t, 2, n)
46+
assert.Equal(t, 2, calledBytes)
47+
assert.Equal(t, calledDuration, mrc.Duration())
48+
49+
mr := NewMeteredReader(&mockRead{}, testfn)
50+
n, err = mr.Read([]byte(""))
51+
assert.NoError(t, err)
52+
assert.Equal(t, 3, n)
53+
assert.Equal(t, 3, calledBytes)
54+
assert.Equal(t, calledDuration, mr.Duration())
55+
}

go/ioutil/writer_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2024 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ioutil
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
type mockWriteCloser struct{}
26+
27+
func (*mockWriteCloser) Write([]byte) (int, error) {
28+
return 2, nil
29+
}
30+
31+
func (*mockWriteCloser) Close() error {
32+
return nil
33+
}
34+
35+
type mockWrite struct{}
36+
37+
func (*mockWrite) Write([]byte) (int, error) {
38+
return 3, nil
39+
}
40+
41+
func TestMeteredWriter(t *testing.T) {
42+
mwc := NewMeteredWriteCloser(&mockWriteCloser{}, testfn)
43+
n, err := mwc.Write([]byte(""))
44+
assert.NoError(t, err)
45+
assert.Equal(t, 2, n)
46+
assert.Equal(t, 2, calledBytes)
47+
assert.Equal(t, calledDuration, mwc.Duration())
48+
49+
mw := NewMeteredWriter(&mockWrite{}, testfn)
50+
n, err = mw.Write([]byte(""))
51+
assert.NoError(t, err)
52+
assert.Equal(t, 3, n)
53+
assert.Equal(t, 3, calledBytes)
54+
assert.Equal(t, calledDuration, mw.Duration())
55+
}

go/jsonutil/json_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package jsonutil
1818

1919
import (
2020
"testing"
21+
22+
"github.com/stretchr/testify/assert"
2123
)
2224

2325
func TestMarshalNoEscape(t *testing.T) {
@@ -53,9 +55,7 @@ func TestMarshalNoEscape(t *testing.T) {
5355
t.Run(c.name, func(t *testing.T) {
5456
json, _ := MarshalNoEscape(c.v)
5557
sjson := string(json[:len(json)-1])
56-
if sjson != c.expected {
57-
t.Errorf("expected: %v, got: %v", c.expected, sjson)
58-
}
58+
assert.Equal(t, c.expected, sjson)
5959
})
6060
}
6161
}
@@ -97,9 +97,7 @@ func TestMarshalIndentNoEscape(t *testing.T) {
9797
t.Run(c.name, func(t *testing.T) {
9898
json, _ := MarshalIndentNoEscape(c.v, c.prefix, c.ident)
9999
sjson := string(json[:len(json)-1])
100-
if sjson != c.expected {
101-
t.Errorf("expected: %v, got: %v", c.expected, sjson)
102-
}
100+
assert.Equal(t, c.expected, sjson)
103101
})
104102
}
105103
}

0 commit comments

Comments
 (0)