list_people_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package main
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. pb "github.com/protocolbuffers/protobuf/examples/tutorial"
  7. )
  8. func TestWritePersonWritesPerson(t *testing.T) {
  9. buf := new(bytes.Buffer)
  10. // [START populate_proto]
  11. p := pb.Person{
  12. Id: 1234,
  13. Name: "John Doe",
  14. Email: "jdoe@example.com",
  15. Phones: []*pb.Person_PhoneNumber{
  16. {Number: "555-4321", Type: pb.Person_HOME},
  17. },
  18. }
  19. // [END populate_proto]
  20. writePerson(buf, &p)
  21. got := buf.String()
  22. want := `Person ID: 1234
  23. Name: John Doe
  24. E-mail address: jdoe@example.com
  25. Home phone #: 555-4321
  26. `
  27. if got != want {
  28. t.Errorf("writePerson(%s) =>\n\t%q, want %q", p.String(), got, want)
  29. }
  30. }
  31. func TestListPeopleWritesList(t *testing.T) {
  32. buf := new(bytes.Buffer)
  33. in := pb.AddressBook{People: []*pb.Person{
  34. {
  35. Name: "John Doe",
  36. Id: 101,
  37. Email: "john@example.com",
  38. },
  39. {
  40. Name: "Jane Doe",
  41. Id: 102,
  42. },
  43. {
  44. Name: "Jack Doe",
  45. Id: 201,
  46. Email: "jack@example.com",
  47. Phones: []*pb.Person_PhoneNumber{
  48. {Number: "555-555-5555", Type: pb.Person_WORK},
  49. },
  50. },
  51. {
  52. Name: "Jack Buck",
  53. Id: 301,
  54. Email: "buck@example.com",
  55. Phones: []*pb.Person_PhoneNumber{
  56. {Number: "555-555-0000", Type: pb.Person_HOME},
  57. {Number: "555-555-0001", Type: pb.Person_MOBILE},
  58. {Number: "555-555-0002", Type: pb.Person_WORK},
  59. },
  60. },
  61. {
  62. Name: "Janet Doe",
  63. Id: 1001,
  64. Email: "janet@example.com",
  65. Phones: []*pb.Person_PhoneNumber{
  66. {Number: "555-777-0000"},
  67. {Number: "555-777-0001", Type: pb.Person_HOME},
  68. },
  69. },
  70. }}
  71. listPeople(buf, &in)
  72. want := strings.Split(`Person ID: 101
  73. Name: John Doe
  74. E-mail address: john@example.com
  75. Person ID: 102
  76. Name: Jane Doe
  77. Person ID: 201
  78. Name: Jack Doe
  79. E-mail address: jack@example.com
  80. Work phone #: 555-555-5555
  81. Person ID: 301
  82. Name: Jack Buck
  83. E-mail address: buck@example.com
  84. Home phone #: 555-555-0000
  85. Mobile phone #: 555-555-0001
  86. Work phone #: 555-555-0002
  87. Person ID: 1001
  88. Name: Janet Doe
  89. E-mail address: janet@example.com
  90. Mobile phone #: 555-777-0000
  91. Home phone #: 555-777-0001
  92. `, "\n")
  93. got := strings.Split(buf.String(), "\n")
  94. if len(got) != len(want) {
  95. t.Errorf(
  96. "listPeople(%s) =>\n\t%q has %d lines, want %d",
  97. in.String(),
  98. buf.String(),
  99. len(got),
  100. len(want))
  101. }
  102. lines := len(got)
  103. if lines > len(want) {
  104. lines = len(want)
  105. }
  106. for i := 0; i < lines; i++ {
  107. if got[i] != want[i] {
  108. t.Errorf(
  109. "listPeople(%s) =>\n\tline %d %q, want %q",
  110. in.String(),
  111. i,
  112. got[i],
  113. want[i])
  114. }
  115. }
  116. }