#include <Foundation/Foundation.h>
#include <Encore/Encore.h>
#include <ECTestSuiteTEST.h>
#include <ECXMLControlTEST.h>
#include <ECParameterStringTEST.h>
#include <ECStringUtilsTEST.h>
#include <ECLoggingTEST.h>


int main( int argc, char *argv[] ) {
  EC_AUTORELEASEPOOL_BEGIN
  ECTestSuite *testSuite;
  BOOL exceptionThrown = NO;
  NSArray *testResult;
  
  NSLog( @"Starting ECTests...." );

  testSuite = [[[ECTestSuite alloc] init] autorelease];

  [testSuite
    addTestcase: [[[ECTestSuiteTEST alloc] init] autorelease]
    withSelector: @selector(testItWithSuccess)];
    
  [testSuite
    addTestcase: [[[ECTestSuiteTEST alloc] init] autorelease]
    withSelector: @selector(testItWithFailure)];

  [testSuite 
    addTestcase: [[[ECXMLControlTEST alloc] init] autorelease]
    withSelector: @selector(testXMLController)];
   
  [testSuite
    addTestcase: [[[ECParameterStringTEST alloc] init] autorelease]
    withSelector: @selector(testParameterString)];

  [testSuite
    addTestcase: [[[ECStringUtilsTEST alloc] init] autorelease]
    withSelector: @selector(testStringSplitting)];

  [testSuite
    addTestcase: [[[ECLoggingTEST alloc] init] autorelease]
    withSelector: @selector(testLogging)];

  [testSuite
    addTestcase: [[[ECLoggingTEST alloc] init] autorelease]
    withSelector: @selector(testLoggingConfiguredViaXML)];

  NS_DURING
    [testSuite runTestcases];
  NS_HANDLER
    exceptionThrown = YES;
  NS_ENDHANDLER
  
  if( !exceptionThrown ) {
    NSLog( @"An Exception should have been thrown!" );
    [[[ECIllegalStateException alloc]
      initWithIllegalStateInfo: @"An ECAssertionException should have been thrown!"]
      raise];
  }
  
  [testSuite proceedOnFailure: YES];
  exceptionThrown = NO;
  
  NS_DURING
    testResult = [testSuite runTestcases];
  NS_HANDLER
    exceptionThrown = YES;
  NS_ENDHANDLER
  
  if( exceptionThrown ) {
    NSLog( @"An ECAssertionException has been thrown but "\
        "test suite had to proceed!" );
    [[[ECIllegalStateException alloc]
      initWithIllegalStateInfo: @"An ECAssertionException has been thrown but "\
        "test suite had to proceed!"]
      raise];
  }
  
  if( nil != testResult ) {
    int i;
    
    NSLog( @"Results of previously performed tests:" );
    for( i = 0; i < [testResult count]; i++ ) {
      ECTestResult *result = (ECTestResult *) [testResult objectAtIndex: i];
      
      if( [result successful] ) {
        NSLog( @"Test nr. %u ran with success!", i );
      } else {
        NSLog( @"Test nr. %u ran with failure and exception: %@!", i,
          [result exception] );
      }
    }
  }
  NSLog( @"ECTests finished!" );
  EC_AUTORELEASEPOOL_END
  
  return 0;
}
