Add Golang test parser

This commit is contained in:
Shamus Taylor 2025-04-21 15:19:16 -05:00 committed by Jozef Izso
parent d33ca7294f
commit 7745ff0ec1
10 changed files with 451 additions and 0 deletions

20
reports/go/calculator.go Normal file
View file

@ -0,0 +1,20 @@
package main
import "errors"
func CalculatorSum(a, b int) int {
return a + b
}
func CalculatorDivide(a, b int) int {
return a / b
}
var ErrDivideByZero = errors.New("divide by zero")
func CalculatorSafeDivide(a, b int) (int, error) {
if b == 0 {
return 0, ErrDivideByZero
}
return a / b, nil
}

View file

@ -0,0 +1,77 @@
package main
import (
"math/rand"
"testing"
"time"
)
func TestPassing(t *testing.T) {
randomSleep()
t.Log("pass!")
}
func TestFailing(t *testing.T) {
randomSleep()
expected := 3
actual := CalculatorSum(1, 1)
if actual != expected {
t.Fatalf("expected 1+1 = %d, got %d", expected, actual)
}
}
func TestPanicInsideFunction(t *testing.T) {
defer catchPanics(t)
expected := 0
actual := CalculatorDivide(1, 0)
if actual != expected {
t.Fatalf("expected 1/1 = %d, got %d", expected, actual)
}
}
func TestPanicInsideTest(t *testing.T) {
defer catchPanics(t)
panic("bad stuff")
}
// Timeouts cause the entire test process to end - so we can't get good output for these
// func TestTimeout(t *testing.T) {
// time.Sleep(time.Second * 5)
// }
func TestSkipped(t *testing.T) {
randomSleep()
t.Skipf("skipping test")
}
func TestCases(t *testing.T) {
for _, tc := range []struct {
name string
a, b, c int
}{
{"1 + 2 = 3", 1, 2, 3},
{"4 + 7 = 11", 4, 7, 11},
{"2 + 3 = 4", 2, 3, 4},
} {
t.Run(tc.name, func(t *testing.T) {
randomSleep()
c := CalculatorSum(tc.a, tc.b)
if c != tc.c {
t.Fatalf("expected %s, got %d", tc.name, c)
}
})
}
}
func catchPanics(t *testing.T) {
err := recover()
if err != nil {
t.Fatalf("caught panic: %v", err)
}
}
func randomSleep() {
time.Sleep(time.Duration(rand.Int63n(int64(time.Second))))
}

3
reports/go/go.mod Normal file
View file

@ -0,0 +1,3 @@
module test_reporter_example
go 1.24.2