diff options
author | Matt Ward <ward.matt@gmail.com> | 2015-04-16 18:20:19 +0300 |
---|---|---|
committer | Matt Ward <ward.matt@gmail.com> | 2015-04-16 18:29:09 +0300 |
commit | b28a36e465d56878384c8a7ac3396406fd5dc598 (patch) | |
tree | d8314b08be1b5be70514e06d71e8a8c11fe54c0f | |
parent | 7c1f45c8c5bdca63f11134fe089dcd63f860d363 (diff) |
[AspNet] Fix web forms code completion tests
3 files changed, 210 insertions, 202 deletions
diff --git a/main/src/addins/AspNet/Tests/WebForms/WebFormsCompletionTests.cs b/main/src/addins/AspNet/Tests/WebForms/WebFormsCompletionTests.cs index 12803d02d8..0c041400dc 100644 --- a/main/src/addins/AspNet/Tests/WebForms/WebFormsCompletionTests.cs +++ b/main/src/addins/AspNet/Tests/WebForms/WebFormsCompletionTests.cs @@ -32,125 +32,124 @@ namespace MonoDevelop.AspNet.Tests.WebForms [TestFixture] class WebFormsCompletionTests : UnitTests.TestBase { - // TODO: Roslyn port -// [Test] -// public void DirectiveCompletion () -// { -// var provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".aspx"); -// Assert.IsNotNull (provider); -// Assert.AreEqual (9, provider.Count); -// Assert.IsNotNull (provider.Find ("Page")); -// Assert.IsNotNull (provider.Find ("Register")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".master"); -// Assert.IsNotNull (provider); -// Assert.AreEqual (7, provider.Count); -// Assert.IsNotNull (provider.Find ("Master")); -// Assert.IsNotNull (provider.Find ("Register")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".ascx"); -// Assert.IsNotNull (provider); -// Assert.AreEqual (7, provider.Count); -// Assert.IsNotNull (provider.Find ("Control")); -// Assert.IsNotNull (provider.Find ("Register")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@$ %>", ".aspx"); -// Assert.IsNull (provider); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".aspx"); -// Assert.IsNull (provider); -// } -// -// [Test] -// public void DirectiveAttributeCompletion () -// { -// var provider = WebFormsTesting.CreateProvider (@"<%@ Page A$ %>", ".aspx"); -// Assert.IsNotNull (provider); -// Assert.AreEqual (41, provider.Count); -// Assert.IsNotNull (provider.Find ("StyleSheetTheme")); -// Assert.IsNotNull (provider.Find ("Inherits")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ Master A$ %>", ".master"); -// Assert.IsNotNull (provider); -// Assert.AreEqual (18, provider.Count); -// Assert.IsNull (provider.Find ("StyleSheetTheme")); -// Assert.IsNotNull (provider.Find ("MasterPageFile")); -// Assert.IsNotNull (provider.Find ("Inherits")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ Control A$ %>", ".ascx"); -// Assert.IsNotNull (provider); -// Assert.AreEqual (17, provider.Count); -// Assert.IsNull (provider.Find ("StyleSheetTheme")); -// Assert.IsNotNull (provider.Find ("Inherits")); -// } -// -// [Test] -// [Ignore ("Not working")] -// public void DirectiveAttributeCtrlSpaceCompletion () -// { -// var provider = WebFormsTesting.CreateProvider (@"<%@ Page $ %>", ".aspx", true); -// Assert.IsNotNull (provider); -// Assert.AreEqual (41, provider.Count); -// Assert.IsNotNull (provider.Find ("StyleSheetTheme")); -// Assert.IsNotNull (provider.Find ("Inherits")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ Master $ %>", ".master", true); -// Assert.IsNotNull (provider); -// Assert.AreEqual (18, provider.Count); -// Assert.IsNull (provider.Find ("StyleSheetTheme")); -// Assert.IsNotNull (provider.Find ("MasterPageFile")); -// Assert.IsNotNull (provider.Find ("Inherits")); -// -// provider = WebFormsTesting.CreateProvider (@"<%@ Control $ %>", ".ascx", true); -// Assert.IsNotNull (provider); -// Assert.AreEqual (17, provider.Count); -// Assert.IsNull (provider.Find ("StyleSheetTheme")); -// Assert.IsNotNull (provider.Find ("Inherits")); -// } -// -// const string pageStart = @"<%@ Page Language=""C#"" %> -//<!DOCTYPE html> -//<html> -//"; -// void HeadBodyCompletion (bool ctrlSpace) -// { -// var provider = WebFormsTesting.CreateProvider (pageStart + "<$", ".aspx", ctrlSpace); -// Assert.IsNotNull (provider); -// Assert.IsNotNull (provider.Find ("head")); -// Assert.IsNotNull (provider.Find ("body")); -// Assert.IsNotNull (provider.Find ("/html>")); -// Assert.IsNull (provider.Find ("div")); -// Assert.IsNotNull (provider.Find ("asp:Button")); -// } -// -// [Test] -// public void HeadBodyCompletionAuto () -// { -// HeadBodyCompletion (false); -// } -// -// [Test] -// public void HeadBodyCompletionCtrlSpace () -// { -// HeadBodyCompletion (true); -// } -// -// [Test] -// public void TagPropertiesAuto () -// { -// var provider = WebFormsTesting.CreateProvider (pageStart + "<asp:Button r$", ".aspx"); -// Assert.IsNotNull (provider.Find ("runat=\"server\"")); -// Assert.IsNotNull (provider.Find ("BorderStyle")); -// } -// -// [Test] -// public void TagPropertiesCtrlSpace () -// { -// var provider = WebFormsTesting.CreateProvider (pageStart + "<asp:Button $", ".aspx", true); -// Assert.IsNotNull (provider.Find ("runat=\"server\"")); -// Assert.IsNotNull (provider.Find ("id")); -// Assert.IsNotNull (provider.Find ("BorderStyle")); -// Assert.IsNotNull (provider.Find ("OnClick")); -// } + [Test] + public void DirectiveCompletion () + { + var provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".aspx"); + Assert.IsNotNull (provider); + Assert.AreEqual (9, provider.Count); + Assert.IsNotNull (provider.Find ("Page")); + Assert.IsNotNull (provider.Find ("Register")); + + provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".master"); + Assert.IsNotNull (provider); + Assert.AreEqual (7, provider.Count); + Assert.IsNotNull (provider.Find ("Master")); + Assert.IsNotNull (provider.Find ("Register")); + + provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".ascx"); + Assert.IsNotNull (provider); + Assert.AreEqual (7, provider.Count); + Assert.IsNotNull (provider.Find ("Control")); + Assert.IsNotNull (provider.Find ("Register")); + + provider = WebFormsTesting.CreateProvider (@"<%@$ %>", ".aspx"); + Assert.IsNull (provider); + + provider = WebFormsTesting.CreateProvider (@"<%@ $ %>", ".aspx"); + Assert.IsNull (provider); + } + + [Test] + public void DirectiveAttributeCompletion () + { + var provider = WebFormsTesting.CreateProvider (@"<%@ Page A$ %>", ".aspx"); + Assert.IsNotNull (provider); + Assert.AreEqual (41, provider.Count); + Assert.IsNotNull (provider.Find ("StyleSheetTheme")); + Assert.IsNotNull (provider.Find ("Inherits")); + + provider = WebFormsTesting.CreateProvider (@"<%@ Master A$ %>", ".master"); + Assert.IsNotNull (provider); + Assert.AreEqual (18, provider.Count); + Assert.IsNull (provider.Find ("StyleSheetTheme")); + Assert.IsNotNull (provider.Find ("MasterPageFile")); + Assert.IsNotNull (provider.Find ("Inherits")); + + provider = WebFormsTesting.CreateProvider (@"<%@ Control A$ %>", ".ascx"); + Assert.IsNotNull (provider); + Assert.AreEqual (17, provider.Count); + Assert.IsNull (provider.Find ("StyleSheetTheme")); + Assert.IsNotNull (provider.Find ("Inherits")); + } + + [Test] + [Ignore ("Not working")] + public void DirectiveAttributeCtrlSpaceCompletion () + { + var provider = WebFormsTesting.CreateProvider (@"<%@ Page $ %>", ".aspx", true); + Assert.IsNotNull (provider); + Assert.AreEqual (41, provider.Count); + Assert.IsNotNull (provider.Find ("StyleSheetTheme")); + Assert.IsNotNull (provider.Find ("Inherits")); + + provider = WebFormsTesting.CreateProvider (@"<%@ Master $ %>", ".master", true); + Assert.IsNotNull (provider); + Assert.AreEqual (18, provider.Count); + Assert.IsNull (provider.Find ("StyleSheetTheme")); + Assert.IsNotNull (provider.Find ("MasterPageFile")); + Assert.IsNotNull (provider.Find ("Inherits")); + + provider = WebFormsTesting.CreateProvider (@"<%@ Control $ %>", ".ascx", true); + Assert.IsNotNull (provider); + Assert.AreEqual (17, provider.Count); + Assert.IsNull (provider.Find ("StyleSheetTheme")); + Assert.IsNotNull (provider.Find ("Inherits")); + } + + const string pageStart = @"<%@ Page Language=""C#"" %> +<!DOCTYPE html> +<html> +"; + void HeadBodyCompletion (bool ctrlSpace) + { + var provider = WebFormsTesting.CreateProvider (pageStart + "<$", ".aspx", ctrlSpace); + Assert.IsNotNull (provider); + Assert.IsNotNull (provider.Find ("head")); + Assert.IsNotNull (provider.Find ("body")); + Assert.IsNotNull (provider.Find ("/html>")); + Assert.IsNull (provider.Find ("div")); + Assert.IsNotNull (provider.Find ("asp:Button")); + } + + [Test] + public void HeadBodyCompletionAuto () + { + HeadBodyCompletion (false); + } + + [Test] + public void HeadBodyCompletionCtrlSpace () + { + HeadBodyCompletion (true); + } + + [Test] + public void TagPropertiesAuto () + { + var provider = WebFormsTesting.CreateProvider (pageStart + "<asp:Button r$", ".aspx"); + Assert.IsNotNull (provider.Find ("runat=\"server\"")); + Assert.IsNotNull (provider.Find ("BorderStyle")); + } + + [Test] + public void TagPropertiesCtrlSpace () + { + var provider = WebFormsTesting.CreateProvider (pageStart + "<asp:Button $", ".aspx", true); + Assert.IsNotNull (provider.Find ("runat=\"server\"")); + Assert.IsNotNull (provider.Find ("id")); + Assert.IsNotNull (provider.Find ("BorderStyle")); + Assert.IsNotNull (provider.Find ("OnClick")); + } } } diff --git a/main/src/addins/AspNet/Tests/WebForms/WebFormsTesting.cs b/main/src/addins/AspNet/Tests/WebForms/WebFormsTesting.cs index c177a300dc..4e27d565df 100644 --- a/main/src/addins/AspNet/Tests/WebForms/WebFormsTesting.cs +++ b/main/src/addins/AspNet/Tests/WebForms/WebFormsTesting.cs @@ -25,8 +25,10 @@ // THE SOFTWARE. using System.IO; +using System.Threading; using MonoDevelop.AspNet.Projects; using MonoDevelop.AspNet.WebForms; +using MonoDevelop.Core.Text; using MonoDevelop.CSharpBinding; using MonoDevelop.CSharpBinding.Tests; using MonoDevelop.Ide.CodeCompletion; @@ -39,85 +41,89 @@ namespace MonoDevelop.AspNet.Tests.WebForms //largely copied from RazorCompletionTesting static class WebFormsTesting { -// TODO : Roslyn port -// public static CompletionDataList CreateProvider (string text, string extension, bool isCtrlSpace = false) -// { -// string editorText; -// TestViewContent sev; -// -// var textEditorCompletion = CreateEditor (text, extension, out editorText, out sev); -// int cursorPosition = text.IndexOf ('$'); -// -// int triggerWordLength = 1; -// var ctx = textEditorCompletion.GetCodeCompletionContext (sev); -// -// if (isCtrlSpace) -// return textEditorCompletion.CodeCompletionCommand (ctx) as CompletionDataList; -// else -// return textEditorCompletion.HandleCodeCompletionAsync (ctx, editorText[cursorPosition - 1], ref triggerWordLength) as CompletionDataList; -// } -// -// static WebFormsTestingEditorExtension CreateEditor (string text, string extension, out string editorText, out TestViewContent sev) -// { -// string parsedText; -// int cursorPosition = text.IndexOf ('$'); -// int endPos = text.IndexOf ('$', cursorPosition + 1); -// if (endPos == -1) -// parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1); -// else { -// parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1); -// editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1); -// cursorPosition = endPos - 1; -// } -// -// var project = new AspNetAppProject ("C#"); -// project.References.Add (new ProjectReference (ReferenceType.Package, "System")); -// project.References.Add (new ProjectReference (ReferenceType.Package, "System.Web")); -// project.FileName = UnitTests.TestBase.GetTempFile (".csproj"); -// string file = UnitTests.TestBase.GetTempFile (extension); -// project.AddFile (file); -// -// var pcw = TypeSystemService.LoadProject (project); -// TypeSystemService.ForceUpdate (pcw); -// pcw.ReconnectAssemblyReferences (); -// -// sev = new TestViewContent (); -// sev.Project = project; -// sev.ContentName = file; -// sev.Text = editorText; -// sev.CursorPosition = cursorPosition; -// -// var tww = new TestWorkbenchWindow (); -// tww.ViewContent = sev; -// -// var doc = new TestDocument (tww); -// doc.Editor.FileName = sev.ContentName; -// var parser = new WebFormsParser (); -// var parsedDoc = (WebFormsParsedDocument) parser.Parse (false, sev.ContentName, new StringReader (parsedText), project); -// doc.HiddenParsedDocument = parsedDoc; -// -// return new WebFormsTestingEditorExtension (doc); -// } -// -// public class WebFormsTestingEditorExtension : WebFormsEditorExtension -// { -// public WebFormsTestingEditorExtension (Document doc) -// { -// Initialize (doc.Editor, doc); -// } -// -// public CodeCompletionContext GetCodeCompletionContext (TestViewContent sev) -// { -// var ctx = new CodeCompletionContext (); -// ctx.TriggerOffset = sev.CursorPosition; -// -// int line, column; -// sev.GetLineColumnFromPosition (ctx.TriggerOffset, out line, out column); -// ctx.TriggerLine = line; -// ctx.TriggerLineOffset = column - 1; -// -// return ctx; -// } -// } + public static CompletionDataList CreateProvider (string text, string extension, bool isCtrlSpace = false) + { + string editorText; + TestViewContent sev; + + var textEditorCompletion = CreateEditor (text, extension, out editorText, out sev); + int cursorPosition = text.IndexOf ('$'); + + var ctx = textEditorCompletion.GetCodeCompletionContext (sev); + + if (isCtrlSpace) + return textEditorCompletion.CodeCompletionCommand (ctx) as CompletionDataList; + else { + var task = textEditorCompletion.HandleCodeCompletionAsync (ctx, editorText [cursorPosition - 1]); + if (task != null) { + return task.Result as CompletionDataList; + } + return null; + } + } + + static WebFormsTestingEditorExtension CreateEditor (string text, string extension, out string editorText, out TestViewContent sev) + { + string parsedText; + int cursorPosition = text.IndexOf ('$'); + int endPos = text.IndexOf ('$', cursorPosition + 1); + if (endPos == -1) + parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1); + else { + parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1); + editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1); + cursorPosition = endPos - 1; + } + + var project = new AspNetAppProject ("C#"); + project.References.Add (new ProjectReference (ReferenceType.Package, "System")); + project.References.Add (new ProjectReference (ReferenceType.Package, "System.Web")); + project.FileName = UnitTests.TestBase.GetTempFile (".csproj"); + string file = UnitTests.TestBase.GetTempFile (extension); + project.AddFile (file); + + sev = new TestViewContent (); + sev.Project = project; + sev.ContentName = file; + sev.Text = editorText; + sev.CursorPosition = cursorPosition; + + var tww = new TestWorkbenchWindow (); + tww.ViewContent = sev; + + var doc = new TestDocument (tww); + doc.Editor.FileName = sev.ContentName; + var parser = new WebFormsParser (); + var options = new ParseOptions { + Project = project, + FileName = sev.ContentName, + Content = new StringTextSource (parsedText) + }; + var parsedDoc = (WebFormsParsedDocument)parser.Parse (options, default(CancellationToken)).Result; + doc.HiddenParsedDocument = parsedDoc; + + return new WebFormsTestingEditorExtension (doc); + } + + public class WebFormsTestingEditorExtension : WebFormsEditorExtension + { + public WebFormsTestingEditorExtension (Document doc) + { + Initialize (doc.Editor, doc); + } + + public CodeCompletionContext GetCodeCompletionContext (TestViewContent sev) + { + var ctx = new CodeCompletionContext (); + ctx.TriggerOffset = sev.CursorPosition; + + int line, column; + sev.GetLineColumnFromPosition (ctx.TriggerOffset, out line, out column); + ctx.TriggerLine = line; + ctx.TriggerLineOffset = column - 1; + + return ctx; + } + } } } diff --git a/main/src/addins/AspNet/WebForms/WebFormsTypeContext.cs b/main/src/addins/AspNet/WebForms/WebFormsTypeContext.cs index 2c97058e3f..d6e7a481c5 100644 --- a/main/src/addins/AspNet/WebForms/WebFormsTypeContext.cs +++ b/main/src/addins/AspNet/WebForms/WebFormsTypeContext.cs @@ -379,8 +379,11 @@ namespace MonoDevelop.AspNet.WebForms { var references = new HashSet<MetadataReference> (); - if (project != null) - references.Add (TypeSystemService.GetCompilationAsync (project).Result.ToMetadataReference ()); + if (project != null) { + var task = TypeSystemService.GetCompilationAsync (project); + if (task.Result != null) + references.Add (task.Result.ToMetadataReference ()); + } if (doc != null) foreach (var asm in doc.Info.Assemblies.Select (a => a.Name).Select (name => GetReferencedAssembly (name))) references.Add (asm); |