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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// BUYModelStringAdditionsTests.m
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <XCTest/XCTest.h>
#import "NSString+BUYAdditions.h"
@interface BUYStringAdditionsTests : XCTestCase
@end
static NSString * const lettersAndNumbers = @"abcdefghijklmnopqrstuvwxyz1234567890";
static NSString * const testMatchingString = @"stringstrongstingstringring";
static NSString * const matchPattern = @"str?\\w?ng";
static NSString * const camelCaseString = @"camelCaseString";
static NSString * const camelCaseSTRING = @"camelCaseSTRING";
static NSString * const camelCASEString = @"camelCASEString";
static NSString * const CAMELCaseString = @"CAMELCaseString";
@implementation BUYStringAdditionsTests {
NSArray *_oldAcronyms;
}
- (void)setUp
{
[super setUp];
_oldAcronyms = [NSString buy_acronymStrings];
}
- (void)tearDown
{
[super tearDown];
[NSString buy_setAcronymStrings:_oldAcronyms];
_oldAcronyms = nil;
}
- (void)testReversedString {
NSString *expected = @"0987654321zyxwvutsrqponmlkjihgfedcba";
NSString *actual = [lettersAndNumbers buy_reversedString];
XCTAssertEqualObjects(actual, expected, @"reversed string was incorrect");
}
- (void)testMatchesForRegularExpression {
NSArray *expected = @[@"string", @"strong", @"sting", @"string"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:matchPattern options:0 error:NULL];
NSArray *actual = [testMatchingString buy_matchesForRegularExpression:regex];
XCTAssertEqualObjects(actual, expected, @"patterns did not match");
}
- (void)testFirstMatchForRegularExpression {
NSString *expected = @"string";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:matchPattern options:0 error:NULL];
NSString *actual = [testMatchingString buy_firstMatchForRegularExpression:regex];
XCTAssertEqualObjects(actual, expected, @"patterns did not match");
}
- (void)testMatchesForPattern {
NSArray *expected = @[@"string", @"strong", @"sting", @"string"];
NSArray *actual = [testMatchingString buy_matchesForPattern:matchPattern];
XCTAssertEqualObjects(actual, expected, @"patterns did not match");
}
- (void)testFirstMatchForPattern {
NSString *expected = @"string";
NSString *actual = [testMatchingString buy_firstMatchForPattern:matchPattern];
XCTAssertEqualObjects(actual, expected, @"patterns did not match");
}
- (void)testCamelCaseTokens {
NSArray *expected = @[@"camel", @"Case", @"String"];
NSArray *actual = [camelCaseString buy_camelCaseTokens];
XCTAssertEqualObjects(actual, expected, @"tokens were incorrect");
}
- (void)testCamelCaseALLCAPSTokens {
NSArray *expected = @[@"camel", @"Case", @"STRING"];
NSArray *actual = [camelCaseSTRING buy_camelCaseTokens];
XCTAssertEqualObjects(actual, expected, @"tokens were incorrect");
expected = @[@"camel", @"CASE", @"String"];
actual = [camelCASEString buy_camelCaseTokens];
XCTAssertEqualObjects(actual, expected, @"tokens were incorrect");
expected = @[@"CAMEL", @"Case", @"String"];
actual = [CAMELCaseString buy_camelCaseTokens];
XCTAssertEqualObjects(actual, expected, @"tokens were incorrect");
}
- (void)testSnakeCaseString {
NSString *expected = @"camel_case_string";
NSString *actual = [camelCaseString buy_snakeCaseString];
XCTAssertEqualObjects(actual, expected, @"snake case string was incorrect");
actual = [camelCaseSTRING buy_snakeCaseString];
XCTAssertEqualObjects(actual, expected, @"snake case string was incorrect");
actual = [camelCASEString buy_snakeCaseString];
XCTAssertEqualObjects(actual, expected, @"snake case string was incorrect");
actual = [CAMELCaseString buy_snakeCaseString];
XCTAssertEqualObjects(actual, expected, @"snake case string was incorrect");
}
- (void)testCamelCaseString {
NSString *expected = @"snakeCaseString";
NSString *actual = [@"snake_case_string" buy_camelCaseString];
XCTAssertEqualObjects(actual, expected, @"camel case string was incorrect");
}
- (void)testAcronyms {
NSArray *expected = @[ @"url"];
NSArray *actual = [NSString buy_acronymStrings];
XCTAssertEqualObjects(actual, expected, @"Default acronyms not correct");
NSArray *acronyms = @[ @"hello" ];
[NSString buy_setAcronymStrings:acronyms];
expected = acronyms;
actual = [NSString buy_acronymStrings];
XCTAssertEqualObjects(actual, expected, @"Custom acronyms not correct");
}
- (void)testCamelCaseStringWithAcronyms {
NSString *expected = @"URLString";
NSString *actual = [@"url_string" buy_camelCaseString];
XCTAssertEqualObjects(actual, expected, @"camel case string was incorrect");
expected = @"stringWithURL";
actual = [@"string_with_url" buy_camelCaseString];
XCTAssertEqualObjects(actual, expected, @"camel case string was incorrect");
[NSString buy_setAcronymStrings:@[@"awol", @"GDP", @"sdk"]];
expected = @"goneAWOL";
actual = [@"gone_awol" buy_camelCaseString];
XCTAssertEqualObjects(actual, expected, @"camel case string was incorrect");
expected = @"SDKTypes";
actual = [@"sdk_types" buy_camelCaseString];
XCTAssertEqualObjects(actual, expected, @"camel case string was incorrect");
expected = @"GDPCanada";
actual = [@"gdp_canada" buy_camelCaseString];
XCTAssertEqualObjects(actual, expected, @"camel case string was incorrect");
}
- (void)testReplacingFileName
{
NSString *path = @"/path/to/file.txt";
NSString *expected = @"/path/to/other_file.txt";
NSString *actual = [path buy_stringByReplacingBaseFileName:@"other_file"];
XCTAssertEqualObjects(actual, expected);
}
- (void)testReplacingDirectory
{
NSString *path = @"/path/to/file.txt";
NSString *expected = @"/directory_2/file.txt";
NSString *actual = [path buy_stringByReplacingDirectory:@"/directory_2"];
XCTAssertEqualObjects(actual, expected);
}
- (void)testStrippingHTML
{
NSString *rawHTML = @"<!DOCTYPE html>\
<html>\
<body>\
\
<h1>My First Heading</h1>\
\
<p>My first paragraph.</p>\
\
</body>\
</html>";
NSString *expected = @"My First Heading\nMy first paragraph.\n";
NSString *actual = [rawHTML buy_stringByStrippingHTML];
XCTAssertEqualObjects(actual, expected);
}
- (void)testAttributeStringWithLineSpacing
{
NSString *string = @"Hello, world";
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 2.0;
style.alignment = NSTextAlignmentCenter;
NSAttributedString *expected = [[NSAttributedString alloc] initWithString:string attributes:@{ NSParagraphStyleAttributeName : style }];
NSAttributedString *actual = [string buy_attributedStringWithLineSpacing:2.0 textAlignment:NSTextAlignmentCenter];
XCTAssertEqualObjects(actual, expected);
}
- (void)testTrim
{
NSString *string = @" \t\n Hello \t\n";
NSString *expected = @"Hello";
NSString *actual = [string buy_trim];
XCTAssertEqualObjects(actual, expected);
}
@end