private void AssertSingleTagAtLocation(string xaml, ICustomAnalyzer analyzer, int startPoint) { var tags = new TagList(); var vsa = new TestVisualStudioAbstraction(); var logger = DefaultTestLogger.Create(); var processors = new List <(string, XamlElementProcessor)> { (analyzer.TargetType(), new CustomProcessorWrapper(analyzer, ProjectType.Uwp, string.Empty, logger, vsa)), }; var snapshot = new FakeTextSnapshot(xaml.Length); XamlElementExtractor.Parse( "SomeTestFile.xaml", snapshot, xaml, processors, tags, null, null, logger); Assert.AreEqual(1, tags.Count); Assert.AreEqual(startPoint, (tags[0] as CustomAnalysisTag).Span.Start); }
public void CanParseWithoutErrors(string filePath) { var result = new RapidXamlDocument(); var text = File.ReadAllText(filePath); if (text.IsValidXml()) { var snapshot = new RapidXamlToolkit.Tests.FakeTextSnapshot(); XamlElementExtractor.Parse(snapshot, text, RapidXamlDocument.GetAllProcessors(), 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 <RapidXamlWarningTag>().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 static string GetSubElementAtPosition(ProjectType projectType, string fileName, ITextSnapshot snapshot, string xaml, int position, ILogger logger, string projectFile, IVisualStudioAbstraction vsAbstraction) { var startPos = xaml.LastIndexOf('<', position, position); var elementName = GetElementName(xaml.AsSpan(), startPos); string result = null; var processor = new SubElementProcessor(new ProcessorEssentials(projectType, logger, projectFile, vsAbstraction)); processor.SubElementFound += (s, e) => { result = e.SubElement; }; XamlElementExtractor.Parse(projectType, fileName, snapshot, xaml.Substring(startPos), new List <(string element, XamlElementProcessor processor)> { (elementName, processor), }, new TagList(), vsAbstraction, skipEveryElementProcessor: true); #if DEBUG if (result == null) { // If get here it's because there's a subelement that can't be identified correctly by XamlElementExtractor // but was detected elsewhere. (Probably by something extending XamlElementProcessor.) System.Diagnostics.Debugger.Break(); } #endif return(result); }
public static string GetSubElementAtPosition(ProjectType projectType, string fileName, ITextSnapshot snapshot, string xaml, int position, ILogger logger) { var startPos = xaml.Substring(0, position).LastIndexOf('<'); var elementName = xaml.Substring(startPos + 1, xaml.IndexOfAny(new[] { ' ', '>', '\r', '\n' }, startPos) - startPos - 1); string result = null; var processor = new SubElementProcessor(projectType, logger); processor.SubElementFound += (s, e) => { result = e.SubElement; }; XamlElementExtractor.Parse(projectType, fileName, snapshot, xaml.Substring(startPos), new List <(string element, XamlElementProcessor processor)> { (elementName, processor), }, new TagList()); #if DEBUG if (result == null) { // If get here it's because there's a subelement that can't be identified correctly by XamlElementExtractor // but was detected elsewhere. (Probably by something extending XamlElementProcessor.) System.Diagnostics.Debugger.Break(); } #endif return(result); }
public void AnalyzeAllCommaSeparatedTypes() { var xaml = @" <StackPanel> <TextBlock /> <TextBox /> </StackPanel>"; var result = new RapidXamlDocument(); var snapshot = new FakeTextSnapshot(xaml.Length); var logger = DefaultTestLogger.Create(); var vsa = new TestVisualStudioAbstraction(); var processors = RapidXamlDocument.WrapCustomProcessors( new List <ICustomAnalyzer> { new TextTypesCustomAnalyzer() }, ProjectType.Unknown, string.Empty, logger, vsa); XamlElementExtractor.Parse("Generic.xaml", snapshot, xaml, processors.ToList(), result.Tags, null, null, logger); Assert.AreEqual(2, result.Tags.Count()); }
public void PassMultipleXmlnsToAnalyzers_SingleQuotes() { var tags = new TagList(); var xaml = "<Page" + Environment.NewLine + " xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" + Environment.NewLine + " xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'" + Environment.NewLine + " xmlns:local='using:XamlChangeTest'" + Environment.NewLine + " xmlns:d='http://schemas.microsoft.com/expression/blend/2008'" + Environment.NewLine + " xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'>" + 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(5, analyzer.Count); Assert.AreEqual("http://schemas.microsoft.com/winfx/2006/xaml/presentation", analyzer.Xmlns[string.Empty]); }
public void CanGetXmlns_WhenSplitOverMultipleLines() { var tags = new TagList(); var xaml = "<Page" + Environment.NewLine + " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" + Environment.NewLine + " xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"" + Environment.NewLine + " xmlns:local" + Environment.NewLine + "=\"using:XamlChangeTest\"" + Environment.NewLine + " xmlns:d=" + Environment.NewLine + "\"http://schemas.microsoft.com/expression/blend/2008\"" + Environment.NewLine + " xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\">" + 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(5, analyzer.Count); foreach (var key in analyzer.Xmlns.Keys) { Assert.IsFalse(string.IsNullOrEmpty(analyzer.Xmlns[key]), $"No value for key '{key}'"); } }
public void OnlyElementsThatContainAreAnalyzed() { var xaml = @" <StackPanel> <TextBlock Text=""Something"" /> <TextBlock Text=""{Binding SomeVmProperty}"" /> <TextBlock Text=""{x:Bind}"" /> </StackPanel>"; var result = new RapidXamlDocument(); var snapshot = new FakeTextSnapshot(xaml.Length); var logger = DefaultTestLogger.Create(); var vsa = new TestVisualStudioAbstraction(); var processors = RapidXamlDocument.WrapCustomProcessors( new List <ICustomAnalyzer> { new AnyOrChildrenContainingCustomAnalyzer() }, ProjectType.Unknown, string.Empty, logger, vsa); XamlElementExtractor.Parse("Generic.xaml", snapshot, xaml, processors.ToList(), result.Tags, null, null, logger); // This will be two becuase called for the 2nd TextBlock and also the containing StackPanel Assert.AreEqual(2, AnyOrChildrenContainingCustomAnalyzer.AnalyzeCallCount); }
public void AnalyzerGetsCorrectContent() { var xaml = @" <StackPanel> <TextBlock><Page.Resources>NONE</Page.Resources></TextBlock> </StackPanel>"; var result = new RapidXamlDocument(); var snapshot = new FakeTextSnapshot(xaml.Length); var logger = DefaultTestLogger.Create(); var vsa = new TestVisualStudioAbstraction(); var analyzer = new XmlEncodingTestAnalyzer(); 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); Assert.AreEqual("<Page.Resources>NONE</Page.Resources>", analyzer.Element.Content); }
private void ParseWithoutError(string filePath, ProjectType projType) { var result = new RapidXamlDocument(); var text = File.ReadAllText(filePath); var snapshot = new FakeTextSnapshot(); var vsa = new TestVisualStudioAbstraction(); var logger = DefaultTestLogger.Create(); try { XamlElementExtractor.Parse( Path.GetFileName(filePath), snapshot, text, RapidXamlDocument.GetAllProcessors(projType, string.Empty, vsa, logger), result.Tags, null, RapidXamlDocument.GetEveryElementProcessor(projType, null, vsa), logger); } catch (Exception exc) { Assert.Fail($"Parsing failed for '{filePath}'{Environment.NewLine}{exc}"); } }
private void AnalyzeXamlFile(string xamlFilePath, IEnumerable <ICustomAnalyzer> analyzers, List <string> output) { output.Add($"Analyzing '{xamlFilePath}'"); var text = this.FileSystem.GetAllFileText(xamlFilePath); var snapshot = new FakeTextSnapshot(); var logger = EmptyLogger.Create(); var vsAbstraction = new AutoFixVisualStudioAbstraction(); 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, string.Empty, logger, vsAbstraction))); } 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 // TODO ISSUE#394: consider adding an actiontype to include xmlns at top level of document foreach (var tag in tags) { // This always should be a CustomAnalysisTag but doesn't hurt to check when casting. if (tag is CustomAnalysisTag 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); newElement = this.UpdateElementXaml(sat, output); text = text.Substring(0, sat.AnalyzedElement.Location.Start) + newElement + text.Substring(sat.AnalyzedElement.Location.End()); } } } this.FileSystem.WriteAllFileText(xamlFilePath, text); } catch (Exception exc) { output.Add(exc.Message); } }
public void Parse(string fileName) { var result = new RapidXamlDocument(); var text = File.ReadAllText($".\\files\\{fileName}"); var snapshot = new FakeTextSnapshot(); XamlElementExtractor.Parse(ProjectType.Uwp, fileName, snapshot, text, RapidXamlDocument.GetAllProcessors(ProjectType.Uwp, string.Empty, new TestVisualStudioAbstraction(), DefaultTestLogger.Create()), result.Tags, new TestVisualStudioAbstraction()); }
public void Parse(string fileName) { var result = new RapidXamlDocument(); var text = File.ReadAllText($".\\files\\{fileName}"); var snapshot = new FakeTextSnapshot(text.Length); var vsa = new TestVisualStudioAbstraction(); var logger = DefaultTestLogger.Create(); XamlElementExtractor.Parse(fileName, snapshot, text, RapidXamlDocument.GetAllProcessors(ProjectType.Uwp, string.Empty, vsa, logger), result.Tags, null, RapidXamlDocument.GetEveryElementProcessor(ProjectType.Uwp, null, vsa), logger); }
public void Real_Generic() { var result = new RapidXamlDocument(); var text = File.ReadAllText(".\\Misc\\Generic.xaml"); var snapshot = new FakeTextSnapshot(); XamlElementExtractor.Parse(ProjectType.Uwp, "Generic.xaml", snapshot, text, RapidXamlDocument.GetAllProcessors(ProjectType.Uwp), result.Tags); Assert.AreEqual(0, result.Tags.OfType <MissingRowDefinitionTag>().Count()); Assert.AreEqual(0, result.Tags.OfType <MissingColumnDefinitionTag>().Count()); }
public void Generic() { var result = new RapidXamlDocument(); var text = File.ReadAllText(".\\XamlAnalysis\\TestDocs\\Generic.xaml"); var snapshot = new RapidXamlToolkit.Tests.FakeTextSnapshot(); XamlElementExtractor.Parse(snapshot, text, RapidXamlDocument.GetAllProcessors(), result.Tags); Assert.AreEqual(0, result.Tags.OfType <MissingRowDefinitionTag>().Count()); Assert.AreEqual(0, result.Tags.OfType <MissingColumnDefinitionTag>().Count()); }
public void Real_Generic() { var result = new RapidXamlDocument(); var text = File.ReadAllText(".\\Misc\\Generic.xaml"); var snapshot = new FakeTextSnapshot(); XamlElementExtractor.Parse(ProjectType.Uwp, "Generic.xaml", snapshot, text, RapidXamlDocument.GetAllProcessors(ProjectType.Uwp, string.Empty, new TestVisualStudioAbstraction(), DefaultTestLogger.Create()), result.Tags, new TestVisualStudioAbstraction()); Assert.AreEqual(0, result.Tags.OfType <MissingRowDefinitionTag>().Count()); Assert.AreEqual(0, result.Tags.OfType <MissingColumnDefinitionTag>().Count()); }
public void Real_AsyncRelayCommandPage() { var result = new RapidXamlDocument(); var text = File.ReadAllText(".\\Misc\\AsyncRelayCommandPage.xaml"); var snapshot = new FakeTextSnapshot(text.Length); var vsa = new TestVisualStudioAbstraction(); var logger = DefaultTestLogger.Create(); XamlElementExtractor.Parse("AsyncRelayCommandPage.xaml", snapshot, text, RapidXamlDocument.GetAllProcessors(ProjectType.Uwp, string.Empty, vsa, logger), result.Tags, null, RapidXamlDocument.GetEveryElementProcessor(ProjectType.Uwp, null, vsa), logger); Assert.IsTrue(true, "Got here without error."); }
public static string GetSubElementAtPosition(string xaml, int position) { var startPos = xaml.Substring(0, position).LastIndexOf('<'); var elementName = xaml.Substring(startPos + 1, xaml.IndexOfAny(new[] { ' ', '>' }, startPos) - startPos - 1); string result = null; var processor = new SubElementProcessor(); processor.SubElementFound += (s, e) => { result = e.SubElement; }; XamlElementExtractor.Parse(null, xaml.Substring(startPos), new List <(string element, XamlElementProcessor processor)> { (elementName, processor), }, new List <IRapidXamlAdornmentTag>()); return(result); }
public void LazyLoading_Children_GetsCorrectLocations() { var xaml = "<StackPanel>" + Environment.NewLine + " <Container>" + Environment.NewLine + " <TextBlock Text=\"Repeated\" />" + Environment.NewLine + " </Container>" + Environment.NewLine + " <Container>" + Environment.NewLine + " <TextBlock Text=\"Repeated\" />" + Environment.NewLine + " </Container>" + Environment.NewLine + " <Grid>" + Environment.NewLine + " <Container>" + Environment.NewLine + " <TextBlock Text=\"Repeated\" />" + Environment.NewLine + " </Container>" + 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[1]; var e2 = analyzer.Elements[3]; var e3 = analyzer.Elements[5]; Assert.AreEqual(37 + (Environment.NewLine.Length * 1), e1.Children[0].Location.Start); Assert.AreEqual(105 + (Environment.NewLine.Length * 4), e2.Children[0].Location.Start); Assert.AreEqual(191 + (Environment.NewLine.Length * 8), e3.Children[0].Location.Start); }
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); }
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); } } }