add: ifaces testing
This commit is contained in:
34
interfaces/18-testing/reader_test.go
Normal file
34
interfaces/18-testing/reader_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_PNG_Reader_Correct_Signature(t *testing.T) {
|
||||
const input = "\x89PNG\r\n\x1a\nHELLO"
|
||||
|
||||
out, err := readSignature(input)
|
||||
if err != nil {
|
||||
t.Fatalf("got: %q; want: <nil> err", err)
|
||||
}
|
||||
if out != input {
|
||||
t.Fatalf("invalid output, got: '%x'; want: '%x'", out, input)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PNG_Reader_Incorrect_Signature(t *testing.T) {
|
||||
_, err := readSignature("\x89INCORRECT")
|
||||
if err == nil {
|
||||
t.Fatal("got: nil; want: !nil err")
|
||||
}
|
||||
}
|
||||
|
||||
func readSignature(in string) (string, error) {
|
||||
r := strings.NewReader(in)
|
||||
w := strings.Builder{}
|
||||
// _, err := io.Copy(&w, pngReader(r))
|
||||
_, err := io.CopyBuffer(&w, pngReader(r), make([]byte, 1))
|
||||
return w.String(), err
|
||||
}
|
||||
Reference in New Issue
Block a user