minor bugfix + doc tests...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-07-02 21:44:38 +03:00
parent 51f8335f0c
commit b2741f958f
3 changed files with 71 additions and 7 deletions

View File

@ -292,6 +292,7 @@ function(obj){
throw new Error(`create(..): invalid name: "${name}"`) } } throw new Error(`create(..): invalid name: "${name}"`) } }
// calable... // calable...
if(typeof(obj) == 'function'){ if(typeof(obj) == 'function'){
/* c8 ignore next 9 */
var func = function(){ var func = function(){
return '__call__' in func ? return '__call__' in func ?
func.__call__(this, ...arguments) func.__call__(this, ...arguments)
@ -710,9 +711,12 @@ function(func){
: '__call__' in this ? : '__call__' in this ?
this.__call__ this.__call__
: this.__proto__) : this.__proto__)
return module.normalizeIndent(f.toString(...args)) },
/*/
return typeof(f) == 'function' ? return typeof(f) == 'function' ?
module.normalizeIndent(f.toString(...args)) module.normalizeIndent(f.toString(...args))
: undefined }, : undefined },
//*/
enumerable: false, enumerable: false,
}) })
return func } return func }

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-object", "name": "ig-object",
"version": "5.5.5", "version": "5.5.6",
"description": "", "description": "",
"main": "object.js", "main": "object.js",
"scripts": { "scripts": {

72
test.js
View File

@ -619,14 +619,18 @@ var tests = test.Tests({
var cases = test.Cases({ var cases = test.Cases({
'edge-cases': function(assert){ 'edge-cases': function(assert){
// bad names...
assert.error('Constructor(..): malformed name',
function(){
var X = object.Constructor('bad name', {}) })
assert.error('create(..): malformed name',
function(){
object.create('bad name', function(){}) })
assert.error('double __extends__ fail', function(){ assert.error('double __extends__ fail', function(){
var X = object.C('X', { var X = object.C('X',
__extends__: Object, { __extends__: Object },
}, { { __extends__: Function }) })
__extends__: Function,
})
})
// native constructor... // native constructor...
assert.array( assert.array(
@ -674,6 +678,7 @@ var cases = test.Cases({
object.values(obj, 'x', function(){ return object.STOP(555) }, true)[0] == 555, object.values(obj, 'x', function(){ return object.STOP(555) }, true)[0] == 555,
// XXX assert ignores the order here -- this should fail... // XXX assert ignores the order here -- this should fail...
'.values(.., func, true) with explicit stop value') '.values(.., func, true) with explicit stop value')
}, },
deepKeys: function(assert){ deepKeys: function(assert){
var a = { var a = {
@ -813,6 +818,61 @@ var cases = test.Cases({
assert.array(b(), ['A', 'B'], 'call') assert.array(b(), ['A', 'B'], 'call')
assert.array(c(), ['A', 'B', 'C'], 'call') assert.array(c(), ['A', 'B', 'C'], 'call')
}, },
'docs': function(assert){
var func = function(){
console.log('some string...') }
var func_w_tostring = Object.assign(
function(){
console.log('some string...') },
{ toString: function(){
return 'func_w_tostring(){ .. }' } })
assert(
object.create(func).toString() == object.normalizeIndent(func.toString()),
'create(..): function .toString() proxy (builtin).')
assert(
object.create(func_w_tostring).toString() == func_w_tostring.toString(),
'create(..): function .toString() proxy (custom).')
var callable_a =
object.Constructor('callable_a',
function(){
console.log(this.constructor.name) })()
var callable_b =
object.Constructor('callable_b', {
__call__: function(){
console.log(this.constructor.name) },
})()
var callable_c =
object.Constructor('callable_c', {
__call__: function(){
console.log(this.constructor.name) },
toString: function(){
return this.constructor.name + '.toString()' },
})()
assert(
callable_b.toString() == object.normalizeIndent(callable_b.__call__.toString()),
'toString(..) proxy to .__call__(..)')
assert(
callable_b.toString() != object.normalizeIndent(callable_b.__proto__.toString()),
'toString(..) proxy not to .__proto__.toString(..)')
assert(
callable_c.toString() == callable_c.__proto__.toString(),
'toString(..) proxy to .toString(..)')
assert(
object.create(callable_a).toString() == callable_a.toString(),
'create(..): callable .toString() proxy (func).')
assert(
object.create(callable_b).toString() == callable_b.toString(),
'create(..): callable .toString() proxy (__call__).')
assert(
object.create(callable_c).toString() == callable_c.toString(),
'create(..): callable .toString() proxy (__call__ w. custom toString).')
},
}) })