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}"); } }
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 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 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 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 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); }
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()); }
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); } } }
#pragma warning restore SA1310 // Field names should not contain underscore public static void Main(string[] args) { try { // Output name and version for help in debugging any issues. Console.WriteLine($"{System.Reflection.Assembly.GetExecutingAssembly().FullName}"); if (args.Length < 1) { Console.WriteLine($"ERROR. Expecting project file path as command line argument."); Environment.ExitCode = ERRORCODE_FILE_PATH_NOT_SPECIFIED; return; } var projectPath = args[0]; Console.WriteLine($"Analyzing {projectPath}"); if (!File.Exists(projectPath)) { Console.WriteLine($"ERROR. File does not exist."); Environment.ExitCode = ERRORCODE_FILE_DOES_NOT_EXIST; return; } var fileExt = Path.GetExtension(projectPath); if (!fileExt.ToLowerInvariant().Equals(".csproj") & !fileExt.ToLowerInvariant().Equals(".vbproj")) { Console.WriteLine($"ERROR. Not a supported project file."); Environment.ExitCode = ERRORCODE_NOT_A_PROJECT_FILE; return; } var projFileLines = File.ReadAllLines(projectPath); var projDir = Path.GetDirectoryName(projectPath); // Ensure any errors are output - otherwise debugging problems is really hard SharedRapidXamlPackage.Logger = new AnalysisExeLogger(); // Treat project type as unknown as unable to resolve referenced projects and installed NuGet packages. var bavsa = new BuildAnalysisVisualStudioAbstraction(projectPath, ProjectType.Unknown); foreach (var line in projFileLines) { var endPos = line.IndexOf(".xaml\""); if (endPos > 1) { var startPos = line.IndexOf("Include"); if (startPos > 1) { var relativeFilePath = line.Substring(startPos + 9, endPos + 5 - startPos - 9); var xamlFilePath = Path.Combine(projDir, relativeFilePath); Console.WriteLine($"- Analyzing: '{xamlFilePath}'"); var snapshot = new BuildAnalysisTextSnapshot(xamlFilePath); var rxdoc = RapidXamlDocument.Create(snapshot, xamlFilePath, bavsa, projectPath); Debug.WriteLine($"Found {rxdoc.Tags.Count} taggable issues in '{xamlFilePath}'."); if (rxdoc.Tags.Count > 0) { var tagsOfInterest = rxdoc.Tags .Where(t => t is RapidXamlDisplayedTag) .Select(t => t as RapidXamlDisplayedTag) .ToList(); Debug.WriteLine($"Found {tagsOfInterest.Count} issues to report in '{xamlFilePath}'."); foreach (var issue in tagsOfInterest) { string messageType = string.Empty; switch (issue.ConfiguredErrorType) { case TagErrorType.Error: messageType = "error"; break; case TagErrorType.Warning: messageType = "warning"; break; case TagErrorType.Suggestion: // Not supported in the build process break; case TagErrorType.Hidden: break; } if (!string.IsNullOrEmpty(messageType)) { // Add 1 to line number to allow for VS counting without zero index // Error code is repeated with the description because it doesn't show in the Visual Studio Error List // For format see https://github.com/Microsoft/msbuild/blob/master/src/Shared/CanonicalError.cs Console.WriteLine($"{xamlFilePath}({issue.Line + 1},{issue.Column}): {messageType} {issue.ErrorCode}: {issue.Description} ({issue.ErrorCode})"); } } } } } } } catch (Exception exc) { Console.WriteLine($"ERROR. {exc}"); } }
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); }
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 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 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); }