Contents

Basic Usage

Ensuarance inherits its syntax from NUnit. There are two main ways to make calls.
  1. Ensure.That( <actual>, <Constraint> );
  2. Ensure.<Constraint>( <actual> );
In addition the the standard constraints, the are specialized classes for files and strings.
Ensure.That( actual, Is.Null );
Ensure.IsNotNull( 42 );
Ensure.That( 42, Is.Not.Null );
Ensure.IsTrue( 2 + 2 == 4 );
Ensure.That( 2 + 2 == 4, Is.True );
Ensure.That( 2 + 2 == 4 );
Ensure.IsFalse( 2 + 2 == 5 );
Ensure.That( 2 + 2 == 5, Is.False );
Ensure.IsNaN( d );
Ensure.IsNaN( f );
Ensure.That( d, Is.NaN );
Ensure.That( f, Is.NaN );
Ensure.IsEmpty( "" );
Ensure.IsNotEmpty( "Hello!" );
Ensure.That( "", Is.Empty );
Ensure.That( "Hello!", Is.Not.Empty );
Ensure.IsEmpty( new bool[0] );
Ensure.IsNotEmpty( new int[] {1, 2, 3} );
Ensure.That( new bool[0], Is.Empty );

String Tests string phrase = "Hello World!";
Ensure.Strings.Contains( "World", phrase );
Ensure.That( phrase, Text.Contains( "World" ) );
Ensure.That( phrase, Text.DoesNotContain( "goodbye" ) );
Ensure.That( phrase, Text.Contains( "WORLD" ).IgnoreCase );
Ensure.That( phrase, Text.DoesNotContain( "BYE" ).IgnoreCase );
Ensure.That( new string[] {"abc", "bad", "dba"}, Text.All.Contains( "b" ) );

Ensure.Strings.StartsWith( "Hello", phrase );
Ensure.That( phrase, Text.StartsWith( "Hello" ) );
Ensure.That( phrase, Text.DoesNotStartWith( "Hi!" ) );
Ensure.That( phrase, Text.StartsWith( "HeLLo" ).IgnoreCase );
Ensure.That( phrase, Text.DoesNotStartWith( "HI" ).IgnoreCase );
Ensure.That( new string[] {"Hello!", "Hi!", "Hola!"}, Text.All.StartsWith( "h" ).IgnoreCase );
Ensure.Strings.EndsWith( "!", phrase );
Ensure.That( phrase, Text.EndsWith( "!" ) );
Ensure.That( phrase, Text.DoesNotEndWith( "?" ) );
Ensure.That( phrase, Text.EndsWith( "WORLD!" ).IgnoreCase );
Ensure.That( greetings, Text.All.EndsWith( "!" ) );
Ensure.Strings.AreEqualIgnoringCase( "hello world!", phrase );
Ensure.That( phrase, Is.EqualTo( "hello world!" ).IgnoreCase );
Ensure.That( phrase, Is.Not.EqualTo( "goodbye world!" ).IgnoreCase );
Ensure.That( new string[] {"Hello", "World"}, Is.EqualTo( new object[] {"HELLO", "WORLD"} ).IgnoreCase );
Ensure.That( new string[] {"HELLO", "Hello", "hello"}, Is.All.EqualTo( "hello" ).IgnoreCase );

string phrase = "Now is the time for all good men to come to the aid of their country.";
Ensure.Strings.IsMatch( "all good men", phrase );
Ensure.Strings.IsMatch( "Now.*come", phrase );
Ensure.That( phrase, Text.Matches( "all good men" ) );
Ensure.That( phrase, Text.Matches( "Now.*come" ) );
Ensure.That( phrase, Text.DoesNotMatch( "all.*men.*good" ) );
Ensure.That( phrase, Text.Matches( "ALL" ).IgnoreCase );
Ensure.That( new string[] {"Never say never", "It's never too late", "Nevermore!"}, Text.All.Matches( "never" ).IgnoreCase );

Attribute Usage

Ensurance supports constraint attributes. This allows the pre-conditions to be seen in the prototpye and documentation of your code. Due to the constraints of .NET, a call must still be made in the method body. There may be support in the futute for proxies and MarshalByRefObject types. There are only a few of the possible attributes implemented, but many more are to come. The attribute usage, like that of the rest of the API, is designed to be fluent.
void MethodCall( [Is.Null] string value ) {
    Ensure.VerifyArguments( value );
    // ...
}

void MethodCall( [Is.Not.Null] string value ) {
    Ensure.VerifyArguments( value );
    // ...
}

void MethodCall( [Is.Not.Null, Is.Not.Empty] ICollection value ) {
    Ensure.VerifyArguments( value );
    // ...
}

void MethodCall( [Is.Unique] ICollection value1, [Has.Property("Length")] ICollection value2 ) {
    Ensure.VerifyArguments( value1, value2 );
    // ...
}

Configuring Ensure

Ensure's configuration is static. If the EnsuranceHandlers are never initialized, Ensure will use an ExceptionEnsuranceHandler for all failures. To configure the responibility handlers, a call to Ensure.SetEnsuranceHandlers
Ensure.SetEnsuranceHandlers( new LoggingEnsuranceHandler() );
Ensure.SetEnsuranceHandlers( new ExceptionEnsuranceHandler() );
Ensure.SetEnsuranceHandlers( new DebuggerEnsuranceHandler() );
Ensure.SetEnsuranceHandlers( new LoggingEnsuranceHandler(), new ExceptionEnsuranceHandler() );
Ensure.SetEnsuranceHandlers( new ExceptionEnsuranceHandler(), new DebuggerEnsuranceHandler() );

The ensurance handlers will be called in the order in which they are passed into SetInsuranceHandlers.

Last edited Oct 20, 2007 at 12:38 AM by idavis, version 5

Comments

No comments yet.