]>
Commit | Line | Data |
---|---|---|
81345200 A |
1 | /* |
2 | * Copyright (C) 2014 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
23 | * THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #import "DateTests.h" | |
27 | #import <Foundation/Foundation.h> | |
28 | ||
29 | #if JSC_OBJC_API_ENABLED | |
30 | ||
31 | extern "C" void checkResult(NSString *description, bool passed); | |
32 | ||
33 | @interface DateTests : NSObject | |
34 | + (void) NSDateToJSDateTest; | |
35 | + (void) JSDateToNSDateTest; | |
36 | + (void) roundTripThroughJSDateTest; | |
37 | + (void) roundTripThroughObjCDateTest; | |
38 | @end | |
39 | ||
81345200 | 40 | static unsigned unitFlags = NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear; |
81345200 A |
41 | |
42 | @implementation DateTests | |
43 | + (void) NSDateToJSDateTest | |
44 | { | |
45 | JSContext *context = [[JSContext alloc] init]; | |
46 | NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0]; | |
47 | NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:now]; | |
48 | JSValue *jsNow = [JSValue valueWithObject:now inContext:context]; | |
49 | int year = [[jsNow invokeMethod:@"getFullYear" withArguments:@[]] toInt32]; | |
50 | // Months are 0-indexed for JavaScript Dates. | |
51 | int month = [[jsNow invokeMethod:@"getMonth" withArguments:@[]] toInt32] + 1; | |
52 | int day = [[jsNow invokeMethod:@"getDate" withArguments:@[]] toInt32]; | |
53 | int hour = [[jsNow invokeMethod:@"getHours" withArguments:@[]] toInt32]; | |
54 | int minute = [[jsNow invokeMethod:@"getMinutes" withArguments:@[]] toInt32]; | |
55 | int second = [[jsNow invokeMethod:@"getSeconds" withArguments:@[]] toInt32]; | |
56 | ||
57 | checkResult(@"NSDate to JS Date", year == [components year] | |
58 | && month == [components month] | |
59 | && day == [components day] | |
60 | && hour == [components hour] | |
61 | && minute == [components minute] | |
62 | && second == [components second]); | |
63 | } | |
64 | ||
65 | + (void) JSDateToNSDateTest | |
66 | { | |
67 | JSContext *context = [[JSContext alloc] init]; | |
68 | NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
69 | [formatter setDateFormat:@"MMMM dd',' yyyy hh:mm:ss"]; | |
70 | NSDate *februaryFourth2014 = [formatter dateFromString:@"February 4, 2014 11:40:03"]; | |
71 | NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:februaryFourth2014]; | |
72 | // Months are 0-indexed for JavaScript Dates. | |
73 | JSValue *jsDate = [context[@"Date"] constructWithArguments:@[@2014, @1, @4, @11, @40, @3]]; | |
74 | ||
75 | int year = [[jsDate invokeMethod:@"getFullYear" withArguments:@[]] toInt32]; | |
76 | int month = [[jsDate invokeMethod:@"getMonth" withArguments:@[]] toInt32] + 1; | |
77 | int day = [[jsDate invokeMethod:@"getDate" withArguments:@[]] toInt32]; | |
78 | int hour = [[jsDate invokeMethod:@"getHours" withArguments:@[]] toInt32]; | |
79 | int minute = [[jsDate invokeMethod:@"getMinutes" withArguments:@[]] toInt32]; | |
80 | int second = [[jsDate invokeMethod:@"getSeconds" withArguments:@[]] toInt32]; | |
81 | ||
82 | checkResult(@"JS Date to NSDate", year == [components year] | |
83 | && month == [components month] | |
84 | && day == [components day] | |
85 | && hour == [components hour] | |
86 | && minute == [components minute] | |
87 | && second == [components second]); | |
88 | } | |
89 | ||
90 | + (void) roundTripThroughJSDateTest | |
91 | { | |
92 | JSContext *context = [[JSContext alloc] init]; | |
93 | [context evaluateScript:@"function jsReturnDate(date) { return date; }"]; | |
94 | NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
95 | [formatter setDateFormat:@"MMMM dd',' yyyy hh:mm:ss"]; | |
96 | NSDate *februaryFourth2014 = [formatter dateFromString:@"February 4, 2014 11:40:03"]; | |
97 | NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:februaryFourth2014]; | |
98 | ||
99 | JSValue *roundTripThroughJS = [context[@"jsReturnDate"] callWithArguments:@[februaryFourth2014]]; | |
100 | int year = [[roundTripThroughJS invokeMethod:@"getFullYear" withArguments:@[]] toInt32]; | |
101 | // Months are 0-indexed for JavaScript Dates. | |
102 | int month = [[roundTripThroughJS invokeMethod:@"getMonth" withArguments:@[]] toInt32] + 1; | |
103 | int day = [[roundTripThroughJS invokeMethod:@"getDate" withArguments:@[]] toInt32]; | |
104 | int hour = [[roundTripThroughJS invokeMethod:@"getHours" withArguments:@[]] toInt32]; | |
105 | int minute = [[roundTripThroughJS invokeMethod:@"getMinutes" withArguments:@[]] toInt32]; | |
106 | int second = [[roundTripThroughJS invokeMethod:@"getSeconds" withArguments:@[]] toInt32]; | |
107 | ||
108 | checkResult(@"JS date round trip", year == [components year] | |
109 | && month == [components month] | |
110 | && day == [components day] | |
111 | && hour == [components hour] | |
112 | && minute == [components minute] | |
113 | && second == [components second]); | |
114 | } | |
115 | ||
116 | + (void) roundTripThroughObjCDateTest | |
117 | { | |
118 | JSContext *context = [[JSContext alloc] init]; | |
119 | context[@"objcReturnDate"] = ^(NSDate *date) { | |
120 | return date; | |
121 | }; | |
122 | [context evaluateScript:@"function test() {\ | |
123 | var date = new Date(2014, 1, 4, 11, 40, 3); \ | |
124 | var result = objcReturnDate(date); \ | |
125 | return date.getYear() === result.getYear() \ | |
126 | && date.getMonth() === result.getMonth() \ | |
127 | && date.getDate() === result.getDate() \ | |
128 | && date.getHours() === result.getHours() \ | |
129 | && date.getMinutes() === result.getMinutes() \ | |
130 | && date.getSeconds() === result.getSeconds() \ | |
131 | && date.getMilliseconds() === result.getMilliseconds();\ | |
132 | }"]; | |
133 | ||
134 | checkResult(@"ObjC date round trip", [[context[@"test"] callWithArguments:@[]] toBool]); | |
135 | } | |
136 | ||
137 | @end | |
138 | ||
139 | void runDateTests() | |
140 | { | |
141 | @autoreleasepool { | |
142 | [DateTests NSDateToJSDateTest]; | |
143 | [DateTests JSDateToNSDateTest]; | |
144 | [DateTests roundTripThroughJSDateTest]; | |
145 | [DateTests roundTripThroughObjCDateTest]; | |
146 | } | |
147 | } | |
148 | ||
149 | #endif // JSC_OBJC_API_ENABLED |