Skip to content

Commit

Permalink
Add tests for getComments() source code util method
Browse files Browse the repository at this point in the history
  • Loading branch information
duaraghav8 committed Apr 7, 2019
1 parent c92994f commit 74f031f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
13 changes: 10 additions & 3 deletions test/lib/solium.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("Checking Exported Solium API", function() {
Solium.getSourceCode.should.be.type("function");
Solium.should.have.ownProperty("getDefaultConfig");
Solium.getDefaultConfig.should.be.type("function");

Solium.should.have.ownProperty("version");
Solium.version.should.be.type("string");

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

sourceCode.should.have.property("getComments");
sourceCode.getComments.should.be.type("function");

done();
});

it("should be completely reset after call to Solium.reset ()", function(done) {
it("should be completely reset after call to Solium.reset()", function(done) {
Solium.reset();

let minmalConfig = { rules: {} };
let sourceCode = Solium.getSourceCode();
let errorMessages = Solium.lint(wrappers.toFunction("var foo = 100;"), minmalConfig, true);
let errorMessages = Solium.lint(wrappers.toFunction("var foo = 100; /* helo */"), minmalConfig, true);

sourceCode.text.should.equal("");
(sourceCode.getText()).should.equal(sourceCode.text);

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

// comments array should be empty
sourceCode.getComments().should.be.empty();

//all event listeners should've been removed
Object.keys(Solium._events).length.should.equal(0);

Expand Down Expand Up @@ -211,7 +218,7 @@ describe("Checking Exported Solium API", function() {
done();
});

it("should push a sample error object in messages upon calling Solium.report ()", function(done) {
it("should push a sample error object in messages upon calling Solium.report()", function(done) {
let sampleErrorObject = {
ruleName: "sample",
type: "error",
Expand Down
62 changes: 48 additions & 14 deletions test/lib/utils/source-code-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ describe("Testing SourceCode instance for exposed functionality", function() {
};

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

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

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

sourceCodeObject.should.have.ownProperty("commentObjects");
sourceCodeObject.commentObjects.should.be.Array();

//functions inherited from astUtils
sourceCodeObject.should.have.property("getLine");
sourceCodeObject.getLine.should.be.type("function");

sourceCodeObject.should.have.property("getEndingLine");
sourceCodeObject.getLine.should.be.type("function");
sourceCodeObject.getEndingLine.should.be.type("function");

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

sourceCodeObject.should.have.property("getTextOnLine");
sourceCodeObject.getText.should.be.type("function");
sourceCodeObject.getTextOnLine.should.be.type("function");

sourceCodeObject.should.have.property("getLines");
sourceCodeObject.getText.should.be.type("function");
sourceCodeObject.getLines.should.be.type("function");

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

sourceCodeObject.should.have.property("getStringBetweenNodes");
sourceCodeObject.getPrevChars.should.be.type("function");
sourceCodeObject.getStringBetweenNodes.should.be.type("function");

sourceCodeObject.should.have.property("getComments");
sourceCodeObject.getComments.should.be.type("function");

done();
});

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

sourceCodeObject = new SourceCode(functionCallText);
sourceCodeObject = new SourceCode(functionCallText, []);

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

done();
});

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

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

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

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

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

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

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

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

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

let prevNode = {
"type": "VariableDeclaration",
Expand Down Expand Up @@ -277,7 +283,7 @@ describe("Testing SourceCode instance for exposed functionality", function() {
});

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

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

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

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

it("should return comment objects list upon calling getComments()", done => {
const dummyCommentObjects = [
{
type: "Line",
text: "// hello world\\t\\t\\t",
start: 204,
end: 221
},
{
type: "Block",
text: "/*\\n\\t\\thwlloo worldd \\n\\t\\t*/",
start: 225,
end: 256
}
];

const sourceCodeObject = new SourceCode("", dummyCommentObjects),
comments = sourceCodeObject.getComments();

comments[0].type.should.equal("Line");
comments[0].start.should.equal(204);

comments[1].type.should.equal("Block");
comments[1].start.should.equal(225);

done();
});

});

0 comments on commit 74f031f

Please sign in to comment.