add: reader test ifaces
This commit is contained in:
@ -1,34 +1,48 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_PNG_Reader_Correct_Signature(t *testing.T) {
|
func TestCorrectSignature(t *testing.T) {
|
||||||
const input = "\x89PNG\r\n\x1a\nHELLO"
|
const signature = "\x89PNG\r\n\x1a\n"
|
||||||
|
const input = signature + "REST OF THE DATA"
|
||||||
|
|
||||||
out, err := readSignature(input)
|
pr := pngReader(strings.NewReader(input))
|
||||||
|
|
||||||
|
out, err := ioutil.ReadAll(pr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("got: %q; want: <nil> err", err)
|
t.Fatalf("got: %q; want: nil err", err)
|
||||||
}
|
}
|
||||||
if out != input {
|
if string(out) != input {
|
||||||
t.Fatalf("invalid output, got: '%x'; want: '%x'", out, input)
|
t.Fatalf("got: % x; want: % x", out, input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_PNG_Reader_Incorrect_Signature(t *testing.T) {
|
func TestIncorrectSignature(t *testing.T) {
|
||||||
_, err := readSignature("\x89INCORRECT")
|
const input = "WITHOUT PNG SIGNATURE"
|
||||||
|
|
||||||
|
// strings.Reader is a read-only, in-memory stream reader.
|
||||||
|
// It's an io.Reader implementation.
|
||||||
|
pr := pngReader(strings.NewReader(input))
|
||||||
|
|
||||||
|
// `ioutil.ReadAll` is a dangerous but useful utility function
|
||||||
|
// that can read all the data from a reader into memory.
|
||||||
|
// Don't use it for large (?) data.
|
||||||
|
_, err := ioutil.ReadAll(pr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("got: nil; want: !nil err")
|
t.Fatalf("got: %v; want: !nil err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSignature(in string) (string, error) {
|
func TestSummation(t *testing.T) {
|
||||||
r := strings.NewReader(in)
|
t.Skip() // skip this test
|
||||||
w := strings.Builder{}
|
|
||||||
// _, err := io.Copy(&w, pngReader(r))
|
result := 1 + 2
|
||||||
_, err := io.CopyBuffer(&w, pngReader(r), make([]byte, 1))
|
if result != 5 { // it'll always fail
|
||||||
return w.String(), err
|
// t.Fatal("want: 5") // not a good error message
|
||||||
|
t.Fatalf("got: %d; want: 5", result) // explanatory error message: good.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user