Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created November 23, 2016 16:56
Show Gist options
  • Save jhyland87/f92eac63187fb312483568d0cc9c6e2c to your computer and use it in GitHub Desktop.
Save jhyland87/f92eac63187fb312483568d0cc9c6e2c to your computer and use it in GitHub Desktop.
'use strict'
const _ = require( '../' )
const Code = require('code')
const Async = require('async')
const Lab = require('lab')
const lab = exports.lab = Lab.script()
const suite = lab.suite
const it = lab.test
const before = lab.before
const describe = lab.experiment
const after = lab.after
const expect = Code.expect
var pathTests = [
{
paths: ['path/to/stuff/foo/', '../bar','./baz.js' ],
expected: 'path/to/stuff/bar/baz.js'
},
{
paths: [ '/path/to/stuff/foo/', '../bar/../test','./baz.js' ],
expected: '/path/to/stuff/test/baz.js'
},
{
paths: [ './', '../bar','./baz.js' ],
expected: '../bar/baz.js'
},
{
paths: [ './src/', '/assets/','/js/', '/custom.js' ],
expected: './src/assets/js/custom.js'
},
{
paths: ['./src/', '/assets/','/js/', '../new-js', '/custom.js' ],
expected: './src/assets/new-js/custom.js'
},
{
paths: ['../../../', 'assets','/js/', '/custom.js' ],
expected: '../../../assets/js/custom.js'
},
{
paths: ['foo', 'bar','/baz/', '../../../' ],
expected: '.'
},
{
paths: ['./array/with/one/string.js' ],
expected: './array/with/one/string.js'
},
{
paths: './string/with/one/path.js',
expected: './string/with/one/path.js'
},
{
paths: '../../../foo/bar/baz.js',
expected: '../../../foo/bar/baz.js'
},
{
paths: ['foo', 'bar','/baz/', '../../../', '.' ],
expected: '.'
},
{
paths: ['foo', 'bar','/baz/', '../../../' ],
expected: '.'
},
{
paths: {
segments: [ './src/', '/assets/','/js/', '../new-js', '/custom.js' ],
},
expected: './src/assets/new-js/custom.js'
},
{
paths: {
segments: [ './src/', '/assets/','/js/', '../new-js', '/custom.js' ],
pathType: '/does/not/exist/',
verify: true
},
expected: Error
},
{
paths: {
segments: [ './src/', '/assets/','/js/', '../new-js', '/custom.js' ],
pathType: '/Users/jhyland/Documents',
verify: true
},
expected: './src/assets/new-js/custom.js'
},
]
describe('_.getFullPath mixin', () => {
Async.each( pathTests, function( test, callback ) {
var result, desc
if ( test.expected == Error ){
desc = 'should throw an Error exception when processing segments:'
result = function(){ _.getFullPath( test.paths ) }
}
else if ( test.expected === false ){
desc = 'should throw false when processing segments:'
result = _.getFullPath( test.paths )
}
else {
desc = 'should return "' + test.expected + '" after processing:'
result = _.getFullPath( test.paths )
}
desc += JSON.stringify( test.paths )
it( desc, done => {
if ( test.expected == Error ){
expect( result ).to.throw( Error )
}
else if ( test.expected === false ){
expect( result ).to.be.false
}
else {
expect( result ).to.not.be.empty
expect( result ).to.equal( test.expected )
}
done()
callback()
})
}, function( err ) {
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.error( 'Async test cases for _.getFullPath() threw an error:', err )
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment