Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
refactor: refactor test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolatoscan committed Oct 18, 2020
1 parent 6ec142e commit 1a4370a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
29 changes: 15 additions & 14 deletions test/lib/generator.test.ts
Expand Up @@ -5,50 +5,51 @@ import getGenerators from '../../source/lib/generators';
import { Logger } from '../../source/lib/utils/logger';


describe('Generator', function () {
describe('Generator', function() {

let paths: { templateFilePaths: string[]; toTestPaths: string[]} = { templateFilePaths: [], toTestPaths: [] };
before(() => {
before(function() {
paths = getTemplateFilesPath();
});

after(() => {
after(function() {
for (const templateFilePath of paths.templateFilePaths) {
fs.unlinkSync(getFileNameFromTemplate(templateFilePath));
}
});

const originalLogFunction = console.log;
let output = '';
beforeEach(() => {
beforeEach(function() {
output = '';
console.log = (msg: string) => {
output += msg + '\n';
};
});

afterEach(function () {
afterEach(function() {
console.log = originalLogFunction;
if (this.currentTest?.state === 'failed') {
console.log(output);
}
});


describe('#constructor(structure: StructureModel, config: ConfigModel)', () => {
describe('#constructor(structure: StructureModel, config: ConfigModel)', function() {

let referenceCode: ReferenceCode = { almostempty: {}, tests: {} };
before(function () {
before(function() {
referenceCode = JSON.parse(fs.readFileSync(`${testConfig.assetsPath}/codereference.json`, 'utf-8'));
});

it(`should get generators`, () => {
it(`should get generators`, function(done) {
let generators = getGenerators(new Logger({}));
assert(generators.length > 0, '0 generator created');
done();
});


it(`should generate identical formatting and code for almost empty structure and config`, () => {
it(`should generate identical formatting and code for almost empty structure and config`, function() {
let generators = getGenerators(new Logger({}));
for (let generator of generators) {
let gen = new generator({id: 'int', sessionName: 'char*', timestamp: 'long'} as any, {}).generated;
Expand All @@ -63,7 +64,7 @@ describe('Generator', function () {
}
});

it(`should generate identical formatting and code for test folder structures and configs`, () => {
it(`should generate identical formatting and code for test folder structures and configs`, function() {

let generators = getGenerators(new Logger({}));
for (const toTestPath of paths.toTestPaths) {
Expand All @@ -89,9 +90,9 @@ describe('Generator', function () {
});


describe('#generate(src?: string, structureModel?: string, configModel?: string, options?: Options): void', () => {
describe('#generate(src?: string, structureModel?: string, configModel?: string, options?: Options): void', function() {

it('should generate files', () => {
it('should generate files', function() {
generateEverything(paths.toTestPaths);
//CHECK
for (const templateFilePath of paths.templateFilePaths) {
Expand All @@ -100,7 +101,7 @@ describe('Generator', function () {
}
});

it('content should be identical', () => {
it('content should be identical', function() {
for (const templateFilePath of paths.templateFilePaths) {
const toCompare = getFileNameFromTemplate(templateFilePath);
const asReference = getCorrectFileNameFromTemplate(templateFilePath);
Expand All @@ -112,7 +113,7 @@ describe('Generator', function () {
}
});

it('content and formatting should be identical', () => {
it('content and formatting should be identical', function() {
for (const templateFilePath of paths.templateFilePaths) {
const toCompare = getFileNameFromTemplate(templateFilePath);
const asReference = getCorrectFileNameFromTemplate(templateFilePath);
Expand Down
12 changes: 6 additions & 6 deletions test/lib/utils.test.ts
Expand Up @@ -19,7 +19,7 @@ describe('Utils', function() {

const originalLogFunction = console.log;
let output = '';
beforeEach(() => {
beforeEach(function() {
output = '';
console.log = (msg: string) => {
output += msg + '\n';
Expand All @@ -33,7 +33,7 @@ describe('Utils', function() {
}
});

describe('logger', () => {
describe('logger', function() {


it('should not log', function() {
Expand All @@ -53,7 +53,7 @@ describe('Utils', function() {
let res = '';

console.log = (log) => {
assert(log === res, `Logged ${JSON.stringify(log)} instead of ${JSON.stringify(res)}`);
assert(JSON.stringify(log) === JSON.stringify(res), `Logged ${JSON.stringify(log)} instead of ${JSON.stringify(res)}`);
};

res = '\u001b[1m\u001b[31m[TAG1]\u001b[39m\u001b[22m test';
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('Utils', function() {
});


describe('parseTemplate', () => {
describe('parseTemplate', function() {

describe('#parseTemplate(template: string, codes: Code[], options: Options): string', function() {
it('should transpile everything', function() {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Utils', function() {

});

describe('options', () => {
describe('options', function() {

describe('#mergeOptions(options: Options): Options', function() {
const DEFAULT_OPTIONS = rewire('../../source/lib/utils/options').__get__('DEFAULT_OPTIONS');
Expand All @@ -175,7 +175,7 @@ describe('Utils', function() {
});


describe('checkModelsSchema', () => {
describe('checkModelsSchema', function() {

describe('#checkModelsSchema(structureModel: string, configModel: string): { structureModelObject: StructureModel, configModelObject: ConfigModel }', function() {

Expand Down

0 comments on commit 1a4370a

Please sign in to comment.