added shadow reporting/handling...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-08-17 13:14:46 +03:00
parent a10e8ae340
commit 89abacdb7e
2 changed files with 45 additions and 3 deletions

View File

@ -31,6 +31,11 @@ tests.Setups({
return {} },
})
tests.Setup('setup',
function(assert){
assert(false, 'setup (shadowed): assert')
return {} })
tests.Test('dummy',
function(assert, setup){
assert(true, 'dummy: assert') })

43
test.js
View File

@ -6,8 +6,10 @@
* Repo and docs:
* https://github.com/flynx/test.js
*
*
* TODO:
* - report shadowing of tests...
* - flexible test chains with 0 or more modifiers...
*
*
***********************************************/ /* c8 ignore next 2 */
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
@ -59,6 +61,8 @@ module.VERBOSE = process ?
//---------------------------------------------------------------------
//---------------------------------------------------------------------
var getCallerFilename =
@ -246,6 +250,8 @@ var mergeIter = function(iter){
// -> merged
//
//
// Merged is the sum of all its members.
//
var Merged =
module.Merged =
object.Constructor('Merged', {
@ -266,6 +272,7 @@ object.Constructor('Merged', {
// in JS...
get size(){
return this.keys().length },
// Like .size but does not count the pass-through elements...
get usize(){
var k = this.keys()
return k.length - (k.includes('-') ? 1 : 0) },
@ -289,21 +296,51 @@ object.Constructor('Merged', {
toObject: function(){
return Object.fromEntries(this.entries()) },
//
// .checkShadowing()
// -> shadows
//
// .checkShadowing(other)
// -> shadows
//
checkShadowing: function(other){
var existing = new Set(this.keys())
other = other || this.values()
return (other instanceof Array ?
other
: [other])
.map(function(o){
return Object.keys(o)
.filter(function(k){
return existing.has(k) }) })
.flat() },
handleShadowing: function(shadowed){
shadowed.length > 0
&& console.warn(` WARNING:`.bold, `shadowing: ${shadowed.join()}`)
return this },
}, {
filename: undefined,
__init__: function(other){
// parse args...
if(arguments.length == 2){
var [name, func] = arguments
other = {[name]: func}
}
other = {[name]: func} }
// set .filename on tests...
var f = getCallerFilename()
Object.entries(Object.getOwnPropertyDescriptors(other))
.forEach(function([k, p]){
typeof(p.value) == 'function'
&& (p.value.filename = p.value.filename || f) })
// check for shadowing...
this.constructor.handleShadowing(
this.constructor.checkShadowing(other))
// mix and merge...
object.mixinFlat(this, other)
this.constructor.add(this) },
})