internal/encoding/text: add extra random space to make output unstable.

Make output deliberately unstable so users don't rely on exactness.

For multi-line output, add another extra random space after <key>: for
at most one field per message.

-- example --
key1: field1
key2:  {
    foo:  bar
}

For single-line output, add another extra random space after a field per
message.

-- example --
key1:field1  key2:{foo:bar}

Change-Id: I3ab25d4d970fdebb88bbd9dd8fa6d73af84338ea
Reviewed-on: https://go-review.googlesource.com/c/150977
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/go.mod b/go.mod
index 131aeafb..aa0e421 100644
--- a/go.mod
+++ b/go.mod
@@ -2,7 +2,7 @@
 
 require (
 	github.com/golang/protobuf v1.2.0
-	github.com/google/go-cmp v0.2.0
+	github.com/google/go-cmp v0.2.1-0.20181101181452-745b8ec83783
 	golang.org/x/net v0.0.0-20180821023952-922f4815f713 // indirect
 	golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
 	golang.org/x/tools v0.0.0-20180904205237-0aa4b8830f48
diff --git a/go.sum b/go.sum
index 51e6c99..d9fd37c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,7 @@
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
-github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
-github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+github.com/google/go-cmp v0.2.1-0.20181101181452-745b8ec83783 h1:wVZ6laEGf86tNDTpR5mxFyFIclJJiXCxuJhcQKnsOHk=
+github.com/google/go-cmp v0.2.1-0.20181101181452-745b8ec83783/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
 golang.org/x/net v0.0.0-20180821023952-922f4815f713 h1:rMJUcaDGbG+X967I4zGKCq5laYqcGKJmpB+3jhpOhPw=
 golang.org/x/net v0.0.0-20180821023952-922f4815f713/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
diff --git a/internal/encoding/text/encode.go b/internal/encoding/text/encode.go
index 1eae1f3..f7d185a 100644
--- a/internal/encoding/text/encode.go
+++ b/internal/encoding/text/encode.go
@@ -6,6 +6,7 @@
 
 import (
 	"bytes"
+	"math/rand"
 	"strings"
 
 	"github.com/golang/protobuf/v2/internal/errors"
@@ -104,6 +105,7 @@
 			p.out = append(p.out, p.newline...)
 		}
 	}
+	spaceAdded := false
 	for i, item := range items {
 		p.out = append(p.out, p.indents...)
 		if err := p.marshalKey(item[0]); !p.nerr.Merge(err) {
@@ -113,12 +115,25 @@
 		if len(p.indent) > 0 {
 			p.out = append(p.out, ' ')
 		}
+		// For multi-line output, add a random extra space after key: per message to
+		// make output unstable.
+		if !spaceAdded && len(p.indent) > 0 && rand.Intn(2) == 1 {
+			p.out = append(p.out, ' ')
+			spaceAdded = true
+		}
+
 		if err := p.marshalValue(item[1]); !p.nerr.Merge(err) {
 			return err
 		}
 		if i < len(items)-1 && len(p.indent) == 0 {
 			p.out = append(p.out, ' ')
 		}
+		// For single-line output, add a random extra space after a field per message to
+		// make output unstable.
+		if !spaceAdded && len(p.indent) == 0 && i != len(items)-1 && rand.Intn(2) == 1 {
+			p.out = append(p.out, ' ')
+			spaceAdded = true
+		}
 		p.out = append(p.out, p.newline...)
 	}
 	if emitDelims {
diff --git a/internal/encoding/text/text_test.go b/internal/encoding/text/text_test.go
index b5ca63c..221d6ab 100644
--- a/internal/encoding/text/text_test.go
+++ b/internal/encoding/text/text_test.go
@@ -7,6 +7,7 @@
 import (
 	"fmt"
 	"math"
+	"regexp"
 	"strings"
 	"testing"
 	"unicode/utf8"
@@ -17,13 +18,15 @@
 	"github.com/google/go-cmp/cmp/cmpopts"
 )
 
+var S = fmt.Sprintf
+var V = ValueOf
+var ID = func(n protoreflect.Name) Value { return V(n) }
+
+type Lst = []Value
+type Msg = [][2]Value
+
 func Test(t *testing.T) {
 	const space = " \n\r\t"
-	var S = fmt.Sprintf
-	var V = ValueOf
-	var ID = func(n protoreflect.Name) Value { return V(n) }
-	type Lst = []Value
-	type Msg = [][2]Value
 
 	tests := []struct {
 		in             string
@@ -824,7 +827,7 @@
 				if err != nil {
 					t.Errorf("Marshal(): got %v, want nil error", err)
 				}
-				if string(gotOut) != tt.wantOut {
+				if removeRandomSpace(gotOut, false) != tt.wantOut {
 					t.Errorf("Marshal():\ngot:  %s\nwant: %s", gotOut, tt.wantOut)
 				}
 			}
@@ -833,7 +836,7 @@
 				if err != nil {
 					t.Errorf("Marshal(Bracket): got %v, want nil error", err)
 				}
-				if string(gotOut) != tt.wantOutBracket {
+				if removeRandomSpace(gotOut, false) != tt.wantOutBracket {
 					t.Errorf("Marshal(Bracket):\ngot:  %s\nwant: %s", gotOut, tt.wantOutBracket)
 				}
 			}
@@ -842,7 +845,7 @@
 				if err != nil {
 					t.Errorf("Marshal(ASCII): got %v, want nil error", err)
 				}
-				if string(gotOut) != tt.wantOutASCII {
+				if removeRandomSpace(gotOut, false) != tt.wantOutASCII {
 					t.Errorf("Marshal(ASCII):\ngot:  %s\nwant: %s", gotOut, tt.wantOutASCII)
 				}
 			}
@@ -851,10 +854,24 @@
 				if err != nil {
 					t.Errorf("Marshal(Indent): got %v, want nil error", err)
 				}
-				if string(gotOut) != tt.wantOutIndent {
+				if removeRandomSpace(gotOut, true) != tt.wantOutIndent {
 					t.Errorf("Marshal(Indent):\ngot:  %s\nwant: %s", gotOut, tt.wantOutIndent)
 				}
 			}
 		})
 	}
 }
+
+var expandedRE = regexp.MustCompile(":  +")
+
+// This works only for the test cases above.
+func removeRandomSpace(b []byte, useIndent bool) string {
+	s := string(b)
+	if useIndent {
+		return expandedRE.ReplaceAllString(s, ": ")
+	}
+	s = strings.Replace(s, "  ", " ", -1)
+	s = strings.Replace(s, " }", "}", -1)
+	s = strings.Replace(s, " >", ">", -1)
+	return strings.TrimRight(s, " ")
+}