test.validate.js

Create test.validate.js contents.

Usage

var create = require( '@stdlib/_tools/scaffold/test-validate-js' );

create( code )

Returns test.validate.js contents based on a code string or Buffer with options annotations.

var code = [
    '/**',
    '* Validates function options.',
    '*',
    '* @private',
    '* @param {Object} opts - destination object',
    '* @param {Options} options - function options',
    '* @param {string} [options.sep] - separator',
    '* @returns {(Error|null)} null or an error object',
    '*/'
];
code = code.join( '\n' );

var tests = create( code );
/* returns
    'use strict';

    // MODULES //

    var tape = require( 'tape' );
    var validate = require( './../lib/validate.js' );


    // TESTS //

    tape( 'main export is a function', function test( t ) {
        t.ok( true, __filename );
        t.equal( typeof validate, 'function', 'main export is a function' );
        t.end();
    });

    tape( 'the function returns an error if not provided an object', function test( t ) {
        var values;
        var err;
        var i;

        values = [
            '5',
            5,
            NaN,
            true,
            false,
            null,
            void 0,
            [],
            function noop() {}
        ];

        for ( i = 0; i < values.length; i++ ) {
            err = validate( {}, values[i] );
            t.equal( err instanceof TypeError, true, 'returns a type error when provided '+values[i] );
        }
        t.end();
    });

    tape( 'the function returns an error if provided a `sep` option which is not a string', function test( t ) {
        var values;
        var err;
        var i;

        values = [
            5,
            true,
            false,
            void 0,
            null,
            NaN,
            [],
            {},
            function noop() {}
        ];

        for ( i = 0; i < values.length; i++ ) {
            err = validate( {}, {
                'sep': values[i]
            });
            t.equal( err instanceof TypeError, true, 'returns a type error when provided '+values[i] );
        }
        t.end();
    });

    tape( 'the function returns `null` if all options are valid', function test( t ) {
        var options;
        var opts;
        var err;

        options = {};
        opts = {
            'sep': 'beep'
        };

        err = validate( options, opts );
        t.equal( err, null, 'returns null' );

        t.deepEqual( options, opts, 'sets options' );
        t.end();
    });

    tape( 'the function ignores unrecognized/unsupported options', function test( t ) {
        var options;
        var opts;
        var err;

        options = {};
        opts = {
            'beep': true,
            'boop': 3.14,
            'foo': 'bar'
        };

        err = validate( options, opts );
        t.equal( err, null, 'returns null' );

        t.deepEqual( options, {}, 'does not set unrecognized options' );
        t.end();
    });
*/

Examples

var join = require( 'path' ).join;
var readFile = require( '@stdlib/fs/read-file' ).sync;
var create = require( '@stdlib/_tools/scaffold/test-validate-js' );

var file = readFile( join( __dirname, 'fixtures', 'validate.js.txt' ) );

console.log( create( file ) );

CLI

Usage

Usage: test-validate-js [options] [<code>]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.

Examples

$ test-validate-js <code>
<tests>

To use as a standard stream,

$ echo <code> | test-validate-js
<tests>