Skip to content

Commit

Permalink
Add hasTooLargeFunction().
Browse files Browse the repository at this point in the history
Fixed document
  • Loading branch information
hideo55 committed Nov 16, 2011
1 parent b1a3603 commit cf1d112
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 12 deletions.
16 changes: 14 additions & 2 deletions Readme.md
Expand Up @@ -3,14 +3,26 @@

Test metrics of your code

# Usage
## Usage

```javascript
var testMetrics = require('test-metrics');
assert.ok(!testMetrics.hasComplexCode('/path/to/sourcecode', 25));
```

# Issue
## Methods

### hasComplexCode(srcPath, threshold)

This method returns boolean value.If cyclomatic complexity of JavaScript code
on `srcPath` exceeds `threshold` value, will return true.

### hasTooLargeFunction(srcPath, threshold)

This method returns boolean value.If lines(per function) of JavaScript code
on `srcPath` exceeds `threshold` value, will return true.

## Issue

Now, can't use it with some testing/spec framework because of colision prototype modification.

Expand Down
37 changes: 30 additions & 7 deletions lib/test-metrics.js
Expand Up @@ -16,7 +16,8 @@ var parse = require('jsmeter/parse').make_parse();
var fs = require('fs');
var path = require('path');

exports.hasComplexCode = function(src, threshold) {
var cache = {};
var getMetrics = function(src) {
var jsonStr;
var res = {
write : function(t) {
Expand All @@ -25,18 +26,40 @@ exports.hasComplexCode = function(src, threshold) {
};
src = path.normalize(path.join(process.cwd(), src));

var source = fs.readFileSync(src, 'utf-8');
var name = src.match(/([^\/])\.js$/)[1];
var source, name, metrics;
if(cache[src]) {
metrics = cache[src];
} else {
source = fs.readFileSync(src, 'utf-8');
name = src.match(/([^\/])\.js$/)[1];

complex.complexity(parse(source), name);
complex.renderStats(res, 'JSON');
complex.complexity(parse(source), name);
complex.renderStats(res, 'JSON');
metrics = JSON.parse(jsonStr);
cache[src] = metrics;
}
return metrics;
};

var comp = JSON.parse(jsonStr);
exports.hasComplexCode = function(src, threshold) {
var metrics = getMetrics(src);
var hasTooComplexCode = false;
comp.forEach(function(elm) {
metrics.forEach(function(elm) {
if(elm.complexity > threshold) {
hasTooComplexCode = true;
}
});
return hasTooComplexCode;
};


exports.hasTooLargeFunction = function(src, threshold) {
var metrics = getMetrics(src);
var hasTooLargeFunction = false;
metrics.forEach(function(elm) {
if(elm.lines> threshold) {
hasTooLargeFunction = true;
}
});
return hasTooLargeFunction;
};
11 changes: 8 additions & 3 deletions test.js
@@ -1,6 +1,11 @@

var testMetrics = require('./lib/test-metrics');
var assert = require('assert');

assert.ok(!testMetrics.hasComplexCode('./test/test_sample1.js',10));
assert.ok(testMetrics.hasComplexCode('./test/test_sample1.js',1));
//Complexity
assert.ok(!testMetrics.hasComplexCode('./test/test_sample1.js', 10), 'complexity of test_sample1.js does not exceed threshold value');
assert.ok(testMetrics.hasComplexCode('./test/test_sample1.js', 1), 'complexity of test_sample1.js exceeds threshold value');
assert.ok(testMetrics.hasComplexCode('./test/test_sample2.js', 5), 'complexity of test_sample2.js exceeds threshold value');

//Lines
assert.ok(!testMetrics.hasTooLargeFunction('./test/test_sample1.js', 100), 'lines of test_sample1.js does not exceed threshold value');
assert.ok(testMetrics.hasTooLargeFunction('./test/test_sample2.js', 100), 'lines of test_sample2.js exceeds threshold value');
145 changes: 145 additions & 0 deletions test/test_sample2.js
@@ -0,0 +1,145 @@
var nop = function() {
};
function foo(x) {
var func = (function(x) {
if(!x)
return nop;
if(x == 0) {
return function(y) {
nop();
};
} else if(x > 0) {
return function() {(function(y) {
nop();
})();
};
} else {
return function() {(function() {
nop();
})();
};
}
})(x);
return func;
}

function bar() {
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
}

0 comments on commit cf1d112

Please sign in to comment.