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