//
//  OAListSerializerTests.m
//  OAUnitTests
//
//  Created by Niko Matsakis on 12/16/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "OAListSerializerTests.h"
#import "OAListSerializerFormatter.h"

@implementation OAListSerializerTests

- (NSArray*) colors {
    return [NSArray arrayWithObjects:@"red", @"white", @"blue", @"yellow", nil];
}

- (void) runTestWithChoices:(NSString*)keyPath 
                 testString:(NSString*)testString 
             expectedResult:expectedResult 
              expectedError:(NSString*)expectedError
{
    OAListSerializerFormatter *form = [OAListSerializerFormatter formatterWithAvailableChoices:[OAKeyPath keyPathWithObject:self 
                                                                                                                    keyPath:@"colors"]
                                                                                      namePath:@"description"];
    id value = nil;
    NSString *error = nil;
    if ([form getObjectValue:&value 
                   forString:testString 
            errorDescription:&error]) 
    {
        STAssertTrue(expectedError == nil, @"Expected an error %@, but got a successful result %@.", expectedError, value);
        STAssertEqualObjects(expectedResult, value, @"Expected value was not returned");
    }
    else 
    {
        STAssertTrue(expectedResult == nil, @"Expected a result %@, but got an error %@.", expectedResult, error);
        STAssertEqualObjects(expectedError, error, @"Expected error was not returned");
    }
}

- (void) testSimpleColors {
    [self runTestWithChoices:@"colors"
                  testString:@"red, blue, white"
              expectedResult:[NSArray arrayWithObjects:@"red", @"blue", @"white", nil]
               expectedError:nil];
}

- (void) testInvalidColors {
    [self runTestWithChoices:@"colors"
                  testString:@"orange, blue, white"
              expectedResult:nil
               expectedError:@"'orange' is not a valid entry."];
}

- (void) testReverseDirection {
    OAListSerializerFormatter *form = [OAListSerializerFormatter formatterWithAvailableChoices:[OAKeyPath keyPathWithObject:self 
                                                                                                                    keyPath:@"colors"]
                                                                                      namePath:@"description"];
    STAssertEqualObjects([form stringForObjectValue:[self colors]], 
                         @"red, white, blue, yellow", 
                         @"Serialization did not work");
}

- (void) testCustomClass {
    OAListSerializerFormatter *form = [OAListSerializerFormatter formatterWithAvailableChoices:[OAKeyPath keyPathWithObject:self 
                                                                                                                    keyPath:@"colors"]
                                                                                      namePath:@"description"];
    Class mutableSet = [NSMutableSet class];
    [form setContainerClass:mutableSet];
    NSObject *value;
    NSString *error;
    bool res = [form getObjectValue:&value 
                          forString:@"red, white, blue"
                   errorDescription:&error];
    STAssertTrue(res, @"Parsing was not successful");
    STAssertTrue([value isKindOfClass:mutableSet], @"Result is of class %@ which is not a mutable set",
                 [value class]);
    /*
    STAssertEqualObjects(value, 
                         [NSMutableSet setWithObjects:@"red", @"white", @"blue", nil],
                         @"Resulting value was incorrect");
     */
}
@end
