Skip to content

Commit

Permalink
more tests; add info to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Oct 8, 2016
1 parent a9815db commit 0880df4
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 85 deletions.
29 changes: 22 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ helpers.existsSync = function (filename) {
* })
*
*/
helpers.makeDirectories = function (outPath, nextStep) {
helpers.makeDirectories = function (outPath, cb) {
mkdirp(path.dirname(outPath), function (err) {
nextStep(err)
cb(err)
})
}

Expand All @@ -249,9 +249,9 @@ helpers.makeDirectoriesSync = function (outPath) {
/**
* Test whether a file name has the given extension
*
* @param {String} fileName The name of the file
* @param {String} extension The extension to test
* @returns {Boolean} whether The extension matched or not
* @param {String} fileName The name of the file.
* @param {String} extension The extension to test. An empty string will match a file with no extension.
* @returns {Boolean} whether The extension matched or not.
*
* @example
* var matches = io.extensionMatches('path/to/data.tsv', 'tsv')
Expand Down Expand Up @@ -701,15 +701,30 @@ shorthandReaders.readCsv = function (path, cb) {
* Synchronously read a comma-separated value file.
*
* @param {String} fileName the name of the file
* @param {Object} [options] Optional options, see below
* @param {Function} [options.readOptions] A conversion called once for each row (header row skipped). See example or d3-dsv documentation for details.
* @returns {Array} the contents of the file as JSON
*
* @example
* var data = io.readCsvSync('path/to/data.csv')
* console.log(data) // Json data
*
* // Transform values on load
* var data = io.readCsvSync('path/to/data.csv', {
* readOptions: function (row, i, columns)
* console.log(columns) // [ 'name', 'occupation', 'height' ]
* row.height = +row.height // Convert this value to a number
* return row
* })
* console.log(data) // Json data with casted values
*
*/
shorthandReaders.readCsvSync = function (path) {
return readers.readDataSync(path, {parser: parsers.csv})
shorthandReaders.readCsvSync = function (path, opts_) {
var readOptions
if (_.isObject(opts_) && opts_.readOptions) {
readOptions = opts_.readOptions
}
return readers.readDataSync(path, {parser: parsers.csv, readOptions: readOptions})
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"http-server": "^0.9.0",
"istanbul": "^0.4.5",
"mocha": "^3.1.0",
"rimraf": "^2.5.4",
"standard": "^8.3.0"
}
}
214 changes: 136 additions & 78 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* global describe, it */

var path = require('path')
var io = require('../lib/index')
var chai = require('chai')
var assert = chai.assert
var path = require('path')
var _ = require('underscore')
var rimraf = require('rimraf')

function testDataPath (name) {
return path.join(__dirname, 'data', name)
Expand Down Expand Up @@ -228,22 +229,23 @@ describe('discernFileFormatter()', function () {
describe('existsSync()', function () {
var dir = path.join(__dirname, 'data', 'csv')
describe('exists', function () {
it('should be equal', function () {
it('should be true', function () {
var exists = io.existsSync(path.join(dir, 'basic.csv'))
assert.equal(exists, true)
})
})
describe('does not exist', function () {
it('should be equal', function () {
it('should be false', function () {
var exists = io.existsSync(path.join(dir, 'doesnt-exist.csv'))
assert.equal(exists, false)
})
})
})

// TODO, how would one test for an error in stat'ing the file?
describe('exists()', function () {
describe('exists', function () {
it('should be equal', function (done) {
it('should be true', function (done) {
io.exists(testDataPath('csv/basic.csv'), function (err, exists) {
if (err) {
console.error(err)
Expand All @@ -254,7 +256,7 @@ describe('exists()', function () {
})
})
describe('does not exist', function () {
it('should be equal', function (done) {
it('should be false', function (done) {
io.exists(testDataPath('csv/doesnt-exist.csv'), function (err, exists) {
if (err) {
console.error(err)
Expand All @@ -266,17 +268,112 @@ describe('exists()', function () {
})
})

describe('readCsvSync()', function () {
describe('empty', function () {
it('should be empty', function () {
assert.lengthOf(io.readCsvSync(testDataPath('csv/empty.csv')), 0)
describe('makeDirectories()', function () {
it('should make multiple directories', function (done) {
var filePath = ['test', 'tmp-md', 'one', 'two', 'three', 'file.txt']
io.makeDirectories(filePath.join(path.sep), function (err) {
assert.equal(err, null)
filePath.pop()
assert.equal(io.existsSync(filePath.join(path.sep)), true)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})
})

describe('basic', function () {
it('should match expected json', function () {
var json = io.readCsvSync(testDataPath('csv/basic.csv'))
assertBasicValid(json, true)
describe('makeDirectoriesSync()', function () {
it('should make multiple directories', function (done) {
var filePath = ['test', 'tmp-mds', 'one', 'two', 'three', 'file.txt']
io.makeDirectoriesSync(filePath.join(path.sep))
filePath.pop()
assert.equal(io.existsSync(filePath.join(path.sep)), true)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})

describe('extensionMatches()', function () {
it('should match the given extension', function () {
assert.equal(io.extensionMatches(testDataPath('csv/basic.csv'), 'csv'), true)
})

it('should not match the given extension', function () {
assert.equal(io.extensionMatches(testDataPath('csv/basic.csv'), 'tsv'), false)
})

it('should match no extension', function () {
assert.equal(io.extensionMatches(testDataPath('csv/basic'), ''), true)
})
})

// matchRegExp
// matches

describe('extend()', function () {
describe('shallow', function () {
it('should be equal', function () {
var mergedObj = io.extend({}, {name: 'indian-ocean'}, {alias: 'io'})
assert.equal(JSON.stringify(mergedObj), JSON.stringify({name: 'indian-ocean', alias: 'io'}))
})
})

describe('deep', function () {
it('should be equal', function () {
var object1 = {
apple: 0,
banana: { weight: 52, price: 100 },
cherry: 97
}
var object2 = {
banana: { price: 200 },
almond: 100
}
io.extend(true, object1, object2)

var desiredResult = {
apple: 0,
banana: {
weight: 52,
price: 200
},
cherry: 97,
almond: 100
}

assert.equal(JSON.stringify(object1), JSON.stringify(desiredResult))
})
})
})

describe('deepExtend()', function () {
describe('deep', function () {
it('should be equal', function () {
var object1 = {
apple: 0,
banana: { weight: 52, price: 100 },
cherry: 97
}
var object2 = {
banana: { price: 200 },
almond: 100
}
io.deepExtend(object1, object2)

var desiredResult = {
apple: 0,
banana: {
weight: 52,
price: 200
},
cherry: 97,
almond: 100
}

assert.equal(JSON.stringify(object1), JSON.stringify(desiredResult))
})
})
})
Expand Down Expand Up @@ -383,7 +480,33 @@ describe('readJsonSync()', function () {
}, Error)
})
})
})

describe('readCsvSync()', function () {
describe('empty', function () {
it('should be empty', function () {
assert.lengthOf(io.readCsvSync(testDataPath('csv/empty.csv')), 0)
})
})

describe('basic', function () {
it('should match expected json', function () {
var json = io.readCsvSync(testDataPath('csv/basic.csv'))
assertBasicValid(json, true)
})
})

describe('basic casted', function () {
it('should match expected json', function () {
var json = io.readCsvSync(testDataPath('csv/basic.csv'), {
readOptions: function (row, i, columns) {
row.height = +row.height
return row
}
})
assertBasicValid(json)
})
})
})

describe('readPsvSync()', function () {
Expand Down Expand Up @@ -781,68 +904,3 @@ describe('readdirFilterSync()', function () {
})
})
})

describe('extend()', function () {
describe('shallow', function () {
it('should be equal', function () {
var mergedObj = io.extend({}, {name: 'indian-ocean'}, {alias: 'io'})
assert.equal(JSON.stringify(mergedObj), JSON.stringify({name: 'indian-ocean', alias: 'io'}))
})
})

describe('deep', function () {
it('should be equal', function () {
var object1 = {
apple: 0,
banana: { weight: 52, price: 100 },
cherry: 97
}
var object2 = {
banana: { price: 200 },
almond: 100
}
io.extend(true, object1, object2)

var desiredResult = {
apple: 0,
banana: {
weight: 52,
price: 200
},
cherry: 97,
almond: 100
}

assert.equal(JSON.stringify(object1), JSON.stringify(desiredResult))
})
})
})

describe('deepExtend()', function () {
describe('deep', function () {
it('should be equal', function () {
var object1 = {
apple: 0,
banana: { weight: 52, price: 100 },
cherry: 97
}
var object2 = {
banana: { price: 200 },
almond: 100
}
io.deepExtend(object1, object2)

var desiredResult = {
apple: 0,
banana: {
weight: 52,
price: 200
},
cherry: 97,
almond: 100
}

assert.equal(JSON.stringify(object1), JSON.stringify(desiredResult))
})
})
})

0 comments on commit 0880df4

Please sign in to comment.