added parentOf(..) / childOf(..) / related(..) tests...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-08-09 14:05:30 +03:00
parent 0cdab633c7
commit 00c17b3ce8
4 changed files with 65 additions and 2 deletions

View File

@ -136,6 +136,7 @@ class B extends A {
- [`parent(..)`](#parent)
- [`parentProperty(..)`](#parentproperty)
- [`parentCall(..)`](#parentcall)
- [`parentOf(..)` / `childOf(..)` / `related(..)`](#parentof--childof--related)
- [`mixin(..)`](#mixin)
- [`mixins(..)`](#mixins)
- [`hasMixin(..)`](#hasmixin)
@ -712,6 +713,22 @@ parentCall(<prototype>, '__call__', <this>)
See [`parent(..)`](#parent) and [`sources(..)`](#sources) for more details.
### `parentOf(..)` / `childOf(..)` / `related(..)`
Test if a is parent of b and/or vice-versa.
```
parentOf(<parent>, <child>)
-> <bool>
childOf(<child>, <parent>)
-> <bool>
related(<a>, <b>)
-> <bool>
```
These are similar to `instanceof` but will test if the two objects are in the
same prototype chain and in case of `parentOf(..)`/`childOf(..)` in what order.
### `mixin(..)`

View File

@ -510,6 +510,31 @@ function(proto, name, that, ...args){
: undefined }
// Test if child is related to parent...
//
// parentOf(parent, child)
// -> bool
//
//
// NOTE: this is like a instanceof b but within the prototype chain
var parentOf =
module.parentOf =
function(parent, child){
return new Set(sources(child)).has(parent) }
// Reverse of parentOf(..)
var childOf =
module.childOf =
function(child, parent){
return parentOf(parent, child) }
var related =
module.related =
function(a, b){
return parentOf(a, b)
|| parentOf(b, a) }
//---------------------------------------------------------------------
// Mixin utils...

View File

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

23
test.js
View File

@ -150,6 +150,27 @@ module.setups = {
}), `inherit (gen1)`),
B: B = assert(object.C('B', A, {
// XXX constructor methods...
testRelations: function(){
assert(object.parentOf(A, this),
'parentOf(A, B): expected to be true')
assert(object.childOf(this, A),
'childOf(B, A): expected to be true')
assert(!object.parentOf(X, this),
'parentOf(X, B): expected to be false')
assert(object.parentOf(A, E),
'parentOf(A, E): expected to be true')
assert(object.childOf(E, A),
'childOf(E, A): expected to be true')
assert(object.related(A, E) && object.related(E, A),
'related(A, E) and related(E, A): expected to be true')
assert(!object.related(X, E)
&& !object.related(E, X),
'related(X, E) and related(E, X): expected to be flase')
},
}, {
get prop(){
return 'B.prop' },
@ -737,7 +758,7 @@ object.Constructor('Assert', {
path
: [path])
],
stats,
this.stats,
this.verbose) },
pop: function(){
return this.constructor(