// 4. Implement the interface explicitly. #region IEnsuranceHandler Members void IEnsuranceHandler.Handle( Constraint constraint, string message, params object[] args ) { // 5. Do your logging. if ( _log.IsErrorEnabled ) { _log.Error( constraint.ToString() ); _log.WarnFormat( message, args ); } }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public override void Handle( Constraint constraint, string message, params object[] args ) { TextWriter.Write( ToString() ); IEnsuranceResponsibilityChainLink handler = Successor; if ( handler != null ) { handler.Handle( constraint, message, args ); } }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for success, false for failure</returns> public override bool Matches( object actual ) { Actual = actual; if ( CaseInsensitive ) { RealConstraint = RealConstraint.IgnoreCase; } return RealConstraint.Matches( actual ); }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// IEnsuranceResponsibilityChainLink handler = successor; /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <exception cref="EnsuranceException">Always</exception> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public override void Handle( Constraint constraint, string message, params object[] args ) { try { MessageWriter messageWriter = new TextMessageWriter( new StringWriter( CultureInfo.CurrentCulture ) ); constraint.WriteMessageTo( messageWriter ); throw new EnsuranceException( messageWriter.ToString() ); } finally { IEnsuranceResponsibilityChainLink handler = Successor; if ( handler != null ) { handler.Handle( constraint, message, args ); } } }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// IEnsuranceResponsibilityChainLink handler = successor; /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public void Handle( Constraint constraint, string message, params object[] args ) { try { if ( Debugger.IsAttached ) { Debugger.Break(); } } finally { IEnsuranceResponsibilityChainLink handler = _successor; if ( handler != null ) { handler.Handle( constraint, message, args ); } } }
/// <summary> /// Set all modifiers applied to the prefix into the base constraint /// before matching /// </summary> protected void PassModifiersToBase() { if ( CaseInsensitive ) { _baseConstraint = _baseConstraint.IgnoreCase; } if ( Tolerance != null ) { _baseConstraint = _baseConstraint.Within( Tolerance ); } if ( CompareAsCollection ) { _baseConstraint = _baseConstraint.AsCollection; } if ( CompareWith != null ) { _baseConstraint = _baseConstraint.Comparer( CompareWith ); } }
// 5. Implement the interface explicitly. void IEnsuranceHandler.Handle( Constraint constraint, string message, params object[] args ) { // 5. Do your logging. try { if ( _log.IsErrorEnabled ) { _log.Error( constraint.ToString() ); _log.WarnFormat( message, args ); } } finally { // 6. ALWAYS pass the call onto your successor. IEnsuranceResponsibilityChainLink handler = _successor; if ( handler != null ) { handler.Handle( constraint, message, args ); } } }
/// <summary> /// Apply a constraint to an actual value, succeeding if the constraint /// is satisfied and throwing an assertion exception on failure. Works /// identically to <see cref="Ensurance.EnsureBase<T>.That(object, Constraint, string)"/> /// </summary> /// <param name="constraint">A Constraint to be applied</param> /// <param name="actual">The actual value to test</param> /// <param name="message">The message that will be displayed on failure</param> public static void Expect( object actual, Constraint constraint, string message ) { That( actual, constraint, message, null ); }
/// <summary> /// Construct a SomeItemsConstraint on top of an existing constraint /// </summary> /// <param name="itemConstraint"></param> public SomeItemsConstraint( Constraint itemConstraint ) : base( itemConstraint ) { }
/// <summary> /// Apply a constraint to an actual value, succeeding if the constraint /// is satisfied and throwing an assertion exception on failure. Works /// identically to <see cref="Ensurance.EnsureBase<T>.That(object, Constraint)"/> /// </summary> /// <param name="constraint">A Constraint to be applied</param> /// <param name="actual">The actual value to test</param> public static void Expect( object actual, Constraint constraint ) { That( actual, constraint, null, null ); }
/// <summary> /// Write the generic 'Actual' line for a constraint /// </summary> /// <param name="constraint">The constraint for which the actual value is to be written</param> private void WriteActualLine( Constraint constraint ) { TextWriter.Write( Pfx_Actual ); constraint.WriteActualValueTo( this ); TextWriter.WriteLine(); }
/// <summary> /// Construct a SomeItemsConstraint on top of an existing constraint /// </summary> /// <param name="itemConstraint"></param> public NoItemConstraint( Constraint itemConstraint ) : base( itemConstraint ) { }
/// <summary> /// Resolve a constraint that has been recognized by applying any /// pending operators and returning the resulting Constraint. /// </summary> /// <returns>A constraint that incorporates all pending operators</returns> private Constraint Resolve( Constraint constraint ) { while ( _ops.Count > 0 ) { switch ( _ops.Pop() ) { case Op.Not: constraint = new NotConstraint( constraint ); break; case Op.All: constraint = new AllItemsConstraint( constraint ); break; case Op.Some: constraint = new SomeItemsConstraint( constraint ); break; case Op.None: constraint = new NoItemConstraint( constraint ); break; case Op.Prop: constraint = new PropertyConstraint( _opnds.Pop(), constraint ); break; } } return constraint; }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public abstract void Handle( Constraint constraint, string message, params object[] args );
/// <summary> /// Construct an AllItemsConstraint on top of an existing constraint /// </summary> /// <param name="itemConstraint"></param> public AllItemsConstraint( Constraint itemConstraint ) : base( itemConstraint ) { }
/// <summary> /// Initializes a new instance of the <see cref="PropertyConstraint"/> /// class. /// </summary> /// <param name="name">The name.</param> /// <param name="baseConstraint">The constraint to apply to the property.</param> public PropertyConstraint( string name, Constraint baseConstraint ) : base( baseConstraint ) { _name = name; }
/// <summary> /// This operator creates a constraint that is satisfied if the argument /// constraint is not satisfied. /// </summary> public static Constraint LogicalNot( Constraint m ) { return new NotConstraint( m ?? new EqualConstraint( null ) ); }
/// <summary> /// Display Expected and Actual lines for a constraint. This is called /// by MessageWriter's default implementation of WriteMessageTo and /// provides the generic two-line display. /// </summary> /// <param name="constraint">The constraint that failed</param> public override void DisplayDifferences( Constraint constraint ) { WriteExpectedLine( constraint ); WriteActualLine( constraint ); }
/// <summary> /// This operator creates a constraint that is satisfied if either of /// the argument constraints is satisfied. /// </summary> public static Constraint BitwiseOr( Constraint left, Constraint right ) { return new OrConstraint( left, right ); }
/// <summary> /// This operator creates a constraint that is satisfied only if both /// argument constraints are satisfied. /// </summary> public static Constraint BitwiseAnd( Constraint left, Constraint right ) { return new AndConstraint( left, right ); }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// if( successor != null ) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public override void Handle( Constraint constraint, string message, params object[] args ) { try { if ( !string.IsNullOrEmpty( message ) ) { WriteMessageLine( message, args ); } constraint.WriteMessageTo( this ); } finally { IEnsuranceResponsibilityChainLink handler = Successor; if ( handler != null ) { handler.Handle( constraint, message, args ); } } }
/// <summary> /// Apply a constraint to an actual value, succeeding if the constraint /// is satisfied and throwing an assertion exception on failure. Works /// identically to <see cref="Ensurance.EnsureBase<T>.That(object, Constraint, string, object[])"/> /// </summary> /// <param name="constraint">A Constraint to be applied</param> /// <param name="actual">The actual value to test</param> /// <param name="message">The message that will be displayed on failure</param> /// <param name="args">Arguments to be used in formatting the message</param> public static void Expect( object actual, Constraint constraint, string message, params object[] args ) { That( actual, constraint, message, args ); }
/// <summary> /// Construct given a base constraint /// </summary> /// <param name="baseConstraint"></param> protected PrefixConstraint( Constraint baseConstraint ) { _baseConstraint = baseConstraint; }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// IEnsuranceResponsibilityChainLink handler = successor; /// if( handler != null ) { /// handler.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public void Handle( Constraint constraint, string message, params object[] args ) { try { MessageWriter messagewriter = new TextMessageWriter(); messagewriter.WriteLine(); constraint.WriteMessageTo( messagewriter ); messagewriter.Write( new StackTraceWriter().ToString() ); string tmpMessage = String.Format( CultureInfo.CurrentCulture, "{0}{1}", message, messagewriter ); switch ( _logger.DefaultLogSeverity ) { case LogSeverity.Debug: _logger.Debug( tmpMessage, args ); break; case LogSeverity.Info: _logger.Info( tmpMessage, args ); break; case LogSeverity.Warn: _logger.Warn( tmpMessage, args ); break; case LogSeverity.Error: _logger.Error( tmpMessage, args ); break; case LogSeverity.Fatal: _logger.Fatal( tmpMessage, args ); break; } } finally { IEnsuranceResponsibilityChainLink handler = _successor; if ( handler != null ) { handler.Handle( constraint, message, args ); } } }
/// <summary> /// Construct a BinaryOperation from two other constraints /// </summary> /// <param name="left">The first constraint</param> /// <param name="right">The second constraint</param> protected BinaryOperation( Constraint left, Constraint right ) { _left = left; _right = right; }
/// <summary> /// Display Expected and Actual lines for a constraint. This is called /// by MessageWriter's default implementation of WriteMessageTo and /// provides the generic two-line display. /// </summary> /// <param name="constraint">The constraint that failed</param> public abstract void DisplayDifferences( Constraint constraint );
/// <summary> /// Create an AndConstraint from two other constraints /// </summary> /// <param name="left">The first constraint</param> /// <param name="right">The second constraint</param> public AndConstraint( Constraint left, Constraint right ) : base( left, right ) { }
/// <summary> /// Initializes a new instance of the <see cref="NotConstraint"/> class. /// </summary> /// <param name="baseConstraint">The base constraint to be negated.</param> public NotConstraint( Constraint baseConstraint ) : base( baseConstraint ) { }
/// <summary> /// Write the generic 'Expected' line for a constraint /// </summary> /// <param name="constraint">The constraint that failed</param> private void WriteExpectedLine( Constraint constraint ) { TextWriter.Write( Pfx_Expected ); constraint.WriteDescriptionTo( this ); TextWriter.WriteLine(); }