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