From 1cc541b3be263d92aba435b183aca5ad7151ae8a Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 15 Dec 2015 15:56:23 -0800 Subject: 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. --- examples/list_people_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'examples/list_people_test.go') 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{ -- cgit v1.2.3