Skip to content

Commit

Permalink
Add example testing directly to Turf core testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Jul 22, 2017
1 parent ee48102 commit 46756c2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ marchingsquares-isobands.js
rollup.config.js
simplepolygon.js
types.js
test.example.js
1 change: 1 addition & 0 deletions packages/turf/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
turf.js
turf.min.js
test.example.js
3 changes: 3 additions & 0 deletions packages/turf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"scripts": {
"test": "node test.js",
"posttest": "echo \"manually execute => node test.example.js\"",
"prepublish": "npm run build",
"build": "browserify index.js -s turf > turf.js && uglifyjs turf.js -c -m > turf.min.js",
"size": "browserify index.js --full-paths -s turf | uglifyjs -c -m | discify | hcat"
Expand Down Expand Up @@ -141,6 +142,8 @@
"devDependencies": {
"browserify": "~9.0.3",
"disc": "^1.3.2",
"documentation": "^4.0.0-rc.1",
"glob": "^7.1.2",
"hcat": "0.0.5",
"jsdoc": "^3.3.0-beta1",
"tape": "^4.7.0",
Expand Down
72 changes: 72 additions & 0 deletions packages/turf/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const test = require('tape');
const documentation = require('documentation');

// Helpers
const directory = path.join(__dirname, '..');
Expand Down Expand Up @@ -127,3 +129,73 @@ test('turf - parsing dependencies from index.js', t => {
}
t.end();
});

/**
* =========================
* Builds => test.example.js
* =========================
*
* To-Do
* -----
* - [ ] Fix @example errors
* - [ ] Add 'node test.example' in package.json => scripts => posttest
* - [ ] Include `addToMap` validation tests
*/

// File Paths
const testFilePath = path.join(__dirname, 'test.example.js');
const turfModulesPath = path.join(__dirname, '..', 'turf-*', 'index.js');

// Test Strings
const requireString = `const test = require('tape');
const turf = require('./');
`;

/**
* Generate Test String
*
* @param {Object} turfFunction Documentation function object
* @param {Object} example Documentation example object
* @returns {string} Test String
*/
function testString(turfFunction, example) {
const turfName = turfFunction.name;
const testFunctionName = turfName + 'Test';
return `
test('turf-${turfName}', t => {
const ${testFunctionName} = () => {
${example.description}
}
${testFunctionName}();
t.pass('${turfName}');
t.end();
});
`;
}

// Iterate over each module and retrieve @example to build tests from them
glob(turfModulesPath, (err, files) => {
if (err) throw err;

// Read each JSDocs from index.js files
documentation.build(files, {}).then(turfFunctions => {
if (err) throw err;

// Write header of test.js
const writeableStream = fs.createWriteStream(testFilePath);
writeableStream.write(requireString);
writeableStream.on('error', err => { throw err; });

// Retrieve @example
turfFunctions.forEach(turfFunction => {
if (turfFunction.examples) {

// Save to test.js
turfFunction.examples.forEach(example => {
writeableStream.write(testString(turfFunction, example));
});
}
});
writeableStream.end();
});
});

3 comments on commit 46756c2

@stebogit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 😍 👏 @DenisCarriere

@DenisCarriere
Copy link
Member Author

@DenisCarriere DenisCarriere commented on 46756c2 Jul 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stebogit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, then 😮 😍 👏 @jvrousseau! 😂
It's great to learn about so many tools and tricks every day. Thanks everybody! 😜

Please sign in to comment.