Example #1
0
        public void CanDetectWhereAndWhatToInsert()
        {
            var profile = TestProfile.CreateEmpty();

            profile.Datacontext.XamlPageAttribute = "DataContext=\"HasBeenSet\"";

            var logger = DefaultTestLogger.Create();

            var activeDocText = "<Page"
                                + Environment.NewLine + "    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\""
                                + Environment.NewLine + "    >"
                                + Environment.NewLine + "    <!-- Content would go here -->"
                                + Environment.NewLine + "</Page>";

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "test.xaml",
                ActiveDocumentText     = activeDocText,
            };

            var fs = new TestFileSystem();

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

            var result = sut.GetPageAttributeToAdd("TestViewModel", "Tests");

            Assert.IsTrue(result.anythingToAdd);
            Assert.AreEqual(2, result.lineNoToAddAfter);
            Assert.AreEqual($"{Environment.NewLine}    DataContext=\"HasBeenSet\"", result.contentToAdd);
        }
        public void DetectsWhenPageContentIsNotAlreadySet()
        {
            var profile = TestProfile.CreateEmpty();
            profile.ViewGeneration.XamlFileSuffix = "Page";
            profile.ViewGeneration.ViewModelFileSuffix = "ViewModel";
            profile.Datacontext.CodeBehindPageContent = @"public $viewmodelclass$ ViewModel
{
    get
    {
        return new $viewmodelclass$();
    }
}";

            var logger = DefaultTestLogger.Create();

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "Testpage.xaml.cs",
                ActiveDocumentText = @"class TestPage
{
    public TestPage()
    {
        this.Initialize();
    }
}",
            };

            var fs = new TestFileSystem();

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

            var result = sut.ShouldEnableCommand();

            Assert.IsTrue(result);
        }
Example #3
0
        public void DetectsWhenConstructorContentIsNotAlreadySet()
        {
            var profile = TestProfile.CreateEmpty();

            profile.Datacontext.CodeBehindConstructorContent = "DataContext = ViewModel";

            var logger = DefaultTestLogger.Create();

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "TestPage.xaml.vb",
                ActiveDocumentText     = @"Public NotInheritable Class TestPage
    Inherits Page

    Sub New()
        InitializeComponent()
    End Sub
End Class",
            };

            var fs = new TestFileSystem();

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

            var result = sut.ShouldEnableCommand();

            Assert.IsTrue(result);
        }
        public void DetectsWhenConstructorContentIsNotAlreadySet()
        {
            var profile = TestProfile.CreateEmpty();

            profile.Datacontext.CodeBehindConstructorContent = "this.DataContext = this.ViewModel;";

            var logger = DefaultTestLogger.Create();

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "test.xaml.cs",
                ActiveDocumentText     = @"class TestPage
{
    public TestPage()
    {
        this.Initialize();
    }
}",
            };

            var fs = new TestFileSystem();

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

            var result = sut.ShouldEnableCommand();

            Assert.IsTrue(result);
        }
Example #5
0
        public void CanDetectWhereAndWhenToInsertConstructorAndPageContentWhenConstructorExists()
        {
            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.CodeBehindPageContent        = pageContent;

            var logger = DefaultTestLogger.Create();

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

    Sub New()
        InitializeComponent()
    End Sub
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);

            Assert.IsTrue(result[0].anythingToAdd);
            Assert.AreEqual(5, result[0].lineNoToAddAfter);
            StringAssert.AreEqual($"{Environment.NewLine}{Environment.NewLine}DataContext = ViewModel", result[0].contentToAdd);

            var expectedContent = ""
                                  + 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(8, result[1].lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent, result[1].contentToAdd);
        }
Example #6
0
        public void DetectsWhenPageContentIsNotAlreadySet()
        {
            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.XamlFileSuffix      = "Page";
            profile.ViewGeneration.ViewModelFileSuffix = "ViewModel";
            profile.Datacontext.CodeBehindPageContent  = @"Public ReadOnly Property ViewModel As $viewmodelclass$
    Get
        Return New $viewmodelclass$
    End Get
End Property";

            var logger = DefaultTestLogger.Create();

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "TestPage.xaml.vb",
                ActiveDocumentText     = @"Public NotInheritable Class TestPage
    Inherits Page

    Sub New()
        InitializeComponent()
    End Sub
End Class",
            };

            var fs = new TestFileSystem();

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

            var result = sut.ShouldEnableCommand();

            Assert.IsTrue(result);
        }
Example #7
0
        public void CanDetectIfNotAlreadySet()
        {
            var profile = TestProfile.CreateEmpty();

            profile.Datacontext.XamlPageAttribute = "DataContext=\"HasBeenSet\"";

            var logger = DefaultTestLogger.Create();

            var vs = new TestVisualStudioAbstraction
            {
                ActiveDocumentFileName = "test.xaml",
                ActiveDocumentText     = @"<Page
    >
    <!-- Content would go here -->
</Page>",
            };

            var fs = new TestFileSystem();

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

            var result = sut.ShouldEnableCommand();

            Assert.IsTrue(result);
        }
        public void CanDetectWhereAndWhenToInsertPageContentAndConstructorExists()
        {
            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.CodeBehindPageContent = pageContent;

            var logger = DefaultTestLogger.Create();

            var fileText = "class TestPage"
   + Environment.NewLine + "{"
   + Environment.NewLine + "    public TestPage()"
   + Environment.NewLine + "    {"
   + Environment.NewLine + "        this.Initialize();"
   + Environment.NewLine + "    }"
   + Environment.NewLine + "}";

            var fs = new TestFileSystem
            {
                FileText = fileText,
            };

            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 (anythingToAdd, lineNoToAddAfter, contentToAdd)
                = sut.GetCodeBehindPageContentToAdd(vs.ActiveDocumentText, vs.SyntaxTree.GetRoot(), "TestViewModel", "TestVmNamespace");

            var expectedContent = ""
          + 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(anythingToAdd);
            Assert.AreEqual(6, lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent, contentToAdd);
        }
        public void CanGetLineEndPos_SecondLine()
        {
            var file = @"abc
67
012";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 2);

            Assert.AreEqual(9, testResult);
        }
        public void CanGetLineEndPos_BlankFirstLine()
        {
            var file = @"
678
123";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 1);

            Assert.AreEqual(2, testResult);
        }
        public void CanGetLineEndPos_BlankFirstLine()
        {
            var file = ""
                       + Environment.NewLine + "678"
                       + Environment.NewLine + "123";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 1);

            Assert.AreEqual(Environment.NewLine.Length, testResult);
        }
        public void CanGetLineEndPos_SecondLine()
        {
            var file = "abc"
                       + Environment.NewLine + "67"
                       + Environment.NewLine + "012";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 2);

            Assert.AreEqual(5 + (Environment.NewLine.Length * 2), testResult);
        }
        public void CanGetLineEndPos_BlankThirdLine()
        {
            var file = "abc"
                       + Environment.NewLine + ""
                       + Environment.NewLine + ""
                       + Environment.NewLine + "012";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 3);

            Assert.AreEqual(3 + (Environment.NewLine.Length * 3), testResult);
        }
        public void CanGetLineEndPos_BlankThirdLine()
        {
            var file = @"abc


012";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 3);

            Assert.AreEqual(9, testResult);
        }
        public void CanGetLineEndPos_ThirdLine()
        {
            var file = "1"
                       + Environment.NewLine + "45"
                       + Environment.NewLine + "890"
                       + Environment.NewLine + "abcd";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 3);

            Assert.AreEqual(6 + (Environment.NewLine.Length * 3), testResult);
        }
        public void CanGetLineEndPos_ThirdLine()
        {
            var file = @"1
45
890
abcd";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 3);

            Assert.AreEqual(12, testResult);
        }
        public void CanDetectWhereAndWhenToInsertPageContentAndConstructorExists()
        {
            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.XamlFileSuffix      = "Page";
            profile.ViewGeneration.ViewModelFileSuffix = "ViewModel";
            profile.Datacontext.CodeBehindPageContent  = @"Public ReadOnly Property ViewModel As $viewmodelclass$
    Get
        Return New $viewmodelclass$
    End Get
End Property";

            var logger = DefaultTestLogger.Create();

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

    Sub New()
        InitializeComponent()
    End Sub
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(anythingToAdd, lineNoToAddAfter, contentToAdd)
                = sut.GetCodeBehindPageContentToAdd(vs.ActiveDocumentText, vs.SyntaxTree.GetRoot(), "TestViewModel", "TestVmNamespace");

            var expectedContent = @"

Public ReadOnly Property ViewModel As TestViewModel
    Get
        Return New TestViewModel
    End Get
End Property";

            Assert.IsTrue(anythingToAdd);
            Assert.AreEqual(6, lineNoToAddAfter);
            Assert.AreEqual(expectedContent, contentToAdd);
        }
        public void CorrectlyInferViewModelName_WhenNoSuffixes()
        {
            var sut = new SetDataContextCommandLogic(
                TestProfile.CreateEmpty(),
                DefaultTestLogger.Create(),
                new TestVisualStudioAbstraction(),
                new TestFileSystem());

            var(view, viewModel, _) = sut.InferViewModelNameFromFileName("Test.xaml.cs");

            Assert.AreEqual("Test", view);
            Assert.AreEqual("Test", viewModel);
        }
        public void CanGetLineEndPos_FifthLine()
        {
            var file = "1"
                       + Environment.NewLine + ""
                       + Environment.NewLine + "123"
                       + Environment.NewLine + "abcd"
                       + Environment.NewLine + "-|-|-|-"
                       + Environment.NewLine + "12345678";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 5);

            Assert.AreEqual(15 + (Environment.NewLine.Length * 5), testResult);
        }
        public void CanGetLineEndPos_FifthLine()
        {
            var file = @"1

123
abcd
-|-|-|-
12345678";

            var testResult = SetDataContextCommandLogic.GetLineEndPos(file, 5);

            Assert.AreEqual(25, testResult);
        }
Example #21
0
        public void CanDetectWhereAndWhenToInsertConstructorContentAndConstructorDoesNotExist()
        {
            var defaultConstructor = "Sub New()"
                                     + Environment.NewLine + "    InitializeComponent()"
                                     + Environment.NewLine + "End Sub";

            var profile = TestProfile.CreateEmpty();

            profile.Datacontext.CodeBehindConstructorContent = "DataContext = ViewModel";
            profile.Datacontext.DefaultCodeBehindConstructor = defaultConstructor;

            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(anythingToAdd, lineNoToAddAfter, contentToAdd, constructorAdded)
                = sut.GetCodeBehindConstructorContentToAdd(vs.ActiveDocumentText, vs.SyntaxTree.GetRoot(), "TestPage", "TestViewModel");

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

            Assert.IsTrue(anythingToAdd);
            Assert.AreEqual(2, lineNoToAddAfter);
            StringAssert.AreEqual(expectedContent, contentToAdd);
            Assert.IsTrue(constructorAdded);
        }
        public void CanDetectWhereAndWhenToInsertConstructorContentAndConstructorDoesNotExist()
        {
            var profile = TestProfile.CreateEmpty();

            profile.Datacontext.CodeBehindConstructorContent = "this.DataContext = this.ViewModel;";
            profile.Datacontext.DefaultCodeBehindConstructor = @"public $viewclass$()
{
    this.Initialize();
}";

            var logger = DefaultTestLogger.Create();

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

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

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

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

            var(anythingToAdd, lineNoToAddAfter, contentToAdd, constructorAdded)
                = sut.GetCodeBehindConstructorContentToAdd(vs.ActiveDocumentText, vs.SyntaxTree.GetRoot(), "TestPage", "TestViewModel");

            var expectedContent = @"
public TestPage()
{
    this.Initialize();

this.DataContext = this.ViewModel;
}
";

            Assert.IsTrue(anythingToAdd);
            Assert.AreEqual(2, lineNoToAddAfter);
            Assert.AreEqual(expectedContent, contentToAdd);
            Assert.IsTrue(constructorAdded);
        }
        public void CorrectlyInferViewModelName_OnlyCodeBehindSuffixConfigured()
        {
            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.ViewModelFileSuffix = "ViewModel";

            var sut = new SetDataContextCommandLogic(
                profile,
                DefaultTestLogger.Create(),
                new TestVisualStudioAbstraction(),
                new TestFileSystem());

            var(view, viewModel, _) = sut.InferViewModelNameFromFileName("TestPage.xaml.cs");

            Assert.AreEqual("TestPage", view);
            Assert.AreEqual("TestPageViewModel", viewModel);
        }
        public void CorrectlyInferViewModelName_BothSuffixesConfigured_NonStandard()
        {
            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.XamlFileSuffix      = "Foo";
            profile.ViewGeneration.ViewModelFileSuffix = "Bar";

            var sut = new SetDataContextCommandLogic(
                profile,
                DefaultTestLogger.Create(),
                new TestVisualStudioAbstraction(),
                new TestFileSystem());

            var(view, viewModel, _) = sut.InferViewModelNameFromFileName("TestFoo.xaml.cs");

            Assert.AreEqual("TestFoo", view);
            Assert.AreEqual("TestBar", viewModel);
        }
        public void CorrectlyInferViewModelNameSpace()
        {
            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.ViewModelDirectoryName = "VMS";

            var sut = new SetDataContextCommandLogic(
                profile,
                DefaultTestLogger.Create(),
                new TestVisualStudioAbstraction
            {
                ActiveProject = new ProjectWrapper {
                    Name = "TestApp"
                },
            },
                new TestFileSystem());

            var(_, _, vmNamespace) = sut.InferViewModelNameFromFileName("Test.xaml.cs");

            Assert.AreEqual("TestApp.VMS", vmNamespace);
        }
        public void CanDetectWhereAndWhenToInsertConstructorAndPageContentWhenConstructorDoesNotExist()
        {
            var profile = TestProfile.CreateEmpty();

            profile.ViewGeneration.XamlFileSuffix            = "Page";
            profile.ViewGeneration.ViewModelFileSuffix       = "ViewModel";
            profile.Datacontext.CodeBehindConstructorContent = "this.DataContext = this.ViewModel;";
            profile.Datacontext.DefaultCodeBehindConstructor = @"public $viewclass$()
{
    this.Initialize();
}";
            profile.Datacontext.CodeBehindPageContent        = @"public $viewmodelclass$ ViewModel
{
    get
    {
        return new $viewmodelclass$();
    }
}";

            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 = @"
public TestPage()
{
    this.Initialize();

this.DataContext = this.ViewModel;
}
";

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

            var expectedContent1 = @"

public TestViewModel ViewModel
{
    get
    {
        return new TestViewModel();
    }
}";

            Assert.IsTrue(result[1].anythingToAdd);
            Assert.AreEqual(8, result[1].lineNoToAddAfter);
            Assert.AreEqual(expectedContent1, result[1].contentToAdd);
        }