Exemplo n.º 1
0
        /// <summary>
        /// Entry point - loads an existing addressbook or creates a new one,
        /// then writes it back to the file.
        /// </summary>
        public static int RUN(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Usage:  AddPerson ADDRESS_BOOK_FILE");
                return(-1);
            }

            AddressBook.Builder addressBook = AddressBook.CreateBuilder();

            if (File.Exists(args[0]))
            {
                using (Stream file = File.OpenRead(args[0]))
                {
                    addressBook.MergeFrom(file);
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
            }

            // Add an address.
            addressBook.AddPerson(PromptForAddress(Console.In, Console.Out));

            // Write the new address book back to disk.
            using (Stream output = File.OpenWrite(args[0]))
            {
                addressBook.Build().WriteTo(output);
            }
            return(0);
        }
        public static IEnumerable <object[]> GetAddressBookCases()
        {
            Person.Builder personBuilder = Person.CreateBuilder();
            personBuilder.SetId(1);
            personBuilder.SetName("Foo");
            personBuilder.SetEmail("foo@bar");

            Person.Types.PhoneNumber.Builder phoneBuilder = Person.Types.PhoneNumber.CreateBuilder();
            phoneBuilder.SetType(Person.Types.PhoneType.HOME);
            phoneBuilder.SetNumber("555-1212");
            Person.Types.PhoneNumber phone1 = phoneBuilder.Build();

            personBuilder.AddPhone(phone1);

            Person person1 = personBuilder.Build();

            AddressBook.Builder addressBuilder = AddressBook.CreateBuilder();
            addressBuilder.AddPerson(person1);

            yield return(new object[]
            {
                addressBuilder.Build(),
                false
            });

            yield return(new object[]
            {
                addressBuilder.Build(),
                true
            });

            phoneBuilder = Person.Types.PhoneNumber.CreateBuilder();
            phoneBuilder.SetType(Person.Types.PhoneType.MOBILE);
            phoneBuilder.SetNumber("+61 123456789");

            Person.Types.PhoneNumber phone2 = phoneBuilder.Build();
            personBuilder.AddPhone(phone1);
            personBuilder.AddPhone(phone2);

            addressBuilder = AddressBook.CreateBuilder();
            addressBuilder.AddPerson(person1);

            yield return(new object[]
            {
                addressBuilder.Build(),
                false
            });

            yield return(new object[]
            {
                addressBuilder.Build(),
                true
            });

            personBuilder = Person.CreateBuilder();
            personBuilder.SetId(2);
            personBuilder.SetName("姓名");
            personBuilder.SetEmail("*****@*****.**");

            personBuilder.AddPhone(phone2);
            personBuilder.AddPhone(phone1);

            Person person2 = personBuilder.Build();

            addressBuilder = AddressBook.CreateBuilder();
            addressBuilder.AddPerson(person1);
            addressBuilder.AddPerson(person2);

            yield return(new object[]
            {
                addressBuilder.Build(),
                false
            });

            yield return(new object[]
            {
                addressBuilder.Build(),
                true
            });
        }