Skip to content

Generic function support #165

Open
Open
@suzmue

Description

@suzmue

Filing this issue to track/ discuss generic function support for gotests.

Currently, gotests produces a table driven test for a function with type parameters, but the test needs to actually be modified to include concrete types (the test file will have build errors).

Example:

typeparams.go:

package typeparams

// SumIntsOrFloats sums the values of map m. It supports both floats and integers
// as map values.
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
	var s V
	for _, v := range m {
		s += v
	}
	return s
}

typeparams_test.go:

package typeparams

import (
	"reflect"
	"testing"
)

func TestSumIntsOrFloats(t *testing.T) {
	type args struct {
		m map[K]V // K and V not defined
	}
	tests := []struct {
		name string
		args args
		want V
	}{
		// TODO: Add test cases.
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := SumIntsOrFloats(tt.args.m); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("SumIntsOrFloats() = %v, want %v", got, tt.want)
			}
		})
	}
}

Can better table driven tests be generated for functions with type params / should the generated tests make sure there are no build errors?

Related go issue: golang/go#50558

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions