diff --git a/.gitignore b/.gitignore index 00c301e..04561b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,9 @@ /problems +tmp + +# Ignore these to avoid leaking local replacements to the devkit +./go.mod +./go.sum + +go.mod +go.sum \ No newline at end of file diff --git a/tests/tests.go b/tests/tests.go new file mode 100644 index 0000000..b30ef32 --- /dev/null +++ b/tests/tests.go @@ -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) +}