// 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 );
            }
        }
Exemple #2
0
        /// <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 );
                }
            }
        }
Exemple #8
0
 /// <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 )
 {
 }
Exemple #10
0
 /// <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 );
 }
Exemple #11
0
 /// <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();
 }
Exemple #12
0
 /// <summary>
 /// Construct a SomeItemsConstraint on top of an existing constraint
 /// </summary>
 /// <param name="itemConstraint"></param>
 public NoItemConstraint( Constraint itemConstraint ) : base( itemConstraint )
 {
 }
Exemple #13
0
        /// <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;
        }
Exemple #14
0
 /// <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 );
Exemple #15
0
 /// <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;
 }
Exemple #17
0
 /// <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 ) );
 }
Exemple #18
0
 /// <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 );
 }
Exemple #19
0
 /// <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 );
 }
Exemple #20
0
 /// <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 );
 }
Exemple #21
0
        /// <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 );
                }
            }
        }
Exemple #22
0
 /// <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 );
 }
Exemple #23
0
 /// <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 );
                }
            }
        }
Exemple #25
0
 /// <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;
 }
Exemple #26
0
 /// <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 );
Exemple #27
0
 /// <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 )
 {
 }
Exemple #28
0
 /// <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 )
 {
 }
Exemple #29
0
 /// <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();
 }