Permalink
Browse files

Add hasTooLargeFunction().

Fixed document
  • Loading branch information...
1 parent b1a3603 commit cf1d1123e92eb02567f30785aca3e7b93e0385fd @hideo55 committed Nov 16, 2011
Showing with 197 additions and 12 deletions.
  1. +14 −2 Readme.md
  2. +30 −7 lib/test-metrics.js
  3. +8 −3 test.js
  4. +145 −0 test/test_sample2.js
View
@@ -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.
View
@@ -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) {
@@ -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;
+};
View
@@ -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');
View
@@ -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.