/**
* @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 tape = require( 'tape' );
var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' );
var Symbol = require( '@stdlib/symbol/ctor' );
var objectKeys = require( './../../keys' );
var getSymbols = require( './../../property-symbols' );
var defineProperty = require( './../../define-property' );
var propertyDescriptorsIn = require( './../lib' );


// TESTS //

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

tape( 'the function returns an object\'s own and inherited property descriptors', function test( t ) {
	var expected;
	var actual;
	var keys;
	var obj;
	var i;

	function Foo() {
		this.beep = 'boop';
		this.a = {
			'b': 'c'
		};
		defineProperty( this, 'c', {
			'configurable': false,
			'enumerable': false,
			'writable': false,
			'value': 'd'
		});
		return this;
	}

	Foo.prototype.foo = [ 'bar' ];
	defineProperty( Foo.prototype, 'boop', {
		'configurable': false,
		'enumerable': false,
		'writable': false,
		'value': 'beep'
	});

	obj = new Foo();

	expected = {
		'beep': {
			'configurable': true,
			'enumerable': true,
			'writable': true,
			'value': 'boop'
		},
		'a': {
			'configurable': true,
			'enumerable': true,
			'writable': true,
			'value': obj.a
		},
		'c': {
			'configurable': false,
			'enumerable': false,
			'writable': false,
			'value': 'd'
		},
		'foo': {
			'configurable': true,
			'enumerable': true,
			'writable': true,
			'value': obj.foo
		},
		'boop': {
			'configurable': false,
			'enumerable': false,
			'writable': false,
			'value': 'beep'
		}
	};
	keys = objectKeys( expected );

	actual = propertyDescriptorsIn( obj );
	t.strictEqual( objectKeys( actual ).length >= keys.length, true, 'has expected number of descriptors' );
	for ( i = 0; i < keys.length; i++ ) {
		t.deepEqual( actual[ keys[i] ], expected[ keys[i] ], 'contains property descriptor for property: '+keys[i] );
	}
	t.end();
});

tape( 'the function returns an object\'s own and inherited property descriptors (symbols)', function test( t ) {
	var expected;
	var actual;
	var keys;
	var sym;
	var obj;
	var i;

	if ( hasSymbolSupport() === false ) {
		t.pass( 'environment lacks Symbol support' );
		return t.end();
	}
	sym = [ Symbol( 'a' ), Symbol( 'b' ), Symbol( 'c' ), Symbol( 'd' ) ];

	function Foo() {
		this[ sym[0] ] = 0;
		defineProperty( this, sym[ 1 ], {
			'configurable': false,
			'enumerable': false,
			'writable': false,
			'value': 1
		});
		this[ sym[2] ] = 2; // duplicate symbol
		return this;
	}

	Foo.prototype[ sym[ 2 ] ] = -1; // duplicate symbol
	defineProperty( Foo.prototype, sym[ 3 ], {
		'configurable': false,
		'enumerable': false,
		'writable': false,
		'value': 3
	});

	obj = new Foo();

	expected = {};
	expected[ sym[0] ] = {
		'configurable': true,
		'enumerable': true,
		'writable': true,
		'value': 0
	};
	expected[ sym[1] ] = {
		'configurable': false,
		'enumerable': false,
		'writable': false,
		'value': 1
	};
	expected[ sym[2] ] = {
		'configurable': true,
		'enumerable': true,
		'writable': true,
		'value': 2
	};
	expected[ sym[3] ] = {
		'configurable': false,
		'enumerable': false,
		'writable': false,
		'value': 3
	};
	keys = getSymbols( expected );

	actual = propertyDescriptorsIn( obj );
	t.strictEqual( getSymbols( actual ).length >= keys.length, true, 'has expected number of descriptors' );
	for ( i = 0; i < keys.length; i++ ) {
		t.deepEqual( actual[ keys[i] ], expected[ keys[i] ], 'contains property descriptor for property: '+keys[i].toString() );
	}
	t.end();
});

tape( 'the function returns an empty object if provided `null` or `undefined`', function test( t ) {
	var expected;
	var actual;
	var values;
	var i;

	values = [
		void 0,
		null
	];
	expected = {};

	for ( i = 0; i < values.length; i++ ) {
		actual = propertyDescriptorsIn( values[ i ] );
		t.deepEqual( actual, expected, 'returns expected value when provided '+values[ i ] );
	}
	t.end();
});
