counter: add baisics
This commit is contained in:
parent
a0bad4d6e9
commit
41ae820a21
13
counter/counter.go
Normal file
13
counter/counter.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package counter
|
||||||
|
|
||||||
|
type Counter struct {
|
||||||
|
count int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Counter) Inc() {
|
||||||
|
c.count++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Counter) Value() int {
|
||||||
|
return c.count
|
||||||
|
}
|
20
counter/counter_test.go
Normal file
20
counter/counter_test.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package counter
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCounter(t *testing.T) {
|
||||||
|
t.Run("incrementing the counter 3 times leaves it at 3", func(t *testing.T) {
|
||||||
|
counter := Counter{}
|
||||||
|
|
||||||
|
counter.Inc()
|
||||||
|
counter.Inc()
|
||||||
|
counter.Inc()
|
||||||
|
assertCounter(t, counter, 3)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertCounter(t testing.TB, got Counter, want int) {
|
||||||
|
if got.Value() != 3 {
|
||||||
|
t.Errorf("got %d, want %d", got.Value(), want)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user