Esempio n. 1
0
        public async Task <IList <NewsItem> > GetNews(int postNumber)
        {
            var newsList   = new List <NewsItem>();
            var pageNumber = 1;
            var moreNews   = true;

            while (newsList.Count < postNumber && pageNumber < PageLimit && moreNews)
            {
                var address = $"https://news.ycombinator.com/news?p={pageNumber}"; //Get from config

                var document = await pageLoader.LoadPage(address);

                var news = htmlDocumentParser.ParseNews(document);

                if (!news.Any())
                {
                    moreNews = false;
                    continue;
                }

                newsList.AddRange(news);

                pageNumber++;
            }

            return(newsList.Take(postNumber).ToList());
        }
Esempio n. 2
0
        public async Task GivenNumberOfRequestedNews_WhenGetNewsIsCalled_ThenNumberOfRequestedNewsAreReturned()
        {
            var numberOfNews = 2;
            var document     = Substitute.For <IDocument>();

            pageLoader.LoadPage(Arg.Is <string>(s => s.Contains("p=1"))).Returns(document);

            hackerNewsHtmlDocumentParser.ParseNews(document)
            .Returns(new List <NewsItem> {
                new NewsItem(), new NewsItem(), new NewsItem()
            });

            var news = await newsProvider.GetNews(numberOfNews);

            news.Count.Should().Be(numberOfNews);
        }
        public async Task InitializeAsync()
        {
            var html = await File.ReadAllTextAsync("./hackernewsHtml.html");

            var context = BrowsingContext.New(Configuration.Default);

            document = await context.OpenAsync(req => req.Content(html));

            pageLoader = NSubstitute.Substitute.For <IAngleSharpPageLoader>();
            pageLoader.LoadPage(Arg.Any <string>()).Returns(document);
            hackerNewsFieldsParser = Substitute.For <INewsParser>();
            hackerNewsFieldsParser.ParseTitle(Arg.Any <string>()).Returns("Title");
            hackerNewsFieldsParser.ParseUser(Arg.Any <string>()).Returns("User");
            hackerNewsFieldsParser.TryParseUrl(Arg.Any <string>(), out Arg.Any <Uri>())
            .Returns(x => {
                x[1] = new Uri("http://test.com");
                return(true);
            });
            hackerNewsFieldsParser.TryParsePoints(Arg.Any <string>(), out Arg.Any <int>())
            .Returns(x => {
                x[1] = 10;
                return(true);
            });
            hackerNewsFieldsParser.TryParseRank(Arg.Any <string>(), out Arg.Any <int>())
            .Returns(x => {
                x[1] = 20;
                return(true);
            });
            hackerNewsFieldsParser.TryParseComments(Arg.Any <string>(), out Arg.Any <int>())
            .Returns(x => {
                x[1] = 30;
                return(true);
            });

            documentParser = new HackerNewsHtmlDocumentParser(hackerNewsFieldsParser);
        }