File: TestNGMailAddressParser.m

package info (click to toggle)
sogo 5.12.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,828 kB
  • sloc: objc: 122,550; javascript: 75,288; sh: 2,352; ansic: 2,055; perl: 861; python: 777; sql: 307; makefile: 187; php: 43; xml: 27
file content (128 lines) | stat: -rw-r--r-- 5,737 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* TestNGMailAddressParser.m - this file is part of SOGo
 *
 * Copyright (C) 2015 Inverse inc.
 *
 * 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 <NGMail/NGMailAddressParser.h>
#import <NGMail/NGMailAddress.h>

#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: 
        @"johndown@test.com",    // email alone
        @"<johndown@test.com>",  // email between brackets
        @"\"<johndown@test.com>\" <johndown@test.com>", // doubled
        @"\"johndown@inverse.ca\" <johndown@test.com>", // with and without br.
        @"=?utf-8?q?=C3=80=C3=B1in=C3=A9oblabla?= <johndown@test.com>", // accented full name
        @"=?utf-8?q?=C3=80=C3=B1in=C3=A9oblabla_Bla_Bl=C3=A9?= <johndown@test.com>", // accented and multiword
        @"John Down \"Bla Bla\" <johndown@test.com>", // partly quoted
        @"John Down <johndown@test.com>", // full name + email
        @"John, Down <johndown@test.com>", // full name with comma + email
        @"john", // name only, no domain
        nil ];
  NSArray *expectedAddresses = [NSArray arrayWithObjects:
        @"johndown@test.com", // email alone
        @"johndown@test.com", // email between brackets
        @"johndown@test.com", // doubled
        @"johndown@test.com", // with and without br.
        @"johndown@test.com", // accented full name
        @"johndown@test.com", // accented
        // and multiword
        /* NOTE: the following are wrong but tolerated for now */
        @"johndown@test.com", // partly quoted
        @"johndown@test.com", // full name + email
        @"johndown@test.com", // full name with comma + email
        @"john", // 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];
      result = [parsedRecipient address];
      error = [NSString
                stringWithFormat: @"[%d] received '%@' instead of '%@' for '%@'",
                count, result, currentExp, rawAddress];
      testWithMessage([result isEqualToString: currentExp], error);
    }
}

- (void) test_multipleEmailParsing_value_
{
  NSArray *rawAddresses = [NSArray arrayWithObjects:
        @"johndown@test.com",    // email alone
        @"test1a@test.com, test1b@here.now",
        @"\"johndown@inverse.ca\" <johndown@test.com>", // with and without br.
        @"John One <test2a@test.com>, John Two <test2b@here.now>", // TWO full names + email
        @"Three, John <test3a@test.com>, Four, John <test3b@here.now>", // TWO full names with comma + email
        @"john, down", // Two partial names
        @"Three A <threea@test.com>, Three B <threeb@test.com>, Three C <threec@test.com>", // Three mails
        nil ];
  NSArray *expectedAddresses = [NSArray arrayWithObjects:
        [NSArray arrayWithObjects: @"johndown@test.com", nil],    // email alone
        [NSArray arrayWithObjects: @"test1a@test.com", @"test1b@here.now", nil],   // test1 a/b
        [NSArray arrayWithObjects: @"johndown@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: @"john",  @"down", 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];
      parser = [NGMailAddressParser mailAddressParserWithString: currentRaw];
      parsedRecipients = [parser parseAddressList];
      int innercount;
      for (innercount = 0; innercount < [parsedRecipients count]; innercount++)
        {
          parsedRecipient = [parsedRecipients objectAtIndex:innercount];
          result = [parsedRecipient address];
          currentExp = [expectedRecipients objectAtIndex:innercount];
          error = [NSString
                stringWithFormat: @"received '%@' instead of '%@' for '%@'",
                result, currentExp, currentRaw];
          testWithMessage([result isEqualToString: currentExp], error);
        }

    }
}
@end