1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using Atc . AutoFormatter . Formatters ;
4
+ using Atc . Formatter . Tests . TestInfrastructure ;
5
+ using Microsoft . VisualStudio . Text ;
6
+ using Microsoft . VisualStudio . Text . Editor ;
7
+ using NSubstitute ;
8
+ using Xunit ;
9
+
10
+ namespace Atc . Formatter . Tests . Formatters
11
+ {
12
+ public class TrailingLineBreakRemoverTests
13
+ {
14
+ private const string LineBreak = "\r \n " ;
15
+
16
+ public static ITextView CreateTextView ( IEnumerable < string > lines )
17
+ {
18
+ var textLines = lines . ToArray ( ) ;
19
+
20
+ var edit = Substitute . For < ITextEdit > ( ) ;
21
+
22
+ var textBuffer = Substitute . For < ITextBuffer > ( ) ;
23
+ textBuffer . CreateEdit ( ) . Returns ( edit ) ;
24
+
25
+ var snapshot = Substitute . For < ITextSnapshot > ( ) ;
26
+ snapshot . TextBuffer . Returns ( textBuffer ) ;
27
+ snapshot . Length . Returns ( lines . Sum ( l => l . Length + LineBreak . Length ) ) ;
28
+ snapshot . LineCount . Returns ( textLines . Length ) ;
29
+ snapshot
30
+ . GetLineFromLineNumber ( default )
31
+ . ReturnsForAnyArgs ( c =>
32
+ {
33
+ var index = c . Arg < int > ( ) ;
34
+ var line = textLines [ index ] ;
35
+
36
+ var lineEndWithLineBreak = textLines . Where ( ( s , i ) => i <= index ) . Sum ( s => s . Length + LineBreak . Length ) ;
37
+ var lineEnd = lineEndWithLineBreak - LineBreak . Length ;
38
+
39
+ var textSnapshotLine = Substitute . For < ITextSnapshotLine > ( ) ;
40
+ textSnapshotLine . GetText ( ) . Returns ( line ) ;
41
+ textSnapshotLine . End . Returns ( c => new SnapshotPoint ( snapshot , lineEnd ) ) ;
42
+ textSnapshotLine . EndIncludingLineBreak . Returns ( c => new SnapshotPoint ( snapshot , lineEndWithLineBreak ) ) ;
43
+ return textSnapshotLine ;
44
+ } ) ;
45
+
46
+ var textView = Substitute . For < ITextView > ( ) ;
47
+ textView . TextSnapshot . Returns ( snapshot ) ;
48
+
49
+ return textView ;
50
+ }
51
+
52
+ [ Theory , AutoNSubstituteData ]
53
+ public void Execute_Calls_CreateEdit_On_TextBuffer (
54
+ TrailingLineBreakRemover sut ,
55
+ string filePath ,
56
+ string [ ] lines )
57
+ {
58
+ var emptyLines = Enumerable . Repeat ( string . Empty , 2 ) ;
59
+ var textView = CreateTextView ( lines . Union ( emptyLines ) ) ;
60
+
61
+ sut . Execute ( filePath , textView ) ;
62
+
63
+ textView . TextSnapshot . TextBuffer
64
+ . Received ( 1 )
65
+ . CreateEdit ( ) ;
66
+ }
67
+
68
+ [ Theory , AutoNSubstituteData ]
69
+ public void Execute_Calls_Delete_On_TextEdit (
70
+ TrailingLineBreakRemover sut ,
71
+ string filePath ,
72
+ string [ ] lines )
73
+ {
74
+ var emptyLines = Enumerable . Repeat ( string . Empty , 2 ) ;
75
+ var allLines = lines . Union ( emptyLines ) ;
76
+ var allText = string . Join ( LineBreak , allLines ) ;
77
+ var textView = CreateTextView ( allLines ) ;
78
+
79
+ sut . Execute ( filePath , textView ) ;
80
+
81
+ var edit = textView . TextSnapshot . TextBuffer . CreateEdit ( ) ;
82
+ edit
83
+ . Received ( 1 )
84
+ . Delete (
85
+ allText . TrimEnd ( ) . Length ,
86
+ emptyLines . Count ( ) * LineBreak . Length ) ;
87
+ }
88
+
89
+ [ Theory , AutoNSubstituteData ]
90
+ public void Execute_Calls_Apply_On_TextEdit (
91
+ TrailingLineBreakRemover sut ,
92
+ string filePath ,
93
+ string [ ] lines )
94
+ {
95
+ var emptyLines = Enumerable . Repeat ( string . Empty , 2 ) ;
96
+ var textView = CreateTextView ( lines . Union ( emptyLines ) ) ;
97
+
98
+ sut . Execute ( filePath , textView ) ;
99
+
100
+ var edit = textView . TextSnapshot . TextBuffer . CreateEdit ( ) ;
101
+ edit
102
+ . Received ( 1 )
103
+ . Apply ( ) ;
104
+ }
105
+
106
+ [ Theory , AutoNSubstituteData ]
107
+ public void Execute_Does_Not_Call_CreateEdit_If_No_Trailing_LineBreaks (
108
+ TrailingLineBreakRemover sut ,
109
+ string filePath ,
110
+ string [ ] lines )
111
+ {
112
+ var textView = CreateTextView ( lines ) ;
113
+
114
+ sut . Execute ( filePath , textView ) ;
115
+
116
+ textView . TextSnapshot . TextBuffer
117
+ . DidNotReceive ( )
118
+ . CreateEdit ( ) ;
119
+ }
120
+ }
121
+ }
0 commit comments