TestCopyNWriteTo

59 views
Skip to first unread message

Stephen

unread,
Apr 14, 2024, 11:05:16 PM4/14/24
to golang-nuts
https://github.com/golang/go/blob/c0a0ba254c48fc855f9501b0bd3b78e6847ca923/src/io/io_test.go#L167

I was walking through this code, and it didn't seem to hit [WriteTo] (https://github.com/golang/go/blob/c0a0ba254c48fc855f9501b0bd3b78e6847ca923/src/io/io.go#L410-L412) because of LimitReader

Is this intended behavior?  

1. TestCopyNWriteTo does not assert if `WriteTo` is called.  It should either add this or test should be renamed.
2. WriteTo not called using CopyN,
 
```
func TestCopyNWriteTo(t *testing.T) {
rb := new(writeToChecker) // implements WriteTo.
wb := new(Buffer)
rb.WriteString("hello, world.")
CopyN(wb, rb, 5)
if wb.String() != "hello" {
t.Errorf("CopyN did not work properly")
} else if !rb.writeToCalled {
t.Errorf("CopyN does not use writeTo")
}
}
```

Ian Lance Taylor

unread,
Apr 14, 2024, 11:15:34 PM4/14/24
to Stephen, golang-nuts
On Sun, Apr 14, 2024 at 8:05 PM Stephen <smcelh...@gmail.com> wrote:
>
> https://github.com/golang/go/blob/c0a0ba254c48fc855f9501b0bd3b78e6847ca923/src/io/io_test.go#L167
>
> I was walking through this code, and it didn't seem to hit [WriteTo] (https://github.com/golang/go/blob/c0a0ba254c48fc855f9501b0bd3b78e6847ca923/src/io/io.go#L410-L412) because of LimitReader
>
> Is this intended behavior?

The purpose of the test is to make sure that CopyN does the correct
thing even if the source type implements WriteTo. So, yes, this is
intended behavior.

Ian
Reply all
Reply to author
Forward
0 new messages