3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'package:dart_style/dart_style.dart' ;
6
+ import 'package:dart_style/src/constants.dart' ;
6
7
import 'package:pub_semver/pub_semver.dart' ;
7
8
import 'package:test/test.dart' ;
8
9
9
10
void main () async {
11
+ group ('short style' , () {
12
+ _runTests (isTall: false );
13
+ });
14
+
15
+ group ('tall style' , () {
16
+ _runTests (isTall: true );
17
+ });
18
+ }
19
+
20
+ /// Run all of the DartFormatter tests either using short or tall style.
21
+ void _runTests ({required bool isTall}) {
22
+ DartFormatter makeFormatter (
23
+ {Version ? languageVersion, int ? indent, String ? lineEnding}) {
24
+ return DartFormatter (
25
+ languageVersion: languageVersion,
26
+ indent: indent,
27
+ lineEnding: lineEnding,
28
+ experimentFlags: [if (isTall) tallStyleExperimentFlag]);
29
+ }
30
+
10
31
group ('language version' , () {
11
32
test ('defaults to latest if omitted' , () {
12
- var formatter = DartFormatter ();
33
+ var formatter = makeFormatter ();
13
34
expect (formatter.languageVersion, DartFormatter .latestLanguageVersion);
14
35
});
15
36
16
37
test ('defaults to latest if null' , () {
17
- var formatter = DartFormatter (languageVersion: null );
38
+ var formatter = makeFormatter (languageVersion: null );
18
39
expect (formatter.languageVersion, DartFormatter .latestLanguageVersion);
19
40
});
20
41
21
42
test ('parses at given older language version' , () {
22
43
// Use a language version before patterns were supported and a pattern
23
44
// is an error.
24
- var formatter = DartFormatter (languageVersion: Version (2 , 19 , 0 ));
45
+ var formatter = makeFormatter (languageVersion: Version (2 , 19 , 0 ));
25
46
expect (() => formatter.format ('main() {switch (o) {case var x: break;}}' ),
26
47
throwsA (isA <FormatterException >()));
27
48
});
28
49
29
50
test ('parses at given newer language version' , () {
30
51
// Use a language version after patterns were supported and `1 + 2` is an
31
52
// error.
32
- var formatter = DartFormatter (languageVersion: Version (3 , 0 , 0 ));
53
+ var formatter = makeFormatter (languageVersion: Version (3 , 0 , 0 ));
33
54
expect (() => formatter.format ('main() {switch (o) {case 1+2: break;}}' ),
34
55
throwsA (isA <FormatterException >()));
35
56
});
36
57
37
58
test ('@dart comment overrides version' , () {
38
59
// Use a language version after patterns were supported and `1 + 2` is an
39
60
// error.
40
- var formatter = DartFormatter (languageVersion: Version (3 , 0 , 0 ));
61
+ var formatter = makeFormatter (languageVersion: Version (3 , 0 , 0 ));
41
62
42
63
// But then have the code opt to the older version.
43
64
const before = '''
@@ -60,22 +81,22 @@ main() {
60
81
});
61
82
62
83
test ('throws a FormatterException on failed parse' , () {
63
- var formatter = DartFormatter ();
84
+ var formatter = makeFormatter ();
64
85
expect (() => formatter.format ('wat?!' ), throwsA (isA <FormatterException >()));
65
86
});
66
87
67
88
test ('FormatterException.message() does not throw' , () {
68
89
// This is a regression test for #358 where an error whose position is
69
90
// past the end of the source caused FormatterException to throw.
70
91
expect (
71
- () => DartFormatter ().format ('library' ),
92
+ () => makeFormatter ().format ('library' ),
72
93
throwsA (isA <FormatterException >().having (
73
94
(e) => e.message (), 'message' , contains ('Could not format' ))));
74
95
});
75
96
76
97
test ('FormatterException describes parse errors' , () {
77
98
expect (() {
78
- DartFormatter ().format ('''
99
+ makeFormatter ().format ('''
79
100
80
101
var a = some error;
81
102
@@ -92,33 +113,33 @@ main() {
92
113
});
93
114
94
115
test ('adds newline to unit' , () {
95
- expect (DartFormatter ().format ('var x = 1;' ), equals ('var x = 1;\n ' ));
116
+ expect (makeFormatter ().format ('var x = 1;' ), equals ('var x = 1;\n ' ));
96
117
});
97
118
98
119
test ('adds newline to unit after trailing comment' , () {
99
- expect (DartFormatter ().format ('library foo; //zamm' ),
120
+ expect (makeFormatter ().format ('library foo; //zamm' ),
100
121
equals ('library foo; //zamm\n ' ));
101
122
});
102
123
103
124
test ('removes extra newlines' , () {
104
- expect (DartFormatter ().format ('var x = 1;\n\n\n ' ), equals ('var x = 1;\n ' ));
125
+ expect (makeFormatter ().format ('var x = 1;\n\n\n ' ), equals ('var x = 1;\n ' ));
105
126
});
106
127
107
128
test ('does not add newline to statement' , () {
108
- expect (DartFormatter ().formatStatement ('var x = 1;' ), equals ('var x = 1;' ));
129
+ expect (makeFormatter ().formatStatement ('var x = 1;' ), equals ('var x = 1;' ));
109
130
});
110
131
111
132
test ('fails if anything is after the statement' , () {
112
133
expect (
113
- () => DartFormatter ().formatStatement ('var x = 1;;' ),
134
+ () => makeFormatter ().formatStatement ('var x = 1;;' ),
114
135
throwsA (isA <FormatterException >()
115
136
.having ((e) => e.errors.length, 'errors.length' , equals (1 ))
116
137
.having ((e) => e.errors.first.offset, 'errors.length.first.offset' ,
117
138
equals (10 ))));
118
139
});
119
140
120
141
test ('preserves initial indent' , () {
121
- var formatter = DartFormatter (indent: 3 );
142
+ var formatter = makeFormatter (indent: 3 );
122
143
expect (
123
144
formatter.formatStatement ('if (foo) {bar;}' ),
124
145
equals (' if (foo) {\n '
@@ -131,29 +152,30 @@ main() {
131
152
// Use zero width no-break space character as the line ending. We have
132
153
// to use a whitespace character for the line ending as the formatter
133
154
// will throw an error if it accidentally makes non-whitespace changes
134
- // as will occur
155
+ // as would occur if we used a non-whitespace character as the line
156
+ // ending.
135
157
var lineEnding = '\t ' ;
136
- expect (DartFormatter (lineEnding: lineEnding).format ('var i = 1;' ),
158
+ expect (makeFormatter (lineEnding: lineEnding).format ('var i = 1;' ),
137
159
equals ('var i = 1;\t ' ));
138
160
});
139
161
140
162
test ('infers \\ r\\ n if the first newline uses that' , () {
141
- expect (DartFormatter ().format ('var\r\n i\n =\n 1;\n ' ),
163
+ expect (makeFormatter ().format ('var\r\n i\n =\n 1;\n ' ),
142
164
equals ('var i = 1;\r\n ' ));
143
165
});
144
166
145
167
test ('infers \\ n if the first newline uses that' , () {
146
- expect (DartFormatter ().format ('var\n i\r\n =\r\n 1;\r\n ' ),
168
+ expect (makeFormatter ().format ('var\n i\r\n =\r\n 1;\r\n ' ),
147
169
equals ('var i = 1;\n ' ));
148
170
});
149
171
150
172
test ('defaults to \\ n if there are no newlines' , () {
151
- expect (DartFormatter ().format ('var i =1;' ), equals ('var i = 1;\n ' ));
173
+ expect (makeFormatter ().format ('var i =1;' ), equals ('var i = 1;\n ' ));
152
174
});
153
175
154
176
test ('handles Windows line endings in multiline strings' , () {
155
177
expect (
156
- DartFormatter (lineEnding: '\r\n ' ).formatStatement (' """first\r\n '
178
+ makeFormatter (lineEnding: '\r\n ' ).formatStatement (' """first\r\n '
157
179
'second\r\n '
158
180
'third""" ;' ),
159
181
equals ('"""first\r\n '
@@ -165,7 +187,7 @@ main() {
165
187
test ('throws an UnexpectedOutputException on non-whitespace changes' , () {
166
188
// Use an invalid line ending character to ensure the formatter will
167
189
// attempt to make non-whitespace changes.
168
- var formatter = DartFormatter (lineEnding: '%' );
190
+ var formatter = makeFormatter (lineEnding: '%' );
169
191
expect (() => formatter.format ('var i = 1;' ),
170
192
throwsA (isA <UnexpectedOutputException >()));
171
193
});
0 commit comments