This repository has been archived on 2026-03-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
apex-mocks/mocks.go
T

123 lines
3.7 KiB
Go
Raw Normal View History

2020-12-10 12:16:30 +01:00
// Copyright (c) 2020 Unbound Software Development Svenska AB
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package apex
import (
"fmt"
"github.com/apex/log"
"github.com/sanity-io/litter"
"testing"
"time"
)
// Mock implements `log.Interface` for use in unit-testing
type Mock struct {
Logged []string
}
// Check verifies that the application has logged the expected strings
func (m *Mock) Check(t testing.TB, wantLogged []string) {
t.Helper()
if len(m.Logged) != 0 || len(wantLogged) != 0 {
got := litter.Sdump(m.Logged)
want := litter.Sdump(wantLogged)
if got != want {
t.Errorf("Mock() got %s, want %s", got, want)
}
}
}
// Debug implements log.Interface Debug
func (m *Mock) Debug(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("DEBUG: %s", s))
}
// Info implements log.Interface Info
func (m *Mock) Info(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("INFO: %s", s))
}
// Warn implements log.Interface Warn
func (m *Mock) Warn(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("WARN: %s", s))
}
// Error implements log.Interface Error
func (m *Mock) Error(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("ERROR: %s", s))
}
// Fatal implements log.Interface Fatal
func (m *Mock) Fatal(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("FATAL: %s", s))
}
// Debugf implements log.Interface Debugf
func (m *Mock) Debugf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("DEBUG: %s", fmt.Sprintf(s, i...)))
}
// Infof implements log.Interface Infof
func (m *Mock) Infof(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("INFO: %s", fmt.Sprintf(s, i...)))
}
// Warnf implements log.Interface Warnf
func (m *Mock) Warnf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("WARN: %s", fmt.Sprintf(s, i...)))
}
// Errorf implements log.Interface Errorf
func (m *Mock) Errorf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("ERROR: %s", fmt.Sprintf(s, i...)))
}
// Fatalf implements log.Interface Fatalf
func (m *Mock) Fatalf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("FATAL: %s", fmt.Sprintf(s, i...)))
}
// Trace implements log.Interface Trace
func (m *Mock) Trace(string) *log.Entry {
return nil
}
// WithFields implements log.Interface WithFields
func (m *Mock) WithFields(log.Fielder) *log.Entry {
return nil
}
// WithField implements log.Interface WithField
func (m *Mock) WithField(string, interface{}) *log.Entry {
return nil
}
// WithDuration implements log.Interface WithDuration
func (m *Mock) WithDuration(time.Duration) *log.Entry {
return nil
}
// WithError implements log.Interface WithError
func (m *Mock) WithError(error) *log.Entry {
return nil
}
var _ log.Interface = &Mock{}