public void CreatesMultipleTags()
        {
            var xaml = @"<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
</Grid>";

            var outputTags = new TagList();

            var sut = new GridProcessor(new ProcessorEssentialsForSimpleTests());

            var snapshot = new FakeTextSnapshot(xaml.Length);

            sut.Process("testfile.xaml", 0, xaml, string.Empty, snapshot, outputTags);

            Assert.AreEqual(3, outputTags.Count);
            Assert.AreEqual(2, outputTags.OfType <InsertRowDefinitionTag>().Count());
            Assert.AreEqual(1, outputTags.OfType <AddColumnDefinitionsTag>().Count());
        }
        public void PassNoXmlnsToAnalyzers()
        {
            var tags = new TagList();

            var xaml = "<Page>" +
                       Environment.NewLine + "    <TestElement />" +
                       Environment.NewLine + "</Page>";

            var snapshot = new FakeTextSnapshot(xaml.Length);
            var vsa      = new TestVisualStudioAbstraction();
            var logger   = DefaultTestLogger.Create();

            var analyzer = new XmnlsCounterAnalyzer();

            var processors = new List <(string, XamlElementProcessor)>
            {
                (analyzer.TargetType(), new CustomProcessorWrapper(analyzer, ProjectType.Any, string.Empty, logger, vsa)),
            };

            XamlElementExtractor.Parse("testfile.xaml", snapshot, xaml, processors, tags, null, RapidXamlDocument.GetEveryElementProcessor(ProjectType.Any, null, vsa), logger);

            Assert.AreEqual(0, analyzer.Count);
        }
        private void ParseWithoutError(string filePath, ProjectType projType)
        {
            var result = new RapidXamlDocument();

            var text = File.ReadAllText(filePath);

            var snapshot = new FakeTextSnapshot();

            try
            {
                XamlElementExtractor.Parse(
                    projType,
                    Path.GetFileName(filePath),
                    snapshot,
                    text,
                    RapidXamlDocument.GetAllProcessors(projType),
                    result.Tags);
            }
            catch (Exception exc)
            {
                Assert.Fail($"Parsing failed for '{filePath}'{Environment.NewLine}{exc}");
            }
        }
        private void CanParseWithoutErrors(string folderPath)
        {
            foreach (var filePath in GetXamlFiles(folderPath))
            {
                var text = File.ReadAllText(filePath);

                if (text.IsValidXml())
                {
                    var result = new RapidXamlDocument();

                    var snapshot = new FakeTextSnapshot();

                    XamlElementExtractor.Parse(ProjectType.Any, filePath, snapshot, text, RapidXamlDocument.GetAllProcessors(ProjectType.Any), result.Tags);

                    Debug.WriteLine($"Found {result.Tags.Count} taggable issues in '{filePath}'.");

                    if (result.Tags.Count > 0)
                    {
                        // if (result.Tags.Count > 10)
                        if (result.Tags.OfType <RapidXamlDisplayedTag>().Any())
                        {
                            Debugger.Break();
                        }

                        this.TestContext.WriteLine($"Found {result.Tags.Count} taggable issues in '{filePath}'.");
                        this.TestContext.AddResultFile(filePath);
                    }
                }
                else
                {
                    Debug.WriteLine($"Invalid XAML found in '{filePath}'.");

                    this.TestContext.WriteLine($"Invalid XAML found in '{filePath}'.");
                    this.TestContext.AddResultFile(filePath);
                }
            }
        }
        public void CheckEveryElementVisited()
        {
            var xaml = @"
<Page x:Name=""lowerCased1"">
    <Grid x:Name=""lowerCased2"">
        <TextBlock x:Name=""lowerCased3""></TextBlock>
        <TextBlock x:Name=""lowerCased4"" />
    </Grid>

    <TextBlock x:Name=""lowerCased5"" />
</Page>";

            var result = new RapidXamlDocument();

            var snapshot = new FakeTextSnapshot(xaml.Length);
            var logger   = DefaultTestLogger.Create();
            var vsa      = new TestVisualStudioAbstraction();

            var procesors = RapidXamlDocument.GetAllProcessors(ProjectType.Uwp, string.Empty, vsa, logger);

            XamlElementExtractor.Parse("Generic.xaml", snapshot, xaml, procesors, result.Tags, null, RapidXamlDocument.GetEveryElementProcessor(ProjectType.Uwp, null, vsa), logger);

            Assert.AreEqual(5, result.Tags.OfType <NameTitleCaseTag>().Count());
        }
        public void EnsureCustomAnalyzersGetTheRightAnalyzedElement()
        {
            var tags = new TagList();

            var xaml = "<Page>" +
                       Environment.NewLine + "<WebView></WebView>" +
                       Environment.NewLine + "<WebView></WebView>" +
                       Environment.NewLine + "</Page>";

            var snapshot = new FakeTextSnapshot(xaml.Length);
            var vsa      = new TestVisualStudioAbstraction();
            var logger   = DefaultTestLogger.Create();

            var processors = new List <(string, XamlElementProcessor)>
            {
                ("WebView", new CustomProcessorWrapper(new WebViewToWebView2Basic(), ProjectType.Any, string.Empty, logger, vsa)),
            };

            XamlElementExtractor.Parse("testfile.xaml", snapshot, xaml, processors, tags, null, RapidXamlDocument.GetEveryElementProcessor(ProjectType.Any, null, vsa), logger);

            Assert.AreEqual(2, tags.Count);
            Assert.AreEqual(8, (tags[0] as CustomAnalysisTag).AnalyzedElement.Location.Start);
            Assert.AreEqual(29, (tags[1] as CustomAnalysisTag).AnalyzedElement.Location.Start);
        }
        public void LazyLoading_Attributes_GetsCorrectLocations()
        {
            var xaml = "<StackPanel>" +
                       Environment.NewLine + "    <TextBlock Text=\"Repeated\" />" +
                       Environment.NewLine + "    <TextBlock Text=\"Repeated\" />" +
                       Environment.NewLine + "    <Grid>" +
                       Environment.NewLine + "        <TextBlock Text=\"Repeated\" />" +
                       Environment.NewLine + "    </Grid>" +
                       Environment.NewLine + "</StackPanel>";

            var result   = new RapidXamlDocument();
            var snapshot = new FakeTextSnapshot(xaml.Length);
            var logger   = DefaultTestLogger.Create();
            var vsa      = new TestVisualStudioAbstraction();

            var analyzer = new LazyLoadingTestAnalyzer();

            var processors = RapidXamlDocument.WrapCustomProcessors(
                new List <ICustomAnalyzer> {
                analyzer
            },
                ProjectType.Unknown,
                string.Empty,
                logger,
                vsa);

            XamlElementExtractor.Parse("Generic.xaml", snapshot, xaml, processors.ToList(), result.Tags, null, null, logger);

            var e1 = analyzer.Elements.First();
            var e2 = analyzer.Elements[1];
            var e3 = analyzer.Elements.Last();

            Assert.AreEqual(27 + Environment.NewLine.Length, e1.Attributes[0].Location.Start);
            Assert.AreEqual(60 + (Environment.NewLine.Length * 2), e2.Attributes[0].Location.Start);
            Assert.AreEqual(107 + (Environment.NewLine.Length * 4), e3.Attributes[0].Location.Start);
        }
        private void AnalyzeXamlFile(string xamlFilePath, IEnumerable <ICustomAnalyzer> analyzers, List <string> output, string projectFilePath = "")
        {
            output.Add($"Analyzing '{xamlFilePath}'");

            var text     = this.FileSystem.GetAllFileText(xamlFilePath);
            var snapshot = new FakeTextSnapshot(text.Length);

            var logger = EmptyLogger.Create();
            var vspfp  = new AutoFixProjectFilePath(projectFilePath);

            try
            {
                var processors = new List <(string, XamlElementProcessor)>();

                foreach (var analyzer in analyzers)
                {
                    output.Add($"Will analyze instances of '{analyzer.TargetType()}'.");

                    processors.Add(
                        (analyzer.TargetType(),
                         new CustomProcessorWrapper(analyzer, ProjectType.Any, projectFilePath, logger, vspfp)));
                }

                var tags = new TagList();

                XamlElementExtractor.Parse("Generic.xaml", snapshot, text, processors, tags, null, null, logger);

                var plural = tags.Count == 1 ? string.Empty : "s";
                output.Add($"Found {tags.Count} place{plural} to make changes.");

                tags.Reverse();  // Work back through the document to allow for modifications changing document length

                var finalActions = new List <CustomAnalysisTag>();

                foreach (var tag in tags)
                {
                    // This always should be a CustomAnalysisTag but doesn't hurt to check when casting.
                    if (tag is CustomAnalysisTag cat)
                    {
                        if (cat.Action == ActionType.AddXmlns)
                        {
                            finalActions.Add(cat);
                        }

                        var newElement = this.UpdateElementXaml(cat, output);

                        text = text.Substring(0, cat.AnalyzedElement.Location.Start) + newElement + text.Substring(cat.AnalyzedElement.Location.End());

                        foreach (var suppAction in cat.SupplementaryActions)
                        {
                            var sat = this.RepurposeTagForSupplementaryAction(cat, suppAction, newElement);

                            if (sat.Action == ActionType.AddXmlns)
                            {
                                finalActions.Add(sat);
                            }
                            else
                            {
                                newElement = this.UpdateElementXaml(sat, output);
                                text       = text.Substring(0, sat.AnalyzedElement.Location.Start) + newElement + text.Substring(sat.AnalyzedElement.Location.End());
                            }
                        }
                    }
                }

                foreach (var faTag in finalActions)
                {
                    if (faTag.Action == ActionType.AddXmlns)
                    {
                        text = this.AddXmlns(faTag, text, output);
                    }
                }

                this.FileSystem.WriteAllFileText(xamlFilePath, text);
            }
            catch (Exception exc)
            {
                output.Add(exc.Message);
            }
        }
예제 #9
0
        private void CanParseWithoutErrors(string folderPath)
        {
            foreach (var filePath in GetXamlFiles(folderPath))
            {
                var text = File.ReadAllText(filePath);

                if (text.IsValidXml())
                {
                    var result = new RapidXamlDocument();

                    var snapshot = new FakeTextSnapshot();
                    var vsa      = new TestVisualStudioAbstraction();
                    var logger   = DefaultTestLogger.Create();

                    var processors = RapidXamlDocument.GetAllProcessors(ProjectType.Any, string.Empty, vsa, logger);

                    var customProcessor = new CustomProcessorWrapper(new StubCustomAnalysisProcessor(), ProjectType.Any, string.Empty, logger, vsa);

                    processors.Add(("Application", customProcessor));
                    processors.Add(("Page", customProcessor));
                    processors.Add(("DrawingGroup", customProcessor));
                    processors.Add(("ResourceDictionary", customProcessor));
                    processors.Add(("UserControl", customProcessor));
                    processors.Add(("Canvas", customProcessor));
                    processors.Add(("Viewbox", customProcessor));
                    processors.Add(("PhoneApplicationPage", customProcessor));
                    processors.Add(("Window", customProcessor));
                    processors.Add(("ContentPage", customProcessor));
                    processors.Add(("MasterDetailPage", customProcessor));
                    processors.Add(("NavigationPage", customProcessor));
                    processors.Add(("TabbedPage", customProcessor));
                    processors.Add(("CarouselPage", customProcessor));
                    processors.Add(("TemplatedPage", customProcessor));
                    processors.Add(("Shell", customProcessor));

                    XamlElementExtractor.Parse(filePath, snapshot, text, processors, result.Tags, null, null, logger);

                    Debug.WriteLine($"Found {result.Tags.Count} taggable issues in '{filePath}'.");

                    if (result.Tags.Count > 0)
                    {
                        // if (result.Tags.Count > 10)
                        if (result.Tags.OfType <RapidXamlDisplayedTag>().Any())
                        {
                            // This can be useful to examine what is being tagged.
                            Debugger.Break();
                        }

                        this.TestContext.WriteLine($"Found {result.Tags.Count} taggable issues in '{filePath}'.");
                        this.TestContext.AddResultFile(filePath);
                    }
                }
                else
                {
                    Debug.WriteLine($"Invalid XAML found in '{filePath}'.");

                    this.TestContext.WriteLine($"Invalid XAML found in '{filePath}'.");
                    this.TestContext.AddResultFile(filePath);
                }
            }
        }