| 167 | } |
| 168 | |
| 169 | func TestReadInput(t *testing.T) { |
| 170 | type args struct { |
| 171 | prompt string |
| 172 | } |
| 173 | tests := []struct { |
| 174 | name string |
| 175 | args args |
| 176 | before func() func() |
| 177 | want []byte |
| 178 | wantErr bool |
| 179 | }{ |
| 180 | {"ok", args{"Write input"}, func() func() { |
| 181 | content := []byte("my file content") |
| 182 | mockStdin, cleanup := newFile(t, content) |
| 183 | reset := setStdin(mockStdin) |
| 184 | return func() { |
| 185 | defer cleanup() |
| 186 | reset() |
| 187 | } |
| 188 | }, []byte("my file content"), false}, |
| 189 | } |
| 190 | for _, tt := range tests { |
| 191 | t.Run(tt.name, func(t *testing.T) { |
| 192 | cleanup := tt.before() |
| 193 | defer cleanup() |
| 194 | got, err := ReadInput(tt.args.prompt) |
| 195 | if (err != nil) != tt.wantErr { |
| 196 | t.Errorf("ReadInput() error = %v, wantErr %v", err, tt.wantErr) |
| 197 | return |
| 198 | } |
| 199 | if !reflect.DeepEqual(got, tt.want) { |
| 200 | t.Errorf("ReadInput() = %v, want %v", got, tt.want) |
| 201 | } |
| 202 | }) |
| 203 | } |
| 204 | } |