void print_person(tutorial::AddressBook &address_book){ //解析消息数据 for (int i = 0; i < address_book.person_size(); i++){ const tutorial::Person &person = address_book.person(i); cout << "Person Id: " << person.id() << endl; cout << "Person Name: " << person.name() << endl; if(person.has_email()) cout << "Person Email: " << person.email() << endl; for (int j = 0; j < person.phone_size(); j++){ const tutorial::Person::PhoneNumber &phone = person.phone(j); switch (phone.type()) { case tutorial::Person_PhoneType_HOME: cout << "Home Phone: "; break; case tutorial::Person_PhoneType_MOBILE: cout << "MOBILE Phone: "; break; case tutorial::Person_PhoneType_WORK: cout << "Work Phone: "; break; } cout << phone.number() << endl; } } }
// Iterates though all people in the AddressBook and prints info about them. void ListPeople(const tutorial::AddressBook& address_book) { for (int i = 0; i < address_book.person_size(); i++) { const tutorial::Person& person = address_book.person(i); cout << "Person ID: " << person.id() << endl; cout << " Name: " << person.name() << endl; //if (person.has_email()) { cout << " E-mail address: " << person.email() << endl; //} for (int j = 0; j < person.phone_size(); j++) { const tutorial::Person::PhoneNumber& phone_number = person.phone(j); switch (phone_number.type()) { case tutorial::Person::MOBILE: cout << " Mobile phone #: "; break; case tutorial::Person::HOME: cout << " Home phone #: "; break; case tutorial::Person::WORK: cout << " Work phone #: "; break; } cout << phone_number.number() << endl; } } }
void Check(const tutorial::AddressBook& ab) { ASSERT_EQ(2, ab.person_size()) << "ab: " << ab.Utf8DebugString(); const tutorial::Person& person2 = ab.person(0); EXPECT_EQ("bin3", person2.name()); ASSERT_EQ(2, person2.phone_size()); EXPECT_EQ("10000", person2.phone(0).number()); EXPECT_EQ("10001", person2.phone(1).number()); }