]> git.saurik.com Git - apple/javascriptcore.git/blob - API/tests/DateTests.mm
JavaScriptCore-7600.1.4.17.5.tar.gz
[apple/javascriptcore.git] / API / tests / DateTests.mm
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
40 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) || (TARGET_OS_MAC && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)
41 static unsigned unitFlags = NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
42 #else
43 static unsigned unitFlags = NSSecondCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
44 #endif
45
46 @implementation DateTests
47 + (void) NSDateToJSDateTest
48 {
49 JSContext *context = [[JSContext alloc] init];
50 NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
51 NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:now];
52 JSValue *jsNow = [JSValue valueWithObject:now inContext:context];
53 int year = [[jsNow invokeMethod:@"getFullYear" withArguments:@[]] toInt32];
54 // Months are 0-indexed for JavaScript Dates.
55 int month = [[jsNow invokeMethod:@"getMonth" withArguments:@[]] toInt32] + 1;
56 int day = [[jsNow invokeMethod:@"getDate" withArguments:@[]] toInt32];
57 int hour = [[jsNow invokeMethod:@"getHours" withArguments:@[]] toInt32];
58 int minute = [[jsNow invokeMethod:@"getMinutes" withArguments:@[]] toInt32];
59 int second = [[jsNow invokeMethod:@"getSeconds" withArguments:@[]] toInt32];
60
61 checkResult(@"NSDate to JS Date", year == [components year]
62 && month == [components month]
63 && day == [components day]
64 && hour == [components hour]
65 && minute == [components minute]
66 && second == [components second]);
67 }
68
69 + (void) JSDateToNSDateTest
70 {
71 JSContext *context = [[JSContext alloc] init];
72 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
73 [formatter setDateFormat:@"MMMM dd',' yyyy hh:mm:ss"];
74 NSDate *februaryFourth2014 = [formatter dateFromString:@"February 4, 2014 11:40:03"];
75 NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:februaryFourth2014];
76 // Months are 0-indexed for JavaScript Dates.
77 JSValue *jsDate = [context[@"Date"] constructWithArguments:@[@2014, @1, @4, @11, @40, @3]];
78
79 int year = [[jsDate invokeMethod:@"getFullYear" withArguments:@[]] toInt32];
80 int month = [[jsDate invokeMethod:@"getMonth" withArguments:@[]] toInt32] + 1;
81 int day = [[jsDate invokeMethod:@"getDate" withArguments:@[]] toInt32];
82 int hour = [[jsDate invokeMethod:@"getHours" withArguments:@[]] toInt32];
83 int minute = [[jsDate invokeMethod:@"getMinutes" withArguments:@[]] toInt32];
84 int second = [[jsDate invokeMethod:@"getSeconds" withArguments:@[]] toInt32];
85
86 checkResult(@"JS Date to NSDate", year == [components year]
87 && month == [components month]
88 && day == [components day]
89 && hour == [components hour]
90 && minute == [components minute]
91 && second == [components second]);
92 }
93
94 + (void) roundTripThroughJSDateTest
95 {
96 JSContext *context = [[JSContext alloc] init];
97 [context evaluateScript:@"function jsReturnDate(date) { return date; }"];
98 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
99 [formatter setDateFormat:@"MMMM dd',' yyyy hh:mm:ss"];
100 NSDate *februaryFourth2014 = [formatter dateFromString:@"February 4, 2014 11:40:03"];
101 NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:februaryFourth2014];
102
103 JSValue *roundTripThroughJS = [context[@"jsReturnDate"] callWithArguments:@[februaryFourth2014]];
104 int year = [[roundTripThroughJS invokeMethod:@"getFullYear" withArguments:@[]] toInt32];
105 // Months are 0-indexed for JavaScript Dates.
106 int month = [[roundTripThroughJS invokeMethod:@"getMonth" withArguments:@[]] toInt32] + 1;
107 int day = [[roundTripThroughJS invokeMethod:@"getDate" withArguments:@[]] toInt32];
108 int hour = [[roundTripThroughJS invokeMethod:@"getHours" withArguments:@[]] toInt32];
109 int minute = [[roundTripThroughJS invokeMethod:@"getMinutes" withArguments:@[]] toInt32];
110 int second = [[roundTripThroughJS invokeMethod:@"getSeconds" withArguments:@[]] toInt32];
111
112 checkResult(@"JS date round trip", year == [components year]
113 && month == [components month]
114 && day == [components day]
115 && hour == [components hour]
116 && minute == [components minute]
117 && second == [components second]);
118 }
119
120 + (void) roundTripThroughObjCDateTest
121 {
122 JSContext *context = [[JSContext alloc] init];
123 context[@"objcReturnDate"] = ^(NSDate *date) {
124 return date;
125 };
126 [context evaluateScript:@"function test() {\
127 var date = new Date(2014, 1, 4, 11, 40, 3); \
128 var result = objcReturnDate(date); \
129 return date.getYear() === result.getYear() \
130 && date.getMonth() === result.getMonth() \
131 && date.getDate() === result.getDate() \
132 && date.getHours() === result.getHours() \
133 && date.getMinutes() === result.getMinutes() \
134 && date.getSeconds() === result.getSeconds() \
135 && date.getMilliseconds() === result.getMilliseconds();\
136 }"];
137
138 checkResult(@"ObjC date round trip", [[context[@"test"] callWithArguments:@[]] toBool]);
139 }
140
141 @end
142
143 void runDateTests()
144 {
145 @autoreleasepool {
146 [DateTests NSDateToJSDateTest];
147 [DateTests JSDateToNSDateTest];
148 [DateTests roundTripThroughJSDateTest];
149 [DateTests roundTripThroughObjCDateTest];
150 }
151 }
152
153 #endif // JSC_OBJC_API_ENABLED