Skip to content

Commit 74f031f

Browse files
author
duaraghav8
committed
Add tests for getComments() source code util method
1 parent c92994f commit 74f031f

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

test/lib/solium.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe("Checking Exported Solium API", function() {
4141
Solium.getSourceCode.should.be.type("function");
4242
Solium.should.have.ownProperty("getDefaultConfig");
4343
Solium.getDefaultConfig.should.be.type("function");
44+
4445
Solium.should.have.ownProperty("version");
4546
Solium.version.should.be.type("string");
4647

@@ -140,22 +141,28 @@ describe("Checking Exported Solium API", function() {
140141
sourceCode.should.have.property("getPrevChars");
141142
sourceCode.getPrevChars.should.be.type("function");
142143

144+
sourceCode.should.have.property("getComments");
145+
sourceCode.getComments.should.be.type("function");
146+
143147
done();
144148
});
145149

146-
it("should be completely reset after call to Solium.reset ()", function(done) {
150+
it("should be completely reset after call to Solium.reset()", function(done) {
147151
Solium.reset();
148152

149153
let minmalConfig = { rules: {} };
150154
let sourceCode = Solium.getSourceCode();
151-
let errorMessages = Solium.lint(wrappers.toFunction("var foo = 100;"), minmalConfig, true);
155+
let errorMessages = Solium.lint(wrappers.toFunction("var foo = 100; /* helo */"), minmalConfig, true);
152156

153157
sourceCode.text.should.equal("");
154158
(sourceCode.getText()).should.equal(sourceCode.text);
155159

156160
errorMessages.should.be.instanceof(Array);
157161
errorMessages.length.should.equal(0);
158162

163+
// comments array should be empty
164+
sourceCode.getComments().should.be.empty();
165+
159166
//all event listeners should've been removed
160167
Object.keys(Solium._events).length.should.equal(0);
161168

@@ -211,7 +218,7 @@ describe("Checking Exported Solium API", function() {
211218
done();
212219
});
213220

214-
it("should push a sample error object in messages upon calling Solium.report ()", function(done) {
221+
it("should push a sample error object in messages upon calling Solium.report()", function(done) {
215222
let sampleErrorObject = {
216223
ruleName: "sample",
217224
type: "error",

test/lib/utils/source-code-utils.js

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ describe("Testing SourceCode instance for exposed functionality", function() {
2020
};
2121

2222
it("should create instance of SourceCode & expose set of functions (its own & those of astUtils)", function(done) {
23-
let sourceCodeObject = new SourceCode(sourceCodeText);
23+
let sourceCodeObject = new SourceCode(sourceCodeText, []);
2424

2525
sourceCodeObject.should.be.type("object");
2626
sourceCodeObject.should.be.instanceof(SourceCode);
2727

2828
sourceCodeObject.should.have.ownProperty("text");
2929
sourceCodeObject.text.should.equal(sourceCodeText);
3030

31+
sourceCodeObject.should.have.ownProperty("commentObjects");
32+
sourceCodeObject.commentObjects.should.be.Array();
33+
3134
//functions inherited from astUtils
3235
sourceCodeObject.should.have.property("getLine");
3336
sourceCodeObject.getLine.should.be.type("function");
3437

3538
sourceCodeObject.should.have.property("getEndingLine");
36-
sourceCodeObject.getLine.should.be.type("function");
39+
sourceCodeObject.getEndingLine.should.be.type("function");
3740

3841
sourceCodeObject.should.have.property("getColumn");
3942
sourceCodeObject.getColumn.should.be.type("function");
@@ -77,10 +80,10 @@ describe("Testing SourceCode instance for exposed functionality", function() {
7780
sourceCodeObject.getText.should.be.type("function");
7881

7982
sourceCodeObject.should.have.property("getTextOnLine");
80-
sourceCodeObject.getText.should.be.type("function");
83+
sourceCodeObject.getTextOnLine.should.be.type("function");
8184

8285
sourceCodeObject.should.have.property("getLines");
83-
sourceCodeObject.getText.should.be.type("function");
86+
sourceCodeObject.getLines.should.be.type("function");
8487

8588
sourceCodeObject.should.have.property("getNextChar");
8689
sourceCodeObject.getNextChar.should.be.type("function");
@@ -95,13 +98,16 @@ describe("Testing SourceCode instance for exposed functionality", function() {
9598
sourceCodeObject.getPrevChars.should.be.type("function");
9699

97100
sourceCodeObject.should.have.property("getStringBetweenNodes");
98-
sourceCodeObject.getPrevChars.should.be.type("function");
101+
sourceCodeObject.getStringBetweenNodes.should.be.type("function");
102+
103+
sourceCodeObject.should.have.property("getComments");
104+
sourceCodeObject.getComments.should.be.type("function");
99105

100106
done();
101107
});
102108

103109
it("should behave as expected upon calling getText ()", function(done) {
104-
let sourceCodeObject = new SourceCode(sourceCodeText);
110+
let sourceCodeObject = new SourceCode(sourceCodeText, []);
105111
let functionCallText = "fooBar ();",
106112
functionCallNode = { type: "ExpressionStatement",
107113
expression:
@@ -123,15 +129,15 @@ describe("Testing SourceCode instance for exposed functionality", function() {
123129
sourceCodeObject.getText(varDeclarator, -4, -1).should.equal("var x = 100;");
124130
sourceCodeObject.getText(varDeclarator, 100, 100).should.equal(sourceCodeText);
125131

126-
sourceCodeObject = new SourceCode(functionCallText);
132+
sourceCodeObject = new SourceCode(functionCallText, []);
127133

128134
sourceCodeObject.getText(functionCallNode).should.equal(functionCallText);
129135

130136
done();
131137
});
132138

133139
it("should behave as expected upon calling getNextChar ()", function(done) {
134-
let sourceCodeObject = new SourceCode(sourceCodeText);
140+
let sourceCodeObject = new SourceCode(sourceCodeText, []);
135141

136142
sourceCodeObject.getNextChar.bind(sourceCodeObject, {}).should.throw();
137143
sourceCodeObject.getNextChar.bind(sourceCodeObject).should.throw();
@@ -150,7 +156,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
150156
});
151157

152158
it("should behave as expected upon calling getPrevChar ()", function(done) {
153-
let sourceCodeObject = new SourceCode(sourceCodeText);
159+
let sourceCodeObject = new SourceCode(sourceCodeText, []);
154160

155161
sourceCodeObject.getPrevChar.bind(sourceCodeObject, {}).should.throw();
156162
sourceCodeObject.getPrevChar.bind(sourceCodeObject).should.throw();
@@ -164,7 +170,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
164170
});
165171

166172
it("should behave as expected upon calling getNextChars ()", function(done) {
167-
let sourceCodeObject = new SourceCode(sourceCodeText);
173+
let sourceCodeObject = new SourceCode(sourceCodeText, []);
168174

169175
sourceCodeObject.getNextChars.bind(sourceCodeObject, {}).should.throw();
170176
sourceCodeObject.getNextChars.bind(sourceCodeObject).should.throw();
@@ -177,7 +183,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
177183
});
178184

179185
it("should behave as expected upon calling getPrevChars ()", function(done) {
180-
let sourceCodeObject = new SourceCode(sourceCodeText);
186+
let sourceCodeObject = new SourceCode(sourceCodeText, []);
181187

182188
sourceCodeObject.getPrevChars.bind(sourceCodeObject, {}).should.throw();
183189
sourceCodeObject.getPrevChars.bind(sourceCodeObject).should.throw();
@@ -193,7 +199,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
193199

194200
it("should behave as expected upon calling getStringBetweenNodes ()", function(done) {
195201
let sourceCodeText = "var x = 100;\n\tvar (y) = 200;\n\n\tvar z = 300;",
196-
sourceCodeObject = new SourceCode(sourceCodeText);
202+
sourceCodeObject = new SourceCode(sourceCodeText, []);
197203

198204
let prevNode = {
199205
"type": "VariableDeclaration",
@@ -277,7 +283,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
277283
});
278284

279285
it("should return source code split into lines when calling getLines()", done => {
280-
const sourceCodeObject = new SourceCode(sourceCodeText),
286+
const sourceCodeObject = new SourceCode(sourceCodeText, []),
281287
sourceCodeTextLines = sourceCodeText.split(/\r?\n/),
282288
linesToTest = sourceCodeObject.getLines();
283289

@@ -289,7 +295,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
289295
});
290296

291297
it("should behave as expected upon calling getTextOnLine()", function(done) {
292-
let sourceCodeObject = new SourceCode(sourceCodeText),
298+
let sourceCodeObject = new SourceCode(sourceCodeText, []),
293299
sourceCodeTextLines = sourceCodeText.split(/\r?\n/);
294300

295301
for (let i = 0; i < sourceCodeTextLines.length; i++) {
@@ -309,4 +315,32 @@ describe("Testing SourceCode instance for exposed functionality", function() {
309315
done();
310316
});
311317

318+
it("should return comment objects list upon calling getComments()", done => {
319+
const dummyCommentObjects = [
320+
{
321+
type: "Line",
322+
text: "// hello world\\t\\t\\t",
323+
start: 204,
324+
end: 221
325+
},
326+
{
327+
type: "Block",
328+
text: "/*\\n\\t\\thwlloo worldd \\n\\t\\t*/",
329+
start: 225,
330+
end: 256
331+
}
332+
];
333+
334+
const sourceCodeObject = new SourceCode("", dummyCommentObjects),
335+
comments = sourceCodeObject.getComments();
336+
337+
comments[0].type.should.equal("Line");
338+
comments[0].start.should.equal(204);
339+
340+
comments[1].type.should.equal("Block");
341+
comments[1].start.should.equal(225);
342+
343+
done();
344+
});
345+
312346
});

0 commit comments

Comments
 (0)