//
//  HomTests.m
//  Oaxaca2Test
//
//  Created by Nicholas Matsakis on 5/13/08.
//  Copyright 2008 Nicholas Matsakis. All rights reserved.
//

#import "HomTests.h"
#import "OxHom.h"
#import "OxNSObject.h"
#import "OxNSArray.h"
#import "Gui.h"
#import "Ox.h"
#import "OxNSString.h"

BOOL yes() { return YES; }

@implementation HomTests

- (void) runTests:(Gui*)gui {
    NSArray *array = OxArr(@"abc", @"ABC");
    NSArray *arrayCaps = OxArr(@"ABC", @"ABC");
	NSArray *justabc = OxArr(@"abc");
	NSArray *justABC = OxArr(@"ABC");
	NSArray *threes = OxArr(OxInt(3), OxInt(3));
    NSArray *empty = OxArr(nil);
	
	// --------------------------------------------------------------------------------
	{
		[gui startTestGroup:@"HomTests.Exists"];
		[gui test:@"Has prefix \"a\"" passesIf:OxExists(array, hasPrefix:@"a")];
		[gui test:@"Has prefix \"A\"" passesIf:OxExists(array, hasPrefix:@"A")];
		[gui test:@"Has prefix \"C\"" passesIf:!OxExists(array, hasPrefix:@"C")];
		[gui test:@"Has prefix \"A\" or \"C\"" passesIf:OxExists(array, hasPrefix:OxForEach(OxArr(@"C", @"A")))];
	}

	// --------------------------------------------------------------------------------
	{
		[gui startTestGroup:@"HomTests.ForAll"];
		[gui test:@"Has prefix \"a\"" passesIf:!OxForAll(array, hasPrefix:@"a")];
		[gui test:@"Equal ignore case to \"abc\"" passesIf:OxForAll(array, isEqualToStringIgnoringCase:@"abc")];	
		[gui test:@"Equal to \"abc\"" passesIf:!OxForAll(array, isEqualTo:@"abc")];	
		[gui test:@"Equal ignore case to \"abc\" and \"ABC\"" 
		 passesIf:OxForAll(array, isEqualToStringIgnoringCase:OxForEach(OxArr(@"abc", @"ABC")))];
		[gui test:@"Equal ignore case to \"abc\" and \"ABCD\"" 
		 passesIf:!OxForAll(array, isEqualToStringIgnoringCase:OxForEach(OxArr(@"abc", @"ABCD")))];
	}
	
	// --------------------------------------------------------------------------------
	{
		[gui startTestGroup:@"HomTests.Map"];
		[gui test:@"Map uppercase" passesIf:[arrayCaps isEqualToArray:OxMap(array, uppercaseString)]];
		[gui test:@"Map length" passesIf:[threes isEqualToArray:OxMap(array, length)]];
		NSArray *expanded = OxMap(array, containsString:OxForEach(OxArr(@"b",@"B")));
		[gui test:@"Map expand" passesIf:[OxArr(OxArr(OxYES, OxNO), OxArr(OxNO, OxYES)) isEqualToArray:expanded]];
	}

	// --------------------------------------------------------------------------------
{
	[gui startTestGroup:@"HomTests.Perform"];
	
	[gui test:@"Empty Perform"
	 passesIf:[[empty performForEach] hasPrefix:@"abc"] || yes()]; // goal: no seg faults!
	
	NSArray *mutableArray = OxArr([NSMutableString stringWithString:@"Foo"],
								  [NSMutableString stringWithString:@"Bar"]);
	[gui test:@"Non-empty Perform Before"
	 passesIf:[mutableArray isEqualToArray:OxArr(@"Foo", @"Bar")]];
	
	[[mutableArray performForEach] appendString:@"'"];
	[gui test:@"Non-empty Perform After"
	 passesIf:[mutableArray isEqualToArray:OxArr(@"Foo'", @"Bar'")]];
	
	OxPerform(mutableArray, appendString:@"'");
	[gui test:@"Non-empty Perform After (Macro)"
	 passesIf:[mutableArray isEqualToArray:OxArr(@"Foo''", @"Bar''")]];
	
	NSMutableString *mutableString = [NSMutableString string];
	[[OxArr(mutableString) performForEach] appendString:OxForEach(mutableArray)];
	[gui test:@"Perform after w/ array"
	 passesIf:[mutableString isEqual:@"Foo''Bar''"]];
	
	mutableString = [NSMutableString string];
	[[mutableString perform] appendString:OxForEach(mutableArray)];
	[gui test:@"Perform after w/ array (2)"
	 passesIf:[mutableString isEqual:@"Foo''Bar''"]];
	
	[[OxForEach(mutableArray) perform] appendString:OxForEach(OxArr(@"x", @"y"))];
	[gui test:@"OxForEach rcvr"
	 passesIf:[mutableArray isEqualToArray:OxArr(@"Foo''xy", @"Bar''xy")]];
}

	// --------------------------------------------------------------------------------
    [gui startTestGroup:@"HomTests.Filter"];
    id filter1 = OxFilter(array, hasPrefix:@"a");
    [gui test:@"Filter \"a\"" passesIf:[justabc isEqualToArray:filter1]];
    [gui test:@"Filter \"A\"" passesIf:[justABC isEqualToArray:OxFilter(array, hasPrefix:@"A")]];
    [gui test:@"Filter \"C\"" passesIf:[empty isEqualToArray:OxFilter(array, hasPrefix:@"C")]];
    [gui test:@"Compare \"abc\"" 
     passesIf:[justABC isEqualToArray:OxFilter(array, compare:@"abc" options:0)]];
    [gui test:@"Compare \"abc\" case" 
     passesIf:[empty isEqualToArray:OxFilter(array, compare:@"abc" options:NSCaseInsensitiveSearch)]];
	[gui test:@"Has Prefix \"a\" OR \"A\""
	 passesIf:[array isEqualToArray:OxFilter(array, hasPrefix:OxForEach(OxArr(@"a", @"A")))]];
	[gui test:@"Has Prefix \"c\" OR \"C\""
	 passesIf:[empty isEqualToArray:OxFilter(array, hasPrefix:OxForEach(OxArr(@"C", @"C")))]];
}

@end
