add_person_test.go 1.4 KB

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