From 549c57f8a667f902f71b1f03f4f1d9f3b3d13a35 Mon Sep 17 00:00:00 2001 From: Luc Charland Date: Wed, 30 Sep 2015 11:02:14 -0400 Subject: [PATCH] Added new tests for NGMailAddressParser --- Tests/Unit/GNUmakefile | 4 +- Tests/Unit/TestNGMailAddressParser.m | 146 +++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 Tests/Unit/TestNGMailAddressParser.m diff --git a/Tests/Unit/GNUmakefile b/Tests/Unit/GNUmakefile index 70f3ff242..2f2c69231 100644 --- a/Tests/Unit/GNUmakefile +++ b/Tests/Unit/GNUmakefile @@ -27,7 +27,9 @@ $(TEST_TOOL)_OBJC_FILES += \ TestNSData+Crypto.m \ TestNSString+Crypto.m \ TestNSString+URLEscaping.m \ - TestNSString+Utilities.m + TestNSString+Utilities.m \ + \ + TestNGMailAddressParser.m TEST_TOOL_NAME = $(TEST_TOOL) diff --git a/Tests/Unit/TestNGMailAddressParser.m b/Tests/Unit/TestNGMailAddressParser.m new file mode 100644 index 000000000..b8680f8e3 --- /dev/null +++ b/Tests/Unit/TestNGMailAddressParser.m @@ -0,0 +1,146 @@ +/* TestNGMailAddressParser.m - this file is part of SOGo + * + * Copyright (C) 2015 Inverse inc. + * + * Author: Luc Charland + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#import +#import + +#import "SOGoTest.h" + +@interface TestNGMailAddressParser : SOGoTest +@end + +@implementation TestNGMailAddressParser + +/* important: this file must be encoded in iso-8859-1, due to issues with the + objc compiler */ +- (void) test_singleEmailParsing_value_ +{ + NSArray *rawAddresses = [NSArray arrayWithObjects: + @"wolfgang@test.com", // email alone + @"", // email between brackets + @"\"\" ", // doubled +// @"\"wolfgang@inverse.ca\" ", // with and without br. + @"Àñinéoblabla ", // accented full name + @"Àñinéoblabla Bla Blé ", // accented and multiword + @"Wolfgang Sourdeau \"Bla Bla\" ", // partly quoted + @"Wolfgang Sourdeau ", // full name + email + @"Wolfgang, Sourdeau ", // full name with comma + email + @"wolf", // name only, no domain + nil ]; + NSArray *expectedAddresses = [NSArray arrayWithObjects: + @"wolfgang@test.com", // email alone + @"wolfgang@test.com", // email between brackets + @"wolfgang@test.com", // doubled +// @"\"wolfgang@inverse.ca\" ", // with and without br. + @"wolfgang@test.com", // accented full name + @"wolfgang@test.com", // accented + // and multiword + + /* NOTE: the following are wrong but tolerated for now */ + @"wolfgang@test.com", // partly quoted + @"wolfgang@test.com", // full name + email + @"wolfgang@test.com", // full name with comma + email + @"wolf", // name only, no domain + nil ]; + NSString *rawAddress, *currentExp, *result, *error; + NGMailAddressParser *parser; + NGMailAddress *parsedRecipient; + + int count = 0; + for (count = 0; count < [rawAddresses count]; count++) + { + rawAddress = [rawAddresses objectAtIndex:count]; + currentExp = [expectedAddresses objectAtIndex:count]; + parser = [NGMailAddressParser mailAddressParserWithString: rawAddress]; + parsedRecipient = [parser parse]; + //NSLog(@"COUNT: %d", count); + //NSLog(@"\trawAddress: %@", rawAddress); + //NSLog(@"\tparsedRecipient: %@", parsedRecipient); + result = [parsedRecipient address]; + //NSLog(@"\tresult: %@", result); + //NSLog(@"\tcurrentExp: %@", currentExp); + error = [NSString + stringWithFormat: @"received '%@' instead of '%@' for '%@'", + result, currentExp, rawAddress]; + testWithMessage([result isEqualToString: currentExp], error); + } +} + +- (void) test_multipleEmailParsing_value_ +{ + NSArray *rawAddresses = [NSArray arrayWithObjects: + @"wolfgang@test.com", // email alone + @"test1a@test.com, test1b@here.now", + @"\"wolfgang@inverse.ca\" ", // with and without br. + @"Wolf One , Wolf Two ", // TWO full names + email + @"Three, Wolf , Four, Wolf ", // TWO full names with comma + email + @"luc, francis", // Two partial names + @"Three A , Three B , Three C ", // Three mails + nil ]; + NSArray *expectedAddresses = [NSArray arrayWithObjects: + [NSArray arrayWithObjects: @"wolfgang@test.com", nil], // email alone + [NSArray arrayWithObjects: @"test1a@test.com", @"test1b@here.now", nil], // test1 a/b + [NSArray arrayWithObjects: @"wolfgang@test.com", nil], // with and without br. + [NSArray arrayWithObjects: @"test2a@test.com", @"test2b@here.now", nil], // test2 a/b + [NSArray arrayWithObjects: @"test3a@test.com", @"test3b@here.now", nil], // test3 a/b + [NSArray arrayWithObjects: @"luc", @"francis", nil], + [NSArray arrayWithObjects: @"threea@test.com", @"threeb@test.com", @"threec@test.com", nil], // test a/b/c + nil ]; + NSString *currentRaw, *currentExp, *result, *error; + NGMailAddressParser *parser = nil; + NSArray *parsedRecipients = nil; + NSArray *expectedRecipients = nil; + NGMailAddress *parsedRecipient = nil; + + int count = 0; + for (count = 0; count < [rawAddresses count]; count++) + { + currentRaw = [rawAddresses objectAtIndex: count]; + expectedRecipients = [expectedAddresses objectAtIndex: count]; + //NSLog(@"COUNT: %d", count); + //NSLog(@"\tcurrentRaw: %@", currentRaw); + //NSLog(@"\texpectedRecipients: %@", expectedRecipients); + parser = [NGMailAddressParser mailAddressParserWithString: currentRaw]; + parsedRecipients = [parser parseAddressList]; + //NSLog(@"\tparsedRecipients: %@ (count %d)", parsedRecipients, [parsedRecipients count]); + int innercount; + for (innercount = 0; innercount < [parsedRecipients count]; innercount++) + { + //NSLog(@"\t\tcount: %d innercount:%d", count, innercount); + parsedRecipient = [parsedRecipients objectAtIndex:innercount]; + //NSLog(@"\t\tparsedRecipient: %@", parsedRecipient); + result = [parsedRecipient address]; + //NSLog(@"\t\tresult: %@", result); + currentExp = [expectedRecipients objectAtIndex:innercount]; + //NSLog(@"\t\tcurrentExp: %@", result, currentExp); + error = [NSString + stringWithFormat: @"received '%@' instead of '%@' for '%@'", + result, currentExp, currentRaw]; + testWithMessage([result isEqualToString: currentExp], error); + } + + currentRaw++; + currentExp++; + } +} +@end +