feat: add Go theory guides (terminology, algorithms, testing, debugging) and 2 new chapters
This commit is contained in:
1 parent
84be3e5d77
commit
011f23e637
13 files changed
+374
-3
No files matched your search
@@ -0,0 +1,13 @@
|
||||
# 05 — Rodzaje Testów: Jednostkowe, Integracyjne i Mutacyjne
|
||||
|
||||
Teoria: [notes/04-testing-pyramid-and-types.md](../../../notes/04-testing-pyramid-and-types.md)
|
||||
|
||||
## Zadania (`testing_types.go` + `testing_types_test.go`)
|
||||
1. **Unit (granice zabijające mutanty):** zaimplementuj `IsWithinRateLimit(current, limit int) (bool, error)` — dla `current < 0` zwróć `ErrNegativeCount`, dla `limit <= 0` zwróć `false, nil`, inaczej `current <= limit, nil`.
|
||||
2. **Integration (prawdziwy system plików):** zaimplementuj `SaveAndLoadConfig(path string, data map[string]string) (map[string]string, error)` — zapis JSON na dysk (`os.WriteFile`) i odczyt z powrotem (`os.ReadFile` + `json.Unmarshal`).
|
||||
3. **Mutation Testing (eksperyment ręczny):** po przejściu testów zmień `<=` na `<` w `IsWithinRateLimit` (mutant!), uruchom `make test PKG=./exercises/02-engineering-2026/05-testing-types-and-mutation` i sprawdź, czy test graniczny `current == limit` zabił mutanta.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/02-engineering-2026/05-testing-types-and-mutation
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
package testingtypes
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrNegativeCount = errors.New("current request count cannot be negative")
|
||||
|
||||
func IsWithinRateLimit(current, limit int) (bool, error) {
|
||||
panic("TODO: ErrNegativeCount dla current < 0; false dla limit <= 0; inaczej current <= limit")
|
||||
}
|
||||
|
||||
func SaveAndLoadConfig(path string, data map[string]string) (map[string]string, error) {
|
||||
panic("TODO: zserializuj data do JSON, zapisz pod path i odczytaj z powrotem")
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package testingtypes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 1. Testy jednostkowe + warunki brzegowe zabijające mutanty (<= vs <, > 0 vs >= 0)
|
||||
func TestRateLimitBoundariesKillMutants(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
current, limit int
|
||||
want bool
|
||||
}{
|
||||
{"below_limit", 9, 10, true},
|
||||
{"at_limit_kills_lt_mutant", 10, 10, true},
|
||||
{"above_limit", 11, 10, false},
|
||||
{"zero_limit_kills_gte_mutant", 0, 0, false},
|
||||
{"negative_limit", 0, -1, false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := IsWithinRateLimit(tc.current, tc.limit)
|
||||
if err != nil || got != tc.want {
|
||||
t.Fatalf("got (%v, %v), want (%v, nil)", got, err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if _, err := IsWithinRateLimit(-1, 10); !errors.Is(err, ErrNegativeCount) {
|
||||
t.Fatalf("expected ErrNegativeCount, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Test integracyjny (prawdziwy zapis i odczyt z systemu plików)
|
||||
func TestSaveAndLoadConfigIntegration(t *testing.T) {
|
||||
cfgPath := filepath.Join(t.TempDir(), "service-config.json")
|
||||
payload := map[string]string{"env": "prod", "region": "eu-central-1"}
|
||||
|
||||
loaded, err := SaveAndLoadConfig(cfgPath, payload)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if _, statErr := os.Stat(cfgPath); statErr != nil {
|
||||
t.Fatalf("expected file to exist on disk: %v", statErr)
|
||||
}
|
||||
if !reflect.DeepEqual(loaded, payload) {
|
||||
t.Fatalf("got %v, want %v", loaded, payload)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user