Skip to content

Commit 3fc8d63

Browse files
committed
feat: add lab3
1 parent 3ab3cfc commit 3fc8d63

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

.github/workflows/lab3.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: lab3
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab3/**'
10+
11+
jobs:
12+
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-go@v4
20+
with:
21+
go-version-file: 'lab3/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab3'
26+
run: go test

lab3/lab3.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func Calculator(w http.ResponseWriter, r *http.Request) {
12+
// TODO: implement a calculator
13+
14+
15+
}
16+
17+
func main() {
18+
http.HandleFunc("/", Calculator)
19+
log.Fatal(http.ListenAndServe(":8083", nil))
20+
}

lab3/lab3_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
func TestCalculator(t *testing.T) {
10+
testData := []struct {
11+
path string
12+
expected string
13+
}{
14+
{"/add/1/2", "1 + 2 = 3"},
15+
{"/add", "Error!"},
16+
{"/sub/7/2", "7 - 2 = 5"},
17+
{"/sub/7/2/3", "Error!"},
18+
{"/mul/3/4", "3 * 4 = 12"},
19+
{"/mul/3", "Error!"},
20+
{"/div/10/3", "10 / 3 = 3, reminder = 1"},
21+
{"/div/10/0", "Error!"},
22+
{"/div/10/3/4", "Error!"},
23+
{"/div/10/a", "Error!"},
24+
{"/div/a/3", "Error!"},
25+
{"/gcd/10/3", "Error!"},
26+
{"/gcd/10/3/4", "Error!"},
27+
{"/favicon.ico", "Error!"},
28+
{"/", "Error!"},
29+
}
30+
31+
for _, tc := range testData {
32+
w := httptest.NewRecorder()
33+
r := httptest.NewRequest("GET", tc.path, nil)
34+
35+
Calculator(w, r)
36+
37+
assert.Equal(t, tc.expected, w.Body.String())
38+
}
39+
}

0 commit comments

Comments
 (0)