Skip to content

testing: expose common struct as interface #56516

@Olshansk

Description

@Olshansk

I recently found myself doing the following in one of our test suites

func Foo[T testing.T | testing.B | testing.F](t *T) *Bar {
   // test prep stuff

	var cleanupFn func(func())
	switch any(t).(type) {
	case *testing.T:
		cleanupFn = any(t).(*testing.T).Cleanup
	case *testing.B:
		cleanupFn = any(t).(*testing.B).Cleanup
	case *testing.F:
		cleanupFn = any(t).(*testing.F).Cleanup
	default:
		log.Fatalf("Error: unsupported type %T", t)
	}
	
	cleanupFn(func(){
      // test cleanup stuff
    })

The cleanup function is common to all of these and is part of the common struct, which is embedded in each of testingT, testing.B, and testing.F.

Screen Shot 2022-10-31 at 11 13 18 AM

I was wondering if common could potentially be exposed as an interface?

I'm happy to take a first stab at the PR if this is something the library mainteras would be open to, unless there's a technical reason this shouldn't be the case. Please lmk!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions