m3 icon indicating copy to clipboard operation
m3 copied to clipboard

The testable example "ExampleObjectPool" is not testing output

Open ameowlia opened this issue 2 years ago • 1 comments

👋 Hello,

I am investigating the golang issue: https://github.com/golang/go/issues/48362, where testable examples are causing false positives when the output comment block is not the last comment block example.

I ran a parser against the top golang repos, including this repo, and I found this bug with your ExampleObjectPool testable example.

The output comment block should be moved to the end like so:

func ExampleObjectPool() {
	opts := pool.NewObjectPoolOptions()
	p := pool.NewObjectPool(opts)
	p.Init(func() interface{} {
		// The Pool's Allocator should generally only return pointer
		// types, since a pointer can be put into the return interface
		// value without an allocation.
		return new(exampleObject)
	})

	// Get an exampleObject from the pool.
	o := p.Get().(*exampleObject)

	fmt.Printf("Retrieved struct should have default values: %+v", o)

	// Use the exampleObject.
	_ = o

	// Reset the exampleObject and return it to the pool.
	o.reset()
	p.Put(o)

	// Output: Retrieved struct should have default values: &{a:0 b:0 c:0}
}

I am happy to PR this in, but it will take a few days for me to get approval from my employer before I can submit it.

ameowlia avatar Sep 21 '21 19:09 ameowlia

@ameowlia sounds good! Let us know when you have something for us to review.

gibbscullen avatar Oct 20 '21 20:10 gibbscullen