UNPKG

3.08 kBJavaScriptView Raw
1describe("Complexity task", function() {
2
3 var grunt = require('grunt');
4 var cut = require('../tasks/complexity')(grunt);
5 var expect = require('chai').expect;
6
7 it ("can normalize options to allow for multiple levels of error reporting", function() {
8
9 var normalized = cut.normalizeOptions({
10 cyclomatic: 3,
11 halstead: 9
12 });
13
14 expect(normalized.cyclomatic).to.eql([3]);
15 expect(normalized.halstead).to.eql([9]);
16 });
17
18 it ('throws an error when encountering an empty file', function () {
19 var targetFile = __dirname + '/fixtures/empty.js';
20 var reporter = {
21 start: function () {}
22 };
23
24 expect(function () {
25 cut.analyze(reporter, [targetFile], null);
26 }).to.throw('Empty source file: \'' + targetFile + '\'.');
27 });
28
29 it ('parses es6', function () {
30 var reporter = {
31 start: function () {},
32 maintainability: function () {},
33 complexity: function () {},
34 finish: function () {}
35 };
36
37 var options = cut.normalizeOptions({
38 cyclomatic: 3,
39 halstead: 8
40 });
41
42 cut.analyze(reporter, [__dirname + '/fixtures/es6.js'], options);
43 });
44
45 it ('parses jsx', function () {
46 var reporter = {
47 start: function () {},
48 maintainability: function () {},
49 complexity: function () {},
50 finish: function () {}
51 };
52
53 var options = cut.normalizeOptions({
54 cyclomatic: 3,
55 halstead: 8
56 });
57
58 cut.analyze(reporter, [__dirname + '/fixtures/react.jsx'], options);
59 });
60
61 describe("isComplicated", function() {
62 var data = {
63 cyclomatic: 5,
64 halstead: {
65 difficulty: 7
66 }
67 };
68
69 it('should return true if cyclomatic complexity is greater than in options', function() {
70 // given
71 var options = cut.normalizeOptions({
72 cyclomatic: 3,
73 halstead: 8
74 });
75
76 // when
77 var isComplicated = cut.isComplicated(data, options);
78
79 // then
80 expect(isComplicated).to.equal(true);
81 });
82
83 it('should return true if halstead is greater than in options', function() {
84 // given
85 var options = cut.normalizeOptions({
86 cyclomatic: 6,
87 halstead: 3
88 });
89
90 // when
91 var isComplicated = cut.isComplicated(data, options);
92
93 // then
94 expect(isComplicated).to.equal(true);
95 });
96
97 it('should return false if no metric is greater than in options', function() {
98 // given
99 var options = cut.normalizeOptions({
100 cyclomatic: 6,
101 halstead: 8
102 });
103
104 // when
105 var isComplicated = cut.isComplicated(data, options);
106
107 // then
108 expect(isComplicated).to.equal(false);
109 });
110 });
111
112 describe("isMaintainable", function() {
113 var data = {
114 maintainability: 120
115 };
116
117 it('should return true if maintainability is greater than or equal to options', function() {
118 // given
119 var options = {
120 maintainability: 120
121 };
122
123 // when
124 var isMaintainable = cut.isMaintainable(data, options);
125
126 // then
127 expect(isMaintainable).to.equal(true);
128 });
129
130 it('should return false otherwise', function() {
131 // given
132 var options = {
133 maintainability: 140
134 };
135
136 // when
137 var isMaintainable = cut.isMaintainable(data, options);
138
139 // then
140 expect(isMaintainable).to.equal(false);
141 });
142
143 });
144
145});