client_golang/internal/errcapture/errcapture_test.go

83 lines
1.8 KiB
Go

// Copyright 2014 The Prometheus Authors
// 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
//
// http://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 errcapture
import (
"errors"
"io"
"testing"
)
type testCloser struct {
err error
}
func (c testCloser) Close() error {
return c.err
}
func TestDo(t *testing.T) {
for _, tcase := range []struct {
err error
closer io.Closer
expectedErrStr string
}{
{
err: nil,
closer: testCloser{err: nil},
expectedErrStr: "",
},
{
err: errors.New("test"),
closer: testCloser{err: nil},
expectedErrStr: "test",
},
{
err: nil,
closer: testCloser{err: errors.New("test")},
expectedErrStr: "1 error(s) occurred:\n* close: test",
},
{
err: errors.New("test"),
closer: testCloser{err: errors.New("test")},
expectedErrStr: "2 error(s) occurred:\n* test\n* close: test",
},
} {
if ok := t.Run("", func(t *testing.T) {
ret := tcase.err
Do(&ret, tcase.closer.Close, "close")
if tcase.expectedErrStr == "" {
if ret != nil {
t.Error("Expected error to be nil")
t.Fail()
}
} else {
if ret == nil {
t.Error("Expected error to be not nil")
t.Fail()
}
if tcase.expectedErrStr != ret.Error() {
t.Errorf("%s != %s", tcase.expectedErrStr, ret.Error())
t.Fail()
}
}
}); !ok {
return
}
}
}