Skip to content

Commit dd25faa

Browse files
committed
more unit tests
1 parent 55d1045 commit dd25faa

File tree

6 files changed

+122
-46
lines changed

6 files changed

+122
-46
lines changed

checkstyle/checks/BlockFormatCheck.hx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ class BlockFormatCheck extends Check {
1212
var lastLineRE = ~/^\s*\}[,;\/*]?/;
1313

1414
override function actualRun() {
15-
ExprUtils.walkFile(_checker.ast, function(e){
15+
ExprUtils.walkFile(_checker.ast, function(e) {
1616
switch(e.expr){
17-
case EBlock([]) | EObjectDecl([]):
18-
if (e.pos.max - e.pos.min > "{}".length)
19-
logPos("Empty block should be written as {}", e.pos, Reflect.field(SeverityLevel, severity));
20-
case EBlock(_) | EObjectDecl(_):
21-
var lmin =_checker.getLinePos(e.pos.min).line;
22-
var lmax =_checker.getLinePos(e.pos.max).line;
17+
case EBlock([]) | EObjectDecl([]):
18+
if (e.pos.max - e.pos.min > "{}".length)
19+
logPos("Empty block should be written as {}", e.pos, Reflect.field(SeverityLevel, severity));
20+
case EBlock(_) | EObjectDecl(_):
21+
var lmin = _checker.getLinePos(e.pos.min).line;
22+
var lmax = _checker.getLinePos(e.pos.max).line;
2323

24-
if (lmin != lmax) {
25-
if (!firstLineRE.match(_checker.lines[lmin])) {
26-
logPos("First line of multiline block should contain only '{'", e.pos, Reflect.field(SeverityLevel, severity));
24+
if (lmin != lmax) {
25+
if (!firstLineRE.match(_checker.lines[lmin])) {
26+
logPos("First line of multiline block should contain only {", e.pos, Reflect.field(SeverityLevel, severity));
27+
}
28+
if (!lastLineRE.match(_checker.lines[lmax])) {
29+
logPos("Last line of multiline block should contain only } and maybe , or ;", e.pos, Reflect.field(SeverityLevel, severity));
30+
}
2731
}
28-
if (!lastLineRE.match(_checker.lines[lmax])) {
29-
logPos("Last line of multiline block should contain only '}' and maybe ',' or ';'", e.pos, Reflect.field(SeverityLevel, severity));
30-
}
31-
}
32-
default:
32+
default:
3333
}
3434
});
3535
}

run.n

-131 Bytes
Binary file not shown.

test/BlockFormatCheckTest.hx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ;
2+
3+
import checkstyle.checks.BlockFormatCheck;
4+
5+
class BlockFormatCheckTest extends CheckTestCase {
6+
7+
public function testCorrectBlockFormat(){
8+
var msg = checkMessage(BlockFormatTests.TEST, new BlockFormatCheck());
9+
assertEquals(msg, '');
10+
}
11+
12+
public function testWrongBlockFormat(){
13+
var msg = checkMessage(BlockFormatTests.TEST1, new BlockFormatCheck());
14+
assertEquals(msg, 'Empty block should be written as {}');
15+
}
16+
17+
public function testBlockFormatFirstLine(){
18+
var msg = checkMessage(BlockFormatTests.TEST2, new BlockFormatCheck());
19+
assertEquals(msg, 'First line of multiline block should contain only {');
20+
}
21+
22+
public function testBlockFormatLastLine(){
23+
var msg = checkMessage(BlockFormatTests.TEST3, new BlockFormatCheck());
24+
assertEquals(msg, 'Last line of multiline block should contain only } and maybe , or ;');
25+
}
26+
}
27+
28+
class BlockFormatTests {
29+
public static inline var TEST:String = "
30+
class Test {
31+
public function new() {}
32+
}";
33+
34+
public static inline var TEST1:String = "
35+
class Test {
36+
public function new(){
37+
38+
}
39+
}";
40+
41+
public static inline var TEST2:String =
42+
"class Test {
43+
public function new() { var a:Int;
44+
45+
}
46+
}";
47+
48+
public static inline var TEST3:String =
49+
"class Test {
50+
public function new() {
51+
var a:Int; }
52+
}";
53+
}

test/EmptyLinesCheckTest.hx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ;
2+
3+
import checkstyle.checks.EmptyLinesCheck;
4+
5+
class EmptyLinesCheckTest extends CheckTestCase {
6+
7+
public function testDefaultEmptyLines(){
8+
var msg = checkMessage(EmptyLinesTests.TEST1, new EmptyLinesCheck());
9+
assertEquals(msg, 'Too many consecutive empty lines (> 1)');
10+
}
11+
12+
public function testCorrectEmptyLines(){
13+
var msg = checkMessage(EmptyLinesTests.TEST2, new EmptyLinesCheck());
14+
assertEquals(msg, '');
15+
}
16+
17+
public function testConfigurableEmptyLines(){
18+
var check = new EmptyLinesCheck();
19+
check.maxConsecutiveEmptyLines = 2;
20+
21+
var msg = checkMessage(EmptyLinesTests.TEST3, check);
22+
assertEquals(msg, '');
23+
}
24+
}
25+
26+
class EmptyLinesTests {
27+
public static inline var TEST1:String = "
28+
class Test {
29+
var _a:Int;
30+
31+
32+
}";
33+
34+
public static inline var TEST2:String =
35+
"class Test {
36+
public function new() {
37+
var b:Int;
38+
39+
}
40+
}";
41+
42+
public static inline var TEST3:String =
43+
"class Test {
44+
public function new() {
45+
var b:Int;
46+
47+
48+
}
49+
}";
50+
}

test/ListenerNameCheckTest.hx

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ class ListenerNameCheckTest extends CheckTestCase {
2323
var msg = checkMessage(ListernerTests.TEST3, new ListenerNameCheck());
2424
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
2525
}
26-
27-
public function testListenerName4(){
28-
var msg = checkMessage(ListernerTests.TEST4, new ListenerNameCheck());
29-
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
30-
}
31-
32-
public function testListenerName5(){
33-
var msg = checkMessage(ListernerTests.TEST5, new ListenerNameCheck());
34-
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
35-
}
3626
}
3727

3828
class ListernerTests {
@@ -75,24 +65,4 @@ class ListernerTests {
7565
7666
function _testUpdate() {}
7767
}";
78-
79-
public static inline var TEST4:String = "
80-
class Test {
81-
var a:Stage;
82-
public function new() {
83-
a.add('update', _testUpdate);
84-
}
85-
86-
function _testUpdate() {}
87-
}";
88-
89-
public static inline var TEST5:String = "
90-
class Test {
91-
var a:Stage;
92-
public function new() {
93-
a.addOnce('update', _testUpdate);
94-
}
95-
96-
function _testUpdate() {}
97-
}";
9868
}

test/TestMain.hx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ class TestMain {
55
public function new() {
66
var runner = new haxe.unit.TestRunner();
77
runner.add(new AnonymousCheckTest());
8-
runner.add(new ListenerNameCheckTest());
98
runner.add(new ArrayInstantiationCheckTest());
9+
runner.add(new BlockFormatCheckTest());
10+
runner.add(new EmptyLinesCheckTest());
11+
12+
runner.add(new ListenerNameCheckTest());
1013

1114
var success = runner.run();
1215
Sys.exit(success ? 0 : 1);

0 commit comments

Comments
 (0)