~mna/webparts-expvar

3387e0d50db37daf14b19de167201533193322ba — Martin Angers 3 years ago 4e247b8
test counter
4 files changed, 54 insertions(+), 4 deletions(-)

M expvar.go
A expvar_test.go
A go.mod
A go.sum
M expvar.go => expvar.go +4 -4
@@ 11,18 11,18 @@ import (

// NewCounter creates a new counter with the specified name.
func NewCounter(name string) metric.Counter {
	return counter(expvar.NewFloat(name))
	return (*counter)(expvar.NewFloat(name))
}

type counter expvar.Float

func (c counter) Inc() {
func (c *counter) Inc() {
	c.Add(1)
}

func (c counter) Add(increment float64) {
func (c *counter) Add(increment float64) {
	if increment < 0 {
		panic(fmt.Sprintf("webparts-expvar: negative increment in call to counter.Add"))
	}
	c.(expvar.Float).Add(increment)
	(*expvar.Float)(c).Add(increment)
}

A expvar_test.go => expvar_test.go +20 -0
@@ 0,0 1,20 @@
package expvar

import (
	"expvar"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestCounter(t *testing.T) {
	c := NewCounter("test")

	c.Inc()
	c.Inc()
	c.Add(3)
	require.Equal(t, "5", (*expvar.Float)(c.(*counter)).String())
	require.Panics(t, func() {
		c.Add(-1)
	})
}

A go.mod => go.mod +8 -0
@@ 0,0 1,8 @@
module git.sr.ht/~mna/webparts-expvar

go 1.13

require (
	git.sr.ht/~mna/webparts v0.0.0-20191020021954-5dff8b21c35a
	github.com/stretchr/testify v1.4.0
)

A go.sum => go.sum +22 -0
@@ 0,0 1,22 @@
git.sr.ht/~mna/webparts v0.0.0-20191020021954-5dff8b21c35a h1:CuIWo7lBN8Z4aPCPBmsPDPEUnJfJmj7xy9lBoForoFs=
git.sr.ht/~mna/webparts v0.0.0-20191020021954-5dff8b21c35a/go.mod h1:I9CmJibFclVrCKUIpZPCOvEMyx58vu6+Ej3rXE1xWzM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ=
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/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191018212557-ed542cd5b28a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=