Browse Source

Converted the unit tests.

Some of them are failing - I expect it has to do with the adapter.
newNRvisualizers
Mike Krüger 15 years ago
parent
commit
c01ba21f1e
  1. 285
      ICSharpCode.NRefactory.Tests/FormattingTests/TestBlankLineFormatting.cs
  2. 405
      ICSharpCode.NRefactory.Tests/FormattingTests/TestBraceStlye.cs
  3. 142
      ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs
  4. 1810
      ICSharpCode.NRefactory.Tests/FormattingTests/TestSpacingVisitor.cs
  5. 1491
      ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs
  6. 512
      ICSharpCode.NRefactory.Tests/FormattingTests/TestTypeLevelIndentation.cs
  7. 250
      ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs
  8. 21
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  9. 14
      ICSharpCode.NRefactory/CSharp/Formatter/AstFormattingVisitor.cs
  10. 10
      ICSharpCode.NRefactory/CSharp/Formatter/Change.cs
  11. 1
      ICSharpCode.NRefactory/CSharp/Formatter/ITextEditorAdapter.cs

285
ICSharpCode.NRefactory.Tests/FormattingTests/TestBlankLineFormatting.cs

@ -0,0 +1,285 @@ @@ -0,0 +1,285 @@
//
// TastBlankLineFormatting.cs
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
//
// 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 System.IO;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp;
namespace ICSharpCode.NRefactory.FormattingTests
{
[TestFixture()]
public class TestBlankLineFormatting : TestBase
{
[Test()]
public void TestBlankLinesAfterUsings ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesAfterUsings = 2;
var adapter = Test (policy, @"using System;
using System.Text;
namespace Test
{
}",
@"using System;
using System.Text;
namespace Test
{
}");
policy.BlankLinesAfterUsings = 0;
Continue (policy, adapter,
@"using System;
using System.Text;
namespace Test
{
}");
}
[Test()]
public void TestBlankLinesBeforeUsings ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesAfterUsings = 0;
policy.BlankLinesBeforeUsings = 2;
var adapter = Test (policy, @"using System;
using System.Text;
namespace Test
{
}",
@"
using System;
using System.Text;
namespace Test
{
}");
policy.BlankLinesBeforeUsings = 0;
Continue (policy, adapter,
@"using System;
using System.Text;
namespace Test
{
}");
}
[Test()]
public void TestBlankLinesBeforeFirstDeclaration ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesBeforeFirstDeclaration = 2;
var adapter = Test (policy, @"namespace Test
{
class Test
{
}
}",
@"namespace Test
{
class Test
{
}
}");
policy.BlankLinesBeforeFirstDeclaration = 0;
Continue (policy, adapter,
@"namespace Test
{
class Test
{
}
}");
}
[Test()]
public void TestBlankLinesBetweenTypes ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesBetweenTypes = 1;
var adapter = Test (policy, @"namespace Test
{
class Test1
{
}
class Test2
{
}
class Test3
{
}
}",
@"namespace Test
{
class Test1
{
}
class Test2
{
}
class Test3
{
}
}");
policy.BlankLinesBetweenTypes = 0;
Continue (policy, adapter, @"namespace Test
{
class Test1
{
}
class Test2
{
}
class Test3
{
}
}");
}
[Test()]
public void TestBlankLinesBetweenFields ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesBetweenFields = 1;
var adapter = Test (policy, @"class Test
{
int a;
int b;
int c;
}",
@"class Test
{
int a;
int b;
int c;
}");
policy.BlankLinesBetweenFields = 0;
Continue (policy, adapter, @"class Test
{
int a;
int b;
int c;
}");
}
[Test()]
public void TestBlankLinesBetweenEventFields ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesBetweenEventFields = 1;
var adapter = Test (policy, @"class Test
{
public event EventHandler a;
public event EventHandler b;
public event EventHandler c;
}",
@"class Test
{
public event EventHandler a;
public event EventHandler b;
public event EventHandler c;
}");
policy.BlankLinesBetweenEventFields = 0;
Continue (policy, adapter,
@"class Test
{
public event EventHandler a;
public event EventHandler b;
public event EventHandler c;
}");
}
[Test()]
public void TestBlankLinesBetweenMembers ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesBetweenMembers = 1;
var adapter = Test (policy,@"class Test
{
void AMethod ()
{
}
void BMethod ()
{
}
void CMethod ()
{
}
}", @"class Test
{
void AMethod ()
{
}
void BMethod ()
{
}
void CMethod ()
{
}
}");
policy.BlankLinesBetweenMembers = 0;
Continue (policy, adapter, @"class Test
{
void AMethod ()
{
}
void BMethod ()
{
}
void CMethod ()
{
}
}");
}
}
}

405
ICSharpCode.NRefactory.Tests/FormattingTests/TestBraceStlye.cs

@ -23,245 +23,184 @@ @@ -23,245 +23,184 @@
// 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 System.IO;
using NUnit.Framework;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects;
using MonoDevelop.Core;
using MonoDevelop.Ide.CodeCompletion;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Projects.Dom.Parser;
using MonoDevelop.CSharp.Parser;
using MonoDevelop.CSharp.Resolver;
using MonoDevelop.CSharp.Completion;
using Mono.TextEditor;
using MonoDevelop.CSharp.Formatting;
using ICSharpCode.NRefactory.CSharp;
namespace MonoDevelop.CSharpBinding.FormattingTests
namespace ICSharpCode.NRefactory.FormattingTests
{
[TestFixture()]
public class TestBraceStyle : UnitTests.TestBase
public class TestBraceStyle : TestBase
{
[Test()]
[Ignore("currently failing because namespaces are not inserted")]
public void TestNamespaceBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"namespace A
{
namespace B {
class Test {}
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"namespace A {
var adapter = Test (policy, @"namespace A
{
namespace B {
class Test {}
}
}",
@"namespace A {
namespace B {
class Test {}
}
}", data.Document.Text);
}");
policy.NamespaceBraceStyle = BraceStyle.NextLineShifted;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"namespace A
Continue (policy, adapter,
@"namespace A
{
namespace B
{
class Test {}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestClassBraceStlye ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test {}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.EndOfLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test {
}", data.Document.Text);
Test (policy,
@"class Test {}",
@"class Test {
}");
}
[Test()]
public void TestStructBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"struct Test {}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.StructBraceStyle = BraceStyle.NextLine;
policy.StructBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"struct Test
Test (policy,
@"struct Test {}",
@"struct Test
{
}", data.Document.Text);
}");
}
[Test()]
public void TestInterfaceBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"interface Test {}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.InterfaceBraceStyle = BraceStyle.NextLine;
policy.InterfaceBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"interface Test
Test (policy,
@"interface Test {}",
@"interface Test
{
}", data.Document.Text);
}");
}
[Test()]
public void TestEnumBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"enum Test {
A
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.EnumBraceStyle = BraceStyle.NextLineShifted;
policy.EnumBraceStyle = BraceStyle.NextLineShifted;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"enum Test
Test (policy, @"enum Test {
A
}",
@"enum Test
{
A
}", data.Document.Text);
}");
}
[Test()]
public void TestMethodBraceStlye ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
{
Test MyMethod() {}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.MethodBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
Test (policy, @"class Test
{
Test MyMethod()
Test MyMethod() {}
}",
@"class Test
{
Test MyMethod ()
{
}
}", data.Document.Text);
}");
}
[Test()]
public void TestConstructorBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
{
Test() {}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ConstructorBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
Test (policy, @"class Test
{
Test()
Test() {}
}",
@"class Test
{
Test ()
{
}
}", data.Document.Text);
}");
}
[Test()]
public void TestDestructorBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
{
~Test() {}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.DestructorBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
Test (policy, @"class Test
{
~Test() {}
}",
@"class Test
{
~Test()
~Test ()
{
}
}", data.Document.Text);
}");
}
[Test()]
public void TestPropertyBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyBraceStyle = BraceStyle.NextLine;
Test (policy, @"class Test
{
Test A {
get;
set;
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
}",
@"class Test
{
Test A
{
get;
set;
}
}", data.Document.Text);
}");
}
[Test()]
public void TestPropertyGetBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyGetBraceStyle = BraceStyle.NextLine;
Test (policy, @"class Test
{
Test A {
get {
@ -269,16 +208,8 @@ namespace B { @@ -269,16 +208,8 @@ namespace B {
}
set;
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyGetBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
}",
@"class Test
{
Test A {
get
@ -287,32 +218,26 @@ namespace B { @@ -287,32 +218,26 @@ namespace B {
}
set;
}
}", data.Document.Text);
}");
}
[Test()]
public void TestAllowPropertyGetBlockInline ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
{
Test A {
get { return null; }
set { ; }
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyBraceStyle = BraceStyle.DoNotChange;
policy.AllowPropertyGetBlockInline = true;
policy.AllowPropertySetBlockInline = false;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
var adapter = Test (policy, @"class Test
{
Test A {
get { return null; }
set { ; }
}
}",
@"class Test
{
Test A {
get { return null; }
@ -320,13 +245,11 @@ namespace B { @@ -320,13 +245,11 @@ namespace B {
;
}
}
}", data.Document.Text);
}");
policy.AllowPropertyGetBlockInline = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Continue (policy, adapter,
@"class Test
{
Test A {
get {
@ -336,32 +259,25 @@ namespace B { @@ -336,32 +259,25 @@ namespace B {
;
}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestAllowPropertySetBlockInline ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
{
Test A {
get { return null; }
set { ; }
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyBraceStyle = BraceStyle.DoNotChange;
policy.AllowPropertyGetBlockInline = false;
policy.AllowPropertySetBlockInline = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
var adapter = Test (policy, @"class Test
{
Test A {
get { return null; }
set { ; }
}
}",
@"class Test
{
Test A {
get {
@ -369,13 +285,11 @@ namespace B { @@ -369,13 +285,11 @@ namespace B {
}
set { ; }
}
}", data.Document.Text);
}");
policy.AllowPropertySetBlockInline = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Continue (policy, adapter,
@"class Test
{
Test A {
get {
@ -385,15 +299,16 @@ namespace B { @@ -385,15 +299,16 @@ namespace B {
;
}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestPropertySetBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertySetBraceStyle = BraceStyle.NextLine;
Test (policy, @"class Test
{
Test A {
get;
@ -401,16 +316,8 @@ namespace B { @@ -401,16 +316,8 @@ namespace B {
;
}
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertySetBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
}",
@"class Test
{
Test A {
get;
@ -419,15 +326,18 @@ namespace B { @@ -419,15 +326,18 @@ namespace B {
;
}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestEventBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.EventBraceStyle = BraceStyle.NextLine;
policy.EventAddBraceStyle = BraceStyle.NextLine;
policy.EventRemoveBraceStyle = BraceStyle.NextLine;
Test (policy, @"class Test
{
public event EventHandler Handler {
add {
@ -435,18 +345,8 @@ namespace B { @@ -435,18 +345,8 @@ namespace B {
remove {
}
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.EventBraceStyle = BraceStyle.NextLine;
policy.EventAddBraceStyle = BraceStyle.NextLine;
policy.EventRemoveBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
}",
@"class Test
{
public event EventHandler Handler
{
@ -457,31 +357,24 @@ namespace B { @@ -457,31 +357,24 @@ namespace B {
{
}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestAllowEventAddBlockInline ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.AllowEventAddBlockInline = true;
policy.AllowEventRemoveBlockInline = false;
Test (policy, @"class Test
{
public event EventHandler Handler {
add { ; }
remove { ; }
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.AllowEventAddBlockInline = true;
policy.AllowEventRemoveBlockInline = false;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
}",
@"class Test
{
public event EventHandler Handler {
add { ; }
@ -489,31 +382,24 @@ namespace B { @@ -489,31 +382,24 @@ namespace B {
;
}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestAllowEventRemoveBlockInline ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"class Test
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.AllowEventAddBlockInline = false;
policy.AllowEventRemoveBlockInline = true;
Test (policy, @"class Test
{
public event EventHandler Handler {
add { ; }
remove { ; }
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.AllowEventAddBlockInline = false;
policy.AllowEventRemoveBlockInline = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Console.WriteLine (data.Document.Text);
Assert.AreEqual (@"class Test
}",
@"class Test
{
public event EventHandler Handler {
add {
@ -521,10 +407,7 @@ namespace B { @@ -521,10 +407,7 @@ namespace B {
}
remove { ; }
}
}", data.Document.Text);
}");
}
}
}*/
}

142
ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs

@ -23,27 +23,16 @@ @@ -23,27 +23,16 @@
// 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 System.IO;
using NUnit.Framework;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects;
using MonoDevelop.Core;
using MonoDevelop.Ide.CodeCompletion;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Projects.Dom.Parser;
using MonoDevelop.CSharp.Parser;
using MonoDevelop.CSharp.Resolver;
using MonoDevelop.CSharp.Completion;
using Mono.TextEditor;
using MonoDevelop.CSharp.Formatting;
using System.Collections.Generic;
using MonoDevelop.Refactoring;
using ICSharpCode.NRefactory.CSharp;
namespace MonoDevelop.CSharpBinding.FormattingTests
namespace ICSharpCode.NRefactory.FormattingTests
{
[TestFixture()]
public class TestFormattingBugs : UnitTests.TestBase
public class TestFormattingBugs : TestBase
{
/// <summary>
/// Bug 325187 - Bug in smart indent
@ -71,8 +60,7 @@ Console.WriteLine (""Bad indent"");", @@ -71,8 +60,7 @@ Console.WriteLine (""Bad indent"");",
/// Bug 415469 - return ternary in a switch is not tabbed properly
/// </summary>
[Test()]
[Ignore("currently failing because of 'string' has the wrong offset - mcs bug")]
public void TestBug415469 ()
public void TestBug415469 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
@ -111,52 +99,98 @@ using (IDisposable b = null) { @@ -111,52 +99,98 @@ using (IDisposable b = null) {
}");
}
/// <summary>
/// Bug 655635 - Auto format document doesn't indent comments as well
/// </summary>
[Test()]
public void TestBug655635 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
TestStatementFormatting (policy,
@"try {
// Comment 1
myObject.x = Run ();
} catch (InvalidOperationException e) {
Console.WriteLine (e.Message);
}", @"try {
// Comment 1
myObject.x = Run ();
} catch (InvalidOperationException e) {
Console.WriteLine (e.Message);
}");
}
static void TestStatementFormatting (CSharpFormattingPolicy policy, string input, string expectedOutput)
void TestStatementFormatting (CSharpFormattingPolicy policy, string input, string expectedOutput)
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@"class Test
var result = GetResult (policy, @"class Test
{
MyType TestMethod ()
{
" + input + @"
}
}";
Console.WriteLine (data.Document.Text);
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
DomSpacingVisitor domSpacingVisitor = new DomSpacingVisitor (policy, data);
domSpacingVisitor.AutoAcceptChanges = false;
compilationUnit.AcceptVisitor (domSpacingVisitor, null);
DomIndentationVisitor domIndentationVisitor = new DomIndentationVisitor (policy, data);
domIndentationVisitor.AutoAcceptChanges = false;
compilationUnit.AcceptVisitor (domIndentationVisitor, null);
List<Change> changes = new List<Change> ();
changes.AddRange (domSpacingVisitor.Changes);
changes.AddRange (domIndentationVisitor.Changes);
RefactoringService.AcceptChanges (null, null, changes);
for (int i = 0; i < data.Document.LineCount; i++) {
LineSegment line = data.Document.GetLine (i);
if (line.EditableLength < 2)
continue;
data.Remove (line.Offset, 2);
}
string text = data.Document.GetTextBetween (data.Document.GetLine (4).Offset,
data.Document.GetLine (data.Document.LineCount - 2).Offset).Trim ();
Console.WriteLine (text);
}");
int start = result.GetLineOffset (5);
int end = result.GetLineOffset (result.LineCount - 1);
string text = result.GetTextAt (start, end - start).Trim ().Replace ("\t\t\t", "\t");
Assert.AreEqual (expectedOutput, text);
}
/// <summary>
/// Bug 659675 - on-the-fly code formatting breaks @-prefixed identifiers
/// </summary>
[Test()]
public void TestBug659675 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
TestStatementFormatting (policy, "@string=@int;", "@string = @int;");
}
/// <summary>
/// Bug 670213 - Document formatter deletes valid text!
/// </summary>
[Test()]
public void TestBug670213 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.MethodBraceStyle = BraceStyle.EndOfLine;
Test (policy, @"class Test
{
Test MyMethod() // Comment
{
}
}",
@"class Test
{
Test MyMethod () { // Comment
}
}");
}
/// <summary>
/// Bug 677261 - Format Document with constructor with over-indented opening curly brace
/// </summary>
[Test()]
public void TestBug677261 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ConstructorBraceStyle = BraceStyle.EndOfLine;
Test (policy, @"class Test
{
Test ()
{
}
}",
@"class Test
{
Test () {
}
}");
}
}
}
*/

1810
ICSharpCode.NRefactory.Tests/FormattingTests/TestSpacingVisitor.cs

File diff suppressed because it is too large Load Diff

1491
ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs

File diff suppressed because it is too large Load Diff

512
ICSharpCode.NRefactory.Tests/FormattingTests/TestTypeLevelIndentation.cs

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
//
// TestTypeLevelIndentation.cs
//
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
@ -23,164 +23,196 @@ @@ -23,164 +23,196 @@
// 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 System.IO;
using NUnit.Framework;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects;
using MonoDevelop.Core;
using MonoDevelop.Ide.CodeCompletion;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Projects.Dom.Parser;
using MonoDevelop.CSharp.Parser;
using MonoDevelop.CSharp.Resolver;
using MonoDevelop.CSharp.Completion;
using Mono.TextEditor;
using MonoDevelop.CSharp.Formatting;
using ICSharpCode.NRefactory.CSharp;
namespace MonoDevelop.CSharpBinding.FormattingTests
namespace ICSharpCode.NRefactory.FormattingTests
{
[TestFixture()]
public class TestTypeLevelIndentation : UnitTests.TestBase
public class TestTypeLevelIndentation : TestBase
{
[Test()]
public void TestClassIndentation ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@" class Test {}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@" class Test {}",
@"class Test {}");
}
[Test()]
public void TestClassIndentationInNamespaces ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@"namespace A { class Test {} }",
@"namespace A {
class Test {}
}");
}
[Test()]
public void TestNoIndentationInNamespaces ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
policy.IndentNamespaceBody = false;
Test (policy,
@"namespace A { class Test {} }",
@"namespace A {
class Test {}
}");
}
[Test()]
public void TestClassIndentationInNamespacesCase2 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.DoNotChange;
policy.NamespaceBraceStyle = BraceStyle.NextLine;
policy.ClassBraceStyle = BraceStyle.NextLine;
policy.ConstructorBraceStyle = BraceStyle.NextLine;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test {}", data.Document.Text);
Test (policy,
@"using System;
namespace MonoDevelop.CSharp.Formatting {
public class FormattingProfileService {
public FormattingProfileService () {
}
}
}",
@"using System;
namespace MonoDevelop.CSharp.Formatting
{
public class FormattingProfileService
{
public FormattingProfileService ()
{
}
}
}");
}
[Test()]
public void TestIndentClassBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentClassBody = true;
Test (policy,
@"class Test
{
Test a;
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentClassBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}", @"class Test
{
Test a;
}", data.Document.Text);
}");
policy.IndentClassBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Test (policy,
@"class Test
{
Test a;
}",
@"class Test
{
Test a;
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentInterfaceBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@"interface Test
{
Test Foo ();
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentInterfaceBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"interface Test
Test (policy,
@"interface Test
{
Test Foo ();
}", @"interface Test
{
Test Foo ();
}", data.Document.Text);
}");
policy.IndentInterfaceBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"interface Test
Test (policy,
@"interface Test
{
Test Foo ();
}", @"interface Test
{
Test Foo ();
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentStructBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@"struct Test
{
Test a;
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentStructBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"struct Test
Test (policy,
@"struct Test
{
Test a;
}", data.Document.Text);
Test Foo ();
}", @"struct Test
{
Test Foo ();
}");
policy.IndentStructBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"struct Test
Test (policy,
@"struct Test
{
Test a;
}", data.Document.Text);
Test Foo ();
}", @"struct Test
{
Test Foo ();
}");
}
[Test()]
public void TestIndentEnumBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@"enum Test
{
A
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentEnumBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"enum Test
Test (policy,
@"enum Test
{
A
}", @"enum Test
{
A
}", data.Document.Text);
}");
policy.IndentEnumBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"enum Test
Test (policy,
@"enum Test
{
A
}", @"enum Test
{
A
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentMethodBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentMethodBody = true;
Test (policy,
@"class Test
{
Test Foo ()
@ -188,40 +220,42 @@ A @@ -188,40 +220,42 @@ A
;
;
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentMethodBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
Test Foo ()
{
;
;
}
}", data.Document.Text);
}");
policy.IndentMethodBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Test (policy,
@"class Test
{
Test Foo ()
{
;
;
}
}",
@"class Test
{
Test Foo ()
{
;
;
}
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentMethodBodyOperatorCase ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentMethodBody = true;
var adapter = Test (policy,
@"class Test
{
static Test operator+(Test left, Test right)
@ -229,78 +263,104 @@ A @@ -229,78 +263,104 @@ A
;
;
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentMethodBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
static Test operator+(Test left, Test right)
static Test operator+ (Test left, Test right)
{
;
;
}
}", data.Document.Text);
}");
policy.IndentMethodBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Continue (policy, adapter, @"class Test
{
static Test operator+(Test left, Test right)
static Test operator+ (Test left, Test right)
{
;
;
}
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentPropertyBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentPropertyBody = true;
var adapter = Test (policy,
@"class Test
{
Test TestMe {
get;
set;
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentPropertyBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
Test TestMe {
get;
set;
}
}", data.Document.Text);
}");
policy.IndentPropertyBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Continue (policy, adapter,
@"class Test
{
Test TestMe {
get;
set;
}
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentPropertyOneLine ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyFormatting = PropertyFormatting.AllowOneLine;
policy.AllowPropertyGetBlockInline = true;
policy.AllowPropertySetBlockInline = true;
Test (policy,
@"class Test
{
Test TestMe { get;set; }
}",
@"class Test
{
Test TestMe { get; set; }
}");
}
[Test()]
public void TestIndentPropertyOneLineCase2 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyFormatting = PropertyFormatting.AllowOneLine;
policy.AllowPropertyGetBlockInline = true;
policy.AllowPropertySetBlockInline = true;
Test (policy,
@"class Test
{
Test TestMe { get { ; }set{;} }
}",
@"class Test
{
Test TestMe { get { ; } set { ; } }
}");
}
[Test()]
public void TestIndentPropertyBodyIndexerCase ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentPropertyBody = true;
var adapter = Test (policy,
@"class Test
{
Test this[int a] {
@ -311,16 +371,10 @@ set { @@ -311,16 +371,10 @@ set {
;
}
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentPropertyBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
Test this[int a] {
Test this [int a] {
get {
return null;
}
@ -328,13 +382,12 @@ set { @@ -328,13 +382,12 @@ set {
;
}
}
}", data.Document.Text);
}");
policy.IndentPropertyBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Continue (policy, adapter,
@"class Test
{
Test this[int a] {
Test this [int a] {
get {
return null;
}
@ -342,117 +395,129 @@ set { @@ -342,117 +395,129 @@ set {
;
}
}
}", data.Document.Text);
}");
}
[Test()]
public void TestPropertyAlignment ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyFormatting = PropertyFormatting.AllowOneLine;
var adapter = Test (policy,
@"class Test
{
Test TestMe { get; set; }
}",
@"class Test
{
Test TestMe { get; set; }
}");
policy.PropertyFormatting = PropertyFormatting.ForceNewLine;
Continue (policy, adapter,
@"class Test
{
Test TestMe {
get;
set;
}
}");
policy.PropertyFormatting = PropertyFormatting.ForceOneLine;
Continue (policy, adapter,
@"class Test
{
Test TestMe { get; set; }
}");
}
[Test()]
[Ignore("currently failing because namespaces are not inserted")]
public void TestIndentNamespaceBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@" namespace Test {
class FooBar {}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.DoNotChange;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.IndentNamespaceBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"namespace Test {
var adapter = Test (policy,
@" namespace Test {
class FooBar {}
}",
@"namespace Test {
class FooBar {}
}", data.Document.Text);
}");
policy.IndentNamespaceBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"namespace Test {
Continue (policy, adapter,
@"namespace Test {
class FooBar {}
}", data.Document.Text);
}");
}
[Test()]
public void TestMethodIndentation ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.MethodBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@"class Test
{
MyType TestMethod () {}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.MethodBraceStyle = BraceStyle.DoNotChange;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
MyType TestMethod () {}
}", data.Document.Text);
}");
}
[Test()]
public void TestPropertyIndentation ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@"class Test
{
public int Prop { get; set; }
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.PropertyBraceStyle = BraceStyle.DoNotChange;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",@"class Test
{
public int Prop { get; set; }
}", data.Document.Text);
}");
}
[Test()]
public void TestPropertyIndentationCase2 ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
Test (policy,
@"class Test
{
public int Prop {
get;
set;
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
public int Prop {
get;
set;
}
}", data.Document.Text);
}");
}
[Test()]
public void TestIndentEventBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentEventBody = true;
var adapter = Test (policy,
@"class Test
{
public event EventHandler TestMe {
@ -463,14 +528,8 @@ remove { @@ -463,14 +528,8 @@ remove {
;
}
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentEventBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
}",
@"class Test
{
public event EventHandler TestMe {
add {
@ -480,11 +539,10 @@ remove { @@ -480,11 +539,10 @@ remove {
;
}
}
}", data.Document.Text);
}");
policy.IndentEventBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
Continue (policy, adapter,
@"class Test
{
public event EventHandler TestMe {
add {
@ -494,7 +552,7 @@ remove { @@ -494,7 +552,7 @@ remove {
;
}
}
}", data.Document.Text);
}");
}
}
}*/
}

250
ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

@ -0,0 +1,250 @@ @@ -0,0 +1,250 @@
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.CSharp;
using System.IO;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.FormattingTests
{
/// <summary>
/// Text editor test adapter. Only implemented for testing purposes. Don't use in production code.
/// </summary>
class TextEditorTestAdapter : ITextEditorAdapter
{
string text;
public string Text {
get {
return this.text;
}
}
List<Delimiter> delimiters;
struct Delimiter
{
public readonly int Offset;
public readonly int Length;
public int EndOffset {
get { return Offset + Length; }
}
public Delimiter (int offset, int length)
{
Offset = offset;
Length = length;
}
}
static IEnumerable<Delimiter> FindDelimiter (string text)
{
for (int i = 0; i < text.Length; i++) {
switch (text [i]) {
case '\r':
if (i + 1 < text.Length && text [i + 1] == '\n') {
yield return new Delimiter (i, 2);
i++;
} else {
yield return new Delimiter (i, 1);
}
break;
case '\n':
yield return new Delimiter (i, 1);
break;
}
}
}
public TextEditorTestAdapter (string text)
{
this.text = text;
delimiters = new List<Delimiter> (FindDelimiter (text));
}
class Segment
{
public readonly int Offset;
public readonly int Length;
public readonly int DelimiterLength;
public Segment (int offset, int length, int delimiterLength)
{
this.Offset = offset;
this.Length = length;
this.DelimiterLength = delimiterLength;
}
}
Segment Get (int number)
{
number--;
if (number < 0 || number - 1 >= delimiters.Count)
return null;
int startOffset = number > 0 ? delimiters [number - 1].EndOffset : 0;
int endOffset;
int delimiterLength;
if (number < delimiters.Count) {
endOffset = delimiters [number].EndOffset;
delimiterLength = delimiters [number].Length;
} else {
endOffset = text.Length;
delimiterLength = 0;
}
return new Segment (startOffset, endOffset - startOffset, delimiterLength);
}
public void ApplyChanges (List<Change> changes)
{
changes.Sort ((x, y) => x.Offset.CompareTo (y.Offset));
changes.Reverse ();
foreach (var change in changes) {
text = text.Substring (0, change.Offset) + change.InsertedText + text.Substring (change.Offset + change.RemovedChars);
}
}
#region ITextEditorAdapter implementation
public int LocationToOffset (int line, int col)
{
Segment seg = Get (line);
if (seg == null)
return 0;
return seg.Offset + col - 1;
}
public char GetCharAt (int offset)
{
if (offset < 0 || offset >= text.Length)
return '\0';
return text [offset];
}
public string GetTextAt (int offset, int length)
{
if (offset < 0 || offset + length >= text.Length)
return "";
if (length <= 0)
return "";
return text.Substring (offset, length);
}
public int GetEditableLength (int lineNumber)
{
var seg = Get (lineNumber);
if (seg == null)
return 0;
return seg.Length - seg.DelimiterLength;
}
public string GetIndentation (int lineNumber)
{
var seg = Get (lineNumber);
if (seg == null)
return "";
int start = seg.Offset;
int end = seg.Offset;
int endOffset = seg.Offset + seg.Length - seg.DelimiterLength;
while (end < endOffset && (text[end] == ' ' || text[end] == '\t'))
end++;
return start < end ? text.Substring (start, end - start) : "";
}
public int GetLineOffset (int lineNumber)
{
var seg = Get (lineNumber);
if (seg == null)
return 0;
return seg.Offset;
}
public int GetLineLength (int lineNumber)
{
var seg = Get (lineNumber);
if (seg == null)
return 0;
return seg.Length;
}
public int GetLineEndOffset (int lineNumber)
{
var seg = Get (lineNumber);
if (seg == null)
return 0;
return seg.Offset + seg.Length;
}
public bool TabsToSpaces {
get {
return false;
}
}
public int TabSize {
get {
return 4;
}
}
public string EolMarker {
get {
return Environment.NewLine;
}
}
public int Length {
get {
return text.Length;
}
}
public int LineCount {
get {
return delimiters.Count + 1;
}
}
#endregion
}
public abstract class TestBase
{
protected static ITextEditorAdapter GetResult (CSharpFormattingPolicy policy, string input)
{
var adapter = new TextEditorTestAdapter (input);
var visitior = new AstFormattingVisitor (policy, adapter);
var compilationUnit = new CSharpParser ().Parse (new StringReader (adapter.Text));
compilationUnit.AcceptVisitor (visitior, null);
adapter.ApplyChanges (visitior.Changes);
return adapter;
}
protected static ITextEditorAdapter Test (CSharpFormattingPolicy policy, string input, string expectedOutput)
{
var adapter = new TextEditorTestAdapter (input);
var visitior = new AstFormattingVisitor (policy, adapter);
var compilationUnit = new CSharpParser ().Parse (new StringReader (adapter.Text));
compilationUnit.AcceptVisitor (visitior, null);
adapter.ApplyChanges (visitior.Changes);
Assert.AreEqual (expectedOutput, adapter.Text);
return adapter;
}
protected static void Continue (CSharpFormattingPolicy policy, ITextEditorAdapter adapter, string expectedOutput)
{
var visitior = new AstFormattingVisitor (policy, adapter);
var compilationUnit = new CSharpParser ().Parse (new StringReader (adapter.Text));
compilationUnit.AcceptVisitor (visitior, null);
((TextEditorTestAdapter)adapter).ApplyChanges (visitior.Changes);
Assert.AreEqual (expectedOutput, adapter.Text);
}
}
}

21
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{63D3B27A-D966-4902-90B3-30290E1692F1}</ProjectGuid>
@ -11,7 +11,6 @@ @@ -11,7 +11,6 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
@ -33,16 +32,10 @@ @@ -33,16 +32,10 @@
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<WarningLevel>4</WarningLevel>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
@ -155,6 +148,8 @@ @@ -155,6 +148,8 @@
<Compile Include="Untested.cs" />
<Compile Include="Utils\CSharpPrimitiveCastTests.cs" />
<Compile Include="Utils\TreeTraversalTests.cs" />
<Compile Include="FormattingTests\TextEditorTestAdapter.cs" />
<Compile Include="FormattingTests\TestBlankLineFormatting.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">
@ -163,11 +158,11 @@ @@ -163,11 +158,11 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="CSharp\Analysis" />
<Folder Include="CSharp\Parser\Expression" />
<Folder Include="CSharp\Parser\GeneralScope" />
<Folder Include="CSharp\Parser\TypeMembers" />
<Folder Include="CSharp\Parser\Statements" />
<Folder Include="CSharp\" />
<Folder Include="CSharp\Parser\" />
<Folder Include="CSharp\Parser\" />
<Folder Include="CSharp\Parser\" />
<Folder Include="CSharp\Parser\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

14
ICSharpCode.NRefactory/CSharp/Formatter/AstFormattingVisitor.cs

@ -87,13 +87,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -87,13 +87,14 @@ namespace ICSharpCode.NRefactory.CSharp
return;
var loc = node.EndLocation;
int line = loc.Line;
while (line < data.LineCount && data.GetEditableLength (line) == data.GetIndentation (line).Length) {
do {
line++;
}
} while (line < data.LineCount && data.GetEditableLength (line) == data.GetIndentation (line).Length);
var start = data.GetLineEndOffset (loc.Line);
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < blankLines; i++)
sb.Append (data.EolMarker);
Console.WriteLine (loc.Line + "--- " + line);
int removedChars = line < data.LineCount ? data.GetLineOffset (line) - start : 0;
AddChange (start, removedChars, sb.ToString ());
}
@ -104,11 +105,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -104,11 +105,12 @@ namespace ICSharpCode.NRefactory.CSharp
return;
var loc = node.StartLocation;
int line = loc.Line;
while (line >= 0 && data.GetEditableLength (line) == data.GetIndentation (line).Length)
do {
line--;
} while (line > 0 && data.GetEditableLength (line) == data.GetIndentation (line).Length);
int end = data.GetLineOffset (loc.Line);
int start = line >= 0 ? data.GetLineEndOffset (line) : 0;
int start = line >= 1 ? data.GetLineEndOffset (line) : 0;
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < blankLines; i++)
sb.Append (data.EolMarker);
@ -939,8 +941,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -939,8 +941,8 @@ namespace ICSharpCode.NRefactory.CSharp
return;
}
}
//Console.WriteLine ("offset={0}, removedChars={1}, insertedText={2}", offset, removedChars , insertedText == null ? "<null>" : insertedText.Replace("\n", "\\n").Replace("\t", "\\t").Replace(" ", "."));
//Console.WriteLine (Environment.StackTrace);
// Console.WriteLine ("offset={0}, removedChars={1}, insertedText={2}", offset, removedChars , insertedText == null ? "<null>" : insertedText.Replace("\n", "\\n").Replace("\t", "\\t").Replace(" ", "."));
// Console.WriteLine (Environment.StackTrace);
changes.Add (new Change (offset, removedChars, insertedText));
}

10
ICSharpCode.NRefactory/CSharp/Formatter/Change.cs

@ -53,11 +53,19 @@ namespace ICSharpCode.NRefactory @@ -53,11 +53,19 @@ namespace ICSharpCode.NRefactory
public Change (int offset, int removedChars, string insertedText)
{
if (removedChars < 0)
throw new ArgumentOutOfRangeException ("removedChars", "removedChars needs to be >= 0");
if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "offset needs to be >= 0");
this.removedChars = removedChars;
this.Offset = offset;
this.InsertedText = insertedText;
}
public override string ToString ()
{
return string.Format ("[Change: Offset={0}, RemovedChars={1}, InsertedText={2}]", Offset, RemovedChars, InsertedText);
}
}
}

1
ICSharpCode.NRefactory/CSharp/Formatter/ITextEditorAdapter.cs

@ -34,6 +34,7 @@ namespace ICSharpCode.NRefactory @@ -34,6 +34,7 @@ namespace ICSharpCode.NRefactory
int TabSize { get; }
string EolMarker { get; }
string Text { get; }
int Length { get; }
int LocationToOffset (int line, int col);
char GetCharAt (int offset);

Loading…
Cancel
Save