|
| 1 | +package ioutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestLimitedReader_Read(t *testing.T) { |
| 9 | + eofLimited := LimitReader(strings.NewReader("all"), 5).(*LimitedReader) |
| 10 | + |
| 11 | + type args struct { |
| 12 | + p []byte |
| 13 | + } |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + l *LimitedReader |
| 17 | + args args |
| 18 | + wantN int |
| 19 | + wantErr bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "not-limited", |
| 23 | + l: LimitReader(strings.NewReader("all"), 3).(*LimitedReader), |
| 24 | + args: args{p: make([]byte, 4)}, |
| 25 | + wantN: 3, |
| 26 | + wantErr: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "not-limited-exact", |
| 30 | + l: LimitReader(strings.NewReader("all"), 3).(*LimitedReader), |
| 31 | + args: args{p: make([]byte, 3)}, |
| 32 | + wantN: 3, |
| 33 | + wantErr: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "not-limited-EOF-OK", |
| 37 | + l: eofLimited, |
| 38 | + args: args{p: make([]byte, 4)}, |
| 39 | + wantN: 3, |
| 40 | + wantErr: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "not-limited-EOF", |
| 44 | + l: eofLimited, |
| 45 | + args: args{p: make([]byte, 4)}, |
| 46 | + wantN: 0, |
| 47 | + wantErr: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "limited", |
| 51 | + l: LimitReader(strings.NewReader("limited"), 1).(*LimitedReader), |
| 52 | + args: args{p: make([]byte, 3)}, |
| 53 | + wantN: 2, // need to read one past to know we're past |
| 54 | + wantErr: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "limited-buff-under-N", |
| 58 | + l: LimitReader(strings.NewReader("limited"), 0).(*LimitedReader), |
| 59 | + args: args{p: make([]byte, 1)}, |
| 60 | + wantN: 1, |
| 61 | + wantErr: true, |
| 62 | + }, |
| 63 | + } |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + gotN, err := tt.l.Read(tt.args.p) |
| 67 | + if (err != nil) != tt.wantErr { |
| 68 | + t.Errorf("LimitedReader.Read() error = %v, wantErr %v", err, tt.wantErr) |
| 69 | + return |
| 70 | + } |
| 71 | + if gotN != tt.wantN { |
| 72 | + t.Errorf("LimitedReader.Read() = %v, want %v", gotN, tt.wantN) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments