Skip to content

Commit

Permalink
[FIX] deps, deepcloseto, typedarray + validate test correctly copied
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Jul 14, 2015
1 parent 734c52a commit 0d9ffbb
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 85 deletions.
7 changes: 7 additions & 0 deletions elementwise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ var Generator = yeoman.generators.Base.extend({
mkdirp( 'examples' );
mkdirp( 'lib' );
mkdirp( 'test' );
mkdirp( 'test/utils');
mkdirp( 'docs/img');
}, // end METHOD mkdirs()

Expand Down Expand Up @@ -356,6 +357,7 @@ var Generator = yeoman.generators.Base.extend({
'noInputs': this.noInputs
};

this.copy( 'test/utils/_deepcloseto.js', 'test/utils/deepcloseto.js' );
this.fs.copyTpl(
this.templatePath( 'test/_test.accessor.js' ),
this.destinationPath( 'test/test.accessor.js' ),
Expand Down Expand Up @@ -388,6 +390,11 @@ var Generator = yeoman.generators.Base.extend({
);
this.fs.copyTpl(
this.templatePath( 'test/_test.typedarray.js' ),
this.destinationPath( 'test/test.typedarray.js' ),
context
);
this.fs.copyTpl(
this.templatePath( 'test/_test.validate.js' ),
this.destinationPath( 'test/test.validate.js' ),
context
);
Expand Down
12 changes: 11 additions & 1 deletion elementwise/templates/lib/_accessor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
'use strict';
<% if ( noInputs !== 'One' ) { %>
// MODULES //

var isArrayLike = require( 'validate.io-array-like' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
isTypedArrayLike = require( 'validate.io-typed-array-like' );

<% } %>

// FUNCTIONS //

Expand Down Expand Up @@ -46,7 +54,9 @@ function <%= functionName %>( out, arr, y, clbk ) {
i,
arrVal, yVal;

if ( isTypedArrayLike( y ) ) {
if ( isMatrixLike( y ) ) {
throw new Error( '<%= functionName %>()::invalid input argument. `y` has to be an array or scalar.' );
} else if ( isTypedArrayLike( y ) ) {
if ( len !== y.length ) {
throw new Error( '<%= functionName %>()::invalid input argument. Input arrays must have the same length.' );
}
Expand Down
1 change: 1 addition & 0 deletions elementwise/templates/lib/_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// MODULES //

var isArrayLike = require( 'validate.io-array-like' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
isTypedArrayLike = require( 'validate.io-typed-array-like' );

<% } %>
Expand Down
6 changes: 5 additions & 1 deletion elementwise/templates/lib/_deepset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var deepSet = require( 'utils-deep-set' ).factory,
deepGet = require( 'utils-deep-get' ).factory,
<% if ( noInputs !== 'One' ) { %>
isArrayLike = require( 'validate.io-array-like' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
isTypedArrayLike = require( 'validate.io-typed-array-like' ),
<% } %>
<%= functionName.toUpperCase() %> = require( './number.js' );
Expand Down Expand Up @@ -70,7 +71,10 @@ function <%= functionName %>( x, y, path, sep ) {
if ( len ) {
dget = deepGet( path, opts );
dset = deepSet( path, opts );
if ( isTypedArrayLike( y ) ) {
if ( isMatrixLike( y ) ) {
throw new Error( '<%= functionName %>()::invalid input argument. `y` has to be an array or scalar.' );
} else if ( isTypedArrayLike( y ) ) {
for ( i = 0; i < len; i++ ) {
v = dget( x[ i ] );
if ( typeof v === 'number' ) {
Expand Down
16 changes: 10 additions & 6 deletions elementwise/templates/lib/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ function <%= functionName %>( x, y, options ) {
out,
dt,
d;
if ( arguments.length < 2 ) {
throw new Error( '<%= functionName %>()::`y` argument is missing.' );
}
// Handle cases where first argument is a number
if ( isNumber( x ) || isnan( x ) ) {
for ( var key in options ) {
Expand All @@ -168,7 +172,7 @@ function <%= functionName %>( x, y, options ) {
if ( !isNumber( y ) ) {
return NaN;
}
return <%= functionName %>6( x, y );
return <%= functionName %>1( x, y );
}
if ( arguments.length > 2 ) {
err = validate( opts, options );
Expand All @@ -189,7 +193,7 @@ function <%= functionName %>( x, y, options ) {
} else {
out = x;
}
return <%= functionName %>4( out, x, y );
return <%= functionName %>5( out, x, y );
}
if ( isTypedArrayLike( x ) ) {
if ( opts.copy === false ) {
Expand All @@ -202,13 +206,13 @@ function <%= functionName %>( x, y, options ) {
}
out = new ctor( x.length );
}
return <%= functionName %>5( out, x, y );
return <%= functionName %>6( out, x, y );
}
if ( isArrayLike( x ) ) {
// Handle deepset first...
if ( opts.path ) {
opts.sep = opts.sep || '.';
return <%= functionName %>3( x, y, opts.path, opts.sep );
return <%= functionName %>4( x, y, opts.path, opts.sep );
}
// Handle regular and accessor arrays next...
if ( opts.copy === false ) {
Expand All @@ -225,9 +229,9 @@ function <%= functionName %>( x, y, options ) {
out = new Array( x.length );
}
if ( opts.accessor ) {
return <%= functionName %>2( out, x, y, opts.accessor );
return <%= functionName %>3( out, x, y, opts.accessor );
}
return <%= functionName %>1( out, x, y );
return <%= functionName %>2( out, x, y );
}
return NaN;
} // end FUNCTION <%= functionName %>()
Expand Down
7 changes: 7 additions & 0 deletions elementwise/templates/lib/_typedarray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'use strict';
<% if ( noInputs !== 'One' ) { %>
// MODULES //

var isArrayLike = require( 'validate.io-array-like' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
isTypedArrayLike = require( 'validate.io-typed-array-like' );

<% } %>
// FUNCTIONS //

var <%= functionName.toUpperCase() %> = require( './number.js' );
Expand Down
48 changes: 19 additions & 29 deletions elementwise/templates/test/_test.accessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
var // Expectation library:
chai = require( 'chai' ),

// Deep close to:
deepCloseTo = require( './utils/deepcloseto.js' ),

// Module to be tested:
<%= functionName %> = require( './../lib/accessor.js' );

Expand All @@ -25,7 +28,7 @@ describe( 'accessor <%= functionName %>', function tests() {
});
<% if ( noInputs === 'One' ) { %>
it( 'should evaluate the <%= functionName %> function using an accessor', function test() {
var data, actual, expected, i;
var data, actual, expected;

data = [
{'x':-3},
Expand All @@ -44,9 +47,7 @@ describe( 'accessor <%= functionName %>', function tests() {

];

for ( i = 0; i < actual.length; i++ ) {
assert.closeTo( actual[ i ], expected[ i ], 1e-7 );
}
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue( d ) {
return d.x;
Expand All @@ -69,17 +70,15 @@ describe( 'accessor <%= functionName %>', function tests() {
[14,-3]
];

// Evaluated on Wolfram Alpha:
expected = [

];

actual = new Array( data.length );
actual = <%= functionName %>( actual, data, getValue2 );

for ( i = 0; i < actual.length; i++ ) {
assert.closeTo( actual[ i ], expected[ i ], 1e-7 );
}
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue2( d ) {
return d[ 1 ];
}
Expand Down Expand Up @@ -126,13 +125,10 @@ describe( 'accessor <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, 2, getValue );

expected = [
0,
1,
4,
9

];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue( d ) {
return d.x;
Expand Down Expand Up @@ -161,21 +157,18 @@ describe( 'accessor <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y, getValue );

expected = [
1,
1,
4,
27

];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue( d, i ) {
return d.x;
}

});

it( 'should perform an element-wise multiplication of another object array using an accessor', function test() {
it( 'should evaluate the <%= functionName %> function for two object arrays using an accessor', function test() {
var data, actual, expected, y;

data = [
Expand All @@ -196,13 +189,10 @@ describe( 'accessor <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y, getValue );

expected = [
1,
1,
4,
27

];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue( d, i, j ) {
if ( j === 0 ) {
Expand Down Expand Up @@ -234,23 +224,23 @@ describe( 'accessor <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, 1, getValue );

expected = [ 1, NaN, 3 ];
assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

// single non-numeric value
y = false;
actual = new Array( data.length );
actual = <%= functionName %>( actual, data, y, getValue );
expected = [ NaN, NaN, NaN ];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

// numeric array
y = [ 1, 2, 3 ];
actual = new Array( data.length );
actual = <%= functionName %>( actual, data, y, getValue );
expected = [ 1, NaN, 27 ];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue( d, i ) {
return d.x;
Expand All @@ -262,7 +252,7 @@ describe( 'accessor <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y, getValue );
expected = [ 1, NaN, 27 ];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

// object array
y = [
Expand All @@ -274,7 +264,7 @@ describe( 'accessor <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y, getValue2 );
expected = [ 1, NaN, 27 ];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

function getValue2( d, i, j ) {
if ( j === 0 ) {
Expand Down
24 changes: 11 additions & 13 deletions elementwise/templates/test/_test.array.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
var // Expectation library:
chai = require( 'chai' ),

// Deep close to:
deepCloseTo = require( './utils/deepcloseto.js' ),

// Module to be tested:
<%= functionName %> = require( './../lib/array.js' );

Expand Down Expand Up @@ -51,9 +54,8 @@ describe( 'array <%= functionName %>', function tests() {

];

for ( i = 0; i < actual.length; i++ ) {
assert.closeTo( actual[ i ], expected[ i ], 1e-7 );
}
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

});

it( 'should return an empty array if provided an empty array', function test() {
Expand Down Expand Up @@ -94,7 +96,7 @@ describe( 'array <%= functionName %>', function tests() {
25
];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

// Typed arrays...
data = new Int32Array( data );
Expand All @@ -103,7 +105,7 @@ describe( 'array <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, 2 );
expected = new Int32Array( expected );

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );
});

it( 'should evaluate the function when y is an array', function test() {
Expand All @@ -129,14 +131,10 @@ describe( 'array <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y );

expected = [
1,
1,
4,
27,
256

];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

// Typed arrays...
data = new Int32Array( data );
Expand All @@ -145,7 +143,7 @@ describe( 'array <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y );
expected = new Int32Array( expected );

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );
});

it( 'should return an empty array if provided an empty array', function test() {
Expand Down Expand Up @@ -186,7 +184,7 @@ describe( 'array <%= functionName %>', function tests() {
actual = <%= functionName %>( actual, data, y );
expected = [ 1, NaN, 27 ];

assert.deepEqual( actual, expected );
assert.isTrue( deepCloseTo( actual, expected, 1e-7 ) );

});

Expand Down

0 comments on commit 0d9ffbb

Please sign in to comment.