/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var join = require( 'path' ).join;
var tape = require( 'tape' );
var getKeys = require( 'object-keys' ).shim();
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
var isRegExp = require( '@stdlib/assert/is-regexp' );
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var headerRegExp = require( './../lib' );


// VARIABLES //

var opts = {
	'skip': IS_BROWSER
};
var LANGS = [
	'AWK',
	'awk',
	'C',
	'c',
	'C++',
	'cpp',
	'CSS',
	'css',
	'Fortran',
	'f',
	'GYP',
	'gyp',
	'GYPI',
	'gypi',
	'h',
	'hpp',
	'HTML',
	'html',
	'INI',
	'ini',
	'Julia',
	'jl',
	'JavaScript',
	'js',
	'Markdown',
	'markdown',
	'md',
	'mdown',
	'Makefile',
	'mk',
	'make',
	'Python',
	'py',
	'R',
	'shell',
	'sh',
	'bash',
	'YAML',
	'yml'
];
var FIXTURES = {
	'file.awk.txt': 'awk',
	'file.bash.txt': 'bash',
	'file.c.txt': 'c',
	'file.cpp.txt': 'cpp',
	'file.css.txt': 'css',
	'file.f.txt': 'fortran',
	'file.gyp.txt': 'gyp',
	'file.gypi.txt': 'gypi',
	'file.h.txt': 'h',
	'file.html.txt': 'html',
	'file.ini.txt': 'ini',
	'file.js.txt': 'javascript',
	'file.julia.txt': 'julia',
	'file.md.txt': 'markdown',
	'file.node.txt': 'javascript',
	'file.py.txt': 'python',
	'file.r.txt': 'r',
	'file.yml.txt': 'yaml'
};


// TESTS //

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

tape( 'the function throws an error if not provided a supported SPDX license identifier', function test( t ) {
	var values;
	var i;

	values = [
		'5',
		'apache',
		'apache 2.0',
		'apache-2.0',
		'mit',
		'isc',
		'unlicense',
		'cc0',
		5,
		NaN,
		true,
		false,
		null,
		void 0,
		[],
		{},
		function noop() {}
	];
	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), Error, 'throws an error when provided ' + values[ i ] );
	}
	t.end();

	function badValue( value ) {
		return function badValue() {
			headerRegExp( value, 'js' );
		};
	}
});

tape( 'the function throws an error if not provided a file type argument which is a string', function test( t ) {
	var values;
	var i;

	values = [
		5,
		NaN,
		true,
		false,
		null,
		void 0,
		[],
		{},
		function noop() {}
	];
	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[ i ] );
	}
	t.end();

	function badValue( value ) {
		return function badValue() {
			headerRegExp( 'Apache-2.0', value );
		};
	}
});

tape( 'the function returns `null` if provided an unsupported file type', opts, function test( t ) {
	var out = headerRegExp( 'Apache-2.0', 'adkfljadlkfjas' );
	t.strictEqual( out, null, 'returns expected value' );
	t.end();
});

tape( 'the function returns `null` if provided an unsupported file type (options)', opts, function test( t ) {
	var out = headerRegExp( 'Apache-2.0', 'adkfljadlkfjas', {} );
	t.strictEqual( out, null, 'returns expected value' );
	t.end();
});

tape( 'the function returns a license header regular expression', opts, function test( t ) {
	var out;
	var i;

	for ( i = 0; i < LANGS.length; i++ ) {
		out = headerRegExp( 'Apache-2.0', LANGS[ i ] );
		t.strictEqual( isRegExp( out ), true, 'returns a regular expression for ' + LANGS[ i ] );
	}
	t.end();
});

tape( 'the function returns a license header regular expression which can be used to test for a license header', opts, function test( t ) {
	var fpath;
	var fopts;
	var file;
	var keys;
	var re;
	var i;

	keys = getKeys( FIXTURES );
	fopts = {
		'encoding': 'utf8'
	};
	for ( i = 0; i < keys.length; i++ ) {
		// Read a fixture file:
		fpath = join( __dirname, 'fixtures', keys[ i ] );
		file = readFileSync( fpath, fopts );

		// Generate a regular expression:
		re = headerRegExp( 'Apache-2.0', FIXTURES[ keys[i] ] );

		// Test that the regular expression positively validates:
		t.strictEqual( re.test( file ), true, 'positively validates (' + keys[ i ] + ')' );

		// Test that the regular expression negatively validates:
		t.strictEqual( re.test( 'beep boop bop' ), false, 'negatively validates (' + FIXTURES[ keys[i] ] + ')' );
	}
	t.end();
});

@@ -3345,252 +3345,8 @@
 );%0A%0A
-tape( 'the function returns %60null%60 if provided an unsupported file type (options)', opts, function test( t ) %7B%0A%09var out = headerRegExp( 'Apache-2.0', 'adkfljadlkfjas', %7B%7D );%0A%09t.strictEqual( out, null, 'returns expected value' );%0A%09t.end();%0A%7D);%0A%0A
 tape

Remove stray test