Skip to content

Commit

Permalink
Add test case for internal/errors/io.go (#910)
Browse files Browse the repository at this point in the history
* feat: Add test case and godoc comment

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>

* 🤖 Update license headers / Format go codes and yaml files

Signed-off-by: vdaas-ci <ci@vdaas.org>

Co-authored-by: vdaas-ci <ci@vdaas.org>
  • Loading branch information
hlts2 and vdaas-ci committed Jan 8, 2021
1 parent 0e93f6b commit c4df6ef
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/errors/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
package errors

var (
// io
// NewErrContextNotProvided represents a function to generate an error that the context is not provided.
NewErrContextNotProvided = func() error {
return New("context not provided")
}

// NewErrReaderNotProvided represents a function to generate an error that the io.Reader is not provided.
NewErrReaderNotProvided = func() error {
return New("io.Reader not provided")
}

// NewErrWriterNotProvided represents a function to generate an error that the io.Writer is not provided.
NewErrWriterNotProvided = func() error {
return New("io.Writer not provided")
}
Expand Down
156 changes: 156 additions & 0 deletions internal/errors/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//
// Copyright (C) 2019-2021 vdaas.org vald team <vald@vdaas.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package errors

import "testing"

func TestNewErrContextNotProvided(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "return ErrContextNotProvided error",
want: want{
want: New("context not provided"),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := NewErrContextNotProvided()
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestNewErrReaderNotProvided(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "return ErrReaderNotProvided error",
want: want{
want: New("io.Reader not provided"),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := NewErrReaderNotProvided()
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestNewErrWriterNotProvided(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "return ErrWriterNotProvided error",
want: want{
want: New("io.Writer not provided"),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := NewErrWriterNotProvided()
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

0 comments on commit c4df6ef

Please sign in to comment.