add: reader test ifaces

This commit is contained in:
Inanc Gumus
2019-11-13 14:15:39 +03:00
parent 8298a64a0c
commit 029b522a02

View File

@ -1,34 +1,48 @@
package main
import (
"io"
"io/ioutil"
"strings"
"testing"
)
func Test_PNG_Reader_Correct_Signature(t *testing.T) {
const input = "\x89PNG\r\n\x1a\nHELLO"
func TestCorrectSignature(t *testing.T) {
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 {
t.Fatalf("got: %q; want: <nil> err", err)
t.Fatalf("got: %q; want: nil err", err)
}
if out != input {
t.Fatalf("invalid output, got: '%x'; want: '%x'", out, input)
if string(out) != input {
t.Fatalf("got: % x; want: % x", out, input)
}
}
func Test_PNG_Reader_Incorrect_Signature(t *testing.T) {
_, err := readSignature("\x89INCORRECT")
func TestIncorrectSignature(t *testing.T) {
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 {
t.Fatal("got: nil; want: !nil err")
t.Fatalf("got: %v; want: !nil err", 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
func TestSummation(t *testing.T) {
t.Skip() // skip this test
result := 1 + 2
if result != 5 { // it'll always fail
// t.Fatal("want: 5") // not a good error message
t.Fatalf("got: %d; want: 5", result) // explanatory error message: good.
}
}