예제 #1
0
        public void ReplaceWith_ArgumentsNull_ArgumentNullException()
        {
            // Setup Fake Data
            var joke = new Joke {
                Value = "Some joke"
            };

            // Execute & Verify mocks and assertions
            Assert.Throws <ArgumentNullException>(() => joke.ReplaceWith(null));
        }
예제 #2
0
        public void ReplaceWith_JokeIsNull_ArgumentNullException()
        {
            // Setup Fake Data
            Joke joke     = null;
            var  userName = new UserName {
                Name = "John", Surname = "Doe"
            };

            // Execute & Verify mocks and assertions
            Assert.Throws <ArgumentNullException>(() => joke.ReplaceWith(userName));
        }
예제 #3
0
        public void ReplaceWith_DoesNotMatch_DoesNotReplace()
        {
            // Setup Fake Data
            var joke = new Joke {
                Value = "Some joke"
            };
            var userName = new UserName {
                Name = "John", Surname = "Doe"
            };

            // Execute Test
            var result = joke.ReplaceWith(userName);

            // Verify mocks and assertions
            Assert.IsNotNull(result);
            Assert.AreEqual(joke.Value, result.Value);
        }
예제 #4
0
        public void ReplaceWith_Match_Replace()
        {
            // Setup Fake Data
            var chuck  = "Chuck";
            var norris = "Norris";
            var value  = $"Some joke with {chuck} {norris}";
            var joke   = new Joke {
                Value = value
            };
            var userName = new UserName {
                Name = "John", Surname = "Doe"
            };

            // Execute Test
            var result = joke.ReplaceWith(userName);

            // Verify mocks and assertions
            Assert.IsNotNull(result);
            Assert.AreNotEqual(value, result.Value);
            Assert.IsTrue(result.Value.Contains(userName.Name));
            Assert.IsTrue(result.Value.Contains(userName.Surname));
            Assert.IsFalse(result.Value.Contains(chuck));
            Assert.IsFalse(result.Value.Contains(norris));
        }
예제 #5
0
        private async Task ProcessRandomJokesAsync()
        {
            var useRandomName = Tuple.Create(false, (UserInfo)null);
            var useCategory   = Tuple.Create(false, (string)null);

            Output.Write("Want to use a random name? y/n");
            var input = Input.ReadKey();

            if (input == 'y')
            {
                var userNamesTask = UserInfoClient.GetUserInfoAsync();
                await AwaitTasksWithIndicator(userNamesTask);

                useRandomName = Tuple.Create(true, await userNamesTask);
            }

            Output.Write("Want to specify a category? y/n");
            input = Input.ReadKey();
            if (input == 'y')
            {
                var categoriesTask = ChuckNorrisClient.GetCategoriesAsync();
                await AwaitTasksWithIndicator(categoriesTask);

                PrintResults("Enter a category ", (await categoriesTask).ToList());
                var category = Input.ReadString();
                useCategory = Tuple.Create(true, category);
            }

            int inputNumber = 0;

            while (1 > inputNumber || inputNumber > 9)
            {
                Output.Write("How many jokes do you want? (1-9)");
                inputNumber = Input.ReadNumber();

                if (1 > inputNumber || inputNumber > 9)
                {
                    Output.Write("You input is wrong number. Please try again.");
                }
            }

            if (inputNumber > 1)
            {
                Output.Write("Want to process in parallel? y/n");
                input = Input.ReadKey();
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            var  results   = new List <string>();
            var  jokeTasks = new Task <Joke> [inputNumber];
            Joke joke      = null;

            if (inputNumber > 1 && input == 'y')
            {
                for (int i = 0; i < inputNumber; i++)
                {
                    var jokeTask = useCategory.Item1 ?
                                   ChuckNorrisClient.GetJokeByCategoryNameAsync(useCategory.Item2) :
                                   ChuckNorrisClient.GetRandonJokeAsync();

                    jokeTasks[i] = jokeTask;
                }

                try
                {
                    await AwaitTasksWithIndicator(jokeTasks);

                    foreach (var jokeTask in jokeTasks)
                    {
                        joke = await jokeTask;

                        if (useRandomName.Item1)
                        {
                            joke = joke.ReplaceWith(useRandomName.Item2);
                        }

                        results.Add(joke.Value);
                    }
                }
                catch (HttpRequestException e)
                {
                    Output.Write(e.Message);
                    Output.Write("Hmm. Why did you do that?");
                }
            }
            else
            {
                try
                {
                    for (int i = 0; i < inputNumber; i++)
                    {
                        var jokeTask = useCategory.Item1 ?
                                       ChuckNorrisClient.GetJokeByCategoryNameAsync(useCategory.Item2) :
                                       ChuckNorrisClient.GetRandonJokeAsync();

                        await AwaitTasksWithIndicator(jokeTask);

                        joke = await jokeTask;

                        if (useRandomName.Item1)
                        {
                            joke = joke.ReplaceWith(useRandomName.Item2);
                        }

                        results.Add(joke.Value);
                    }
                }
                catch (HttpRequestException e)
                {
                    Output.Write(e.Message);
                    Output.Write("Hmm. Why did you do that?");
                }
            }

            sw.Stop();

            PrintResults(string.Empty, results);
            Output.WriteFormat("Done in {0} milliseconds.", sw.ElapsedMilliseconds);
        }