PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Constraints/ConstraintExpressionBase.cs

#
C# | 101 lines | 44 code | 8 blank | 49 comment | 0 complexity | 12f0c057424dfea4addd6f27ef7083d0 MD5 | raw file
  1// ****************************************************************
  2// Copyright 2008, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org
  5// ****************************************************************
  6
  7using System;
  8using System.Collections;
  9
 10namespace NUnit.Framework.Constraints
 11{
 12    /// <summary>
 13    /// ConstraintExpressionBase is the abstract base class for the 
 14    /// generated ConstraintExpression class, which represents a 
 15    /// compound constraint in the process of being constructed 
 16    /// from a series of syntactic elements.
 17    /// 
 18    /// NOTE: ConstraintExpressionBase is aware of some of its
 19    /// derived classes, which is an apparent violation of 
 20    /// encapsulation. Ideally, these classes would be a 
 21    /// single class, but they must be separated in order to
 22    /// allow parts to be generated under .NET 1.x and to
 23    /// provide proper user feedback in syntactically 
 24    /// aware IDEs.
 25    /// </summary>
 26    public abstract class ConstraintExpressionBase
 27    {
 28        #region Instance Fields
 29        /// <summary>
 30        /// The ConstraintBuilder holding the elements recognized so far
 31        /// </summary>
 32        protected ConstraintBuilder builder;
 33        #endregion
 34
 35        #region Constructors
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="T:ConstraintExpressionBase"/> class.
 38        /// </summary>
 39        public ConstraintExpressionBase()
 40        {
 41            this.builder = new ConstraintBuilder();
 42        }
 43
 44        /// <summary>
 45        /// Initializes a new instance of the <see cref="T:ConstraintExpressionBase"/> 
 46        /// class passing in a ConstraintBuilder, which may be pre-populated.
 47        /// </summary>
 48        /// <param name="builder">The builder.</param>
 49        public ConstraintExpressionBase(ConstraintBuilder builder)
 50        {
 51            this.builder = builder;
 52        }
 53        #endregion
 54
 55        #region ToString()
 56        /// <summary>
 57        /// Returns a string representation of the expression as it
 58        /// currently stands. This should only be used for testing,
 59        /// since it has the side-effect of resolving the expression.
 60        /// </summary>
 61        /// <returns></returns>
 62        public override string ToString()
 63        {
 64            return builder.Resolve().ToString();
 65        }
 66        #endregion
 67
 68        #region Append Methods
 69        /// <summary>
 70        /// Appends an operator to the expression and returns the
 71        /// resulting expression itself.
 72        /// </summary>
 73        public ConstraintExpression Append(ConstraintOperator op)
 74        {
 75            builder.Append(op);
 76            return (ConstraintExpression)this;
 77        }
 78
 79        /// <summary>
 80        /// Appends a self-resolving operator to the expression and
 81        /// returns a new ResolvableConstraintExpression.
 82        /// </summary>
 83        public ResolvableConstraintExpression Append(SelfResolvingOperator op)
 84        {
 85            builder.Append(op);
 86            return new ResolvableConstraintExpression(builder);
 87        }
 88
 89        /// <summary>
 90        /// Appends a constraint to the expression and returns that
 91        /// constraint, which is associated with the current state
 92        /// of the expression being built.
 93        /// </summary>
 94        public Constraint Append(Constraint constraint)
 95        {
 96            builder.Append(constraint);
 97            return constraint;
 98        }
 99        #endregion
100    }
101}