diff options
author | Feng Xiao <xiaofeng@google.com> | 2015-11-30 13:43:56 -0800 |
---|---|---|
committer | Feng Xiao <xiaofeng@google.com> | 2015-11-30 13:43:56 -0800 |
commit | 1a59a715dc5fa584340197aac0811ba3de9850b5 (patch) | |
tree | 054a47a347534b6bdb31dcf08b24f7cc07f5669e /examples/add_person_test.go | |
parent | f4e4be638634b74cbc9be10150bd8bf7a6cb4e8d (diff) | |
parent | 7e31c4d930efa3f80d0f03c93e788ba73b847fd8 (diff) | |
download | protobuf-1a59a715dc5fa584340197aac0811ba3de9850b5.tar.gz protobuf-1a59a715dc5fa584340197aac0811ba3de9850b5.tar.bz2 protobuf-1a59a715dc5fa584340197aac0811ba3de9850b5.zip |
Merge pull request #998 from tswast/goexample
Add a Go language example.
Diffstat (limited to 'examples/add_person_test.go')
-rw-r--r-- | examples/add_person_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/add_person_test.go b/examples/add_person_test.go new file mode 100644 index 00000000..0507db6f --- /dev/null +++ b/examples/add_person_test.go @@ -0,0 +1,58 @@ +package main + +import ( + "strings" + "testing" + + "github.com/golang/protobuf/proto" + pb "github.com/google/protobuf/examples/tutorial" +) + +func TestPromptForAddressReturnsAddress(t *testing.T) { + in := `12345 +Example Name +name@example.com +123-456-7890 +home +222-222-2222 +mobile +111-111-1111 +work +777-777-7777 +unknown + +` + got, err := promptForAddress(strings.NewReader(in)) + if err != nil { + t.Fatalf("promptForAddress(%q) had unexpected error: %s", in, err.Error()) + } + if got.Id != 12345 { + t.Errorf("promptForAddress(%q) got %d, want ID %d", in, got.Id, 12345) + } + if got.Name != "Example Name" { + t.Errorf("promptForAddress(%q) => want name %q, got %q", "Example Name", got.Name) + } + if got.Email != "name@example.com" { + t.Errorf("promptForAddress(%q) => want email %q, got %q", "name@example.com", got.Email) + } + + want := []*pb.Person_PhoneNumber{ + {Number: "123-456-7890", Type: pb.Person_HOME}, + {Number: "222-222-2222", Type: pb.Person_MOBILE}, + {Number: "111-111-1111", Type: pb.Person_WORK}, + {Number: "777-777-7777", Type: pb.Person_MOBILE}, + } + if len(got.Phones) != len(want) { + t.Errorf("want %d phone numbers, got %d", len(want), len(got.Phones)) + } + phones := len(got.Phones) + if phones > len(want) { + phones = len(want) + } + for i := 0; i < phones; i++ { + if !proto.Equal(got.Phones[i], want[i]) { + t.Errorf("want phone %q, got %q", *want[i], *got.Phones[i]) + } + + } +} |