MCPcopy
hub / github.com/Terry-Mao/goim / TestReaderDiscard

Function TestReaderDiscard

pkg/bufio/bufio_test.go:733–860  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

731}
732
733func TestReaderDiscard(t *testing.T) {
734 tests := []struct {
735 name string
736 r io.Reader
737 bufSize int // 0 means 16
738 peekSize int
739
740 n int // input to Discard
741
742 want int // from Discard
743 wantErr error // from Discard
744
745 wantBuffered int
746 }{
747 {
748 name: "normal case",
749 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"),
750 peekSize: 16,
751 n: 6,
752 want: 6,
753 wantBuffered: 10,
754 },
755 {
756 name: "discard causing read",
757 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"),
758 n: 6,
759 want: 6,
760 wantBuffered: 10,
761 },
762 {
763 name: "discard all without peek",
764 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"),
765 n: 26,
766 want: 26,
767 wantBuffered: 0,
768 },
769 {
770 name: "discard more than end",
771 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"),
772 n: 27,
773 want: 26,
774 wantErr: io.EOF,
775 wantBuffered: 0,
776 },
777 // Any error from filling shouldn't show up until we
778 // get past the valid bytes. Here we return we return 5 valid bytes at the same time
779 // as an error, but test that we don't see the error from Discard.
780 {
781 name: "fill error, discard less",
782 r: newScriptedReader(func(p []byte) (n int, err error) {
783 if len(p) < 5 {
784 panic("unexpected small read")
785 }
786 return 5, errors.New("5-then-error")
787 }),
788 n: 4,
789 want: 4,
790 wantErr: nil,

Callers

nothing calls this directly

Calls 5

newScriptedReaderFunction · 0.85
NewReaderSizeFunction · 0.85
DiscardMethod · 0.80
PeekMethod · 0.45
BufferedMethod · 0.45

Tested by

no test coverage detected