Skip to content

Commit

Permalink
feat: add test case and comment
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>
  • Loading branch information
hlts2 committed Dec 21, 2020
1 parent f8d5097 commit 6ec2631
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/errors/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package errors

var (
// ErrWatchDirNotFound represents an error that the watch directory is not found.
ErrWatchDirNotFound = New("fs watcher watch dir not found")

// ErrFileAlreadyExists represents a function to generate an error that the file already exists.
ErrFileAlreadyExists = func(path string) error {
return Errorf("file already exists: %s", path)
}

// ErrPathNotSpecified represents an error that the path is not specified.
ErrPathNotSpecified = New("the path is not specified")
)
159 changes: 159 additions & 0 deletions internal/errors/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package errors

import (
"testing"
)

func TestErrWatchDirNotFound(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: "returns error",
want: want{
want: New("fs watcher watch dir not found"),
},
},
}

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 := ErrWatchDirNotFound
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrFileAlreadyExists(t *testing.T) {
type args struct {
path string
}
type want struct {
want error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
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: "returns error when compression path is 'metadata.json'",
args: args{
path: "metadata.json",
},
want: want{
want: New("file already exists: metadata.json"),
},
},
{
name: "returns error when compression path is empty",
args: args{
path: "",
},
want: want{
want: New("file already exists: "),
},
},
}

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

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

func TestErrPathNotSpecified(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: "returns error",
want: want{
want: New("the path is not specified"),
},
},
}

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 := ErrPathNotSpecified
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

0 comments on commit 6ec2631

Please sign in to comment.