mirror of
https://github.com/golang/go.git
synced 2025-11-06 19:51:00 +00:00
testing: trim spaces before comparing example output
bytes: add two Buffer examples R=golang-dev, r CC=golang-dev https://golang.org/cl/5490048
This commit is contained in:
parent
a369004e23
commit
9834a25d33
2 changed files with 28 additions and 5 deletions
24
src/pkg/bytes/example_test.go
Normal file
24
src/pkg/bytes/example_test.go
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
package bytes_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Hello world!
|
||||||
|
func ExampleBuffer() {
|
||||||
|
var b Buffer // A Buffer needs no initialization.
|
||||||
|
b.Write([]byte("Hello "))
|
||||||
|
b.Write([]byte("world!"))
|
||||||
|
b.WriteTo(os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gophers rule!
|
||||||
|
func ExampleBuffer_reader() {
|
||||||
|
// A Buffer can turn a string or a []byte into an io.Reader.
|
||||||
|
buf := NewBufferString("R29waGVycyBydWxlIQ==")
|
||||||
|
dec := base64.NewDecoder(base64.StdEncoding, buf)
|
||||||
|
io.Copy(os.Stdout, dec)
|
||||||
|
}
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -67,11 +68,9 @@ func RunExamples(examples []InternalExample) (ok bool) {
|
||||||
|
|
||||||
// report any errors
|
// report any errors
|
||||||
tstr := fmt.Sprintf("(%.2f seconds)", dt.Seconds())
|
tstr := fmt.Sprintf("(%.2f seconds)", dt.Seconds())
|
||||||
if out != eg.Output {
|
if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e {
|
||||||
fmt.Printf(
|
fmt.Printf("--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
|
||||||
"--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
|
eg.Name, tstr, g, e)
|
||||||
eg.Name, tstr, out, eg.Output,
|
|
||||||
)
|
|
||||||
ok = false
|
ok = false
|
||||||
} else if *chatty {
|
} else if *chatty {
|
||||||
fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)
|
fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue