From c58220701ada0ccca85cd290a57b3e2277537b75 Mon Sep 17 00:00:00 2001 From: "Hoa V. DINH" Date: Tue, 21 Oct 2014 20:50:13 -0700 Subject: Implement unit tests for message builder and message parsers --- unittest/unittest.mm | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 unittest/unittest.mm (limited to 'unittest/unittest.mm') diff --git a/unittest/unittest.mm b/unittest/unittest.mm new file mode 100644 index 00000000..fc604e1b --- /dev/null +++ b/unittest/unittest.mm @@ -0,0 +1,196 @@ +// +// unittest.m +// unittest +// +// Created by Hoa V. DINH on 10/20/14. +// Copyright (c) 2014 MailCore. All rights reserved. +// + +#import +#import +#import + +@interface MCOMessageBuilder (UnitTest) + +- (void) _setBoundaries:(NSArray *)boundaries; + +@end + +@interface MCOMessageHeader (UnitTest) + +- (void) prepareForUnitTest; + +@end + +@implementation MCOMessageHeader (UnitTest) + +- (void) prepareForUnitTest +{ + if ([[self date] timeIntervalSinceNow] <= 2) { + // Date might be generated, set to known date. + [self setDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]]; + } + if ([[self receivedDate] timeIntervalSinceNow] <= 2) { + // Date might be generated, set to known date. + [self setReceivedDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]]; + } + if ([self isMessageIDAutoGenerated]) { + [self setMessageID:@"MyMessageID123@mail.gmail.com"]; + } +} + +@end + +@interface MCOAbstractPart (UnitTest) + +- (void) prepareForUnitTest; + +@end + +@implementation MCOAbstractPart (UnitTest) + +- (void) prepareForUnitTest +{ +} + +@end + +@implementation MCOAbstractMessagePart (UnitTest) + +- (void) prepareForUnitTest +{ + [[self header] prepareForUnitTest]; + [[self mainPart] prepareForUnitTest]; +} + +@end + +@implementation MCOAbstractMultipart (UnitTest) + +- (void) prepareForUnitTest +{ + for(MCOAbstractPart * part in [self parts]) { + [part prepareForUnitTest]; + } +} + +@end + +@interface unittest : XCTestCase + +@end + +@implementation unittest { + NSString * _mainPath; + NSString * _builderPath; + NSString * _parserPath; + NSString * _builderOutputPath; + NSString * _parserOutputPath; +} + +- (void)setUp { + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. + _mainPath = [[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"data"]; + _builderPath = [_mainPath stringByAppendingPathComponent:@"builder/input"]; + _builderOutputPath = [_mainPath stringByAppendingPathComponent:@"builder/output"]; + _parserPath = [_mainPath stringByAppendingPathComponent:@"parser/input"]; + _parserOutputPath = [_mainPath stringByAppendingPathComponent:@"parser/output"]; +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testMessageBuilder1 { + // This is an example of a functional test case. + //XCTAssert(YES, @"Pass"); + MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init]; + [[builder header] setFrom:[MCOAddress addressWithRFC822String:@"Hoà "]]; + [[builder header] setTo:@[[MCOAddress addressWithRFC822String:@"Foo Bar "]]]; + [[builder header] setSubject:@"testMessageBuilder1"]; + [[builder header] setDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]]; + [[builder header] setMessageID:@"MyMessageID123@mail.gmail.com"]; + [builder setHTMLBody:@"This is a HTML content"]; + NSString * path = [_builderOutputPath stringByAppendingPathComponent:@"builder1.eml"]; + NSData * expectedData = [NSData dataWithContentsOfFile:path]; + [builder _setBoundaries:@[@"1", @"2", @"3", @"4", @"5"]]; + //[[builder data] writeToFile:@"/Users/hoa/builder1-now.eml" atomically:YES]; + XCTAssertEqualObjects([builder data], expectedData, @"Pass"); +} + +- (void)testMessageBuilder2 { + MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init]; + [[builder header] setFrom:[MCOAddress addressWithRFC822String:@"Hoà "]]; + [[builder header] setTo:@[[MCOAddress addressWithRFC822String:@"Foo Bar "], [MCOAddress addressWithRFC822String:@"Other Recipient "]]]; + [[builder header] setCc:@[[MCOAddress addressWithRFC822String:@"Carbon Copy "], [MCOAddress addressWithRFC822String:@"Other Recipient "]]]; + [[builder header] setSubject:@"testMessageBuilder2"]; + [[builder header] setDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]]; + [[builder header] setMessageID:@"MyMessageID123@mail.gmail.com"]; + [builder setHTMLBody:@"This is a HTML content"]; + NSString * path = [_builderPath stringByAppendingPathComponent:@"photo.jpg"]; + [builder addAttachment:[MCOAttachment attachmentWithContentsOfFile:path]]; + path = [_builderPath stringByAppendingPathComponent:@"photo2.jpg"]; + [builder addAttachment:[MCOAttachment attachmentWithContentsOfFile:path]]; + [builder _setBoundaries:@[@"1", @"2", @"3", @"4", @"5"]]; + path = [_builderOutputPath stringByAppendingPathComponent:@"builder2.eml"]; + NSData * expectedData = [NSData dataWithContentsOfFile:path]; + //[[builder data] writeToFile:@"/Users/hoa/builder2-now.eml" atomically:YES]; + XCTAssertEqualObjects([builder data], expectedData, @"Pass"); +} + +- (void)testMessageBuilder3 { + MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init]; + [[builder header] setFrom:[MCOAddress addressWithRFC822String:@"Hoà "]]; + [[builder header] setTo:@[[MCOAddress addressWithRFC822String:@"Foo Bar "], [MCOAddress addressWithRFC822String:@"Other Recipient "]]]; + [[builder header] setCc:@[[MCOAddress addressWithRFC822String:@"Carbon Copy "], [MCOAddress addressWithRFC822String:@"Other Recipient "]]]; + [[builder header] setSubject:@"testMessageBuilder3"]; + [[builder header] setDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]]; + [[builder header] setMessageID:@"MyMessageID123@mail.gmail.com"]; + [builder setHTMLBody:@"
This is a HTML content
"]; + NSString * path = [_builderPath stringByAppendingPathComponent:@"photo.jpg"]; + [builder addAttachment:[MCOAttachment attachmentWithContentsOfFile:path]]; + path = [_builderPath stringByAppendingPathComponent:@"photo2.jpg"]; + MCOAttachment * attachment = [MCOAttachment attachmentWithContentsOfFile:path]; + [attachment setContentID:@"123"]; + [builder addRelatedAttachment:attachment]; + [builder _setBoundaries:@[@"1", @"2", @"3", @"4", @"5"]]; + path = [_builderOutputPath stringByAppendingPathComponent:@"builder3.eml"]; + NSData * expectedData = [NSData dataWithContentsOfFile:path]; + //[[builder data] writeToFile:@"/Users/hoa/builder3-now.eml" atomically:YES]; + XCTAssertEqualObjects([builder data], expectedData, @"Pass"); +} + +- (void)testMessageParser { + NSArray * list = [[NSFileManager defaultManager] subpathsAtPath:_parserPath]; + for(NSString * name in list) { + NSString * path = [_parserPath stringByAppendingPathComponent:name]; + BOOL isDirectory = NO; + [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]; + if (isDirectory) { + continue; + } + NSData * data = [NSData dataWithContentsOfFile:path]; + MCOMessageParser * parser = [[MCOMessageParser alloc] initWithData:data]; + [[parser header] prepareForUnitTest]; + [[parser mainPart] prepareForUnitTest]; + mailcore::MessageParser * mcParser = MCO_FROM_OBJC(mailcore::MessageParser, parser); + NSDictionary * result = MCO_TO_OBJC(mcParser->serializable()); + + path = [_parserOutputPath stringByAppendingPathComponent:name]; + NSData * expectedData = [NSData dataWithContentsOfFile:path]; + NSDictionary * expectedResult = [NSJSONSerialization JSONObjectWithData:expectedData options:0 error:NULL]; + + XCTAssertEqualObjects(result, expectedResult, @"file %@", name); + + //mailcore::String * jsonString = mailcore::JSON::objectToJSONString(mcParser->serializable()); + //NSString * str = MCO_TO_OBJC(jsonString); + //NSString * resultPath = [@"/Users/hoa/mc2-results" stringByAppendingPathComponent:name]; + //NSString * directory = [resultPath stringByDeletingLastPathComponent]; + //[[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL]; + //[str writeToFile:resultPath atomically:YES encoding:NSUTF8StringEncoding error:NULL]; + } +} + +@end -- cgit v1.2.3