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

testing: expose common struct as interface #56516

Closed
Olshansk opened this issue Nov 1, 2022 · 4 comments
Closed

testing: expose common struct as interface #56516

Olshansk opened this issue Nov 1, 2022 · 4 comments

Comments

@Olshansk
Copy link

Olshansk commented Nov 1, 2022

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!

@seankhliao
Copy link
Member

https://pkg.go.dev/testing#TB

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2022
@Olshansk
Copy link
Author

Olshansk commented Nov 2, 2022

@seankhliao This is a proposal though. I'm proposing to make an interface for the common struct under testing. I didn't want to put the effort into implementation before getting a 👍 so I don't waste time implementing.

@cespare
Copy link
Contributor

cespare commented Nov 2, 2022

@Olshansk @seankhliao's message includes a link to the already-existing interface: https://pkg.go.dev/testing#TB.

@Olshansk
Copy link
Author

Olshansk commented Nov 3, 2022

That's exactly what I needed. Sorry for not RTFM the first time @seankhliao!

@golang golang locked and limited conversation to collaborators Nov 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants