예제 #1
0
        public void WordsProviderCanBeCreatedUsingItsConstructorWhenMinimumLengthIsGreaterThan4()
        {
            WordsProvider provider = new WordsProvider(_configurationMock);

            Assert.IsNotNull(provider);
            Assert.AreEqual(4, provider.MinimumWordLength);
        }
예제 #2
0
        public Result <ITagCloud> GenerateTagCloud()
        {
            var filteredWords = WordsProvider.GetWords()
                                .Then(words => WordsFilter.Apply(words));

            if (!filteredWords.IsSuccess)
            {
                return(Result.Fail <ITagCloud>(filteredWords.Error));
            }

            var wordsFrequencies = GetTokens(filteredWords.Value)
                                   .OrderByDescending(wordToken => wordToken.Frequency)
                                   .ToArray();
            var wordsCount = wordsFrequencies.Select(wordToken => wordToken.Frequency).Sum();

            foreach (var wordToken in wordsFrequencies)
            {
                var width  = Math.Max(100, 500 * wordToken.Frequency / wordsCount);
                var height = Math.Max(50, 250 * wordToken.Frequency / wordsCount);
                var size   = new Size(width, height);
                PutNextWord(wordToken.Word, size);
            }

            return(this);
        }
예제 #3
0
        public void CreateNodesFromDictionaryFileReturnsEmptyDictionaryIfFileCouldNotBeLoaded()
        {
            WordsProvider provider = new WordsProvider(_configurationMock);
            Dictionary <char, TreeNode> results = provider.CreateNodesFromDictionaryFile("fakeFile.txt");

            Assert.IsNotNull(results);
            Assert.IsTrue(!results.Any());
        }
예제 #4
0
        public void WordsProviderCanBeCreatedUsingItsConstructorWhenMinimumLengthIsLowerThan4()
        {
            var configurationMockSetup = new Mock <IConfiguration>();

            configurationMockSetup.Setup(x => x["MinimumWordLength"]).Returns("1");

            WordsProvider provider = new WordsProvider(configurationMockSetup.Object);

            Assert.IsNotNull(provider);
            Assert.AreEqual(4, provider.MinimumWordLength);
        }
예제 #5
0
        public void GenerateTagCloud()
        {
            var words            = WordsProvider.GetWords();
            var filteredWords    = WordsFilter.Apply(words);
            var wordsFrequencies = GetTokens(filteredWords)
                                   .OrderByDescending(wordToken => wordToken.Frequency);
            var wordsCount = wordsFrequencies.Select(wordToken => wordToken.Frequency).Sum();

            foreach (var wordToken in wordsFrequencies)
            {
                var width  = Math.Max(100, 500 * wordToken.Frequency / wordsCount);
                var height = Math.Max(50, 250 * wordToken.Frequency / wordsCount);
                var size   = new Size(width, height);
                PutNextWord(wordToken.Word, size);
            }
        }
        public async Task GetWordsByCategoryOrEmptyAsync_ReturnsEmptyIfResponseStatusCodeIsNotSuccessful()
        {
            // Arrange
            var clientFactoryMock =
                GetHttpClientFactoryMock(
                    HttpStatusCode.BadRequest, InvalidResponseText);
            var systemUnderTest =
                new WordsProvider(_gameOptionsMock, clientFactoryMock, _loggerMock);

            // Act
            var wordsByCategory = await systemUnderTest.GetWordsByCategoryOrEmptyAsync(Category);

            // Assert
            wordsByCategory.Should()
            .BeEmpty();
        }
        public async Task GetWordsByCategoryAsync_ThrowsIfResponseStatusCodeIsNotSuccessful()
        {
            // Arrange
            var clientFactoryMock =
                GetHttpClientFactoryMock(
                    HttpStatusCode.BadRequest, InvalidResponseText);
            var systemUnderTest =
                new WordsProvider(_gameOptionsMock, clientFactoryMock, _loggerMock);

            // Act
            Func <Task <IReadOnlyCollection <string> > > action =
                () => systemUnderTest.GetWordsByCategoryAsync(Category);

            // Assert
            await action.Should()
            .ThrowAsync <HttpRequestException>();
        }
        public async Task GetWordsByCategoryOrEmptyAsync_ReturnsEmptyIfResponseCannotBeParsed(
            string serverResponseContent)
        {
            // Arrange
            var clientFactoryMock =
                GetHttpClientFactoryMock(
                    HttpStatusCode.OK, serverResponseContent);
            var systemUnderTest =
                new WordsProvider(_gameOptionsMock, clientFactoryMock, _loggerMock);

            // Act
            var wordsByCategory = await systemUnderTest.GetWordsByCategoryOrEmptyAsync(Category);

            // Assert
            wordsByCategory.Should()
            .BeEmpty();
        }
        public async Task GetWordsByCategoryAsync_ThrowsIfResponseCannotBeParsed(
            string serverResponseContent)
        {
            // Arrange
            var clientFactoryMock =
                GetHttpClientFactoryMock(
                    HttpStatusCode.OK, serverResponseContent);
            var systemUnderTest =
                new WordsProvider(_gameOptionsMock, clientFactoryMock, _loggerMock);

            // Act
            Func <Task <IReadOnlyCollection <string> > > action =
                () => systemUnderTest.GetWordsByCategoryAsync(Category);

            // Assert
            await action.Should()
            .ThrowAsync <JsonReaderException>();
        }
        public async Task GetWordsByCategoryOrEmptyAsync_CanReturnWordsByCategory()
        {
            // Arrange
            var clientFactoryMock =
                GetHttpClientFactoryMock(
                    HttpStatusCode.OK, ServerResponses.WordAssociationsJsonResponse);
            var systemUnderTest =
                new WordsProvider(_gameOptionsMock, clientFactoryMock, _loggerMock);

            // Act
            var wordsByCategory = await systemUnderTest.GetWordsByCategoryOrEmptyAsync(Category);

            // Assert
            wordsByCategory.Should()
            .Contain("gardening");

            wordsByCategory.Should()
            .Contain("collecting");

            wordsByCategory.Should()
            .Contain("cooking");
        }