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> );
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 );

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 13, 2007 at 8:49 PM by idavis, version 1

Comments

No comments yet.