aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/list_people_test.go
diff options
context:
space:
mode:
authorGravatar Tim Swast <swast@google.com>2015-12-15 15:56:23 -0800
committerGravatar Tim Swast <swast@google.com>2015-12-15 15:56:23 -0800
commit1cc541b3be263d92aba435b183aca5ad7151ae8a (patch)
treea6e62dd8b8811529f555c436a206410eb49544ac /examples/list_people_test.go
parent1a59a715dc5fa584340197aac0811ba3de9850b5 (diff)
Add region tags to the Go protobuf examples.
This will allow us to like to specific snippets of code in the documentation. I plan to create a tutorial similar to the C# tutorial https://developers.google.com/protocol-buffers/docs/csharptutorial Since that tutorial has sections for populating a proto, parsing, and serializing, I made a region for each of these for Go. To make the populating sample more self-contained, I refactor the listing example slightly.
Diffstat (limited to 'examples/list_people_test.go')
-rw-r--r--examples/list_people_test.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/list_people_test.go b/examples/list_people_test.go
index 721d3555..d995aa1c 100644
--- a/examples/list_people_test.go
+++ b/examples/list_people_test.go
@@ -8,6 +8,50 @@ import (
pb "github.com/google/protobuf/examples/tutorial"
)
+func TestWritePersonWritesPerson(t *testing.T) {
+ buf := new(bytes.Buffer)
+ // [START populate_proto]
+ p := pb.Person{
+ Id: 1234,
+ Name: "John Doe",
+ Email: "jdoe@example.com",
+ Phones: []*pb.Person_PhoneNumber{
+ {Number: "555-4321", Type: pb.Person_HOME},
+ },
+ }
+ // [END populate_proto]
+ writePerson(buf, &p)
+ want := strings.Split(`Person ID: 1234
+ Name: John Doe
+ E-mail address: jdoe@example.com
+ Home phone #: 555-4321
+`, "\n")
+
+ got := strings.Split(buf.String(), "\n")
+ if len(got) != len(want) {
+ t.Errorf(
+ "writePerson(%s) =>\n\t%q has %d lines, want %d",
+ p.String(),
+ buf.String(),
+ len(got),
+ len(want))
+ }
+ lines := len(got)
+ if lines > len(want) {
+ lines = len(want)
+ }
+ for i := 0; i < lines; i++ {
+ if got[i] != want[i] {
+ t.Errorf(
+ "writePerson(%s) =>\n\tline %d %q, want %q",
+ p.String(),
+ i,
+ got[i],
+ want[i])
+ }
+ }
+}
+
func TestListPeopleWritesList(t *testing.T) {
buf := new(bytes.Buffer)
in := pb.AddressBook{[]*pb.Person{