public void GetDefaultLayoutPageTest()
        {
            //Arrange
            IEnumerable<Page> pages = new Page[1] { new Page { Name = "_ViewStart.cshtml", Content = "foo" } };
            _mockRegexService.Setup(r => r.GetDefaultLayoutPageName(It.IsAny<string>())).Returns("bar");

            //Act
            _target.GetDefaultLayoutPage(pages);

            //Assert
            _mockRegexService.Verify(r => r.GetDefaultLayoutPageName(It.IsAny<string>()), Times.Once());
        }
        public void FilterPagesTest()
        {
            //Arrange
            var foo = new Page { Name = "foo.cshtml" };
            var bar = new Page { Name = "bar.vbhtml" };
            var guf = new Page { Name = "guf.html" };
            IEnumerable<Page> pages = new Page[3] { foo, bar, guf };

            //Arrange
            IEnumerable<Page> actual = _target.FilterPages(pages);

            //Act
            Assert.AreEqual(2, actual.Count());
        }
        public void FilterPagesTest()
        {
            //Arrange
            var foo = new Page { Name = "foo.aspx" };
            var bar = new Page { Name = "bar.ascx" };
            var guf = new Page { Name = "guf.Master" };
            var baz = new Page { Name = "baz.html" };
            IEnumerable<Page> pages = new Page[4] { foo, bar, guf, baz };

            //Arrange
            IEnumerable<Page> actual = _target.FilterPages(pages);

            //Act
            Assert.AreEqual(3, actual.Count());
        }
        public void AddToParentsRecursiveTest()
        {
            //Arrange
            Page e = new Page { Name = "e", Parents = new List<Page>() };
            Page c = new Page { Name = "c", Parents = new List<Page> { e } };
            Page b = new Page { Name = "b", Parents = new List<Page> { c, e } };
            Page a = new Page { Name = "a", Parents = new List<Page> { b } };

            //Act
            var parents = new Dictionary<string, Page>();
            _target.AddToParentsRecursive(a, parents);

            //Assert
            Assert.AreEqual(3, parents.Count);
        }
        public void GetMasterPageTest()
        {
            //Arrange
            string pageName = "Index.cshtml";
            string pageContent = string.Empty;
            var page = new Page { Name = "_Layout.cshtml" };
            IEnumerable<Page> pagesList = new Page[1] { page };
            List<Page> parentList = new List<Page>();
            _mockRegexService.Setup(r => r.GetMultipleLayouts(It.IsAny<string>())).Returns(new string[1] { page.Name });

            //Act
            _target.GetMasterPage(pageName, pageContent, pagesList, parentList);

            //Assert
            _mockRegexService.Verify(r => r.GetMultipleLayouts(It.IsAny<string>()), Times.Once());
            Assert.AreEqual(parentList.Count, 1);
            Assert.AreEqual(parentList[0], page);
        }
        private Page GetLayoutPage(IEnumerable<Page> pages, Page currentPage)
        {
            string layoutPageName = _regexService.GetLayoutPageName(currentPage.Content);

            if (layoutPageName == "null")
            {
                return null;
            }

            if (string.IsNullOrEmpty(layoutPageName))
            {
                Page viewStart = pages.Where(p => p.Area == currentPage.Area && p.Name.Contains("_ViewStart")).SingleOrDefault();

                if (viewStart == null)
                {
                    viewStart = pages.Where(p => string.IsNullOrEmpty(p.Area) && p.Name.Contains("_ViewStart")).SingleOrDefault();
                }

                if (viewStart != null)
                {
                    layoutPageName = _regexService.GetLayoutPageName(viewStart.Content);
                }

                if (string.IsNullOrEmpty(layoutPageName))
                {
                    return null;
                }
            }

            Page layout = pages.Where(p => p.Area == currentPage.Area && p.Name.Contains(layoutPageName)).SingleOrDefault();

            if (layout == null)
            {
                var xxxxx = pages.Where(p => string.IsNullOrEmpty(p.Area) && p.Name.Contains(layoutPageName)).ToArray();
                layout = pages.Where(p => string.IsNullOrEmpty(p.Area) && p.Name.Contains(layoutPageName)).SingleOrDefault();
            }

            return layout;
        }
        private Dictionary<string, int> GetReferences(Page page)
        {
            var referencesList = _regexService.GetReferences(page.Content);

            var referencesFrequency = from reference in referencesList
                                      group reference by reference into g
                                      orderby g.Key descending
                                      select new
                                      {
                                          Reference = g.Key,
                                          ReferencesCount = g.Count()
                                      };

            return referencesFrequency.ToDictionary(e => e.Reference.Remove(0, 1), e => e.ReferencesCount);
        }
        protected void AddToParentsRecursive(Page page, Dictionary<string, Page> parents)
        {
            if (page.Parents.Count == 0)
            {
                return;
            }

            foreach (var item in page.Parents)
            {
                if (!parents.ContainsKey(item.Name))
                {
                    parents.Add(item.Name, item);
                }

                AddToParentsRecursive(item, parents);
            }
        }
        public void GetDuplicatesInLayoutAndFileTest()
        {
            //Arrange
            IPagesService pagesService = new WebFormPagesService(_mockRegexService.Object);
            var commonReference = "foo";

            Page parentPage = new Page
            {
                References = new Dictionary<string, int> { { commonReference, 1 }, { "bar", 1 } },
                Parents = new List<Page> ()
            };
            Page childPage = new Page
            {
                References = new Dictionary<string, int> { { commonReference, 1 }},
                Parents = new List<Page> { parentPage }
            };
            IEnumerable<Page> pagesList = new Page[2] { parentPage, childPage };

            //Act
            var result = pagesService.GetDuplicatesInLayoutAndFile(pagesList);

            //Assert
            Assert.AreEqual(result.Count(), 1);
            Assert.AreEqual(result.First().Duplicates.Count(), 1);
            Assert.AreEqual(result.First().Duplicates.ElementAt(0), commonReference);
        }
        public void GetReferencesTest()
        {
            //Arrange
            Page page = new Page();
            string[] referencesList = new string[3] { "_foo", "_foo", "_bar" };
            _mockRegexService.Setup(p => p.GetReferences(It.IsAny<string>())).Returns(referencesList);

            //Act
            var actual = _target.GetReferences(page);

            //Assert
            _mockRegexService.Verify(r => r.GetReferences(It.IsAny<string>()), Times.Once());
            Assert.AreEqual(2, actual.Count());
            Assert.AreEqual(actual["foo"], 2);
            Assert.AreEqual(actual["bar"], 1);
        }
        public void GetDuplicatesInSameFileTest()
        {
            //Arrange
            IPagesService pagesService = new WebFormPagesService(_mockRegexService.Object);
            IEnumerable<Page> pagesList = new Page[2]
            {
                new Page{References=new Dictionary<string,int>{{"shouldn't appear",1},{"ref",2}}},
                new Page{References=new Dictionary<string,int>()}
            };

            //Act
            var result = pagesService.GetDuplicatesInSameFile(pagesList);

            //Assert
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(1, result.First().References.Count);
        }
 private bool CheckPageHasDefaultLayout(Page item, string layout)
 {
     bool isMainPage = item.Name.Contains("_ViewStart") || item.Content.Contains("RenderBody");
     return item.Parents.Count == 0 && !isMainPage && !_regexService.CheckPageHasNullLayout(item.Content);
 }