list_people_test.go 2.8 KB

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