Exemplo n.º 1
0
        static async Task Main()
        {
            var wordsToCount     = new[] { "war", "peace" };
            var dataSource       = new FileDataSource("WarAndPeace.txt");
            var wordCountService = new WordCounterService(dataSource);
            var warCount         = 0;
            var peaceCount       = 0;
            var linesProcessed   = 0;

            var progress = new Progress <int>(n => linesProcessed += n);

            await foreach (var update in wordCountService.GetWordCountUpdates(wordsToCount, CancellationToken.None, progress))
            {
                if (update.Word == "war")
                {
                    warCount += update.OccurrencesCount;
                }
                else
                {
                    peaceCount += update.OccurrencesCount;
                }

                Console.SetCursorPosition(0, 0);
                Console.WriteLine($"Lines processed: {linesProcessed} war: {warCount} peace {peaceCount}");
            }
        }
        public HomeController(IWordCountRepository wordCountRepository, ILoggerFactory loggerFactory)
        {
            var webPageReaderService = new WebPageReaderService();

            _textSplitterService = new TextSplitterService(webPageReaderService);
            _wordCounterService  = new WordCounterService(wordCountRepository, loggerFactory);
        }
Exemplo n.º 3
0
        public Resolver Initialize()
        {
            var parser      = new ParserCombinator(new StartStriper(), new EndStriper(), new PageParser());
            var wordCounter = new WordCounterService(new WordCounter(), parser, new FileStoreReader());

            _resolver.Register(wordCounter);

            return(_resolver);
        }
        public void Count_ReturnsExpectedWordCount(string testDescription, string text, int expectedWordCount)
        {
            // Arrange
            var sut = new WordCounterService();

            // Act
            var actualCount = sut.Count(text);

            // Assert
            Assert.AreEqual(expectedWordCount, actualCount, testDescription);
        }
Exemplo n.º 5
0
        private void WritingRichEditBox_TextChanged(object sender, RoutedEventArgs e)
        {
            if (!textLoadedIn)
            {
                UnsavedWork = true;
            }
            textLoadedIn = false;
            var reb = (RichEditBox)sender;

            reb.Document.GetText(TextGetOptions.None, out string rebContent);
            WordCounterService.CalculateWordCount(rebContent);
        }
        public void GetWordCountUpdates_DataSourceThrowsAndException_RethrowsTheException()
        {
            async IAsyncEnumerable <string> StreamData()
            {
                await Task.FromException(new IOException("hard disk is corrupted"));

                yield break; // otherwise compiler will complain "not all path return a value"
            }

            var dataSource = new Mock <IDataSource>();

            dataSource.Setup(x => x.GetData()).Returns(StreamData);
            var service = new WordCounterService(dataSource.Object);

            var firstUpdate = service.GetWordCountUpdates(new[] { "foo" }).FirstAsync();

            Assert.That(async() => await firstUpdate, Throws.InstanceOf <IOException>());
        }
Exemplo n.º 7
0
        public void GetWordCountUpdates_CanBeCancelledAfterItemInStream()
        {
            var dataSource        = new Mock <IDataSource>();
            var source            = new CancellationTokenSource();
            var cancellationToken = source.Token;

            async IAsyncEnumerable <string> StreamData()
            {
                yield return("foo");

                await Task.CompletedTask;
            }

            dataSource.Setup(x => x.GetData()).Returns(StreamData);
            var service    = new WordCounterService(dataSource.Object);
            var enumerator = service.GetWordCountUpdates(new[] { "foo" }, cancellationToken).GetAsyncEnumerator(cancellationToken);

            source.Cancel();

            Assert.That(async() => await enumerator.MoveNextAsync(), Throws.TypeOf <OperationCanceledException>());
        }
Exemplo n.º 8
0
 public InfoBarViewModel()
 {
     _wordCounterService = App.WordCounterService;
     _wordCounterService.WordCountChanged += _wordCounterService_WordCountChanged;
 }
 public void BeforeEachTest()
 {
     m_DataSource         = new Mock <IDataSource>();
     m_WordCounterService = new WordCounterService(m_DataSource.Object);
 }
Exemplo n.º 10
0
 public AdminController(IWordCountRepository repository, ILoggerFactory loggerFactory)
 {
     _service = new WordCounterService(repository, loggerFactory);
 }