]> Cypherpunks repositories - gostls13.git/commitdiff
testing: trim spaces before comparing example output
authorAndrew Gerrand <adg@golang.org>
Thu, 15 Dec 2011 22:43:58 +0000 (09:43 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 15 Dec 2011 22:43:58 +0000 (09:43 +1100)
bytes: add two Buffer examples

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5490048

src/pkg/bytes/example_test.go [new file with mode: 0644]
src/pkg/testing/example.go

diff --git a/src/pkg/bytes/example_test.go b/src/pkg/bytes/example_test.go
new file mode 100644 (file)
index 0000000..02da1ac
--- /dev/null
@@ -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)
+}
index e23f13b6f16794b5d01c94d14c887a30d93b5201..fdeda137e7614facde2b4a4ade14e67970610ade 100644 (file)
@@ -9,6 +9,7 @@ import (
        "fmt"
        "io"
        "os"
+       "strings"
        "time"
 )
 
@@ -67,11 +68,9 @@ func RunExamples(examples []InternalExample) (ok bool) {
 
                // report any errors
                tstr := fmt.Sprintf("(%.2f seconds)", dt.Seconds())
-               if out != eg.Output {
-                       fmt.Printf(
-                               "--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
-                               eg.Name, tstr, out, eg.Output,
-                       )
+               if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e {
+                       fmt.Printf("--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
+                               eg.Name, tstr, g, e)
                        ok = false
                } else if *chatty {
                        fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)