Пример #1
0
        public void GroupByJokeSize_OnlyLongJokeSizes_ReturnsEmptyForOtherSizes()
        {
            // Arrange
            int expected = 0;

            var jokeLong1 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty"
            };
            var jokeLong2 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one"
            };

            var input = new JokeResponse[]
            {
                jokeLong1,
                jokeLong2
            };

            // Act
            var output            = DadJokeServiceHelpers.GroupByJokeSize(input);
            int actualShortCount  = output.Where(p => p.Key == JokeSize.Short.ToString()).Select(p => p.Value).SingleOrDefault().Count();
            int actualMediumCount = output.Where(p => p.Key == JokeSize.Medium.ToString()).Select(p => p.Value).SingleOrDefault().Count();

            // Assert
            Assert.AreEqual(expected, actualShortCount);
            Assert.AreEqual(expected, actualMediumCount);
        }
Пример #2
0
        public void GroupByJokeSize_AllJokeSizes_GroupsCorrectly()
        {
            // Arrange
            var jokeShort1 = new JokeResponse()
            {
                Joke = "one two three"
            };
            var jokeShort2 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine"
            };
            var jokeMedium1 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine ten"
            };
            var jokeMedium2 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen"
            };
            var jokeLong1 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty"
            };
            var jokeLong2 = new JokeResponse()
            {
                Joke = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one"
            };

            var input = new JokeResponse[]
            {
                jokeShort1,
                jokeShort2,
                jokeMedium1,
                jokeMedium2,
                jokeLong1,
                jokeLong2
            };

            // Act
            var output = DadJokeServiceHelpers.GroupByJokeSize(input);

            // Assert
            foreach (var group in output)
            {
                foreach (var joke in group.Value)
                {
                    Assert.AreEqual(group.Key, joke.Size);
                }
            }
        }
Пример #3
0
        public void GroupByJokeSize_EmptyInput_ReturnsEmptyForAllSizes()
        {
            // Arrange
            int expected = 0;

            var input = new JokeResponse[] { };

            // Act
            var output            = DadJokeServiceHelpers.GroupByJokeSize(input);
            int actualShortCount  = output.Where(p => p.Key == JokeSize.Short.ToString()).Select(p => p.Value).SingleOrDefault().Count();
            int actualMediumCount = output.Where(p => p.Key == JokeSize.Medium.ToString()).Select(p => p.Value).SingleOrDefault().Count();
            int actualLongCount   = output.Where(p => p.Key == JokeSize.Long.ToString()).Select(p => p.Value).SingleOrDefault().Count();

            // Assert
            Assert.AreEqual(expected, actualShortCount);
            Assert.AreEqual(expected, actualMediumCount);
            Assert.AreEqual(expected, actualLongCount);
        }
Пример #4
0
        /// <summary>
        /// Gets a collection of jokes by the search term provided. Supports multiple search terms, split by whitespaces.
        /// An OR operation is used by the API if more than one term is given.
        /// </summary>
        /// <param name="searchTerm">The term to search for jokes with.</param>
        /// <param name="limit">The maximum number of search results to return. Default is 30.</param>
        /// <returns>Collection of jokes that contain the search term provided.</returns>
        public async Task <JokeSearchResponse> GetBySearchTerm(string searchTerm, int limit = 30)
        {
            var responseMessage = await _httpClient.GetAsync(
                DadJokeServiceHelpers.GetSearchRequestUriWithQueryString(_endpointGetBySearchTerm, searchTerm, limit));

            responseMessage.EnsureSuccessStatusCode();

            string content = await responseMessage.Content.ReadAsStringAsync();

            var jokeSearchReponse = JsonConvert.DeserializeObject <JokeSearchResponse>(content);

            if (searchTerm != null)
            {
                // Account for cases where there are two or more terms in the search term
                string[] searchTermSplit = searchTerm.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                jokeSearchReponse.Results = DadJokeServiceHelpers.EmphasizeWithUppercase(jokeSearchReponse.Results, searchTermSplit);
            }

            jokeSearchReponse.ResultsBySize = DadJokeServiceHelpers.GroupByJokeSize(jokeSearchReponse.Results);

            return(jokeSearchReponse);
        }