mirror of
https://github.com/flynx/object.js.git
synced 2025-10-30 19:10:11 +00:00
more testing + minor cleanup...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
e5055b4fc5
commit
74fc255b12
@ -740,7 +740,10 @@ function(context, constructor, ...args){
|
|||||||
constructor.__proto__.__rawinstance__(context, ...args)
|
constructor.__proto__.__rawinstance__(context, ...args)
|
||||||
// use parent's constructor...
|
// use parent's constructor...
|
||||||
: (typeof(constructor.__proto__) == 'function'
|
: (typeof(constructor.__proto__) == 'function'
|
||||||
&& constructor.__proto__ !== (function(){}).__proto__) ?
|
// XXX for some reason if using (function(){}).__proto__
|
||||||
|
// instead of Function.prototype below coverage is
|
||||||
|
// not counted for the condition....
|
||||||
|
&& constructor.__proto__ !== Function.prototype) ?
|
||||||
Reflect.construct(constructor.__proto__, args, constructor)
|
Reflect.construct(constructor.__proto__, args, constructor)
|
||||||
// default object base...
|
// default object base...
|
||||||
: Reflect.construct(Object, [], constructor)
|
: Reflect.construct(Object, [], constructor)
|
||||||
|
|||||||
110
test.js
110
test.js
@ -115,10 +115,21 @@ module.setups = {
|
|||||||
// basic constructor and inheritance...
|
// basic constructor and inheritance...
|
||||||
//
|
//
|
||||||
// X
|
// X
|
||||||
// Y <- A <- B <- C
|
// Y <- A <- B <- C <- D <- E
|
||||||
|
//
|
||||||
|
// This will test:
|
||||||
|
// - object creation
|
||||||
|
// - basic inheritance
|
||||||
|
// - general usecase
|
||||||
|
// - .__extends__
|
||||||
|
// - method overloading
|
||||||
|
// - .parent(..)
|
||||||
|
// - .parentCall(..)
|
||||||
|
// - .parentProperty(..)
|
||||||
|
// - constructor methods (XXX not done...)
|
||||||
//
|
//
|
||||||
basic: function(assert){
|
basic: function(assert){
|
||||||
var X, Y, A, B, C
|
var X, Y, A, B, C, D, E
|
||||||
return {
|
return {
|
||||||
X: X = assert(object.Constructor('X'), `.Constructor(..)`),
|
X: X = assert(object.Constructor('X'), `.Constructor(..)`),
|
||||||
Y: Y = assert(object.C('Y', {
|
Y: Y = assert(object.C('Y', {
|
||||||
@ -132,6 +143,8 @@ module.setups = {
|
|||||||
}), `.C(..)`),
|
}), `.C(..)`),
|
||||||
|
|
||||||
A: A = assert(object.C('A', Y, {
|
A: A = assert(object.C('A', Y, {
|
||||||
|
get prop(){
|
||||||
|
return 'A.prop' },
|
||||||
method: function(){
|
method: function(){
|
||||||
var x
|
var x
|
||||||
assert(
|
assert(
|
||||||
@ -143,6 +156,8 @@ module.setups = {
|
|||||||
B: B = assert(object.C('B', A, {
|
B: B = assert(object.C('B', A, {
|
||||||
// XXX constructor methods...
|
// XXX constructor methods...
|
||||||
}, {
|
}, {
|
||||||
|
get prop(){
|
||||||
|
return 'B.prop' },
|
||||||
// XXX methods...
|
// XXX methods...
|
||||||
}), `inherit (gen2) with constructor mixin`),
|
}), `inherit (gen2) with constructor mixin`),
|
||||||
C: C = assert(object.C('C', B, {
|
C: C = assert(object.C('C', B, {
|
||||||
@ -151,9 +166,42 @@ module.setups = {
|
|||||||
assert(
|
assert(
|
||||||
(x = object.parentCall(C.prototype.method, this, ...arguments)) == 'A',
|
(x = object.parentCall(C.prototype.method, this, ...arguments)) == 'A',
|
||||||
'c.method(..): expected:', 'A', 'got:', x)
|
'c.method(..): expected:', 'A', 'got:', x)
|
||||||
|
|
||||||
|
assert(this.prop == 'B.prop',
|
||||||
|
'get property value')
|
||||||
|
// NOTE: these get "next visible" not "outside current object",
|
||||||
|
// this is intentional, the "outside" value is simply
|
||||||
|
// accessible via:
|
||||||
|
// C.prototype.prop
|
||||||
|
assert(object.parent(C.prototype, 'prop') == 'A.prop',
|
||||||
|
'get parent property value')
|
||||||
|
assert(object.parentProperty(C.prototype, 'prop').get() == 'A.prop',
|
||||||
|
'get parent property')
|
||||||
|
|
||||||
return 'C'
|
return 'C'
|
||||||
},
|
},
|
||||||
}), `inherit (gen3)`),
|
}), `inherit (gen3)`),
|
||||||
|
D: D = assert(object.C('D', {}, {
|
||||||
|
__extends__: C,
|
||||||
|
method: function(){
|
||||||
|
var x
|
||||||
|
assert(
|
||||||
|
(x = object.parentCall(D.prototype.method, this, ...arguments)) == 'C',
|
||||||
|
'c.method(..): expected:', 'C', 'got:', x)
|
||||||
|
return 'D'
|
||||||
|
},
|
||||||
|
}), '.__extends__ test'),
|
||||||
|
E: E = assert(object.C('E', {
|
||||||
|
__extends__: C,
|
||||||
|
}, {
|
||||||
|
method: function(){
|
||||||
|
var x
|
||||||
|
assert(
|
||||||
|
(x = object.parentCall(D.prototype.method, this, ...arguments)) === undefined,
|
||||||
|
'c.method(..): expected:', undefined, 'got:', x)
|
||||||
|
return 'E'
|
||||||
|
},
|
||||||
|
}), '.__extends__ test'),
|
||||||
} },
|
} },
|
||||||
|
|
||||||
// initialization...
|
// initialization...
|
||||||
@ -257,7 +305,6 @@ module.setups = {
|
|||||||
|
|
||||||
// compatibility: native constructors...
|
// compatibility: native constructors...
|
||||||
js_constructors: function(assert){
|
js_constructors: function(assert){
|
||||||
var X, Y, Z, a, b, c, d
|
|
||||||
return {
|
return {
|
||||||
Object,
|
Object,
|
||||||
Array,
|
Array,
|
||||||
@ -283,23 +330,23 @@ module.setups = {
|
|||||||
x: 'c',
|
x: 'c',
|
||||||
method: function(){
|
method: function(){
|
||||||
var x, y
|
var x, y
|
||||||
assert(arrayCmp(
|
assert.array(
|
||||||
x = object.values(c, 'x'),
|
object.values(c, 'x'),
|
||||||
y = ['c', 'a', 'b']),
|
['c', 'a', 'b'],
|
||||||
'reach all values of attr: expected:', x, 'got:'.bold.yellow, y)
|
'reach all values of attr')
|
||||||
assert(arrayCmp(
|
assert.array(
|
||||||
x = object.values(c, 'x', function(v, o){
|
object.values(c, 'x', function(v, o){
|
||||||
return v.toUpperCase() }),
|
return v.toUpperCase() }),
|
||||||
y = ['C', 'A', 'B']),
|
['C', 'A', 'B'],
|
||||||
'reach all values of attr: expected:', x, 'got:'.bold.yellow, y)
|
'reach all values of attr')
|
||||||
assert(arrayCmp(
|
assert.array(
|
||||||
x = object.sources(c, 'method'),
|
object.sources(c, 'method'),
|
||||||
// NOTE: not passing an explicit list as we need
|
// NOTE: not passing an explicit list as we need
|
||||||
// to account for mixins...
|
// to account for mixins...
|
||||||
y = object.sources(c)
|
object.sources(c)
|
||||||
.filter(function(s){
|
.filter(function(s){
|
||||||
return s.hasOwnProperty('method') })),
|
return s.hasOwnProperty('method') }),
|
||||||
'reach all values of method: expected:', y, 'got:'.bold.yellow, x)
|
'reach all values of method')
|
||||||
assert(
|
assert(
|
||||||
(x = object.parent(c, 'x')) == 'b',
|
(x = object.parent(c, 'x')) == 'b',
|
||||||
'reach parent attr: expected:', 'b', 'got:'.bold.yellow, x)
|
'reach parent attr: expected:', 'b', 'got:'.bold.yellow, x)
|
||||||
@ -331,18 +378,18 @@ module.setups = {
|
|||||||
x = 'z'
|
x = 'z'
|
||||||
method(){
|
method(){
|
||||||
// XXX this is almost the same as for js_prototype...
|
// XXX this is almost the same as for js_prototype...
|
||||||
assert(arrayCmp(
|
assert.array(
|
||||||
object.values(c, 'x'),
|
object.values(c, 'x'),
|
||||||
['z', 'y', 'x']),
|
['z', 'y', 'x'],
|
||||||
'reach all values of attr (class)')
|
'reach all values of attr (class)')
|
||||||
assert(arrayCmp(
|
assert.array(
|
||||||
object.values(c, 'x', function(v, o){
|
object.values(c, 'x', function(v, o){
|
||||||
return v.toUpperCase() }),
|
return v.toUpperCase() }),
|
||||||
['C', 'A', 'B']),
|
['C', 'A', 'B'],
|
||||||
'reach all values of attr (class)')
|
'reach all values of attr (class)')
|
||||||
assert(arrayCmp(
|
assert.array(
|
||||||
object.sources(c, 'method'),
|
object.sources(c, 'method'),
|
||||||
[Z.prototype, X.prototype]),
|
[Z.prototype, X.prototype],
|
||||||
'reach all values of method (class)')
|
'reach all values of method (class)')
|
||||||
assert(
|
assert(
|
||||||
object.parent(c, 'x') == super.x,
|
object.parent(c, 'x') == super.x,
|
||||||
@ -573,6 +620,23 @@ module.tests = {
|
|||||||
var cases =
|
var cases =
|
||||||
module.cases = {
|
module.cases = {
|
||||||
'edge-cases': function(assert){
|
'edge-cases': function(assert){
|
||||||
|
|
||||||
|
assert.error('double __extends__ fail', function(){
|
||||||
|
var X = object.C('X', {
|
||||||
|
__extends__: Object,
|
||||||
|
}, {
|
||||||
|
__extends__: Function,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// native constructor...
|
||||||
|
assert.array(
|
||||||
|
object.RawInstance(null, Array, 'a', 'b', 'c'),
|
||||||
|
['a', 'b', 'c'],
|
||||||
|
'native constructor')
|
||||||
|
assert(object.RawInstance(null, Number, '123') == 123, 'native constructor')
|
||||||
|
|
||||||
|
|
||||||
var x, y
|
var x, y
|
||||||
|
|
||||||
// object.match(..)
|
// object.match(..)
|
||||||
@ -589,7 +653,7 @@ module.cases = {
|
|||||||
assert.error('.parent(..) of anonymous function', function(){
|
assert.error('.parent(..) of anonymous function', function(){
|
||||||
object.parent(function(){}, {}) })
|
object.parent(function(){}, {}) })
|
||||||
},
|
},
|
||||||
'deepKeys': function(assert){
|
deepKeys: function(assert){
|
||||||
var a = {
|
var a = {
|
||||||
a: true
|
a: true
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user