Skip to content

Commit

Permalink
test added for AstStages.GenerateCode method
Browse files Browse the repository at this point in the history
  • Loading branch information
ozer authored and ozer committed Nov 3, 2018
1 parent 0f4e672 commit 5bed340
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions test/astStages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,96 @@ const fs = require('fs')


suite("AST Stages", function() {
let fnParser = sinon.spy(AstStages.prototype, 'parseToAst')
let fnGenerate = sinon.spy(AstStages.prototype, 'generateCode')
const reactFilePath = './test/mockData/reactCodeMock.js'
const parsedFile = fnParser(reactFilePath)
const returnedAST = require('./mockData/reactCodeASTMock').reactFileAST
const returnedCode = fs.readFileSync(reactFilePath,'utf-8');

suite("Parser", () => {
/**
* stages.parseToAst(file)
* 1- test file parameter
* 2- test parsedFile variable it should be equal to given ast
* 3- returned value equal to parsedFile AST
*/
let fn = sinon.spy(AstStages.prototype, 'parseToAst')
const reactFilePath = './test/mockData/reactCodeMock.js'
const returnedAST = require('./mockData/reactCodeASTMock').reactFileAST

suite("given parameter to the parser is testing", () => {

test("it should be defined", function() {
expect(fn).to.be.a('function');
expect(fnParser).to.be.a('function');

});
test("it should throw error if parameter is not string or Buffer", () => {
expect(fn).withArgs(3).to.throwException();
expect(fnParser).withArgs(3).to.throwException();
})

test("it should throw error if no such a file", () => {
expect(fn).withArgs('asdas').to.throwException();
expect(fnParser).withArgs('asdas').to.throwException();
})

test("it should be a valid file path", () => {
expect(fn).withArgs(reactFilePath).to.not.throwException();
expect(fnParser).withArgs(reactFilePath).to.not.throwException();
})

test("it should be a JavaScript file", () => {
expect(reactFilePath).to.contain('.js');
})

test("returned value should be an Object", () => {
expect(fn).withArgs(reactFilePath).to.not.throwException();
expect(fnParser).withArgs(reactFilePath).to.not.throwException();
})
})

suite("returned AST Object is testing", () => {

test("it should be an object", () => {
expect(fn(reactFilePath)).to.be.an('object');
expect(fnParser(reactFilePath)).to.be.an('object');
})

test("it should not be an empy object", () => {
expect(fn(reactFilePath)).to.not.be.empty();
expect(fnParser(reactFilePath)).to.not.be.empty();
})

test("it should be have property named PROGRAM", () => {
expect(fn(reactFilePath)).to.have.property('program');
expect(fnParser(reactFilePath)).to.have.property('program');
})

test("it shouldn't be an empty AST", () => {
expect(fn(reactFilePath)).to.not.be.empty()
expect(fnParser(reactFilePath)).to.not.be.empty()
})

test("it should be a valid AST", () => {
assert.deepEqual(fn(reactFilePath), returnedAST)
assert.deepEqual(fnParser(reactFilePath), returnedAST)
})

})
})

// FIXME: after code transform... leadcomment does not exist
suite("Code Generation", () => {
suite("given parameter to the generator is testing", () => {
test("it should be defined", function() {
expect(fnGenerate).to.be.a('function');

});
test("it should throw error if parameter is not an Object", () => {
expect(fnGenerate).withArgs(3).to.throwException();
expect(fnGenerate).withArgs('3').to.throwException();
expect(fnGenerate).withArgs([]).to.throwException();
})

test("it should be a valid AST Object", () => {
expect(fnGenerate).withArgs(parsedFile).to.not.throwException();
})

test("it should be a valid code", () => {
expect(fnGenerate(parsedFile).code).to.be(returnedCode)
})


})
})


});

0 comments on commit 5bed340

Please sign in to comment.