Comparing version 1.0.3 to 1.1.0
@@ -144,4 +144,3 @@ class Format { | ||
fn () { | ||
const name = this.object.name ? ` ${this.object.name}` : '' | ||
return `[${this.getClass()}${name}]` | ||
return this.style.fn(this.object, this.getClass()) | ||
} | ||
@@ -163,4 +162,9 @@ | ||
isError () { | ||
return this.object instanceof Error | ||
} | ||
collection () { | ||
const out = this.isSet() ? this.set() | ||
const out = this.isError() ? this.error() | ||
: this.isSet() ? this.set() | ||
: this.isMap() ? this.map() | ||
@@ -274,2 +278,24 @@ : Buffer.isBuffer(this.object) ? this.buffer() | ||
error () { | ||
if (this.errorIsEmpty()) | ||
return this.errorEmpty() | ||
const b = this.errorBody() | ||
return this.errorHead() + b + this.errorTail() | ||
} | ||
errorIsEmpty () { | ||
return this.pojoIsEmpty() | ||
} | ||
errorEmpty () { | ||
return this.style.errorEmpty(this.object, this.getClass()) | ||
} | ||
errorHead () { | ||
return this.style.errorHead(this.object, this.getClass()) | ||
} | ||
errorTail () { | ||
return `${this.indentLevel()}${this.style.errorTail()}` | ||
} | ||
errorBody () { | ||
return this.pojoBody() | ||
} | ||
pojo () { | ||
@@ -325,6 +351,6 @@ // get the body first so the id can be seen | ||
const ts = objToString(this.object).slice(8, -1) | ||
return ts === 'Object' && | ||
return this.object.constructor !== Object && | ||
this.object.constructor && | ||
this.object.constructor.name && | ||
this.object.constructor.name !== 'Object' | ||
this.object.constructor.name !== ts | ||
? this.object.constructor.name | ||
@@ -342,2 +368,13 @@ : ts | ||
pretty: { | ||
fn: (fn, cls) => { | ||
const name = fn.name | ||
const args = fn.toString().split('{')[0] | ||
.split('=>')[0] | ||
.replace(/[\n\r\s\t]+/g, '') | ||
.replace(/^[^\(]*\( */, '') | ||
.replace(/ *\).*/g, '') | ||
.split(',').join(', ') | ||
.trim() | ||
return `${cls} ${name || ''}(${args})` | ||
}, | ||
setEmpty: cls => `${cls} \{\}`, | ||
@@ -355,2 +392,10 @@ setHead: cls => `${cls} \{\n`, | ||
nodeId: id => `&ref_${id} `, | ||
errorEmpty: er => `${er.toString()}`, | ||
errorHead: (er, cls) => { | ||
// assertion errors sometimes generate WACKY stuff | ||
return (cls === 'AssertionError' && er.generatedMessage) | ||
? er.name + ' {\n' | ||
: `${er.toString()} \{\n` | ||
}, | ||
errorTail: () => '}', | ||
pojoEmpty: cls => `${cls} \{\}`, | ||
@@ -372,2 +417,3 @@ pojoHead: cls => `${cls} \{\n`, | ||
js: { | ||
fn: (fn, cls) => fn.toString(), | ||
setEmpty: cls => `new ${cls}()`, | ||
@@ -385,2 +431,5 @@ setHead: cls => `new ${cls}([\n`, | ||
nodeId: id => `&ref_${id} `, | ||
errorEmpty: (er, cls) => `new ${cls}(${JSON.stringify(er.message)})`, | ||
errorHead: (er, cls) => `Object.assign(new ${cls}(${JSON.stringify(er.message)}), {`, | ||
errorTail: () => '})', | ||
pojoEmpty: cls => '{}', | ||
@@ -403,2 +452,3 @@ pojoHead: cls => `\{\n`, | ||
tight: { | ||
fn: (fn, cls) => fn.toString(), | ||
setEmpty: cls => `new ${cls}()`, | ||
@@ -416,2 +466,5 @@ setHead: cls => `new ${cls}([`, | ||
nodeId: id => `&${id} `, | ||
errorEmpty: (er, cls) => `new ${cls}(${JSON.stringify(er.message)})`, | ||
errorHead: (er, cls) => `Object.assign(new ${cls}(${JSON.stringify(er.message)}), {`, | ||
errorTail: () => '})', | ||
pojoEmpty: cls => '{}', | ||
@@ -418,0 +471,0 @@ pojoHead: cls => `\{`, |
@@ -34,3 +34,8 @@ const Format = require('./format.js') | ||
: pattern === Array ? this.isArray() | ||
: !this.isError() && (pattern instanceof Error) ? false | ||
: this.isError() && ( | ||
pattern.message && | ||
!new Match(obj.message, {expect:pattern.message}).test() || | ||
pattern.name && | ||
!new Match(obj.name, {expect:pattern.name}).test()) ? false | ||
// standard deep matching stuff, same as parent, but not simple. | ||
@@ -37,0 +42,0 @@ : this.isSet() && !(pattern instanceof Set) ? false |
@@ -38,2 +38,6 @@ const Format = require('./format.js') | ||
: typeof a !== 'object' || typeof b !== 'object' ? false | ||
: !this.isError() && b instanceof Error ? false | ||
: this.isError() && ( | ||
b.message && b.message !== a.message || | ||
b.name && b.name !== a.name) ? false | ||
: this.isSet() && !(new Format(b).isSet()) ? false | ||
@@ -167,2 +171,5 @@ : this.isMap() && !(new Format(b).isMap()) ? false | ||
pojoTail () { return ' ' + super.pojoTail() } | ||
errorEmpty () { return this.spaceIfNoPref(super.errorEmpty()) } | ||
errorHead () { return this.spaceIfNoPref(super.errorHead()) } | ||
errorTail () { return ' ' + super.errorTail() } | ||
@@ -325,2 +332,36 @@ mapIsEmpty () { | ||
error () { | ||
if (this.errorIsEmpty()) | ||
return this.errorEmpty() | ||
const b = this.errorBody() | ||
if (!b) | ||
return this.errorEmpty() | ||
return this.errorHead() + b + this.errorTail() | ||
} | ||
errorIsEmpty () { | ||
const Ctor = this.constructor | ||
return super.errorIsEmpty() && ( | ||
new Ctor(this.object.name, {expect: this.expect.name}).test() && | ||
new Ctor(this.object.message, {expect: this.expect.message}).test() | ||
) | ||
} | ||
errorBody () { | ||
let out = this.pojoBody() | ||
let objKeys = Object.keys(this.object) | ||
let expKeys = Object.keys(this.expect) | ||
// catch 2 often non-enumerable but very important items | ||
if (objKeys.indexOf('name') === -1 && | ||
expKeys.indexOf('name') === -1 && | ||
this.object.name) { | ||
out += this.pojoEntry('name', this.object.name) | ||
} | ||
if (objKeys.indexOf('message') === -1 && | ||
expKeys.indexOf('message') === -1 && | ||
this.object.name) { | ||
out += this.pojoEntry('message', this.object.message) | ||
} | ||
return out | ||
} | ||
pojoBody () { | ||
@@ -356,2 +397,3 @@ const objEnt = Object.entries(this.object) | ||
diff () { | ||
this.unmatch() | ||
const expect = this.expect | ||
@@ -389,5 +431,7 @@ const obj = this.object | ||
unmatch () { | ||
this.match = false | ||
if (!this.provisional) | ||
this.parent && this.parent.unmatch() | ||
if (this.match) { | ||
this.match = false | ||
if (!this.provisional) | ||
this.parent && this.parent.unmatch() | ||
} | ||
} | ||
@@ -394,0 +438,0 @@ |
{ | ||
"name": "tcompare", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A comprehensive comparison library, for use in test frameworks", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36952
1065