tests
This commit is contained in:
parent
4b8bf419e2
commit
796a7a1b4f
8
.gitignore
vendored
8
.gitignore
vendored
@ -1 +1,9 @@
|
|||||||
/problems
|
/problems
|
||||||
|
tmp
|
||||||
|
|
||||||
|
# Ignore these to avoid leaking local replacements to the devkit
|
||||||
|
./go.mod
|
||||||
|
./go.sum
|
||||||
|
|
||||||
|
go.mod
|
||||||
|
go.sum
|
||||||
56
tests/tests.go
Normal file
56
tests/tests.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.bizdoc.ro/gabi-public/Advent-of-Code.git/aoc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func assert(t *testing.T, err error, a, b any) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
want := fmt.Sprintf("%v", a)
|
||||||
|
got := fmt.Sprintf("%v", b)
|
||||||
|
if got != want {
|
||||||
|
err = fmt.Errorf("want %s, got %s", want, got)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Handler[T any](f func(ctx aoc.Context) (T, error)) func(ctx aoc.Context) (any, error) {
|
||||||
|
return func(c aoc.Context) (any, error) {
|
||||||
|
return f(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Handler2[T any, K any](param T, f func(ctx aoc.Context, param T) (K, error)) func(ctx aoc.Context) (any, error) {
|
||||||
|
return func(c aoc.Context) (any, error) {
|
||||||
|
return f(c, param)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test(t *testing.T, file string, handler func(aoc.Context) (any, error), want any) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
f, err := os.Open("tmp/" + file)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
got, err := handler(aoc.Context{
|
||||||
|
Body: f,
|
||||||
|
Logger: nil,
|
||||||
|
Context: nil,
|
||||||
|
})
|
||||||
|
t.Log("took", time.Since(start))
|
||||||
|
|
||||||
|
assert(t, err, want, got)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user