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); }