From cf4ee051bb23182c790e8870c8eb4c5a4c3b7666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Fri, 30 May 2025 01:05:36 +0200 Subject: [PATCH] add test --- .woodpecker.yml | 10 +++++++++- main.go | 10 ++++++++++ main_test.go | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 main_test.go diff --git a/.woodpecker.yml b/.woodpecker.yml index 58d6ca9..569c6b1 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -3,7 +3,15 @@ when: branch: main 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 environment: CGO_ENABLED: 0 diff --git a/main.go b/main.go index b599544..136433f 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "net/http" + "strconv" ) const ( @@ -21,6 +22,15 @@ func main() { mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { 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) } + +func sum(a, b int) int { + return a + b +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..1e71c14 --- /dev/null +++ b/main_test.go @@ -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) + } + } +}