Skip to content

Commit 8d7f888

Browse files
committed
added unittests for EmptyBlockCheck
removed tokens from EmptyBlockCheck because object decls get detected as empty blocks
1 parent bd93768 commit 8d7f888

File tree

5 files changed

+89
-20
lines changed

5 files changed

+89
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
8383
"type": "EmptyBlock",
8484
"props": {
8585
"severity": "ERROR",
86-
"option": "empty",
87-
"tokens": []
86+
"option": "empty"
8887
}
88+
},
8989
{
9090
"type": "EmptyLines",
9191
"props": {

checkstyle/checks/EmptyBlockCheck.hx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,24 @@ import haxe.macro.Expr;
77
@desc("Checks empty blocks / object declarations")
88
class EmptyBlockCheck extends Check {
99

10-
public static inline var BLOCK:String = "BLOCK";
11-
public static inline var OBJECT_DECL:String = "OBJECT_DECL";
12-
1310
// require comment in empty block / object decl
1411
public static inline var TEXT:String = "text";
1512
// empty block / object decl can be empty or have comments
1613
// if block has no comments, enforces {} notation
1714
public static inline var EMPTY:String = "empty";
1815

19-
public var tokens:Array<String>;
2016
public var option:String;
2117

2218
public function new() {
2319
super();
24-
tokens = [];
2520
option = EMPTY;
2621
}
2722

28-
function hasToken(token:String):Bool {
29-
if (tokens.length == 0) return true;
30-
if (tokens.indexOf(token) > -1) return true;
31-
return false;
32-
}
33-
3423
override function actualRun() {
3524
ExprUtils.walkFile(checker.ast, function(e) {
3625
if (isPosSuppressed(e.pos)) return;
3726
switch(e.expr){
38-
case EBlock([]):
39-
if (!hasToken(BLOCK)) return;
40-
checkEmptyBlock(e);
41-
case EObjectDecl([]):
42-
if (!hasToken(OBJECT_DECL)) return;
27+
case EBlock([]) | EObjectDecl([]):
4328
checkEmptyBlock(e);
4429
default:
4530
}

resources/config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@
6060
"type": "EmptyBlock",
6161
"props": {
6262
"severity": "ERROR",
63-
"option": "empty",
64-
"tokens": []
63+
"option": "empty"
6564
}
6665
},
6766
{

test/EmptyBlockCheckTest.hx

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package ;
2+
3+
import checkstyle.checks.EmptyBlockCheck;
4+
5+
class EmptyBlockCheckTest extends CheckTestCase {
6+
7+
public function testCorrectEmptyBlock() {
8+
var check = new EmptyBlockCheck ();
9+
assertMsg(check, EmptyBlockTests.TEST, '');
10+
assertMsg(check, EmptyBlockTests.TEST2, '');
11+
assertMsg(check, EmptyBlockTests.TEST3, '');
12+
assertMsg(check, EmptyBlockTests.TEST5, '');
13+
}
14+
15+
public function testWrongEmptyBlock() {
16+
var check = new EmptyBlockCheck ();
17+
assertMsg(check, EmptyBlockTests.TEST1, 'Empty block should be written as {}');
18+
assertMsg(check, EmptyBlockTests.TEST4, 'Empty block should be written as {}');
19+
assertMsg(check, EmptyBlockTests.TEST6, 'Empty block should be written as {}');
20+
}
21+
22+
public function testOptionText () {
23+
var check = new EmptyBlockCheck ();
24+
check.option = EmptyBlockCheck.TEXT;
25+
26+
assertMsg(check, EmptyBlockTests.TEST, 'Empty block should contain a comment');
27+
assertMsg(check, EmptyBlockTests.TEST1, 'Empty block should contain a comment');
28+
assertMsg(check, EmptyBlockTests.TEST2, '');
29+
assertMsg(check, EmptyBlockTests.TEST3, 'Empty block should contain a comment');
30+
assertMsg(check, EmptyBlockTests.TEST4, 'Empty block should contain a comment');
31+
assertMsg(check, EmptyBlockTests.TEST5, '');
32+
}
33+
}
34+
35+
class EmptyBlockTests {
36+
public static inline var TEST:String = "
37+
class Test {
38+
public function new() {}
39+
}";
40+
41+
public static inline var TEST1:String = "
42+
class Test {
43+
public function new(){
44+
45+
}
46+
}";
47+
48+
public static inline var TEST2:String =
49+
"class Test {
50+
public function new() {
51+
// comment
52+
}
53+
}";
54+
55+
public static inline var TEST3:String =
56+
"class Test {
57+
public function new() {
58+
var a = {};
59+
}
60+
}";
61+
62+
public static inline var TEST4:String = "
63+
class Test {
64+
public function new() {
65+
var a = {
66+
};
67+
}
68+
}";
69+
70+
public static inline var TEST5:String = "
71+
class Test {
72+
public function new() {
73+
var a = {
74+
// comment
75+
};
76+
}
77+
}";
78+
79+
public static inline var TEST6:String = "
80+
class Test {
81+
public function new() {
82+
}
83+
}";
84+
}

test/TestMain.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TestMain {
99
runner.add(new BlockFormatCheckTest());
1010
runner.add(new ConstantNameCheckTest());
1111
runner.add(new DynamicCheckTest());
12+
runner.add(new EmptyBlockCheckTest());
1213
runner.add(new EmptyLinesCheckTest());
1314
runner.add(new ERegInstantiationCheckTest());
1415
runner.add(new FileLengthCheckTest());

0 commit comments

Comments
 (0)