add test
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Pedro Pérez 2025-05-30 01:05:36 +02:00
parent 8d79ac08a3
commit cf4ee051bb
3 changed files with 41 additions and 1 deletions

View File

@ -3,7 +3,15 @@ when:
branch: main branch: main
steps: steps:
build: go-test:
image: golang:1.24
environment:
CGO_ENABLED: 0
GOOS: linux
GOARCH: amd64
commands:
- go test ./...
go-build:
image: golang:1.24 image: golang:1.24
environment: environment:
CGO_ENABLED: 0 CGO_ENABLED: 0

10
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strconv"
) )
const ( const (
@ -21,6 +22,15 @@ func main() {
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"version": version}) json.NewEncoder(w).Encode(map[string]string{"version": version})
}) })
mux.HandleFunc("/sum", func(w http.ResponseWriter, r *http.Request) {
a, _ := strconv.Atoi(r.URL.Query().Get("a"))
b, _ := strconv.Atoi(r.URL.Query().Get("b"))
json.NewEncoder(w).Encode(map[string]int{"result": sum(a, b)})
})
http.ListenAndServe(":8080", mux) http.ListenAndServe(":8080", mux)
} }
func sum(a, b int) int {
return a + b
}

22
main_test.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"testing"
)
func Test_sum(t *testing.T) {
tests := []struct {
a, b int
want int
}{
{1, 2, 3},
{-1, 1, 0},
{0, 0, 0},
}
for _, test := range tests {
if got := sum(test.a, test.b); got != test.want {
t.Errorf("sum(%d, %d) = %d, want %d", test.a, test.b, got, test.want)
}
}
}