aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/list_people_test.go
diff options
context:
space:
mode:
authorGravatar Tim Swast <swast@google.com>2015-11-20 15:32:53 -0800
committerGravatar Tim Swast <swast@google.com>2015-11-25 10:46:35 -0800
commit7e31c4d930efa3f80d0f03c93e788ba73b847fd8 (patch)
tree9e3980cce5ad25c42e013b51d28d503cd4b7891c /examples/list_people_test.go
parentf1e14fba2300010d6f2966b2e32d4aa0842cdffb (diff)
Add a Go language example.
This follows the other examples so that it can be used as a tutorial, such as the ones at: https://developers.google.com/protocol-buffers/docs/tutorials Even though Go generally does not use Makefiles, I added targets for the Go examples to be consistent with the other languages. Edit: Fix Travis run. Change to use $HOME instead of ~. Add protoc to path. GOPATH entry cannot start with shell metacharacter '~': "~/gocode" Edit(2): Fix Go code style to address comments.
Diffstat (limited to 'examples/list_people_test.go')
-rw-r--r--examples/list_people_test.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/examples/list_people_test.go b/examples/list_people_test.go
new file mode 100644
index 00000000..721d3555
--- /dev/null
+++ b/examples/list_people_test.go
@@ -0,0 +1,96 @@
+package main
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+
+ pb "github.com/google/protobuf/examples/tutorial"
+)
+
+func TestListPeopleWritesList(t *testing.T) {
+ buf := new(bytes.Buffer)
+ in := pb.AddressBook{[]*pb.Person{
+ {
+ Name: "John Doe",
+ Id: 101,
+ Email: "john@example.com",
+ },
+ {
+ Name: "Jane Doe",
+ Id: 102,
+ },
+ {
+ Name: "Jack Doe",
+ Id: 201,
+ Email: "jack@example.com",
+ Phones: []*pb.Person_PhoneNumber{
+ {Number: "555-555-5555", Type: pb.Person_WORK},
+ },
+ },
+ {
+ Name: "Jack Buck",
+ Id: 301,
+ Email: "buck@example.com",
+ Phones: []*pb.Person_PhoneNumber{
+ {Number: "555-555-0000", Type: pb.Person_HOME},
+ {Number: "555-555-0001", Type: pb.Person_MOBILE},
+ {Number: "555-555-0002", Type: pb.Person_WORK},
+ },
+ },
+ {
+ Name: "Janet Doe",
+ Id: 1001,
+ Email: "janet@example.com",
+ Phones: []*pb.Person_PhoneNumber{
+ {Number: "555-777-0000"},
+ {Number: "555-777-0001", Type: pb.Person_HOME},
+ },
+ },
+ }}
+ listPeople(buf, &in)
+ want := strings.Split(`Person ID: 101
+ Name: John Doe
+ E-mail address: john@example.com
+Person ID: 102
+ Name: Jane Doe
+Person ID: 201
+ Name: Jack Doe
+ E-mail address: jack@example.com
+ Work phone #: 555-555-5555
+Person ID: 301
+ Name: Jack Buck
+ E-mail address: buck@example.com
+ Home phone #: 555-555-0000
+ Mobile phone #: 555-555-0001
+ Work phone #: 555-555-0002
+Person ID: 1001
+ Name: Janet Doe
+ E-mail address: janet@example.com
+ Mobile phone #: 555-777-0000
+ Home phone #: 555-777-0001
+`, "\n")
+ got := strings.Split(buf.String(), "\n")
+ if len(got) != len(want) {
+ t.Errorf(
+ "listPeople(%s) =>\n\t%q has %d lines, want %d",
+ in.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(
+ "listPeople(%s) =>\n\tline %d %q, want %q",
+ in.String(),
+ i,
+ got[i],
+ want[i])
+ }
+ }
+}