PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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