Skip to content

Commit

Permalink
use t.Run to run unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Congrool committed Nov 2, 2022
1 parent 4cf50db commit 0cdd6de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
32 changes: 15 additions & 17 deletions pkg/yurthub/storage/disk/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,23 @@ func TestKeyFunc(t *testing.T) {
}
keyFunc := disk.KeyFunc
for c, s := range cases {
key, err := keyFunc(s.info)
if err != s.err {
t.Errorf("unexpected err for case: %s, want: %s, got: %s", c, err, s.err)
continue
}
t.Run(c, func(t *testing.T) {
key, err := keyFunc(s.info)
if err != s.err {
t.Errorf("unexpected err for case: %s, want: %s, got: %s", c, err, s.err)
}

if err != nil {
continue
}
if err == nil {
storageKey := key.(storageKey)
if storageKey.Key() != s.key {
t.Errorf("unexpected key for case: %s, want: %s, got: %s", c, s.key, storageKey.Key())
}

storageKey := key.(storageKey)
if storageKey.Key() != s.key {
t.Errorf("unexpected key for case: %s, want: %s, got: %s", c, s.key, storageKey.Key())
continue
}

if storageKey.isRootKey() != s.isRoot {
t.Errorf("unexpected key type for case: %s, want: %v, got: %v", c, s.isRoot, storageKey.isRootKey())
}
if storageKey.isRootKey() != s.isRoot {
t.Errorf("unexpected key type for case: %s, want: %v, got: %v", c, s.isRoot, storageKey.isRootKey())
}
}
})
}
os.RemoveAll(keyFuncTestDir)
}
29 changes: 15 additions & 14 deletions pkg/yurthub/storage/disk/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,19 +1351,20 @@ func TestExtractInfoFromPath(t *testing.T) {
}

for c, d := range cases {
comp, res, ns, n, err := extractInfoFromPath(d.baseDir, d.path, d.isRoot)
var gotErrOut string
if err != nil {
gotErrOut = err.Error()
}
if d.wantErrOut != gotErrOut {
t.Errorf("failed at case: %s, wrong error, want: %s, got: %s", c, d.wantErrOut, gotErrOut)
continue
}
got := strings.Join([]string{comp, res, ns, n}, " ")
want := strings.Join(d.want, " ")
if got != want {
t.Errorf("failed at case: %s, want: %s, got: %s", c, want, got)
}
t.Run(c, func(t *testing.T) {
comp, res, ns, n, err := extractInfoFromPath(d.baseDir, d.path, d.isRoot)
var gotErrOut string
if err != nil {
gotErrOut = err.Error()
}
if d.wantErrOut != gotErrOut {
t.Errorf("failed at case: %s, wrong error, want: %s, got: %s", c, d.wantErrOut, gotErrOut)
}
got := strings.Join([]string{comp, res, ns, n}, " ")
want := strings.Join(d.want, " ")
if got != want {
t.Errorf("failed at case: %s, want: %s, got: %s", c, want, got)
}
})
}
}

0 comments on commit 0cdd6de

Please sign in to comment.