Files
learngo/interfaces/18-testing/reader_test.go

49 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-11-11 15:29:44 +03:00
package main
import (
2019-11-13 14:15:39 +03:00
"io/ioutil"
2019-11-11 15:29:44 +03:00
"strings"
"testing"
)
2019-11-13 14:15:39 +03:00
func TestCorrectSignature(t *testing.T) {
const signature = "\x89PNG\r\n\x1a\n"
const input = signature + "REST OF THE DATA"
2019-11-11 15:29:44 +03:00
2019-11-13 14:15:39 +03:00
pr := pngReader(strings.NewReader(input))
out, err := ioutil.ReadAll(pr)
2019-11-11 15:29:44 +03:00
if err != nil {
2019-11-13 14:15:39 +03:00
t.Fatalf("got: %q; want: nil err", err)
2019-11-11 15:29:44 +03:00
}
2019-11-13 14:15:39 +03:00
if string(out) != input {
t.Fatalf("got: % x; want: % x", out, input)
2019-11-11 15:29:44 +03:00
}
}
2019-11-13 14:15:39 +03:00
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)
2019-11-11 15:29:44 +03:00
if err == nil {
2019-11-13 14:15:39 +03:00
t.Fatalf("got: %v; want: !nil err", err)
2019-11-11 15:29:44 +03:00
}
}
2019-11-13 14:15:39 +03:00
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.
}
2019-11-11 15:29:44 +03:00
}