From 6e69a40ac16128e56d2e25486397ab9b967a19d1 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Thu, 10 Dec 2020 12:16:30 +0100 Subject: [PATCH] feat: initial commit --- .editorconfig | 11 ++ .gitignore | 1 + .gitlab-ci.yml | 53 ++++++ LICENSE | 21 +++ README.md | 20 +++ go.mod | 8 + go.sum | 80 +++++++++ mocks.go | 122 +++++++++++++ mocks_test.go | 468 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 784 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 mocks.go create mode 100644 mocks_test.go diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..06f4bc7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +trim_trailing_whitespace = true + +[*.go] +indent_style = tab +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..a561094 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,53 @@ +# 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. + +include: + - template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml' + +image: golang:1.15 + +variables: + GOFLAGS: -mod=readonly + +stages: + - deps + - test + +deps: + stage: deps + script: + - go mod download + +test: + stage: test + dependencies: + - deps + script: + - go get golang.org/x/lint/golint + - go fmt $(go list ./...) + - go vet $(go list ./...) + - golint -set_exit_status ./... + - CGO_ENABLED=1 go test -mod=readonly -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=$(go list ./... | tr '\n' , | sed 's/,$//') ./... + - go tool cover -html=coverage.txt -o coverage.html + - go tool cover -func=coverage.txt + - bash <(curl -s https://codecov.io/bash) + artifacts: + paths: + - coverage.html + - coverage.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1501635 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1e123fc --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# apex mocks + +[![GoReportCard](https://goreportcard.com/badge/gitlab.com/unboundsoftware/apex-mocks)](https://goreportcard.com/report/gitlab.com/unboundsoftware/apex-mocks) [![GoDoc](https://godoc.org/gitlab.com/unboundsoftware/apex-mocks?status.svg)](https://godoc.org/gitlab.com/unboundsoftware/apex-mocks) [![Build Status](https://gitlab.com/unboundsoftware/apex-mocks/badges/master/pipeline.svg)](https://gitlab.com/unboundsoftware/apex-mocks/commits/master)[![coverage report](https://gitlab.com/unboundsoftware/apex-mocks/badges/master/coverage.svg)](https://gitlab.com/unboundsoftware/apex-mocks/commits/master) + +Package `mocks` provides a mock implementation of [Apex](https://github.com/apex/logs) which can be used to unit test applications using Apex logging. + +### Download +```shell +go get gitlab.com/unboundsoftware/apex-mocks +``` + +### Usage +Make sure that your application uses `log.Interface` to be able to pass in a `Mock` from your tests instead. + +To verify what has been logged: +```go + logger := &apex.Mock{} + // ... run your actual test using the logger above + logger.Check(t, tt.wantLogged) +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f1f2636 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module gitlab.com/unboundsoftware/apex-mocks + +go 1.15 + +require ( + github.com/apex/log v1.9.0 + github.com/sanity-io/litter v1.3.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9ed2d6c --- /dev/null +++ b/go.sum @@ -0,0 +1,80 @@ +github.com/apex/log v1.9.0 h1:FHtw/xuaM8AgmvDDTI9fiwoAL25Sq2cxojnZICUU8l0= +github.com/apex/log v1.9.0/go.mod h1:m82fZlWIuiWzWP04XCTXmnX0xRkYYbCdYn8jbJeLBEA= +github.com/apex/logs v1.0.0/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo= +github.com/aphistic/golf v0.0.0-20180712155816-02c07f170c5a/go.mod h1:3NqKYiepwy8kCu4PNA+aP7WUV72eXWJeP9/r3/K9aLE= +github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys= +github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I= +github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/sanity-io/litter v1.3.0 h1:5ZO+weUsqdSWMUng5JnpkW/Oz8iTXiIdeumhQr1sSjs= +github.com/sanity-io/litter v1.3.0/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM= +github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= +github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0= +github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk= +github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk= +github.com/tj/go-buffer v1.1.0/go.mod h1:iyiJpfFcR2B9sXu7KvjbT9fpM4mOelRSDTbntVj52Uc= +github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= +github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= +github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo= +gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mocks.go b/mocks.go new file mode 100644 index 0000000..22d3651 --- /dev/null +++ b/mocks.go @@ -0,0 +1,122 @@ +// 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{} diff --git a/mocks_test.go b/mocks_test.go new file mode 100644 index 0000000..b64841a --- /dev/null +++ b/mocks_test.go @@ -0,0 +1,468 @@ +// 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" + "reflect" + "testing" + "time" +) + +func TestMock_Check(t *testing.T) { + type fields struct { + Logged []string + } + type args struct { + t *testing.T + wantLogged []string + } + tests := []struct { + name string + fields fields + args args + want []string + }{ + { + name: "same", + fields: fields{Logged: []string{"same"}}, + args: args{wantLogged: []string{"same"}}, + want: nil, + }, + { + name: "different", + fields: fields{Logged: []string{"same"}}, + args: args{wantLogged: []string{"different"}}, + want: []string{`Mock() got []string{ + "same", +}, want []string{ + "different", +}`}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{ + Logged: tt.fields.Logged, + } + temp := &MockT{} + m.Check(temp, tt.args.wantLogged) + if !reflect.DeepEqual(temp.errors, tt.want) { + t.Errorf("Check() got %+v, want %+v", temp.errors, tt.want) + } + }) + } +} + +func TestMock_Debug(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "debug", + args: args{s: "something"}, + want: []string{"DEBUG: something"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Debug(tt.args.s) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Debugf(t *testing.T) { + type args struct { + s string + i []interface{} + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "debugf", + args: args{ + s: "%s - %d", + i: []interface{}{"a", 5}, + }, + want: []string{"DEBUG: a - 5"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Debugf(tt.args.s, tt.args.i...) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Error(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "error", + args: args{s: "something"}, + want: []string{"ERROR: something"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Error(tt.args.s) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Errorf(t *testing.T) { + type args struct { + s string + i []interface{} + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "errorf", + args: args{ + s: "%s - %d", + i: []interface{}{"a", 5}, + }, + want: []string{"ERROR: a - 5"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Errorf(tt.args.s, tt.args.i...) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Fatal(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "fatal", + args: args{s: "something"}, + want: []string{"FATAL: something"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Fatal(tt.args.s) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Fatalf(t *testing.T) { + type args struct { + s string + i []interface{} + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "fatalf", + args: args{ + s: "%s - %d", + i: []interface{}{"a", 5}, + }, + want: []string{"FATAL: a - 5"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Fatalf(tt.args.s, tt.args.i...) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Info(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "info", + args: args{s: "something"}, + want: []string{"INFO: something"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Info(tt.args.s) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Infof(t *testing.T) { + type args struct { + s string + i []interface{} + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "infof", + args: args{ + s: "%s - %d", + i: []interface{}{"a", 5}, + }, + want: []string{"INFO: a - 5"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Infof(tt.args.s, tt.args.i...) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Trace(t *testing.T) { + type args struct { + in0 string + } + tests := []struct { + name string + args args + want *log.Entry + }{ + { + name: "always return nil", + args: args{}, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + if got := m.Trace(tt.args.in0); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Trace() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMock_Warn(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "warn", + args: args{s: "something"}, + want: []string{"WARN: something"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Warn(tt.args.s) + m.Check(t, tt.want) + }) + } +} + +func TestMock_Warnf(t *testing.T) { + type args struct { + s string + i []interface{} + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "warnf", + args: args{ + s: "%s - %d", + i: []interface{}{"a", 5}, + }, + want: []string{"WARN: a - 5"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + m.Warnf(tt.args.s, tt.args.i...) + m.Check(t, tt.want) + }) + } +} + +func TestMock_WithDuration(t *testing.T) { + type args struct { + in0 time.Duration + } + tests := []struct { + name string + args args + want *log.Entry + }{ + { + name: "always return nil", + args: args{}, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + if got := m.WithDuration(tt.args.in0); !reflect.DeepEqual(got, tt.want) { + t.Errorf("WithDuration() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMock_WithError(t *testing.T) { + type args struct { + in0 error + } + tests := []struct { + name string + args args + want *log.Entry + }{ + { + name: "always return nil", + args: args{}, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + if got := m.WithError(tt.args.in0); !reflect.DeepEqual(got, tt.want) { + t.Errorf("WithError() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMock_WithField(t *testing.T) { + type args struct { + in0 string + in1 interface{} + } + tests := []struct { + name string + args args + want *log.Entry + }{ + { + name: "always return nil", + args: args{}, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + if got := m.WithField(tt.args.in0, tt.args.in1); !reflect.DeepEqual(got, tt.want) { + t.Errorf("WithField() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMock_WithFields(t *testing.T) { + type args struct { + in0 log.Fielder + } + tests := []struct { + name string + args args + want *log.Entry + }{ + { + name: "always return nil", + args: args{}, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &Mock{} + if got := m.WithFields(tt.args.in0); !reflect.DeepEqual(got, tt.want) { + t.Errorf("WithFields() = %v, want %v", got, tt.want) + } + }) + } +} + +type MockT struct { + testing.T + errors []string +} + +func (m *MockT) Errorf(format string, args ...interface{}) { + m.errors = append(m.errors, fmt.Sprintf(format, args...)) +}