Andrew Stopford's Weblog

poobah

News

Articles

MbUnit Folks

Old Blogs

Write your own NUnit constraint (in VB) - Part 2

So in part 1 we created our own constraint and want some of that syntax sugar, the Is helper class uses static (shared) methods to create the constraints so we either  hack the NUnit source or try something else. What I ended up doing may not be ideal as it really plays with the syntax but it was fun all the same :) I created a new class called This and did the following.

This is really a modfied version of the [Is] class with our new constraint. Note that the class exposes 'Is' as it's self so we now write

   1:  Imports System.Collections
   2:  Imports NUnit.Framework.Constraints
   3:  Imports NUnit.Framework.SyntaxHelpers
   4:   
   5:  Public Class This
   6:      Inherits SyntaxHelper
   7:   
   8:      Public Shared ReadOnly Property [Is]() As This
   9:          Get
  10:              Return New This()
  11:          End Get
  12:      End Property
  13:   
  14:      Public Function AssignableFrom(ByVal expectedType As Type) As Constraint
  15:          Return New AssignableFromConstraint(expectedType)
  16:      End Function
  17:   
  18:      Public Function AtLeast(ByVal expected As IComparable) As Constraint
  19:          Return GreaterThanOrEqualTo(expected)
  20:      End Function
  21:   
  22:      Public Function AtMost(ByVal expected As IComparable) As Constraint
  23:          Return LessThanOrEqualTo(expected)
  24:      End Function
  25:   
  26:      Public Function EqualTo(ByVal expected As Object) As EqualConstraint
  27:          Return New EqualConstraint(expected)
  28:      End Function
  29:   
  30:      Public Function EquivalentTo(ByVal expected As ICollection) As Constraint
  31:          Return New CollectionEquivalentConstraint(expected)
  32:      End Function
  33:   
  34:      Public Function GreaterThan(ByVal expected As IComparable) As Constraint
  35:          Return New GreaterThanConstraint(expected)
  36:      End Function
  37:   
  38:      Public Function GreaterThanOrEqualTo(ByVal expected As IComparable) As Constraint
  39:          Return New GreaterThanOrEqualConstraint(expected)
  40:      End Function
  41:   
  42:      Public Function InstanceOfType(ByVal expectedType As Type) As Constraint
  43:          Return New InstanceOfTypeConstraint(expectedType)
  44:      End Function
  45:   
  46:      Public Function LessThan(ByVal expected As IComparable) As Constraint
  47:          Return New LessThanConstraint(expected)
  48:      End Function
  49:   
  50:      Public Function LessThanOrEqualTo(ByVal expected As IComparable) As Constraint
  51:          Return New LessThanOrEqualConstraint(expected)
  52:      End Function
  53:   
  54:      Public Function SameAs(ByVal expected As Object) As Constraint
  55:          Return New SameAsConstraint(expected)
  56:      End Function
  57:   
  58:      Public Function SubsetOf(ByVal expected As ICollection) As Constraint
  59:          Return New CollectionSubsetConstraint(expected)
  60:      End Function
  61:   
  62:      Public Function [TypeOf](ByVal expectedType As Type) As Constraint
  63:          Return New ExactTypeConstraint(expectedType)
  64:      End Function
  65:   
  66:   
  67:      ' Fields
  68:      Public ReadOnly Empty As Constraint = New EmptyConstraint
  69:      Public ReadOnly [False] As Constraint = New EqualConstraint(False)
  70:      Public ReadOnly NaN As Constraint = New EqualConstraint(NaN)
  71:      Public ReadOnly [Null] As Constraint = New EqualConstraint(Nothing)
  72:      Public ReadOnly [True] As Constraint = New EqualConstraint(True)
  73:      Public ReadOnly Unique As Constraint = New UniqueItemsConstraint
  74:   
  75:   
  76:      Public Function DateNull() As Constraint
  77:          Return New DateFormatConstraint()
  78:      End Function
  79:   
  80:      Public ReadOnly Property [Really]() As ConstraintBuilder
  81:          Get
  82:              Return New ConstraintBuilder()
  83:          End Get
  84:      End Property
  85:   
  86:   
  87:  End Class

This.Is

and can do

This.Is.EqualTo

or use our new constraint

 This.Is.DateNull

The trouble is that the ConstraintBuilders are exposed as follows

This.Not.EqualTo

Which does'nt read too well, so instead we expose the builders as a property 'Really' and we can now do

This.Is.Really.Not.EqualTo

To add your new constraint to a builder would mean overriding that class in the same way as the helper.

Posted: Aug 06 2008, 12:35 PM by andrewstopford | with no comments
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)