Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic function support #165

Open
suzmue opened this issue Jan 14, 2022 · 1 comment
Open

Generic function support #165

suzmue opened this issue Jan 14, 2022 · 1 comment

Comments

@suzmue
Copy link

suzmue commented Jan 14, 2022

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

@KevinSnyderCodes
Copy link

For some inputs, gotests fails to generate tests:

typeparams.go

package typeparams

type Set[T comparable] struct {
	m map[T]struct{}
}

func (o *Set[T]) Add(v T) {
	o.m[v] = struct{}{}
}

func (o *Set[T]) Has(v T) bool {
	_, ok := o.m[v]
	return ok
}

gotests output:

output.Process: imports.Process: /var/folders/fw/grysx2h56354vh4_30px6_r80000gp/T/gotests_112301585:9:14: all type parameters must be named (and 1 more errors)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants