#104, refact the unit tests

This commit is contained in:
Basile Burg 2016-11-16 14:42:58 +01:00
parent 204d0fb22c
commit 94e5e8c81a
No known key found for this signature in database
GPG Key ID: 1868039F415CB8CF
1 changed files with 377 additions and 419 deletions

View File

@ -22,6 +22,10 @@ private struct Function
string name; string name;
size_t N1, n1; size_t N1, n1;
size_t N2, n2; size_t N2, n2;
alias operatorsSum = N1;
alias operatorsKinds = n1;
alias operandsSum = N2;
alias operandsKinds = n2;
} }
private final class HalsteadMetric: ASTVisitor private final class HalsteadMetric: ASTVisitor
@ -124,10 +128,9 @@ private final class HalsteadMetric: ASTVisitor
override void visit(const(FunctionDeclaration) decl) override void visit(const(FunctionDeclaration) decl)
{ {
beginFunction;
if (!decl.functionBody) if (!decl.functionBody)
return; return;
beginFunction;
decl.accept(this); decl.accept(this);
endFunction(decl.name.text, decl.name.line); endFunction(decl.name.text, decl.name.line);
} }
@ -474,83 +477,83 @@ version(unittest)
return result; return result;
} }
unittest Function test(string source)
{ {
string src = import std.typecons;
HalsteadMetric hm = parseAndVisit!(HalsteadMetric)(source);
scope(exit) destruct(hm);
return hm.functions[$-1];
}
}
unittest
{
Function r =
q{ q{
void foo() void foo()
{ {
Object o = new Object; Object o = new Object;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 1);
assert(r.operands.length == 1); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
auto o = new Object; auto o = new Object;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 1);
assert(r.operands.length == 1); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
auto o = 1 + 2; auto o = 1 + 2;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
foo(bar,baz); foo(bar,baz);
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
int i = foo(bar,baz) + foo(bar,baz); int i = foo(bar,baz) + foo(bar,baz);
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 3);
assert(r.operators.length == 3); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
@ -558,88 +561,76 @@ version(unittest)
E e; E e;
bar!(e,"lit")(baz(e)); bar!(e,"lit")(baz(e));
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo(); void foo();
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 0);
assert(r.operands.length == 0); assert(r.operatorsKinds == 0);
assert(r.operators.length == 0); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
shared static this() shared static this()
{ {
int i = 0; int i = 0;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
shared static ~this() shared static ~this()
{ {
int i = 0; int i = 0;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
static this() static this()
{ {
int i = 0; int i = 0;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
static ~this() static ~this()
{ {
int i = 0; int i = 0;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
class Foo class Foo
{ {
@ -648,16 +639,14 @@ version(unittest)
int i = 0; int i = 0;
} }
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
class Foo class Foo
{ {
@ -666,77 +655,67 @@ version(unittest)
int i = 0; int i = 0;
} }
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
i += a << b; i += a << b;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
++h; ++h;
i--; i--;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
++i--; ++i--;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 1);
assert(r.operands.length == 1); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
i = a | b & c && d || e^f + g^^h - a in b + a[0]; i = a | b & c && d || e^f + g^^h - a in b + a[0];
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 10);
assert(r.operands.length == 10); assert(r.operatorsKinds == 11);
assert(r.operators.length == 11); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
@ -744,91 +723,79 @@ version(unittest)
auto baz = cast(Baz) bar; auto baz = cast(Baz) bar;
delete bar; delete bar;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 4);
assert(r.operators.length == 4); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
foreach(i,a;z){} foreach(i,a;z){}
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
foreach(i; l..h){} foreach(i; l..h){}
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 1);
assert(r.operators.length == 1); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
for(i = 0; i < len; i++){} for(i = 0; i < len; i++){}
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 4);
assert(r.operators.length == 4); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
for(;;){continue;} for(;;){continue;}
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 0);
assert(r.operands.length == 0); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
int foo() int foo()
{ {
while(true) {return 0;} while(true) {return 0;}
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 2);
assert(r.operators.length == 2); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
@ -839,16 +806,14 @@ version(unittest)
case 2: .. case 8: ; case 2: .. case 8: ;
} }
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 4);
assert(r.operands.length == 4); assert(r.operatorsKinds == 4);
assert(r.operators.length == 4); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
@ -856,42 +821,35 @@ version(unittest)
catch(Exception e) catch(Exception e)
throw v; throw v;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 2);
assert(r.operands.length == 2); assert(r.operatorsKinds == 4);
assert(r.operators.length == 4); }
r.destruct;
}
unittest unittest
{ {
string src = Function r =
q{ q{
void foo() void foo()
{ {
if (true) {} else {i = 0;} if (true) {} else {i = 0;}
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 3);
assert(r.operands.length == 3); assert(r.operatorsKinds == 4);
assert(r.operators.length == 4); }
r.destruct;
}
unittest version(none) unittest
{ {
// TODO: detect function call w/o parens // TODO: detect function call w/o parens
string src = Function r =
q{ q{
void foo() void foo()
{ {
bar; bar;
} }
}; }.test;
HalsteadMetric r = src.parseAndVisit!HalsteadMetric; assert(r.operandsKinds == 0);
//assert(r.operands.length == 0); assert(r.operatorsKinds == 1);
//assert(r.operators.length == 1);
r.destruct;
}
} }