@@ -19,6 +19,11 @@ describe('rules/require-newline-before-block-statements', function() {
1919 . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
2020 } ) ;
2121
22+ it ( 'should report missing newline before opening brace for "switch"' , function ( ) {
23+ expect ( checker . checkString ( 'switch (a) {\n\tcase 1: break;\n}' ) )
24+ . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
25+ } ) ;
26+
2227 it ( 'should report missing newline before opening brace when there are white-spaces between' , function ( ) {
2328 expect ( checker . checkString ( 'function test() /* COOOMMMENTTT*/ {abc();}' ) )
2429 . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
@@ -42,14 +47,38 @@ describe('rules/require-newline-before-block-statements', function() {
4247 . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
4348 } ) ;
4449
45- it ( 'should report missing newline for all 3 statements' , function ( ) {
46- expect ( checker . checkString ( 'function test(){\nif(true){\nreturn 1;\n}\nfor(var i in [1,2,3]){\n}\n}' ) )
47- . to . have . error . count . equal ( 3 ) ;
50+ it ( 'should report missing newline for all 4 statements' , function ( ) {
51+ expect ( checker . checkString ( [
52+ 'function test(){' ,
53+ 'if(true){' ,
54+ 'switch (a){' ,
55+ 'case 1: break;' ,
56+ '}' ,
57+ 'return 1;' ,
58+ '}' ,
59+ 'for(var i in [1,2,3]){' ,
60+ '}' ,
61+ '}'
62+ ] . join ( '\n' ) ) ) . to . have . error . count . equal ( 4 ) ;
4863 } ) ;
4964
5065 it ( 'should not report missing newline' , function ( ) {
51- expect ( checker . checkString ( 'function test()\n{\nif(true)\n{\nreturn 1;\n}\nfor(var i in [1,2,3])\n{\n}\n}' ) )
52- . to . have . no . errors ( ) ;
66+ expect ( checker . checkString ( [
67+ 'function test()' ,
68+ '{' ,
69+ 'if(true)' ,
70+ '{' ,
71+ 'switch (a)' ,
72+ '{' ,
73+ 'case 1: break;' ,
74+ '}' ,
75+ 'return 1;' ,
76+ '}' ,
77+ 'for(var i in [1,2,3])' ,
78+ '{' ,
79+ '}' ,
80+ '}'
81+ ] . join ( '\n' ) ) ) . to . have . no . errors ( ) ;
5382 } ) ;
5483
5584 it ( 'should not throw error if opening parentheses is first symbol in the file' , function ( ) {
@@ -132,12 +161,56 @@ describe('rules/require-newline-before-block-statements', function() {
132161 expect ( checker . checkString ( 'for (var i = 0, len = 10; i < 10; ++i)\n{\n\tx++;\n}' ) ) . to . have . no . errors ( ) ;
133162 } ) ;
134163
135- it ( 'should not complain when note configured with "for"' , function ( ) {
164+ it ( 'should not complain when not configured with "for"' , function ( ) {
136165 checker . configure ( { requireNewlineBeforeBlockStatements : [ 'if' ] } ) ;
137166 expect ( checker . checkString ( 'for (var i = 0, len = 10; i < 10; ++i) {\n\tx++;\n}' ) ) . to . have . no . errors ( ) ;
138167 } ) ;
139168 } ) ;
140169
170+ describe ( '"switch" statements' , function ( ) {
171+ beforeEach ( function ( ) {
172+ checker . configure ( { requireNewlineBeforeBlockStatements : [ 'switch' ] } ) ;
173+ } ) ;
174+
175+ it ( 'should report missing newlines when configured with "switch"' , function ( ) {
176+ expect ( checker . checkString ( 'switch (a) {\n\tcase 1: break;\n}' ) )
177+ . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
178+ } ) ;
179+
180+ it ( 'should report missing newline before opening brace when there are white-spaces between' , function ( ) {
181+ expect ( checker . checkString ( 'switch (a) /* COOOMMMENTTT*/ {\n\tcase 1: break;\n}' ) )
182+ . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
183+ } ) ;
184+
185+ it ( 'should not report missing newline if there are more of them combined with white-spaces' , function ( ) {
186+ expect ( checker . checkString ( 'switch (a) \n \n/*BLOCK*/ {\n\tcase 1: break;\n}' ) )
187+ . to . have . no . errors ( ) ;
188+ } ) ;
189+
190+ it ( 'should complain when configured with "switch" and no cases' , function ( ) {
191+ expect ( checker . checkString ( 'switch (a) {\n}' ) )
192+ . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
193+ } ) ;
194+
195+ it ( 'should complain when configured with "switch" and parenthesized discriminant' , function ( ) {
196+ expect ( checker . checkString ( 'switch ((function(){}())) {\n\tcase 1: break;\n}' ) )
197+ . to . have . one . validation . error . from ( 'requireNewlineBeforeBlockStatements' ) ;
198+ } ) ;
199+
200+ it ( 'should not complain when configured with "switch" and newline exists' , function ( ) {
201+ expect ( checker . checkString ( 'switch (a)\n{\n\tcase 1: break;\n}' ) ) . to . have . no . errors ( ) ;
202+ } ) ;
203+
204+ it ( 'should not complain when configured with "switch" and case on brace line' , function ( ) {
205+ expect ( checker . checkString ( 'switch (a)\n{default: 1;\n}' ) ) . to . have . no . errors ( ) ;
206+ } ) ;
207+
208+ it ( 'should not complain when not configured with "switch"' , function ( ) {
209+ checker . configure ( { requireNewlineBeforeBlockStatements : [ 'if' ] } ) ;
210+ expect ( checker . checkString ( 'switch (a) {\n\tdefault: 1;\n}' ) ) . to . have . no . errors ( ) ;
211+ } ) ;
212+ } ) ;
213+
141214 describe ( '"for...in" loops' , function ( ) {
142215 beforeEach ( function ( ) {
143216 checker . configure ( { requireNewlineBeforeBlockStatements : [ 'for' ] } ) ;
0 commit comments