Add Failure#merge
* src/validate/Failure.js (merge): Added * test/validate/FailureTest.js: Respective tests addedmaster
parent
895441850e
commit
e406c198d2
|
@ -150,5 +150,26 @@ module.exports = Class( 'Failure',
|
|||
__toString: function()
|
||||
{
|
||||
return this._reason;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Produce a new Failure merging two failures' messages and causes
|
||||
*/
|
||||
'public merge': function( failure )
|
||||
{
|
||||
if ( !Class.isA( module.exports, failure ) )
|
||||
{
|
||||
throw TypeError( 'Cannot merge with non-Failure ' + failure );
|
||||
}
|
||||
|
||||
var all_reasons = this._reason + '; ' + failure.getReason(),
|
||||
all_causes = this._causes.concat( failure.getCauses() );
|
||||
|
||||
return new this.constructor(
|
||||
this._field,
|
||||
all_reasons,
|
||||
all_causes
|
||||
);
|
||||
}
|
||||
} );
|
||||
|
|
|
@ -131,4 +131,44 @@ describe( 'Failure', function()
|
|||
.to.equal( reason );
|
||||
} );
|
||||
} );
|
||||
|
||||
|
||||
describe( '#merge', function()
|
||||
{
|
||||
it( 'rejects non-Failure merges', function()
|
||||
{
|
||||
expect( function()
|
||||
{
|
||||
Sut( DummyField() ).merge( {} );
|
||||
} ).to.throw( TypeError );
|
||||
} );
|
||||
|
||||
|
||||
it( 'merges causes', function()
|
||||
{
|
||||
var cause1 = DummyField(),
|
||||
cause2 = DummyField(),
|
||||
cause3 = DummyField();
|
||||
|
||||
var result = Sut( DummyField(), '', [ cause1 ] )
|
||||
.merge( Sut( DummyField(), '', [ cause2, cause3 ] ) );
|
||||
|
||||
expect( result.getCauses() )
|
||||
.to.deep.equal( [ cause1, cause2, cause3 ] );
|
||||
} );
|
||||
|
||||
|
||||
it( 'merges reasons', function()
|
||||
{
|
||||
var msg1 = 'message 1',
|
||||
msg2 = 'message 2';
|
||||
|
||||
var result = Sut( DummyField(), msg1 )
|
||||
.merge( Sut( DummyField(), msg2 ) )
|
||||
.getReason();
|
||||
|
||||
expect( result ).to.contain( msg1 );
|
||||
expect( result ).to.contain( msg2 );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
|
Loading…
Reference in New Issue