예제 #1
0
        public async Task CorrectOutputInSameProjectAsync()
        {
            var profile = this.GetDefaultTestProfile();

            profile.ViewGeneration.AllInSameProject       = true;
            profile.ViewGeneration.ViewModelDirectoryName = "ViewModels";
            profile.ViewGeneration.ViewModelFileSuffix    = "ViewModel";
            profile.ViewGeneration.XamlFileDirectoryName  = "Views";
            profile.ViewGeneration.XamlFileSuffix         = "Page";

            var fs = new TestFileSystem
            {
                FileExistsResponse = false,
                FileText           = @"Public Class TestViewModel
    Public Property OnlyProperty As String
End Class",
            };

            var synTree  = VisualBasicSyntaxTree.ParseText(fs.FileText);
            var semModel = VisualBasicCompilation.Create(string.Empty).AddSyntaxTrees(synTree).GetSemanticModel(synTree, ignoreAccessibility: true);

            var vsa = new TestVisualStudioAbstraction
            {
                SyntaxTree    = synTree,
                SemanticModel = semModel,
                ActiveProject = new ProjectWrapper()
                {
                    Name = "App", FileName = @"C:\Test\App\App.vbproj"
                },
            };

            var sut = new CreateViewCommandLogic(profile, DefaultTestLogger.Create(), vsa, fs);

            await sut.ExecuteAsync(@"C:\Test\App\ViewModels\TestViewModel.vb");

            var expectedXaml = @"<Page
    x:Class=""App.Views.TestPage"">
    <Grid>
        <StackPanel>
<TextBlock FB=""True"" Text=""OnlyProperty"" />
</StackPanel>
    </Grid>
</Page>
";

            var expectedCodeBehind = @"Imports System
Imports Windows.UI.Xaml.Controls
Imports App.ViewModels

Namespace Views

    Public NotInheritable Partial Class TestPage
        Inherits Page

        Public Property ViewModel As TestViewModel

        Public Sub New()
            Me.InitializeComponent()
            Me.ViewModel = New TestViewModel()
        End Sub
    End Class
End Namespace
";

            Assert.IsTrue(sut.CreateView);
            Assert.AreEqual(@"C:\Test\App\Views\TestPage.xaml", sut.XamlFileName);
            Assert.AreEqual(@"C:\Test\App\Views\TestPage.xaml.vb", sut.CodeFileName);
            Assert.AreEqual(expectedXaml, sut.XamlFileContents);
            Assert.AreEqual(expectedCodeBehind, sut.CodeFileContents);
        }
        public async Task CorrectOutputInOtherProject()
        {
            var profile = this.GetDefaultTestProfile();

            profile.ViewGeneration.AllInSameProject = false;

            profile.ViewGeneration.XamlProjectSuffix      = string.Empty;
            profile.ViewGeneration.ViewModelProjectSuffix = ".ViewModels";

            profile.ViewGeneration.ViewModelDirectoryName = "";
            profile.ViewGeneration.ViewModelFileSuffix    = "ViewModel";
            profile.ViewGeneration.XamlFileDirectoryName  = "Views";
            profile.ViewGeneration.XamlFileSuffix         = "Page";

            var fs = new TestFileSystem
            {
                FileExistsResponse = false,
                FileText           = " public class TestViewModel { public string OnlyProperty { get; set;} }",
            };

            var synTree  = CSharpSyntaxTree.ParseText(fs.FileText);
            var semModel = CSharpCompilation.Create(string.Empty).AddSyntaxTrees(synTree).GetSemanticModel(synTree, ignoreAccessibility: true);

            var vsa = new TestVisualStudioAbstraction
            {
                SyntaxTree    = synTree,
                SemanticModel = semModel,
                ActiveProject = new ProjectWrapper {
                    Name = "App.ViewModels", FileName = @"C:\Test\App.ViewModels\App.ViewModels.csproj"
                },
                NamedProject = new ProjectWrapper {
                    Name = "App", FileName = @"C:\Test\App\App.csproj"
                },
            };

            var sut = new CreateViewCommandLogic(profile, DefaultTestLogger.Create(), vsa, fs);

            await sut.ExecuteAsync(@"C:\Test\App.ViewModels\TestViewModel.cs");

            var expectedXaml = "<Page"
                               + Environment.NewLine + "    x:Class=\"App.Views.TestPage\">"
                               + Environment.NewLine + "    <Grid>"
                               + Environment.NewLine + "        <StackPanel>"
                               + Environment.NewLine + "            <TextBlock FB=\"True\" Text=\"OnlyProperty\" />"
                               + Environment.NewLine + "        </StackPanel>"
                               + Environment.NewLine + "    </Grid>"
                               + Environment.NewLine + "</Page>"
                               + Environment.NewLine + "";

            var expectedCodeBehind = "using System;"
                                     + Environment.NewLine + "using Windows.UI.Xaml.Controls;"
                                     + Environment.NewLine + "using App.ViewModels;"
                                     + Environment.NewLine + ""
                                     + Environment.NewLine + "namespace App.Views"
                                     + Environment.NewLine + "{"
                                     + Environment.NewLine + "    public sealed partial class TestPage : Page"
                                     + Environment.NewLine + "    {"
                                     + Environment.NewLine + "        public TestViewModel ViewModel { get; set; }"
                                     + Environment.NewLine + ""
                                     + Environment.NewLine + "        public TestPage()"
                                     + Environment.NewLine + "        {"
                                     + Environment.NewLine + "            this.InitializeComponent();"
                                     + Environment.NewLine + "            this.ViewModel = new TestViewModel();"
                                     + Environment.NewLine + "        }"
                                     + Environment.NewLine + "    }"
                                     + Environment.NewLine + "}"
                                     + Environment.NewLine + "";

            Assert.IsTrue(sut.CreateView);
            Assert.AreEqual(@"C:\Test\App\Views\TestPage.xaml", sut.XamlFileName);
            Assert.AreEqual(@"C:\Test\App\Views\TestPage.xaml.cs", sut.CodeFileName);
            StringAssert.AreEqual(expectedXaml, sut.XamlFileContents);
            StringAssert.AreEqual(expectedCodeBehind, sut.CodeFileContents);
        }
        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, DefaultTestLogger.Create()), result.Tags);

            Assert.AreEqual(0, result.Tags.OfType<MissingRowDefinitionTag>().Count());
            Assert.AreEqual(0, result.Tags.OfType<MissingColumnDefinitionTag>().Count());
        }
예제 #4
0
        public void CheckConstructorRequiredParam_VS()
        {
            var profile = TestProfile.CreateEmpty();

            var sut = new DropHandlerLogic(profile, DefaultTestLogger.Create(), null);
        }
        protected void EachPositionBetweenStarsShouldProduceExpected(string code, AnalyzerOutput expected, bool isCSharp, Profile profileOverload)
        {
            var startPos = code.IndexOf("*", StringComparison.Ordinal);
            var endPos   = code.LastIndexOf("*", StringComparison.Ordinal) - 1;

            var syntaxTree = isCSharp ? CSharpSyntaxTree.ParseText(code.Replace("*", string.Empty))
                                      : VisualBasicSyntaxTree.ParseText(code.Replace("*", string.Empty));

            Assert.IsNotNull(syntaxTree);

            var semModel = isCSharp ? CSharpCompilation.Create(string.Empty).AddSyntaxTrees(syntaxTree).GetSemanticModel(syntaxTree, ignoreAccessibility: true)
                                    : VisualBasicCompilation.Create(string.Empty).AddSyntaxTrees(syntaxTree).GetSemanticModel(syntaxTree, ignoreAccessibility: true);

            var positionsTested = 0;

            for (var pos = startPos; pos < endPos; pos++)
            {
                var analyzer = isCSharp ? new CSharpAnalyzer(DefaultTestLogger.Create()) as IDocumentAnalyzer : new VisualBasicAnalyzer(DefaultTestLogger.Create());

                var actual = analyzer.GetSingleItemOutput(syntaxTree.GetRoot(), semModel, pos, new TestVisualStudioAbstraction().XamlIndent, profileOverload);

                Assert.AreEqual(expected.OutputType, actual.OutputType, $"Failure at {pos} ({startPos}-{endPos})");
                Assert.AreEqual(expected.Name, actual.Name, $"Failure at {pos} ({startPos}-{endPos})");
                StringAssert.AreEqual(expected.Output, actual.Output, $"Failure at {pos} ({startPos}-{endPos})");

                positionsTested += 1;
            }

            this.TestContext.WriteLine($"{positionsTested} different positions tested.");
        }
        protected void SelectionBetweenStarsShouldProduceExpected(string code, AnalyzerOutput expected, bool isCSharp, Profile profileOverload)
        {
            var startPos = code.IndexOf("*", StringComparison.Ordinal);
            var endPos   = code.LastIndexOf("*", StringComparison.Ordinal) - 1;

            var syntaxTree = isCSharp ? CSharpSyntaxTree.ParseText(code.Replace("*", string.Empty))
                                      : VisualBasicSyntaxTree.ParseText(code.Replace("*", string.Empty));

            Assert.IsNotNull(syntaxTree);

            var semModel = isCSharp ? CSharpCompilation.Create(string.Empty).AddSyntaxTrees(syntaxTree).GetSemanticModel(syntaxTree, true)
                                    : VisualBasicCompilation.Create(string.Empty).AddSyntaxTrees(syntaxTree).GetSemanticModel(syntaxTree, true);

            var analyzer = isCSharp ? new CSharpAnalyzer(DefaultTestLogger.Create()) as IDocumentAnalyzer : new VisualBasicAnalyzer(DefaultTestLogger.Create());

            var actual = analyzer.GetSelectionOutput(syntaxTree.GetRoot(), semModel, startPos, endPos, new TestVisualStudioAbstraction().XamlIndent, profileOverload);

            Assert.AreEqual(expected.OutputType, actual.OutputType);
            Assert.AreEqual(expected.Name, actual.Name);
            StringAssert.AreEqual(expected.Output, actual.Output);
        }
예제 #7
0
        public void CanDetectWhereAndWhenToInsertConstructorAndPageContentWhenConstructorDoesNotExist()
        {
            var defaultConstructor = "Sub New()"
                                     + Environment.NewLine + "    InitializeComponent()"
                                     + Environment.NewLine + "End Sub";

            var pageContent = "Public ReadOnly Property ViewModel As $viewmodelclass$"
                              + Environment.NewLine + "    Get"
                              + Environment.NewLine + "        Return New $viewmodelclass$"
                              + Environment.NewLine + "    End Get"
                              + Environment.NewLine + "End Property";

            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.XamlFileSuffix            = "Page";
            profile.ViewGeneration.ViewModelFileSuffix       = "ViewModel";
            profile.Datacontext.CodeBehindConstructorContent = "DataContext = ViewModel";
            profile.Datacontext.DefaultCodeBehindConstructor = defaultConstructor;
            profile.Datacontext.CodeBehindPageContent        = pageContent;

            var logger = DefaultTestLogger.Create();

            var fs = new TestFileSystem
            {
                FileText = @"Public NotInheritable Class TestPage
    Inherits Page

End Class",
            };

            var synTree = VisualBasicSyntaxTree.ParseText(fs.FileText);

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "TestPage.xaml.vb",
                ActiveDocumentText     = fs.FileText,
                SyntaxTree             = synTree,
                DocumentIsCSharp       = false,
            };

            var sut = new SetDataContextCommandLogic(profile, logger, vs, fs);

            var documentRoot = synTree.GetRoot();

            var result = sut.GetCodeBehindContentToAdd("TestPage", "TestViewModel", "TestVmNamespace", documentRoot);

            var expectedContent0 = ""
                                   + Environment.NewLine + "Sub New()"
                                   + Environment.NewLine + "    InitializeComponent()"
                                   + Environment.NewLine + ""
                                   + Environment.NewLine + "DataContext = ViewModel"
                                   + Environment.NewLine + "End Sub"
                                   + Environment.NewLine + "";

            Assert.IsTrue(result[0].anythingToAdd);
            Assert.AreEqual(2, result[0].lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent0, result[0].contentToAdd);

            var expectedContent1 = ""
                                   + Environment.NewLine + ""
                                   + Environment.NewLine + "Public ReadOnly Property ViewModel As TestViewModel"
                                   + Environment.NewLine + "    Get"
                                   + Environment.NewLine + "        Return New TestViewModel"
                                   + Environment.NewLine + "    End Get"
                                   + Environment.NewLine + "End Property";

            Assert.IsTrue(result[1].anythingToAdd);
            Assert.AreEqual(7, result[1].lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent1, result[1].contentToAdd);
        }
예제 #8
0
        public async Task CorrectOutputInSameFolder()
        {
            var profile = this.GetDefaultTestProfile();

            profile.ViewGeneration.AllInSameProject       = true;
            profile.ViewGeneration.ViewModelDirectoryName = "Files";
            profile.ViewGeneration.ViewModelFileSuffix    = "ViewModel";
            profile.ViewGeneration.XamlFileDirectoryName  = "Files";
            profile.ViewGeneration.XamlFileSuffix         = "Page";

            var fs = new TestFileSystem
            {
                FileExistsResponse = false,
                FileText           = " public class TestViewModel { public string OnlyProperty { get; set;} }",
            };

            var synTree  = CSharpSyntaxTree.ParseText(fs.FileText);
            var semModel = CSharpCompilation.Create(string.Empty).AddSyntaxTrees(synTree).GetSemanticModel(synTree, ignoreAccessibility: true);

            var vsa = new TestVisualStudioAbstraction
            {
                SyntaxTree    = synTree,
                SemanticModel = semModel,
                ActiveProject = new ProjectWrapper()
                {
                    Name = "App", FileName = @"C:\Test\App\App.csproj"
                },
            };

            var sut = new CreateViewCommandLogic(profile, DefaultTestLogger.Create(), vsa, fs);

            await sut.ExecuteAsync(@"C:\Test\App\Files\TestViewModel.cs");

            var expectedXaml = @"<Page
    x:Class=""App.Files.TestPage"">
    <Grid>
        <StackPanel>
            <TextBlock FB=""True"" Text=""OnlyProperty"" />
        </StackPanel>
    </Grid>
</Page>
";

            var expectedCodeBehind = @"using System;
using Windows.UI.Xaml.Controls;
using App.Files;

namespace App.Files
{
    public sealed partial class TestPage : Page
    {
        public TestViewModel ViewModel { get; set; }

        public TestPage()
        {
            this.InitializeComponent();
            this.ViewModel = new TestViewModel();
        }
    }
}
";

            Assert.IsTrue(sut.CreateView);
            Assert.AreEqual(@"C:\Test\App\Files\TestPage.xaml", sut.XamlFileName);
            Assert.AreEqual(@"C:\Test\App\Files\TestPage.xaml.cs", sut.CodeFileName);
            Assert.AreEqual(expectedXaml, sut.XamlFileContents);
            Assert.AreEqual(expectedCodeBehind, sut.CodeFileContents);
        }
        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());
        }
예제 #10
0
        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, string.Empty, new TestVisualStudioAbstraction(), DefaultTestLogger.Create()),
                    result.Tags,
                    new TestVisualStudioAbstraction());
            }
            catch (Exception exc)
            {
                Assert.Fail($"Parsing failed for '{filePath}'{Environment.NewLine}{exc}");
            }
        }
        public void CreatePreviewCorrectly_Row1()
        {
            var original = "<Page>"
                           + Environment.NewLine + "    <Grid>"
                           + Environment.NewLine + "        <Grid.RowDefinitions>"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            ☆<RowDefinition Height=\"Auto\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "        </Grid.RowDefinitions>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"Footer\" Grid.Row=\"2\" />"
                           + Environment.NewLine + "    </Grid>"
                           + Environment.NewLine + "</Page>";

            var expected = "<Page>"
                           + Environment.NewLine + "    <Grid>"
                           + Environment.NewLine + "        <Grid.RowDefinitions>"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"XXX\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"Auto\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "        </Grid.RowDefinitions>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"Footer\" Grid.Row=\"3\" />"
                           + Environment.NewLine + "    </Grid>"
                           + Environment.NewLine + "</Page>";

            var tag = new InsertRowDefinitionTag(new Span(0, 0), new FakeTextSnapshot(), "testfile.xaml", DefaultTestLogger.Create())
            {
                XamlTag      = "<RowDefinition Height=\"XXX\" />",
                InsertPoint  = original.IndexOf("☆", StringComparison.Ordinal),
                GridStartPos = 12,
            };

            var actual = InsertRowDefinitionAction.GetPreviewText(
                original.Replace("☆", string.Empty),
                InsertRowDefinitionAction.GetReplacements(1, 3),
                null,
                tag);

            StringAssert.AreEqual(expected, actual);
        }
        public void RealWorldExample()
        {
            var original = ""
                           + Environment.NewLine + "<Window"
                           + Environment.NewLine + "    x:Class=\"RxtWpfDemo.MainWindow\""
                           + 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:d=\"http://schemas.microsoft.com/expression/blend/2008\""
                           + Environment.NewLine + "    xmlns:local=\"clr-namespace:RxtWpfDemo\""
                           + Environment.NewLine + "    xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\""
                           + Environment.NewLine + "    Title=\"MainWindow\""
                           + Environment.NewLine + "    Width=\"800\""
                           + Environment.NewLine + "    Height=\"450\""
                           + Environment.NewLine + "    mc:Ignorable=\"d\">"
                           + Environment.NewLine + "    <Grid>"
                           + Environment.NewLine + "        <Grid.RowDefinitions>"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "            ☆<RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "        </Grid.RowDefinitions>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"hello\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"world\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Grid.Row=\"2\" Text=\"line 3\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <Grid>"
                           + Environment.NewLine + "            <TextBlock Text=\"hello world\" />"
                           + Environment.NewLine + "        </Grid>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <Button Grid.Row=\"1\" Content=\"click here\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "    </Grid>"
                           + Environment.NewLine + "</Window>";

            var expected = ""
                           + Environment.NewLine + "<Window"
                           + Environment.NewLine + "    x:Class=\"RxtWpfDemo.MainWindow\""
                           + 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:d=\"http://schemas.microsoft.com/expression/blend/2008\""
                           + Environment.NewLine + "    xmlns:local=\"clr-namespace:RxtWpfDemo\""
                           + Environment.NewLine + "    xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\""
                           + Environment.NewLine + "    Title=\"MainWindow\""
                           + Environment.NewLine + "    Width=\"800\""
                           + Environment.NewLine + "    Height=\"450\""
                           + Environment.NewLine + "    mc:Ignorable=\"d\">"
                           + Environment.NewLine + "    <Grid>"
                           + Environment.NewLine + "        <Grid.RowDefinitions>"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "            <RowDefinition Height=\"XXX\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "            <RowDefinition />"
                           + Environment.NewLine + "        </Grid.RowDefinitions>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"hello\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"world\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Grid.Row=\"3\" Text=\"line 3\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <Grid>"
                           + Environment.NewLine + "            <TextBlock Text=\"hello world\" />"
                           + Environment.NewLine + "        </Grid>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <Button Grid.Row=\"2\" Content=\"click here\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "    </Grid>"
                           + Environment.NewLine + "</Window>";

            var tag = new InsertRowDefinitionTag(new Span(0, 0), new FakeTextSnapshot(), "testfile.xaml", DefaultTestLogger.Create())
            {
                XamlTag      = "<RowDefinition Height=\"XXX\" />",
                InsertPoint  = original.IndexOf("☆", StringComparison.Ordinal),
                GridStartPos = 451,
            };

            var actual = InsertRowDefinitionAction.GetPreviewText(
                original.Replace("☆", string.Empty),
                InsertRowDefinitionAction.GetReplacements(1, 5),
                null,
                tag);

            StringAssert.AreEqual(expected, actual);
        }
        public void CreatePreviewCorrectly_WithNestedGrid_AndExclusions()
        {
            var original = "<Page>"
                           + Environment.NewLine + "    <Grid>"
                           + Environment.NewLine + "        <Grid.RowDefinitions>"
                           + Environment.NewLine + "            ☆<RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"Auto\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "        </Grid.RowDefinitions>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <Grid Grid.Row=\"1\">"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "            <TextBlock Tex=\"Excluded\" Grid.Row=\"2\" />"
                           + Environment.NewLine + "        </Grid>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"Footer\" Grid.Row=\"2\" />"
                           + Environment.NewLine + "    </Grid>"
                           + Environment.NewLine + "</Page>";

            var expected = "<Page>"
                           + Environment.NewLine + "    <Grid>"
                           + Environment.NewLine + "        <Grid.RowDefinitions>"
                           + Environment.NewLine + "            <RowDefinition Height=\"XXX\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"Auto\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "        </Grid.RowDefinitions>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <Grid Grid.Row=\"2\">"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + "            <RowDefinition Height=\"*\" />"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "            <TextBlock Tex=\"Excluded\" Grid.Row=\"2\" />"
                           + Environment.NewLine + "        </Grid>"
                           + Environment.NewLine + ""
                           + Environment.NewLine + "        <TextBlock Text=\"Footer\" Grid.Row=\"3\" />"
                           + Environment.NewLine + "    </Grid>"
                           + Environment.NewLine + "</Page>";

            var tag = new InsertRowDefinitionTag(new Span(0, 0), new FakeTextSnapshot(), "testfile.xaml", DefaultTestLogger.Create())
            {
                XamlTag      = "<RowDefinition Height=\"XXX\" />",
                InsertPoint  = original.IndexOf("☆", StringComparison.Ordinal),
                GridStartPos = 12,
            };

            var actualXaml = original.Replace("☆", string.Empty);

            // Get the position of the first grid and use it to find exclusions
            var exclusionGridPos = actualXaml.IndexOf("<Grid>", StringComparison.Ordinal);
            var exclusions       = InsertRowDefinitionAction.GetExclusions(actualXaml.Substring(exclusionGridPos));

            var actual = InsertRowDefinitionAction.GetPreviewText(
                actualXaml,
                InsertRowDefinitionAction.GetReplacements(1, 3),
                exclusions,
                tag);

            StringAssert.AreEqual(expected, actual);
        }
        public void CanDetectWhereAndWhenToInsertConstructorAndPageContentWhenConstructorDoesNotExist()
        {
            var defaultConstructor = "public $viewclass$()"
             + Environment.NewLine + "{"
             + Environment.NewLine + "    this.Initialize();"
             + Environment.NewLine + "}";

            var pageContent = "public $viewmodelclass$ ViewModel"
      + Environment.NewLine + "{"
      + Environment.NewLine + "    get"
      + Environment.NewLine + "    {"
      + Environment.NewLine + "        return new $viewmodelclass$();"
      + Environment.NewLine + "    }"
      + Environment.NewLine + "}";

            var profile = TestProfile.CreateEmpty();
            profile.ViewGeneration.XamlFileSuffix = "Page";
            profile.ViewGeneration.ViewModelFileSuffix = "ViewModel";
            profile.Datacontext.CodeBehindConstructorContent = "this.DataContext = this.ViewModel;";
            profile.Datacontext.DefaultCodeBehindConstructor = defaultConstructor;
            profile.Datacontext.CodeBehindPageContent = pageContent;

            var logger = DefaultTestLogger.Create();

            var fs = new TestFileSystem
            {
                FileText = @"class TestPage
{
}",
            };

            var synTree = CSharpSyntaxTree.ParseText(fs.FileText);

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "TestPage.xaml.cs",
                ActiveDocumentText = fs.FileText,
                SyntaxTree = synTree,
                DocumentIsCSharp = true,
            };

            var sut = new SetDataContextCommandLogic(profile, logger, vs, fs);

            var documentRoot = synTree.GetRoot();

            var result = sut.GetCodeBehindContentToAdd("TestPage", "TestViewModel", "TestVmNamespace", documentRoot);

            var expectedContent0 = ""
           + Environment.NewLine + "public TestPage()"
           + Environment.NewLine + "{"
           + Environment.NewLine + "    this.Initialize();"
           + Environment.NewLine + ""
           + Environment.NewLine + "this.DataContext = this.ViewModel;"
           + Environment.NewLine + "}"
           + Environment.NewLine + "";

            Assert.IsTrue(result[0].anythingToAdd);
            Assert.AreEqual(2, result[0].lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent0, result[0].contentToAdd);

            var expectedContent1 = ""
           + Environment.NewLine + ""
           + Environment.NewLine + "public TestViewModel ViewModel"
           + Environment.NewLine + "{"
           + Environment.NewLine + "    get"
           + Environment.NewLine + "    {"
           + Environment.NewLine + "        return new TestViewModel();"
           + Environment.NewLine + "    }"
           + Environment.NewLine + "}";

            Assert.IsTrue(result[1].anythingToAdd);
            Assert.AreEqual(8, result[1].lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent1, result[1].contentToAdd);
        }
예제 #15
0
        protected void PositionAtStarShouldProduceExpected(string code, AnalyzerOutput expected, bool isCSharp, Profile profileOverload)
        {
            var pos = code.IndexOf("*", StringComparison.Ordinal);

            var syntaxTree = isCSharp ? CSharpSyntaxTree.ParseText(code.Replace("*", string.Empty))
                                      : VisualBasicSyntaxTree.ParseText(code.Replace("*", string.Empty));

            Assert.IsNotNull(syntaxTree);

            var semModel = isCSharp ? CSharpCompilation.Create(string.Empty).AddSyntaxTrees(syntaxTree).GetSemanticModel(syntaxTree, true)
                                    : VisualBasicCompilation.Create(string.Empty).AddSyntaxTrees(syntaxTree).GetSemanticModel(syntaxTree, true);

            var analyzer = isCSharp ? new CSharpAnalyzer(DefaultTestLogger.Create()) as IDocumentAnalyzer : new VisualBasicAnalyzer(DefaultTestLogger.Create());

            var actual = analyzer.GetSingleItemOutput(syntaxTree.GetRoot(), semModel, pos, profileOverload);

            Assert.AreEqual(expected.OutputType, actual.OutputType);
            Assert.AreEqual(expected.Name, actual.Name);
            Assert.AreEqual(expected.Output, actual.Output);
        }