Skip to content

Commit d5d5fc4

Browse files
authored
added token tree verification tests (#400)
* added token tree verification tests * removed `.` from default for SeparatorWrapCheck
1 parent 501873e commit d5d5fc4

File tree

11 files changed

+357
-8
lines changed

11 files changed

+357
-8
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- Fixed bad offset crash with C++ build on7 Windows 10 [#398](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/398)
99
- Fixed object declaration handling [#399](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/399)
1010
- Added unittests for ParserQueue and CheckerPool [#393](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/393)
11+
- Added unittests for TokenTree structure verification [#400](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/400)
12+
- Removed `.` from default settings in SeparatorWrapCheck [#400](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/400)
1113
- Improved wrapped code detection [#392](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/392)
1214

1315
## version 2.2.1

checkstyle.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@
379379
{
380380
"props": {
381381
"tokens": [
382-
",",
383-
"."
382+
","
384383
],
385384
"option": "eol"
386385
},

resources/default-config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@
447447
{
448448
"props": {
449449
"tokens": [
450-
",",
451-
"."
450+
","
452451
],
453452
"option": "eol"
454453
},

src/checkstyle/checks/whitespace/SeparatorWrapCheck.hx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class SeparatorWrapCheck extends WrapCheckBase {
88
super();
99
tokens = [
1010
",",
11-
"."
1211
];
1312
}
1413

test/TestSuite.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import checks.CheckTestCase;
22
import misc.ThreadTest;
33
import token.TokenTreeBuilderTest;
44
import token.TokenTreeBuilderParsingTest;
5+
import token.verify.VerifyTokenTreeTest;
56

67
class TestSuite extends massive.munit.TestSuite {
78
public function new() {
@@ -12,6 +13,7 @@ class TestSuite extends massive.munit.TestSuite {
1213

1314
add(TokenTreeBuilderTest);
1415
add(TokenTreeBuilderParsingTest);
16+
add(VerifyTokenTreeTest);
1517
add(ThreadTest);
1618

1719
var tests = CompileTime.getAllClasses(CheckTestCase);

test/checks/whitespace/IndentationCheckTest.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ long comment
149149
a: 1,
150150
b: 2
151151
});
152+
checkInfos[names[i]] = {
153+
name: names[i],
154+
clazz: cl,
155+
isAlias: i > 0,
156+
description: (i == 0) ? desc : desc + ' [DEPRECATED, use ' + names[0] + ' instead]'
157+
};
152158
builder()
153159
.create
154160
.something()

test/checks/whitespace/SeparatorWrapCheckTest.hx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import checkstyle.checks.whitespace.SeparatorWrapCheck;
55
class SeparatorWrapCheckTest extends CheckTestCase<SeparatorWrapCheckTests> {
66

77
static inline var MSG_COMMA_EOL:String = 'Token "," must be at the end of the line';
8-
static inline var MSG_DOT_EOL:String = 'Token "." must be at the end of the line';
98

109
static inline var MSG_COMMA_NL:String = 'Token "," must be on a new line';
1110
static inline var MSG_DOT_NL:String = 'Token "." must be on a new line';
@@ -28,8 +27,6 @@ class SeparatorWrapCheckTest extends CheckTestCase<SeparatorWrapCheckTests> {
2827
assertMsg(check, NL_WRAP_FUNC, MSG_COMMA_EOL);
2928
assertMsg(check, NL_WRAP_OBJECT_DECL, MSG_COMMA_EOL);
3029
assertMsg(check, NL_WRAP_ARRAY, MSG_COMMA_EOL);
31-
assertMsg(check, NL_WRAP_CALL, MSG_DOT_EOL);
32-
assertMsg(check, NL_WRAP_IMPORT, MSG_DOT_EOL);
3330
}
3431

3532
@Test
@@ -46,7 +43,9 @@ class SeparatorWrapCheckTest extends CheckTestCase<SeparatorWrapCheckTests> {
4643
assertNoMsg(check, NOWRAP_ARRAY);
4744
assertNoMsg(check, NOWRAP_CALL);
4845
assertNoMsg(check, NOWRAP_IMPORT);
46+
assertNoMsg(check, EOL_WRAP_IMPORT);
4947

48+
check.tokens = [",", "."];
5049
assertMsg(check, CORRECT_WRAP, MSG_COMMA_NL);
5150
assertMsg(check, EOL_WRAP_ARRAY, MSG_COMMA_NL);
5251
assertMsg(check, EOL_WRAP_IMPORT, MSG_DOT_NL);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package token.verify;
2+
3+
import haxe.PosInfos;
4+
5+
import haxeparser.Data;
6+
7+
interface IVerifyTokenTree {
8+
9+
function filter(tok:TokenDef, ?pos:PosInfos):IVerifyTokenTree;
10+
function childs(?pos:PosInfos):IVerifyTokenTree;
11+
function first(?pos:PosInfos):IVerifyTokenTree;
12+
function last(?pos:PosInfos):IVerifyTokenTree;
13+
function at(index:Int, ?pos:PosInfos):IVerifyTokenTree;
14+
function count(count:Int, ?pos:PosInfos):IVerifyTokenTree;
15+
function noChilds(?pos:PosInfos):IVerifyTokenTree;
16+
function oneChild(?pos:PosInfos):IVerifyTokenTree;
17+
function childFirst(?pos:PosInfos):IVerifyTokenTree;
18+
function childLast(?pos:PosInfos):IVerifyTokenTree;
19+
function childAt(index:Int, ?pos:PosInfos):IVerifyTokenTree;
20+
function childCount(num:Int, ?pos:PosInfos):IVerifyTokenTree;
21+
function childCountAtLeast(count:Int, ?pos:PosInfos):IVerifyTokenTree;
22+
function is(tok:TokenDef, ?pos:PosInfos):IVerifyTokenTree;
23+
function isComment(?pos:PosInfos):IVerifyTokenTree;
24+
function isEmpty(?pos:PosInfos):Bool;
25+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package token.verify;
2+
3+
import haxe.PosInfos;
4+
5+
import massive.munit.Assert;
6+
7+
import haxeparser.Data;
8+
9+
import checkstyle.token.TokenTree;
10+
11+
class VerifyTokenTree implements IVerifyTokenTree {
12+
13+
var token:TokenTree;
14+
15+
public function new(token:TokenTree) {
16+
Assert.isNotNull(token);
17+
this.token = token;
18+
}
19+
20+
public function filter(tok:TokenDef, ?pos:PosInfos):IVerifyTokenTree {
21+
var list:Array<IVerifyTokenTree> = [];
22+
for (child in token.children) {
23+
if (child.is(tok)) list.push(new VerifyTokenTree(child));
24+
}
25+
return new VerifyTokenTreeList(list);
26+
}
27+
28+
public function childs(?pos:PosInfos):IVerifyTokenTree {
29+
var list:Array<IVerifyTokenTree> = [];
30+
for (child in token.children) list.push(new VerifyTokenTree(child));
31+
return new VerifyTokenTreeList(list);
32+
}
33+
34+
public function first(?pos:PosInfos):IVerifyTokenTree {
35+
return childFirst(pos);
36+
}
37+
38+
public function last(?pos:PosInfos):IVerifyTokenTree {
39+
return childLast(pos);
40+
}
41+
42+
public function at(index:Int, ?pos:PosInfos):IVerifyTokenTree {
43+
return childAt(index, pos);
44+
}
45+
46+
public function count(num:Int, ?pos:PosInfos):IVerifyTokenTree {
47+
Assert.areEqual(num, 1, pos);
48+
return this;
49+
}
50+
51+
public function noChilds(?pos:PosInfos):IVerifyTokenTree {
52+
if (token.children == null) return this;
53+
Assert.areEqual(0, token.children.length, pos);
54+
return this;
55+
}
56+
57+
public function oneChild(?pos:PosInfos):IVerifyTokenTree {
58+
Assert.areEqual(1, token.children.length, pos);
59+
return this;
60+
}
61+
62+
public function childFirst(?pos:PosInfos):IVerifyTokenTree {
63+
childCountAtLeast(1, pos);
64+
return new VerifyTokenTree(token.getFirstChild());
65+
}
66+
67+
public function childLast(?pos:PosInfos):IVerifyTokenTree {
68+
childCountAtLeast(1, pos);
69+
return new VerifyTokenTree(token.getLastChild());
70+
}
71+
72+
public function childAt(index:Int, ?pos:PosInfos):IVerifyTokenTree {
73+
childCountAtLeast(index + 1, pos);
74+
return new VerifyTokenTree(token.children[index]);
75+
}
76+
77+
public function childCount(count:Int, ?pos:PosInfos):IVerifyTokenTree {
78+
if ((token.children == null) && (count != 0)) Assert.fail('${token.tok} has no childs', pos);
79+
Assert.areEqual(count, token.children.length, '[${token.tok}] child count [${token.children.length}] was not equal to expected [$count]', pos);
80+
return this;
81+
}
82+
83+
public function childCountAtLeast(count:Int, ?pos:PosInfos):IVerifyTokenTree {
84+
if ((token.children == null) && (count != 0)) Assert.fail('${token.tok} has no childs', pos);
85+
Assert.isTrue(token.children.length >= count, pos);
86+
return this;
87+
}
88+
89+
public function is(tok:TokenDef, ?pos:PosInfos):IVerifyTokenTree {
90+
Assert.isTrue(token.is(tok), '$tok != ${token.tok}', pos);
91+
return this;
92+
}
93+
94+
public function isComment(?pos:PosInfos):IVerifyTokenTree {
95+
switch (token.tok) {
96+
case Comment(_), CommentLine(_): return this;
97+
default:
98+
Assert.fail('${token.tok} is not a comment', pos);
99+
}
100+
return this;
101+
}
102+
103+
public function isEmpty(?pos:PosInfos):Bool {
104+
return token == null;
105+
}
106+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package token.verify;
2+
3+
import haxe.PosInfos;
4+
5+
import massive.munit.Assert;
6+
7+
import haxeparser.Data;
8+
9+
class VerifyTokenTreeList implements IVerifyTokenTree {
10+
11+
var tokens:Array<IVerifyTokenTree>;
12+
13+
public function new(tokens:Array<IVerifyTokenTree>) {
14+
Assert.isNotNull(tokens);
15+
this.tokens = tokens;
16+
}
17+
18+
public function filter(tok:TokenDef, ?pos:PosInfos):IVerifyTokenTree {
19+
var list:Array<IVerifyTokenTree> = [];
20+
for (token in tokens) {
21+
var child:IVerifyTokenTree = token.filter(tok, pos);
22+
if (!child.isEmpty()) list.push(child);
23+
}
24+
return new VerifyTokenTreeList(list);
25+
}
26+
27+
public function childs(?pos:PosInfos):IVerifyTokenTree {
28+
var list:Array<IVerifyTokenTree> = [];
29+
for (token in tokens) {
30+
var childList:VerifyTokenTreeList = cast token.childs(pos);
31+
for (i in 0...childList.tokens.length) {
32+
list.push(childList.tokens[i]);
33+
}
34+
}
35+
return new VerifyTokenTreeList(list);
36+
}
37+
38+
public function first(?pos:PosInfos):IVerifyTokenTree {
39+
Assert.isTrue (tokens.length >= 1, pos);
40+
return tokens[0];
41+
}
42+
43+
public function last(?pos:PosInfos):IVerifyTokenTree {
44+
Assert.isTrue (tokens.length >= 2, pos);
45+
return tokens[tokens.length - 1];
46+
}
47+
48+
public function at(index:Int, ?pos:PosInfos):IVerifyTokenTree {
49+
Assert.isTrue (tokens.length > index, pos);
50+
return tokens[index];
51+
}
52+
53+
public function count(num:Int, ?pos:PosInfos):IVerifyTokenTree {
54+
Assert.areEqual(num, tokens.length, pos);
55+
return this;
56+
}
57+
58+
public function noChilds(?pos:PosInfos):IVerifyTokenTree {
59+
var list:Array<IVerifyTokenTree> = [];
60+
for (token in tokens) list.push (token.noChilds(pos));
61+
return new VerifyTokenTreeList(list);
62+
}
63+
64+
public function oneChild(?pos:PosInfos):IVerifyTokenTree {
65+
var list:Array<IVerifyTokenTree> = [];
66+
for (token in tokens) list.push (token.oneChild());
67+
return new VerifyTokenTreeList(list);
68+
}
69+
70+
public function childFirst(?pos:PosInfos):IVerifyTokenTree {
71+
var list:Array<IVerifyTokenTree> = [];
72+
for (token in tokens) list.push (token.childFirst(pos));
73+
return new VerifyTokenTreeList(list);
74+
}
75+
76+
public function childLast(?pos:PosInfos):IVerifyTokenTree {
77+
var list:Array<IVerifyTokenTree> = [];
78+
for (token in tokens) list.push (token.childLast(pos));
79+
return new VerifyTokenTreeList(list);
80+
}
81+
82+
public function childAt(index:Int, ?pos:PosInfos):IVerifyTokenTree {
83+
var list:Array<IVerifyTokenTree> = [];
84+
for (token in tokens) list.push (token.childAt(index, pos));
85+
return new VerifyTokenTreeList(list);
86+
}
87+
88+
public function childCount(count:Int, ?pos:PosInfos):IVerifyTokenTree {
89+
var list:Array<IVerifyTokenTree> = [];
90+
for (token in tokens) list.push (token.childCount(count, pos));
91+
return new VerifyTokenTreeList(list);
92+
}
93+
94+
public function childCountAtLeast(count:Int, ?pos:PosInfos):IVerifyTokenTree {
95+
var list:Array<IVerifyTokenTree> = [];
96+
for (token in tokens) list.push (token.childCountAtLeast(count, pos));
97+
return new VerifyTokenTreeList(list);
98+
}
99+
100+
public function is(tok:TokenDef, ?pos:PosInfos):IVerifyTokenTree {
101+
var list:Array<IVerifyTokenTree> = [];
102+
for (token in tokens) list.push (token.is(tok, pos));
103+
return new VerifyTokenTreeList(list);
104+
}
105+
106+
public function isComment(?pos:PosInfos):IVerifyTokenTree {
107+
var list:Array<IVerifyTokenTree> = [];
108+
for (token in tokens) list.push (token.isComment(pos));
109+
return new VerifyTokenTreeList(list);
110+
}
111+
112+
public function isEmpty(?pos:PosInfos):Bool {
113+
return ((tokens == null) || (tokens.length <= 0));
114+
}
115+
}

0 commit comments

Comments
 (0)