Skip to content

Commit

Permalink
test: add tests for new features
Browse files Browse the repository at this point in the history
* test 14: Folders as modules / batch exports / barrel files
- export * from './File'
import { File } from './dir'

* test 15:
- Variable declaration with..
a) object destructuring alias assignment
b) array destructuring
- Import Decaration with...
* glob import and namespace specifier

* See: Issue oslabs-beta#85, oslabs-beta#99
  • Loading branch information
MajorLift committed Nov 30, 2021
1 parent ec4d986 commit ccd630a
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 17 deletions.
118 changes: 101 additions & 17 deletions sapling/src/test/suite/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import * as assert from 'assert';
import { SaplingParser } from '../../SaplingParser';
import { describe, suite , test, before} from 'mocha';
import { expect } from 'chai';
import * as path from 'path';
import { describe, suite, test, before } from 'mocha';
import { expect } from 'chai';
import * as assert from 'assert';

import { SaplingParser } from '../../controllers/SaplingParser';
import { Tree } from '../../types';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
// import * as vscode from 'vscode';
// import * as myExtension from '../../extension';

suite('Parser Test Suite', () => {
let parser : SaplingParser, tree : Tree, file : string;
let parser: SaplingParser, tree: Tree, file: string;

// UNPARSED TREE TEST
describe('It initializes correctly', () => {
before( () => {
before(() => {
file = path.join(__dirname, '../../../src/test/test_apps/test_0/index.js');
parser = new SaplingParser(file);
});
Expand All @@ -31,7 +33,7 @@ suite('Parser Test Suite', () => {

// TEST 0: ONE CHILD
describe('It works for simple apps', () => {
before( () => {
before(() => {
file = path.join(__dirname, '../../../src/test/test_apps/test_0/index.js');
parser = new SaplingParser(file);
tree = parser.parse();
Expand Down Expand Up @@ -88,7 +90,6 @@ suite('Parser Test Suite', () => {
expect(tree.children[0]).to.have.own.property('name').that.is.oneOf(['Switch', 'Route']);
expect(tree.children[1]).to.have.own.property('name').that.is.oneOf(['Switch', 'Route']);
expect(tree.children[2]).to.have.own.property('name').that.is.equal('Tippy');

});

test('reactRouter should be designated as third party and reactRouter', () => {
Expand Down Expand Up @@ -142,22 +143,22 @@ suite('Parser Test Suite', () => {

// TEST 5: MISSING EXTENSIONS AND UNUSED IMPORTS
describe('It works for extension-less imports', () => {
let names: string[], paths: string [], expectedNames : string[], expectedPaths : string[];
let names: string[], paths: string[], expectedNames: string[], expectedPaths: string[];
before(() => {
file = path.join(__dirname, '../../../src/test/test_apps/test_5/index.js');
parser = new SaplingParser(file);
tree = parser.parse();

names = tree.children.map(child => child.name);
paths = tree.children.map(child => child.filePath);
names = tree.children.map((child) => child.name);
paths = tree.children.map((child) => child.filePath);

expectedNames = ['JS', 'JSX', 'TS', 'TSX'];
expectedPaths = [
'../../../src/test/test_apps/test_5/components/JS.js',
'../../../src/test/test_apps/test_5/components/JSX.jsx',
'../../../src/test/test_apps/test_5/components/TS.ts',
'../../../src/test/test_apps/test_5/components/TSX.tsx'
].map( el => path.resolve(__dirname, el));
'../../../src/test/test_apps/test_5/components/TSX.tsx',
].map((el) => path.resolve(__dirname, el));
});

test('Check children match expected children', () => {
Expand Down Expand Up @@ -254,8 +255,12 @@ suite('Parser Test Suite', () => {
expect(tree.children[0]).to.have.own.property('name').that.is.equal('BrowserRouter');
expect(tree.children[1]).to.have.own.property('name').that.is.equal('App');

expect(tree.children[1].children[3]).to.have.own.property('name').that.is.equal('DrillCreator');
expect(tree.children[1].children[4]).to.have.own.property('name').that.is.equal('HistoryDisplay');
expect(tree.children[1].children[3])
.to.have.own.property('name')
.that.is.equal('DrillCreator');
expect(tree.children[1].children[4])
.to.have.own.property('name')
.that.is.equal('HistoryDisplay');
});
});

Expand All @@ -278,7 +283,9 @@ suite('Parser Test Suite', () => {
expect(tree.children[0].children).to.have.lengthOf(1);
expect(tree.children[0].children[0]).to.have.own.property('name').that.is.equal('App2');
expect(tree.children[0].children[0].children).to.have.lengthOf(1);
expect(tree.children[0].children[0].children[0]).to.have.own.property('name').that.is.equal('App1');
expect(tree.children[0].children[0].children[0])
.to.have.own.property('name')
.that.is.equal('App1');
expect(tree.children[0].children[0].children[0].children).to.have.lengthOf(0);
});
});
Expand Down Expand Up @@ -327,4 +334,81 @@ suite('Parser Test Suite', () => {
expect(tree.children[0].children[2]).to.have.own.property('thirdParty').that.is.false;
});
});
});

// TEST 14: BARREL FILES / BATCH EXPORTS / FOLDERS AS MODULES
describe('It should parse folders that are imported as modules', () => {
before(() => {
file = path.join(__dirname, '../../../src/test/test_apps/test_14/index.js');
parser = new SaplingParser(file);
tree = parser.parse();
});

test('Root should be named index, it should have one child named App', () => {
expect(tree).to.have.own.property('name').that.is.equal('index');
expect(tree.children).to.have.lengthOf(1);
expect(tree.children[0]).to.have.own.property('name').that.is.equal('App');
});

test('App should have three children, Page1, Page2 and Page3, all found successfully', () => {
expect(tree.children[0].children[0]).to.have.own.property('name').that.is.equal('Page1');
expect(tree.children[0].children[0]).to.have.own.property('thirdParty').that.is.false;
expect(tree.children[0].children[1]).to.have.own.property('name').that.is.equal('Page2');
expect(tree.children[0].children[1]).to.have.own.property('thirdParty').that.is.false;
expect(tree.children[0].children[2]).to.have.own.property('name').that.is.equal('Page3');
expect(tree.children[0].children[2]).to.have.own.property('thirdParty').that.is.false;
});
});

// TODO: TEST 15: MORE DESTRUCTURING, ALIASING, NAMESPACE CASES
describe('It should parse VariableDeclaration imports with Array Destructuring and Object Destructuring with Aliasing; also Glob imports with Namespace Specifiers', () => {
before(() => {
file = path.join(__dirname, '../../../src/test/test_apps/test_15/index.js');
parser = new SaplingParser(file);
tree = parser.parse();
});

test('Root should be named index, it should have one child named App', () => {
expect(tree).to.have.own.property('name').that.is.equal('index');
expect(tree.children).to.have.lengthOf(1);
expect(tree.children[0]).to.have.own.property('name').that.is.equal('App');
});

test('Object destructuring aliased child Page1 found successfully with its filename, not as Alias', () => {
expect(tree.children[0].children[0]).to.have.own.property('name').that.is.not.equal('Alias');
expect(tree.children[0].children[0]).to.have.own.property('name').that.is.equal('Page1');
expect(tree.children[0].children[0]).to.have.own.property('thirdParty').that.is.false;
});
test('Array destructured variable declaration imports Page1of2, Page2f2 all found successfully', () => {
expect(tree.children[0].children[1]).to.have.own.property('name').that.is.equal('Page1of2');
expect(tree.children[0].children[1]).to.have.own.property('thirdParty').that.is.false;
expect(tree.children[0].children[2]).to.have.own.property('name').that.is.equal('Page2of2');
expect(tree.children[0].children[2]).to.have.own.property('thirdParty').that.is.false;
});

test('App should also have four more children namespace.Page3_1, namespace.Page3_2, LastPage.Page4_1, LastPage.Page4_2, all found successfully', () => {
expect(tree.children[0].children[3])
.to.have.own.property('name')
.that.is.equal('namespace.Page3_1');
expect(tree.children[0].children[3]).to.have.own.property('thirdParty').that.is.false;
expect(tree.children[0].children[4])
.to.have.own.property('name')
.that.is.equal('namespace.Page3_2');
expect(tree.children[0].children[4]).to.have.own.property('thirdParty').that.is.false;
expect(tree.children[0].children[5])
.to.have.own.property('name')
.that.is.equal('LastPage.Page4_1');
expect(tree.children[0].children[5]).to.have.own.property('thirdParty').that.is.false;
expect(tree.children[0].children[6])
.to.have.own.property('name')
.that.is.equal('LastPage.Page4_2');
expect(tree.children[0].children[6]).to.have.own.property('thirdParty').that.is.false;
});

test('DefaultExport', () => {
expect(tree.children[0].children[6])
.to.have.own.property('name')
.that.is.equal('DefaultExport');
expect(tree.children[0].children[6]).to.have.own.property('thirdParty').that.is.false;
});
});
});
11 changes: 11 additions & 0 deletions sapling/src/test/test_apps/test_14/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Page1, Page2, Page3 } from './components';

export default function Routes() {
return (
<div>
<Page1 />
<Page2 />
<Page3 />
</div>
);
}
13 changes: 13 additions & 0 deletions sapling/src/test/test_apps/test_14/components/Page1.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';

class Page1 extends Component {
render () {
return (
<section>
<div>This is Page 1</div>
</section>
)
}
}

export default Page1;
13 changes: 13 additions & 0 deletions sapling/src/test/test_apps/test_14/components/Page2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';

class Page2 extends Component {
render () {
return (
<section>
<div>I am Page2.</div>
</section>
)
}
}

export default Page2;
13 changes: 13 additions & 0 deletions sapling/src/test/test_apps/test_14/components/Page3.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';

class Page3 extends Component {
render () {
return (
<section>
<div>I am Page3.</div>
</section>
)
}
}

export default Page3;
3 changes: 3 additions & 0 deletions sapling/src/test/test_apps/test_14/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './Page1';
export * from './Page2';
export * from './Page3';
16 changes: 16 additions & 0 deletions sapling/src/test/test_apps/test_14/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { render } from 'react-dom';

import App from './App.jsx';

// TEST 15 - Barrel Files
// index.js, index.ts files that contain "export * from './file'" statements.
// and enable folders to be used as modules
// e.g. import { file } from './dir'

render(
<div>
<App />
</div>,
document.getElementById('root')
);
20 changes: 20 additions & 0 deletions sapling/src/test/test_apps/test_15/components/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { Page1: Alias } = import('./Page1');
const [ Page1of2, Page2of2 ] = import('./Page2');
import * as namespace from './Wrapper';
import * as LastPage from './Page4';
import * as DefaultExport from './Page5';

export default function Routes() {
return (
<div>
<Alias />
<Page1of2 />
<Page2of2 />
<namespace.Page3_1 />
<namespace.Page3_2 />
<LastPage.Page4_1 />
<LastPage.Page4_2 />
<DefaultExport />
</div>
);
}
13 changes: 13 additions & 0 deletions sapling/src/test/test_apps/test_15/components/Page1.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from 'react';

class Page1 extends Component {
render () {
return (
<section>
<div>This is Page 1</div>
</section>
)
}
}

export default Page1;
23 changes: 23 additions & 0 deletions sapling/src/test/test_apps/test_15/components/Page2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { Component } from 'react';

class Page1of2 extends Component {
render () {
return (
<section>
<div>I am Page1of2.</div>
</section>
)
}
}

class Page2of2 extends Component {
render () {
return (
<section>
<div>I am Page2of2.</div>
</section>
)
}
}

export default [ Page1of2, Page2of2 ];
21 changes: 21 additions & 0 deletions sapling/src/test/test_apps/test_15/components/Page3.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { Component } from 'react';

class Page3_1 extends Component {
render () {
return (
<section>
<div>I am Page3_1.</div>
</section>
)
}
}

class Page3_2 extends Component {
render () {
return (
<section>
<div>I am Page3_2.</div>
</section>
)
}
}
21 changes: 21 additions & 0 deletions sapling/src/test/test_apps/test_15/components/Page4.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { Component } from 'react';

export class Page4_1 extends Component {
render () {
return (
<section>
<div>This is Page 4_1</div>
</section>
)
}
};

export class Page4_2 extends Component {
render () {
return (
<section>
<div>This is Page 4_2</div>
</section>
)
}
};
11 changes: 11 additions & 0 deletions sapling/src/test/test_apps/test_15/components/Page5.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { Component } from 'react';

export default class Page5 extends Component {
render () {
return (
<section>
<div>This is Page5</div>
</section>
)
}
};
1 change: 1 addition & 0 deletions sapling/src/test/test_apps/test_15/components/Wrapper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Page3';
Loading

0 comments on commit ccd630a

Please sign in to comment.