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); }
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); } } }
public void GetSearchRequestUriWithQueryString_ValidInputs_ReturnsCorrectly() { // Arrange string expected = "search?limit=30&term=dog"; string inputEndpoint = "search"; string inputTerm = "dog"; int inputLimit = 30; // Act string actual = DadJokeServiceHelpers.GetSearchRequestUriWithQueryString(inputEndpoint, inputTerm, inputLimit); // Assert Assert.AreEqual(expected, actual); }
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); }
/// <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); }
public void EmphasizeWithUppercase_ZeroMatches_DoesNotEmphasize() { // Arrange string expected = "this is a bird joke"; var inputJokeResponses = new JokeResponse[] { new JokeResponse() { Joke = "this is a bird joke" } }; var inputTermsToEmphasize = new string[] { "cat", "dog" }; // Act string actual = DadJokeServiceHelpers .EmphasizeWithUppercase(inputJokeResponses, inputTermsToEmphasize). Single() .Joke; // Assert Assert.AreEqual(expected, actual); }
public void EmphasizeWithUppercase_MultipleSearchTermOneMatch_EmphasizesMatchingTermOnly() { // Arrange string expected = "this is a DOG joke"; var inputJokeResponses = new JokeResponse[] { new JokeResponse() { Joke = "this is a dog joke" } }; var inputTermsToEmphasize = new string[] { "cat", "dog" }; // Act string actual = DadJokeServiceHelpers .EmphasizeWithUppercase(inputJokeResponses, inputTermsToEmphasize). Single() .Joke; // Assert Assert.AreEqual(expected, actual); }