counter: add baisics

This commit is contained in:
vinchent 2024-09-21 17:51:58 +02:00
parent a0bad4d6e9
commit 41ae820a21
2 changed files with 33 additions and 0 deletions

13
counter/counter.go Normal file
View 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
View 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)
}
}