From 86b3da295fff2fd0b6f14dbe3f9cc89533de1365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Mon, 31 Oct 2011 12:04:50 +0100 Subject: [PATCH] Added object initializer tests & fixed them. --- .../Completion/CSharpCompletionEngine.cs | 16 +- .../CodeCompletion/CodeCompletionBugTests.cs | 81 ++----- .../CodeCompletionCSharp3Tests.cs | 24 --- .../CSharp/CodeCompletion/KeywordTests.cs | 141 +++---------- .../CodeCompletion/ObjectInitializerTests.cs | 198 ++++++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 1 + 6 files changed, 251 insertions(+), 210 deletions(-) create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs index 29ba9e2dde..f5ec91fd55 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs @@ -165,6 +165,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion if (methodGroup != null) return CreateParameterCompletion (methodGroup, invocationResult.Item2, invoke.Item2, 0, controlSpace); return null; + case '=': + return controlSpace ? DefaultControlSpaceItems () : null; case ',': int cpos2; if (!GetParameterCompletionCommandOffset (out cpos2)) @@ -357,7 +359,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion default: if (IsInsideComment () || IsInsideString ()) return null; - var identifierStart = GetExpressionAtCursor (); if (IsInLinqContext (offset)) { tokenIndex = offset; token = GetPreviousToken (ref tokenIndex, false); // token last typed @@ -373,6 +374,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion var contextList = new CompletionDataWrapper (this); + var identifierStart = GetExpressionAtCursor (); if (!(char.IsLetter (completionChar) || completionChar == '_') && (identifierStart == null || !(identifierStart.Item2 is ArrayInitializerExpression))) return controlSpace ? HandleAccessorContext () ?? DefaultControlSpaceItems () : null; char prevCh = offset > 2 ? document.GetCharAt (offset - 2) : '\0'; @@ -398,13 +400,20 @@ namespace ICSharpCode.NRefactory.CSharp.Completion AstNode n = identifierStart.Item2; if (n is ArrayInitializerExpression) { var initalizerResult = ResolveExpression (identifierStart.Item1, n.Parent, identifierStart.Item3); - if (initalizerResult != null) { + + var concreteNode = identifierStart.Item3.GetNodeAt (location); + // check if we're on the right side of an initializer expression + if (concreteNode != null && concreteNode.Parent != null && concreteNode.Parent.Parent != null && concreteNode.Identifier != "a" && concreteNode.Parent.Parent is NamedExpression) + return DefaultControlSpaceItems (); + + if (initalizerResult != null) { + foreach (var property in initalizerResult.Item1.Type.GetProperties (ctx)) { if (!property.IsPublic) continue; contextList.AddMember (property); } - foreach (var field in initalizerResult.Item1.Type.GetProperties (ctx)){ + foreach (var field in initalizerResult.Item1.Type.GetFields (ctx)){ if (!field.IsPublic) continue; contextList.AddMember (field); @@ -568,6 +577,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion node = Unit.GetNodeAt (location); rr = ResolveExpression (CSharpParsedFile, node, Unit); } + AddContextCompletion (wrapper, rr != null && (node is Expression) ? rr.Item2 : GetState (), node); return wrapper.Result; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs index e07ddfe545..0cbd3c654a 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs @@ -55,6 +55,17 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion return CreateProvider (text, true); } + public static void CombinedProviderTest (string text, Action act) + { + var provider = CreateProvider (text); + Assert.IsNotNull (provider, "provider == null"); + act (provider); + + provider = CreateCtrlSpaceProvider (text); + Assert.IsNotNull (provider, "provider == null"); + act (provider); + } + class TestFactory : ICompletionDataFactory { class CompletionData : ICompletionData @@ -1569,32 +1580,6 @@ public class SomeControl : Control Assert.IsNull (provider.Find ("Right"), "enum 'Right' found"); } - /// - /// Bug 487236 - Object initializer completion uses wrong type - /// - [Test()] - public void TestBug487236 () - { - CompletionDataList provider = CreateCtrlSpaceProvider ( -@" -public class A -{ - public string Name { get; set; } -} - -class MyTest -{ - public void Test () - { - $var x = new A () { $ - } -} -"); - Assert.IsNotNull (provider, "provider not found."); - - Assert.IsNotNull (provider.Find ("Name"), "property 'Name' not found."); - } - /// /// Bug 487236 - Object initializer completion uses wrong type /// @@ -1918,49 +1903,6 @@ public class Program } - /// - /// Bug 526667 - wrong code completion in object initialisation (new O() {...};) - /// - [Test()] - public void TestBug526667 () - { - CompletionDataList provider = CreateCtrlSpaceProvider ( -@" -using System; -using System.Collections.Generic; - -public class O -{ - public string X { - get; - set; - } - public string Y { - get; - set; - } - public List Z { - get; - set; - } - - public static O A () - { - return new O { - X = ""x"", - Z = new List (new string[] { - ""abc"", - ""def"" - }) - $, $ - }; - } -} -"); - Assert.IsNotNull (provider, "provider not found."); - Assert.IsNotNull (provider.Find ("Y"), "property 'Y' not found."); - } - /// @@ -3635,5 +3577,6 @@ void TestMethod () Assert.IsNotNull (provider.Find ("TM2")); Assert.IsNotNull (provider.Find ("TF1")); } + } } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionCSharp3Tests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionCSharp3Tests.cs index 33dbc3621b..9be82088f9 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionCSharp3Tests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionCSharp3Tests.cs @@ -170,30 +170,6 @@ class Program Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found."); } - [Test()] - public void TestObjectInitializer () - { - CompletionDataList provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( -@" -class foo { - public string bar { get; set; } - public string baz { get; set; } -} - -class test { - public void testcc () - { - foo f = new foo () { - $$ - }; - } -} -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("bar"), "property 'bar' not found."); - Assert.IsNotNull (provider.Find ("baz"), "property 'baz' not found."); - } - [Test()] public void TestLambdaExpressionCase1 () { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs index acd5b539fd..d02519aafd 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs @@ -89,134 +89,68 @@ void Test (string t) [Test()] public void ModifierKeywordTest () { - var provider = CodeCompletionBugTests.CreateProvider ( + CodeCompletionBugTests.CombinedProviderTest ( @" $p$ -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); - Assert.IsNotNull (provider.Find ("namespace"), "keyword 'namespace' not found."); - } - - [Test()] - public void ModifierKeywordTestCtrlSpace () - { - var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( -@" -$p$ -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); - Assert.IsNotNull (provider.Find ("namespace"), "keyword 'namespace' not found."); +", provider => { + Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); + Assert.IsNotNull (provider.Find ("namespace"), "keyword 'namespace' not found."); + }); } [Test()] public void ModifierKeywordTestCase2 () { - var provider = CodeCompletionBugTests.CreateProvider ( + CodeCompletionBugTests.CombinedProviderTest ( @" class Test { $p$ } -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); - Assert.IsNull (provider.Find ("namespace"), "keyword 'namespace' found."); - } - - [Test()] - public void ModifierKeywordTestCase2CrtlSpace () - { - var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( -@" -class Test -{ - $p$ -} -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); - Assert.IsNull (provider.Find ("namespace"), "keyword 'namespace' found."); +", provider => { + Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); + Assert.IsNull (provider.Find ("namespace"), "keyword 'namespace' found."); + }); } [Test()] public void GetSetKeywordTest () { - var provider = CodeCompletionBugTests.CreateProvider ( + CodeCompletionBugTests.CombinedProviderTest ( @"class Test { public int MyProperty { $g$ } -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); - Assert.IsNotNull (provider.Find ("get"), "keyword 'get' not found."); - Assert.IsNotNull (provider.Find ("set"), "keyword 'set' not found."); - } - - - [Test()] - public void GetSetKeywordTestCtrlSpace () - { - var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( -@"class Test -{ - public int MyProperty { - $$ -} -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); - Assert.IsNotNull (provider.Find ("get"), "keyword 'get' not found."); - Assert.IsNotNull (provider.Find ("set"), "keyword 'set' not found."); +", provider => { + Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); + Assert.IsNotNull (provider.Find ("get"), "keyword 'get' not found."); + Assert.IsNotNull (provider.Find ("set"), "keyword 'set' not found."); + }); } - [Test()] public void AddRemoveKeywordTest () { - var provider = CodeCompletionBugTests.CreateProvider ( -@" -using System; -class Test -{ - public event EventHandler MyProperty { - $g$ -} -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.AreEqual (2, provider.Count); - Assert.IsNotNull (provider.Find ("add"), "keyword 'add' not found."); - Assert.IsNotNull (provider.Find ("remove"), "keyword 'remove' not found."); - } - - [Test()] - public void AddRemoveKeywordTestCtrlSpace () - { - var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( -@" -using System; + CodeCompletionBugTests.CombinedProviderTest ( +@"using System; class Test { public event EventHandler MyProperty { $g$ } -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.AreEqual (2, provider.Count); - Assert.IsNotNull (provider.Find ("add"), "keyword 'add' not found."); - Assert.IsNotNull (provider.Find ("remove"), "keyword 'remove' not found."); +", (provider) => { + Assert.AreEqual (2, provider.Count); + Assert.IsNotNull (provider.Find ("add"), "keyword 'add' not found."); + Assert.IsNotNull (provider.Find ("remove"), "keyword 'remove' not found."); + }); } - [Test()] public void IsAsKeywordTest () { - var provider = CodeCompletionBugTests.CreateProvider ( -@" -using System; + CodeCompletionBugTests.CombinedProviderTest ( +@"using System; class Test { public void Method () @@ -227,32 +161,11 @@ class Test } } } -"); - Assert.IsNotNull (provider, "provider == null"); - Assert.IsNotNull (provider.Find ("is"), "keyword 'is' not found."); - Assert.IsNotNull (provider.Find ("as"), "keyword 'as' not found."); - } - - [Test()] - public void IsAsKeywordTestCtrlSpace () - { - var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( -@" -using System; -class Test -{ - public void Method () - { - void TestMe (object o) - { - if (o i$$ - } - } -} -"); +", (provider) => { Assert.IsNotNull (provider, "provider == null"); Assert.IsNotNull (provider.Find ("is"), "keyword 'is' not found."); Assert.IsNotNull (provider.Find ("as"), "keyword 'as' not found."); + }); } } } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs new file mode 100644 index 0000000000..25b4be94d3 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs @@ -0,0 +1,198 @@ +// +// ObjectInitializerTests.cs +// +// Author: +// Mike Krüger +// +// Copyright (c) 2011 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using NUnit.Framework; + +namespace ICSharpCode.NRefactory.CSharp.CodeCompletion +{ + public class ObjectInitializerTests : TestBase + { + /// + /// Bug 487236 - Object initializer completion uses wrong type + /// + [Test()] + public void TestBug487236 () + { + CompletionDataList provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +public class A +{ + public string Name { get; set; } +} + +class MyTest +{ + public void Test () + { + $var x = new A () { $ + } +} +"); + Assert.IsNotNull (provider, "provider not found."); + + Assert.IsNotNull (provider.Find ("Name"), "property 'Name' not found."); + } + + [Test()] + public void TestField () + { + CodeCompletionBugTests.CombinedProviderTest ( +@" +public class A +{ + public int Test; +} + +class MyTest +{ + public void Test () + { + $new A () { T$ + } +} +", provider => { + Assert.IsNotNull (provider.Find ("Test"), "field 'Test' not found."); + }); + } + + [Test()] + public void TestProperty () + { + CodeCompletionBugTests.CombinedProviderTest ( +@" +public class A +{ + public int Test { get; set; } +} + +class MyTest +{ + public void Test () + { + $new A () { T$ + } +} +", provider => { + Assert.IsNotNull (provider.Find ("Test"), "property 'Test' not found."); + }); + } + + + +/// + /// Bug 526667 - wrong code completion in object initialisation (new O() {...};) + /// + [Test()] + public void TestBug526667 () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +using System; +using System.Collections.Generic; + +public class O +{ + public string X { + get; + set; + } + public string Y { + get; + set; + } + public List Z { + get; + set; + } + + public static O A () + { + return new O { + X = ""x"", + Z = new List (new string[] { + ""abc"", + ""def"" + }) + $, $ + }; + } +} +"); + Assert.IsNotNull (provider, "provider not found."); + Assert.IsNotNull (provider.Find ("Y"), "property 'Y' not found."); + } + + [Test()] + public void TestObjectInitializer () + { + CodeCompletionBugTests.CombinedProviderTest ( +@"class foo { + public string bar { get; set; } + public string baz { get; set; } +} + +class test { + public void testcc () + { + foo f = new foo () { + $$ + }; + } +} +", provider => { + Assert.IsNotNull (provider.Find ("bar"), "property 'bar' not found."); + Assert.IsNotNull (provider.Find ("baz"), "property 'baz' not found."); + }); + } + + + /// + /// Bug 1745 - [New Resolver] Invalid completion in class initialization + /// + [Test()] + public void TestBug1745 () + { + CodeCompletionBugTests.CombinedProviderTest ( + +@"class Test { + public int TF1 { get; set; } +} + +class CCTest { + void TestMethod () + { + $new Test () { TF1 = T$ + } +} +", provider => { + Assert.IsNull (provider.Find ("TF1")); + Assert.IsNotNull (provider.Find ("Test")); + }); + } + } +} + diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index e0be8199c8..ac60dcd333 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -196,6 +196,7 @@ +