1package main 2 3import ( 4 "strings" 5 "testing" 6 7 "github.com/golang/protobuf/proto" 8 pb "github.com/protocolbuffers/protobuf/examples/tutorial" 9) 10 11func TestPromptForAddressReturnsAddress(t *testing.T) { 12 in := `12345 13Example Name 14name@example.com 15123-456-7890 16home 17222-222-2222 18mobile 19111-111-1111 20work 21777-777-7777 22unknown 23 24` 25 got, err := promptForAddress(strings.NewReader(in)) 26 if err != nil { 27 t.Fatalf("promptForAddress(%q) had unexpected error: %s", in, err.Error()) 28 } 29 if got.Id != 12345 { 30 t.Errorf("promptForAddress(%q) got %d, want ID %d", in, got.Id, 12345) 31 } 32 if got.Name != "Example Name" { 33 t.Errorf("promptForAddress(%q) => want name %q, got %q", in, "Example Name", got.Name) 34 } 35 if got.Email != "name@example.com" { 36 t.Errorf("promptForAddress(%q) => want email %q, got %q", in, "name@example.com", got.Email) 37 } 38 39 want := []*pb.Person_PhoneNumber{ 40 {Number: "123-456-7890", Type: pb.Person_HOME}, 41 {Number: "222-222-2222", Type: pb.Person_MOBILE}, 42 {Number: "111-111-1111", Type: pb.Person_WORK}, 43 {Number: "777-777-7777", Type: pb.Person_MOBILE}, 44 } 45 if len(got.Phones) != len(want) { 46 t.Errorf("want %d phone numbers, got %d", len(want), len(got.Phones)) 47 } 48 phones := len(got.Phones) 49 if phones > len(want) { 50 phones = len(want) 51 } 52 for i := 0; i < phones; i++ { 53 if !proto.Equal(got.Phones[i], want[i]) { 54 t.Errorf("want phone %q, got %q", *want[i], *got.Phones[i]) 55 } 56 57 } 58} 59