2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import <Foundation/Foundation.h>
29 #if JSC_OBJC_API_ENABLED
31 extern "C" void checkResult(NSString *description, bool passed);
33 @interface DateTests : NSObject
34 + (void) NSDateToJSDateTest;
35 + (void) JSDateToNSDateTest;
36 + (void) roundTripThroughJSDateTest;
37 + (void) roundTripThroughObjCDateTest;
40 static unsigned unitFlags = NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
42 @implementation DateTests
43 + (void) NSDateToJSDateTest
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];
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]);
65 + (void) JSDateToNSDateTest
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]];
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];
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]);
90 + (void) roundTripThroughJSDateTest
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];
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];
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]);
116 + (void) roundTripThroughObjCDateTest
118 JSContext *context = [[JSContext alloc] init];
119 context[@"objcReturnDate"] = ^(NSDate *date) {
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();\
134 checkResult(@"ObjC date round trip", [[context[@"test"] callWithArguments:@[]] toBool]);
142 [DateTests NSDateToJSDateTest];
143 [DateTests JSDateToNSDateTest];
144 [DateTests roundTripThroughJSDateTest];
145 [DateTests roundTripThroughObjCDateTest];
149 #endif // JSC_OBJC_API_ENABLED