public void CSharpWebsiteReplaceMethodTest() { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(4)); ProjectItem codeFile = testProject.ProjectItems.Item("Default.aspx.cs"); BaseHardCodedString hcs = new CSharpHardCodedString(codeFile, 376, 389); TestReplaceMethod(codeFile, "Resource.resx", hcs, TestStrings.CSharpWebsiteTestString, "TestResource"); }
public void IndexGetterTests() { CSharpHardCodedString hcs = new CSharpHardCodedString(this.codeFile, 349, 362); TextDocument doc = ((EnvDTE.TextDocument)this.codeFile.Document.Object(null)); EditPoint ep = doc.StartPoint.CreateEditPoint(); string text = ep.GetLines(17, 18); Assert.AreEqual(text.IndexOf("\"Test\\\"\\\"\\n\\t\""), hcs.StartIndex); Assert.AreEqual(15, hcs.StartingLine); }
/// <summary> /// Returns an instance of BaseHardCodedString, based the type of the current document. /// </summary> /// <param name="currentDocument">The document that contains an hard-coded string.</param> /// <returns>A hard coded string that is file type aware.</returns> public static BaseHardCodedString GetHardCodedString(Document currentDocument) { BaseHardCodedString stringInstance = null; // Create the hard coded string instance switch (currentDocument.Language) { case "CSharp": stringInstance = new CSharpHardCodedString(); break; case "Basic": stringInstance = new VBHardCodedString(); break; case "XAML": stringInstance = new XamlHardCodedString(); break; case "HTMLX": if (currentDocument.Name.EndsWith(".cshtml", StringComparison.CurrentCultureIgnoreCase)) { stringInstance = new CSharpRazorHardCodedString(); } else if (currentDocument.Name.EndsWith(".vbhtml", StringComparison.CurrentCultureIgnoreCase)) { stringInstance = new VBRazorHardCodedString(); } break; case "HTML": if (currentDocument.Name.EndsWith(".aspx", StringComparison.CurrentCultureIgnoreCase) || currentDocument.Name.EndsWith(".ascx", StringComparison.CurrentCultureIgnoreCase) || currentDocument.Name.EndsWith(".master", StringComparison.CurrentCultureIgnoreCase)) { stringInstance = new AspxHardCodedString(); } break; } return(stringInstance); }
public void GenericCSharpReplaceMethodReadOnlyTest() { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(3)); ProjectItem codeFile = testProject.ProjectItems.Item("Program.cs"); CSharpHardCodedString hcs = new CSharpHardCodedString(codeFile, 19, 32); IExtractResourceAction actionObject = new Common.GenericCSharpExtractResourceAction(); ResourceFileCollection resources = new ResourceFileCollection(testProject, new FilterMethod(actionObject.IsValidResourceFile)); ResourceFile resFile = resources["Resource1.resx"]; string fileName = codeFile.get_FileNames(1); FileAttributes oldAttributes = System.IO.File.GetAttributes(fileName); System.IO.File.SetAttributes(fileName, oldAttributes | FileAttributes.ReadOnly); try { ExtractToResourceActionSite refactorSite = new ExtractToResourceActionSite(hcs); refactorSite.ExtractStringToResource(resFile, "Test"); } finally { System.IO.File.SetAttributes(fileName, oldAttributes); } }
public void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VariantIn, ref object VariantOut, ref bool Handled) { if (CmdName == null) { throw new ArgumentNullException("CmdName"); } if (this.availableRefactorOptions.ContainsKey(CmdName)) { TextSelection selection = (TextSelection)(applicationObject.ActiveDocument.Selection); if (applicationObject.ActiveDocument.ProjectItem.Object != null) { Common.BaseHardCodedString stringInstance = null; /// Create the hard coded string instance switch (applicationObject.ActiveDocument.Language) { case "CSharp": stringInstance = new Common.CSharpHardCodedString(); break; case "Basic": stringInstance = new Common.VBHardCodedString(); break; case "XAML": stringInstance = new Common.XamlHardCodedString(); break; case "HTML": if (applicationObject.ActiveDocument.Name.Contains(".cshtml")) { stringInstance = new Common.CSharpRazorHardCodedString(); } break; default: MessageBox.Show( Strings.UnsupportedFile + " (" + applicationObject.ActiveDocument.Language + ")", Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Common.MatchResult scanResult = stringInstance.CheckForHardCodedString( selection.Parent, selection.AnchorPoint.AbsoluteCharOffset - 1, selection.BottomPoint.AbsoluteCharOffset - 1); if (!scanResult.Result && selection.AnchorPoint.AbsoluteCharOffset < selection.BottomPoint.AbsoluteCharOffset) { scanResult.StartIndex = selection.AnchorPoint.AbsoluteCharOffset - 1; scanResult.EndIndex = selection.BottomPoint.AbsoluteCharOffset - 1; scanResult.Result = true; } if (scanResult.Result) { stringInstance = stringInstance.CreateInstance(applicationObject.ActiveDocument.ProjectItem, scanResult.StartIndex, scanResult.EndIndex); if (stringInstance != null && stringInstance.Parent != null) { Common.IStringRefactorOption option = this.availableRefactorOptions[CmdName]; if (option.QuerySupportForString(stringInstance)) { option.PerformAction(stringInstance); } else { MessageBox.Show( Strings.UnsupportedFile, Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show( Strings.NotStringLiteral, Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
public void GenericCSharpReplaceMethodTest() { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(3)); ProjectItem codeFile = testProject.ProjectItems.Item("Program.cs"); CSharpHardCodedString hcs = new CSharpHardCodedString(codeFile, 349, 362); TestReplaceMethod(codeFile, "Resource1.resx", hcs, TestStrings.CSharpReplaceTestExpectedLine, "Test"); }
public void VBReplaceTestLocalResource() { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(2)); ProjectItem codeFile = testProject.ProjectItems.Item("Test.vb"); CommonMethods.CloseDocument(codeFile); CSharpHardCodedString hcs = new CSharpHardCodedString(codeFile, 40, 46); ExtractResourceActionTests.TestReplaceMethod(codeFile, "LocalResource.resx", hcs, TestStrings.WebApplicationLocalTestStringVB, "Test"); }
public void CSharpReplaceTestGlobalResource() { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(1)); ProjectItem codeFile = testProject.ProjectItems.Item("Test.cs"); CommonMethods.CloseDocument(codeFile); CSharpHardCodedString hcs = new CSharpHardCodedString(codeFile, 307, 313); ExtractResourceActionTests.TestReplaceMethod(codeFile, "Global.resx", hcs, TestStrings.WebApplicationGlobalTestStringCSharp, "Test"); }
public void VBWebsiteReplaceMethodTest() { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(4)); ProjectItem codeFile = testProject.ProjectItems.Item("App_Code").ProjectItems.Item("Class1.vb"); BaseHardCodedString hcs = new CSharpHardCodedString(codeFile, 73, 86); TestReplaceMethod(codeFile, "Resource.resx", hcs, TestStrings.VBWebsiteTestString, "TestResource"); }
public void ValueGetterTest() { CSharpHardCodedString hcs = new CSharpHardCodedString(this.codeFile, 349, 362); Assert.AreEqual("Test String", hcs.Value, "Simple string Value get failed"); hcs = new CSharpHardCodedString(this.codeFile, 383, 397); Assert.AreEqual("Test\"\"\n\t", hcs.Value, "String with escape characters Value get failed"); hcs = new CSharpHardCodedString(this.codeFile, 418, 432); Assert.AreEqual(@"Test String", hcs.Value, "Value property for simple verbatim string failed"); hcs = new CSharpHardCodedString(this.codeFile, 453,470); Assert.AreEqual(@"Test""String""", hcs.Value, "Value property for verbatim string with escaped quotes failed"); }