| 1 |
use js_sys::*; |
| 2 |
use wasm_bindgen::prelude::*; |
| 3 |
use wasm_bindgen_test::*; |
| 4 |
|
| 5 |
#[wasm_bindgen(module = "tests/wasm/Temporal.js")] |
| 6 |
extern "C" { |
| 7 |
fn is_temporal_supported() -> bool; |
| 8 |
} |
| 9 |
|
| 10 |
#[wasm_bindgen_test] |
| 11 |
fn now() { |
| 12 |
if !is_temporal_supported() { |
| 13 |
return; |
| 14 |
} |
| 15 |
let x = Temporal::Now::plain_date("iso8086"); |
| 16 |
let y: JsValue = x.into(); |
| 17 |
assert!(y.is_object()); |
| 18 |
} |
| 19 |
|
| 20 |
#[wasm_bindgen_test] |
| 21 |
fn duration() { |
| 22 |
if !is_temporal_supported() { |
| 23 |
return; |
| 24 |
} |
| 25 |
let x = Temporal::Duration::from(&"PT1.00001S".into()); |
| 26 |
assert_eq!(x.years(), 0); |
| 27 |
assert_eq!(x.microseconds(), 10); |
| 28 |
} |
| 29 |
|
| 30 |
#[wasm_bindgen_test] |
| 31 |
fn zoned_date_time() { |
| 32 |
if !is_temporal_supported() { |
| 33 |
return; |
| 34 |
} |
| 35 |
let zdt = Temporal::ZonedDateTime::from(&"2020-03-09T00:00-07:00[America/Los_Angeles]".into()); |
| 36 |
let earlier_day = zdt.subtract( |
| 37 |
&Temporal::Duration::from(&"P1D".into()), |
| 38 |
&JsValue::UNDEFINED, |
| 39 |
); |
| 40 |
let earlier_hours = zdt.subtract( |
| 41 |
&Temporal::Duration::from(&"PT24H".into()), |
| 42 |
&JsValue::UNDEFINED, |
| 43 |
); |
| 44 |
assert_eq!(earlier_day.hour(), 0); |
| 45 |
assert_eq!(earlier_hours.hour(), 23); |
| 46 |
} |
| 47 |
|
| 48 |
#[wasm_bindgen_test] |
| 49 |
fn plain_time() { |
| 50 |
if !is_temporal_supported() { |
| 51 |
return; |
| 52 |
} |
| 53 |
let time = Temporal::PlainTime::from(&"19:39:09.068346205".into()); |
| 54 |
assert_eq!(time.microsecond(), 346); |
| 55 |
assert_eq!(time.round(&"hour".into()).hour(), 20); |
| 56 |
} |
| 57 |
|
| 58 |
#[wasm_bindgen_test] |
| 59 |
fn plain_date() { |
| 60 |
if !is_temporal_supported() { |
| 61 |
return; |
| 62 |
} |
| 63 |
let x = Temporal::PlainDate::new(2022, 2, 2).with_calendar("persian"); |
| 64 |
let y = Temporal::PlainDate::new(2022, 2, 5); |
| 65 |
let z = Temporal::PlainDate::from(&"-002022-02-02[u-ca=gregory]".into()); |
| 66 |
assert_eq!(Temporal::PlainDate::compare(&x, &y), -1); |
| 67 |
assert_eq!(x.year(), 1400); |
| 68 |
assert_eq!(x.month(), 11); |
| 69 |
assert_eq!(x.month_code(), "M11"); |
| 70 |
assert_eq!(x.day(), 13); |
| 71 |
assert_eq!(x.era(), "ap"); |
| 72 |
assert_eq!(y.era(), JsValue::UNDEFINED); |
| 73 |
assert_eq!(z.era_year(), 2023i32); |
| 74 |
assert_eq!(y.day_of_week(), 6); |
| 75 |
assert_eq!( |
| 76 |
Reflect::get(&x.get_iso_fields(), &"isoYear".into()).unwrap(), |
| 77 |
2022i32 |
| 78 |
); |
| 79 |
assert_eq!(x.in_leap_year(), false); |
| 80 |
} |