BUYTransformerTests.m 6.81 KB
Newer Older
1
//
2
//  BUYTransformerTests.m
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
//  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>

29 30
#import "BUYDateTransformer.h"
#import "BUYDecimalNumberTransformer.h"
31
#import "BUYFlatCollectionTransformer.h"
32 33
#import "BUYIdentityTransformer.h"
#import "BUYURLTransformer.h"
34

35
@interface BUYTransformerTests : XCTestCase
36 37 38

@end

39
@implementation BUYTransformerTests
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

- (void)testIdentityTransformer {

	id value = @"value";
	NSValueTransformer *transformer = [self identityTransformer];

	id expected = value;
	id actual = [transformer transformedValue:value];
	XCTAssertEqualObjects(actual, expected, @"transformed value was incorrect");

	actual = [transformer reverseTransformedValue:value];
	XCTAssertEqualObjects(actual, expected, @"reverse transformed value was incorrect");
}

- (void)testDecimalTransformer {
	
	id value = [NSDecimalNumber decimalNumberWithMantissa:256 exponent:-2 isNegative:NO];
	id string = @"2.56";
58
	NSValueTransformer *transformer = [[BUYDecimalNumberTransformer alloc] init];
59 60 61 62 63 64 65 66 67 68 69 70
	
	id expected = string;
	id actual = [transformer transformedValue:value];
	XCTAssertEqualObjects(actual, expected, @"transformed value was incorrect");
	
	expected = value;
	actual = [transformer reverseTransformedValue:string];
	XCTAssertEqualObjects(actual, expected, @"reverse transformed value was incorrect");
}

- (void)testPublicationDateTransformer {
	
71
	NSValueTransformer *transformer = [BUYDateTransformer dateTransformerWithFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	
	NSDate *value = [self dateForTransformerTestingWithMilliseconds:0];
	NSString *string = @"1970-06-21T08:11:59-0400";
	
	id expected = string;
	id actual = [transformer transformedValue:value];
	XCTAssertEqualObjects(actual, expected, @"Transformed date with incorrect");
	
	expected = value;
	actual = [transformer reverseTransformedValue:string];
	XCTAssertEqualObjects(actual, expected, @"Reverse transformed date with incorrect");
}

- (void)testShippingDateTransformer {
	
87
	NSValueTransformer *transformer = [BUYDateTransformer dateTransformerWithFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
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
	
	NSDate *value = [self dateForTransformerTestingWithMilliseconds:543];
	NSString *string = @"1970-06-21T08:11:59.543-0400";
	
	id expected = string;
	id actual = [transformer transformedValue:value];
	XCTAssertEqualObjects(actual, expected, @"Transformed date with incorrect");
	
	expected = value;
	actual = [transformer reverseTransformedValue:string];
	XCTAssertEqualObjects(actual, expected, @"Reverse transformed date with incorrect");
}

- (NSDate *)dateForTransformerTestingWithMilliseconds:(NSInteger)milliseconds {
	
	NSDateComponents *dc = [[NSDateComponents alloc] init];
	dc.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
	dc.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
	dc.year = 1970;
	dc.month = 6;
	dc.day = 21;
	dc.hour = 8;
	dc.minute = 11;
	dc.second = 59;
	dc.nanosecond = NSEC_PER_MSEC * milliseconds;
	return dc.date;
}

- (void)testFlatArrayTransformer {
	
118
	NSValueTransformer *elementTransformer = [[BUYURLTransformer alloc] init];
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
	NSValueTransformer *arrayTransformer = [BUYFlatCollectionTransformer arrayTransformerWithElementTransformer:elementTransformer separator:@"||"];
	
	NSString *URLString1 = @"http://www.shopify.com";
	NSString *URLString2 = @"https://test-store.myshopify.com/meta.json";
	NSURL *testURL = [self urlForTesting];
	NSString *URLString3 = [testURL absoluteString];
	NSArray *array = @[
					   [NSURL URLWithString:URLString1],
					   [NSURL URLWithString:URLString2],
					   testURL,
					   ];
	NSString *string = [NSString stringWithFormat:@"%@||%@||%@", URLString1, URLString2, URLString3];
	
	id expected = string;
	id actual = [arrayTransformer transformedValue:array];
	XCTAssertEqualObjects(actual, expected, @"String encoding of flat array of URLs was incorrect");
	
	expected = array;
	actual = [arrayTransformer reverseTransformedValue:string];
	XCTAssertEqualObjects(actual, expected, @"Array of URLs decoded from string was incorrect");
}

- (NSURL *)urlForTesting {
	NSURLComponents *components = [[NSURLComponents alloc] init];
	
	components.scheme = @"ftp";
	components.host = @"private.example.com";
	components.path = @"/authenticated/internal/account";
	components.port = @3322;

	components.query = @"settings=recent";

	components.user = @"user123";
	components.password = @"secret!@#$";
	
	return [components URL];
}

- (void)testFlatSetTransformer {

	NSValueTransformer *setTransformer = [BUYFlatCollectionTransformer setTransformerWithElementTransformer:[self identityTransformer] separator:@", "];
	
	// We can't verify the intermediate string version without decoding it into a set directly,
	// because we can't control the order of the values
	id expected = [NSSet setWithObjects:@"A", @"B", @"C", nil];
	id actual = [setTransformer reverseTransformedValue:[setTransformer transformedValue:expected]];
	XCTAssertEqualObjects(actual, expected, @"Set of strings was not the same after round trip transformation");
}

- (void)testFlatOrderedSetTransformer {
	
	NSValueTransformer *orderedSetTransformer = [BUYFlatCollectionTransformer orderedSetTransformerWithElementTransformer:[self identityTransformer] separator:@", "];
	
	NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithObjects:@"1", @"2", @"3", nil];
	NSString *string = @"1, 2, 3";
	
	id expected = string;
	id actual = [orderedSetTransformer transformedValue:orderedSet];
	XCTAssertEqualObjects(actual, expected, @"String encoding of flat ordered set was incorrect");
	
	expected = orderedSet;
	actual = [orderedSetTransformer reverseTransformedValue:string];
	XCTAssertEqualObjects(actual, expected, @"Ordered set decoded from string was incorrect");
}

- (NSValueTransformer *)identityTransformer {
185
	return [[BUYIdentityTransformer alloc] init];
186 187 188
}

@end