Browse Source

Added variable statement declaration tests.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
bf3fdb3659
  1. 84
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 26
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs
  3. 26
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs
  4. 85
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/VariableDeclarationStatementTests.cs
  5. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

84
ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs

@ -84,6 +84,65 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -84,6 +84,65 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
}
return Enumerable.Empty<ICompletionData> ();
}
IEnumerable<string> GenerateNameProposals (AstType type)
{
if (type is PrimitiveType) {
var pt = (PrimitiveType)type;
Console.WriteLine (pt.Keyword);
switch (pt.Keyword) {
case "object":
yield return "o";
yield return "obj";
break;
case "bool":
yield return "b";
yield return "pred";
break;
case "double":
case "float":
case "decimal":
yield return "d";
yield return "f";
yield return "m";
break;
default:
yield return "i";
yield return "j";
yield return "k";
break;
}
yield break;
}
var names = new List<string> ();
int offset1 = document.GetOffset (type.StartLocation);
int offset2 = document.GetOffset (type.EndLocation);
string name = document.GetText (offset1, offset2 - offset1);
int lastNameStart = 0;
for (int i = 1; i < name.Length; i++) {
if (Char.IsUpper (name [i])) {
names.Add (name.Substring (lastNameStart, i - lastNameStart));
lastNameStart = i;
}
}
names.Add (name.Substring (lastNameStart, name.Length - lastNameStart));
var possibleName = new StringBuilder ();
for (int i = 0; i < names.Count; i++) {
possibleName.Length = 0;
for (int j = i; j < names.Count; j++) {
if (string.IsNullOrEmpty (names [j]))
continue;
if (j == i)
names [j] = Char.ToLower (names [j] [0]) + names [j].Substring (1);
possibleName.Append (names [j]);
}
yield return possibleName.ToString ();
}
}
IEnumerable<ICompletionData> MagicKeyCompletion (char completionChar, bool controlSpace)
{
@ -189,30 +248,9 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -189,30 +248,9 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
var isAsExpression = GetExpressionAt (offset);
if (controlSpace && isAsExpression != null && isAsExpression.Item2 is VariableDeclarationStatement && token != "new") {
var parent = isAsExpression.Item2 as VariableDeclarationStatement;
int offset1 = document.GetOffset (parent.Type.StartLocation);
int offset2 = document.GetOffset (parent.Type.EndLocation);
string name = document.GetText (offset1, offset2 - offset1);
var names = new List<string> ();
int lastNameStart = 0;
for (int i = 1; i < name.Length; i++) {
if (Char.IsUpper (name [i])) {
names.Add (name.Substring (lastNameStart, i - lastNameStart));
lastNameStart = i;
}
}
names.Add (name.Substring (lastNameStart, name.Length - lastNameStart));
var proposeNameList = new CompletionDataWrapper (this);
var possibleName = new StringBuilder ();
for (int i = 0; i < names.Count; i++) {
possibleName.Length = 0;
for (int j = i; j < names.Count; j++) {
if (string.IsNullOrEmpty (names [j]))
continue;
if (j == i)
names [j] = Char.ToLower (names [j] [0]) + names [j].Substring (1);
possibleName.Append (names [j]);
}
foreach (var possibleName in GenerateNameProposals (parent.Type)) {
if (possibleName.Length > 0)
proposeNameList.Result.Add (factory.CreateLiteralCompletionData (possibleName.ToString ()));
}

26
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs

@ -1580,31 +1580,7 @@ public class SomeControl : Control @@ -1580,31 +1580,7 @@ public class SomeControl : Control
Assert.IsNull (provider.Find ("Right"), "enum 'Right' found");
}
/// <summary>
/// Bug 487236 - Object initializer completion uses wrong type
/// </summary>
[Test()]
public void TestBug487236B ()
{
CompletionDataList provider = CreateCtrlSpaceProvider (
@"
public class A
{
public string Name { get; set; }
}
class MyTest
{
public void Test ()
{
$A x = new NotExists () { $
}
}
");
Assert.IsNotNull (provider, "provider not found.");
Assert.IsNull (provider.Find ("Name"), "property 'Name' found, but shouldn't'.");
}
/// <summary>
/// Bug 487228 - No intellisense for implicit arrays

26
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs

@ -57,6 +57,32 @@ class MyTest @@ -57,6 +57,32 @@ class MyTest
Assert.IsNotNull (provider.Find ("Name"), "property 'Name' not found.");
}
/// <summary>
/// Bug 487236 - Object initializer completion uses wrong type
/// </summary>
[Test()]
public void TestBug487236B ()
{
var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider (
@"
public class A
{
public string Name { get; set; }
}
class MyTest
{
public void Test ()
{
$A x = new NotExists () { $
}
}
");
Assert.IsNotNull (provider, "provider not found.");
Assert.IsNull (provider.Find ("Name"), "property 'Name' found, but shouldn't'.");
}
[Test()]
public void TestField ()
{

85
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/VariableDeclarationStatementTests.cs

@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
//
// VariableDeclarationStatementTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// 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 VariableDeclarationStatementTests : TestBase
{
[Test()]
public void TestDefaultBehavior ()
{
var provider = CodeCompletionBugTests.CreateProvider (
@"
class MyTest
{
public void Test ()
{
$var v$
}
}
");
Assert.IsTrue (provider == null || provider.Count == 0, "provider not found.");
}
[Test()]
public void TestIntNameProposal ()
{
var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider (
@"
class MyTest
{
public void Test ()
{
$int $
}
}
");
Assert.IsNotNull (provider.Find ("i"), "name proposal 'i' not found.");
Assert.IsNotNull (provider.Find ("j"), "name proposal 'j' not found.");
Assert.IsNotNull (provider.Find ("k"), "name proposal 'k' not found.");
}
[Test()]
public void TestNameProposal ()
{
var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider (
@"
class MyTest
{
public void Test ()
{
$MyTest $
}
}
");
Assert.IsNotNull (provider.Find ("myTest"), "name proposal 'myTest' not found.");
Assert.IsNotNull (provider.Find ("test"), "name proposal 'test' not found.");
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -197,6 +197,7 @@ @@ -197,6 +197,7 @@
<Compile Include="CSharp\CodeCompletion\TestBase.cs" />
<Compile Include="CSharp\CodeCompletion\KeywordTests.cs" />
<Compile Include="CSharp\CodeCompletion\ObjectInitializerTests.cs" />
<Compile Include="CSharp\CodeCompletion\VariableDeclarationStatementTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory.CSharp\ICSharpCode.NRefactory.CSharp.csproj">

Loading…
Cancel
Save