11 changed files with 2904 additions and 2037 deletions
@ -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 () |
||||
{ |
||||
} |
||||
}");
|
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
} |
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -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); |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue