From 27b64d972cfaa6fef1c043867eab78386bfe6495 Mon Sep 17 00:00:00 2001 From: naskya Date: Tue, 30 Apr 2024 17:12:30 +0900 Subject: [PATCH] chore(napi): update tests Co-authored-by: sup39 --- .../__snapshots__/typegen.spec.ts.md | 28 +++++++- .../__snapshots__/typegen.spec.ts.snap | Bin 4873 -> 4946 bytes examples/napi/__tests__/values.spec.ts | 17 ++++- examples/napi/index.cjs | 9 ++- examples/napi/index.d.cts | 28 +++++++- examples/napi/src/date.rs | 63 +++++++++++++++++- 6 files changed, 135 insertions(+), 10 deletions(-) diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index cc1c64f4..b4138546 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -343,12 +343,26 @@ Generated by [AVA](https://avajs.dev). ␊ export function chronoDateAdd1Minute(input: Date): Date␊ ␊ - export function chronoDateToMillis(input: Date): number␊ + export function chronoDateFixtureReturn1(): Date␊ + ␊ + export function chronoDateFixtureReturn2(): Date␊ + ␊ + export function chronoDateWithTimezoneReturn(): Date | null␊ + ␊ + export function chronoDateWithTimezoneToMillis(input: Date): number␊ + ␊ + export function chronoLocalDateReturn(): Date | null␊ + ␊ + export function chronoLocalDateToMillis(input: Date): number␊ ␊ export function chronoNativeDateTime(date: Date): number␊ ␊ export function chronoNativeDateTimeReturn(): Date | null␊ ␊ + export function chronoUtcDateReturn(): Date | null␊ + ␊ + export function chronoUtcDateToMillis(input: Date): number␊ + ␊ export function concatLatin1(s: string): string␊ ␊ export function concatStr(s: string): string␊ @@ -397,7 +411,7 @@ Generated by [AVA](https://avajs.dev). ␊ export function customStatusCode(): void␊ ␊ - export interface Dates {␊ + export interface DatesWithTimeZone {␊ start: Date␊ end?: Date␊ }␊ @@ -505,6 +519,11 @@ Generated by [AVA](https://avajs.dev). ␊ export function listObjKeys(obj: object): Array␊ ␊ + export interface LocalDates {␊ + start: Date␊ + end?: Date␊ + }␊ + ␊ export function mapOption(val?: number | undefined | null): number | null␊ ␊ export function mutateExternal(external: ExternalObject, newVal: number): void␊ @@ -698,6 +717,11 @@ Generated by [AVA](https://avajs.dev). nullableStringField: string | null␊ }␊ ␊ + export interface UtcDates {␊ + start: Date␊ + end?: Date␊ + }␊ + ␊ export function validateArray(arr: Array): number␊ ␊ export function validateBigint(input: bigint): bigint␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 9dc5d938f76791267ea0aa46b58777b70f4b34d2..24648c6f51911a461077284cc0ca8a366b565807 100644 GIT binary patch literal 4946 zcmV-Y6Rqq)RzVK>)qwA2543c(fRMCD|~XxwVavZ29E1BttXa z-HXq-?dj&s(CTT z@y9otFL$9 zI~6fY{bv3WF8>EhODSu@m?6XtVEzYdp*ww4<3}J4V1p%3pMdw zm-~9OU(=M50R9NR0z#HS3JjzvPzei}zrx%=P?L}f4JDU>BxaX_bWsroYuH_8pT?V! z?)KaFA3oiF{oB!(Uw+aJxA|03Dzh{>M_D{VK|N*$`tCMQD?sm^Pz_5XyjWS)10;P;kVvL^^X*jc3qrEH+7qAFT1v zV9%gO5QcOuC!m?B5m$lFBk~Sx(vxwT1{;@IXb8w;#X)WsL{cG@_E(cK77i^!RYM=f z!6iL7{BsifYjA|RZHy zR${84&pfafRDf#Q3WJuGlHRS+SW(!`mP4uW3hBFTDjsx=M}Mp7`}BHk1|?I_W&^aPK4#zx zv!7^f8rLk6ORLg`WJ%d#DVuGeF}UvpJp;y~hpq(KIM>SAjm{M8F?zd>^0i+liaF+M zzE!pJ9K;Una|hUdc7UHZSb`QRhHXR)TWJ`!lHlPJUuBW~Y-54GfFDehxuWfM>s z4Zc~W^RU!+_m1RONqa>l08G^1%CU{P$neD0ZGv8ltzE&_;sQ<)vc3&dy5=MGdChl? z+_b1q-LZxxVyWu{rE09f?kFF(hUknXAA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sb9}MaN zUjHtyFOIFg?(0OB$I@(6oL(Cz@DIa`XE20+1SaKi3?knQ8hy~+-Y&s5VXIJ0H&qlU+I_J`0|PMn?`C+A9pE=&9bYbb0U!R*Ny#TaEf4b& z`49M@zvNXpejEI%kss{L`d|Oi(E5)_X~_IfOI1lxo+iGJ*aE^Zr-z%UKO$F(x5uwf vU!I@6IG;LO$CZHS{evgP_FZCKp5V7!4ZQI4@DGn4wJ`7RMJur#&{6;Z>qnC> diff --git a/examples/napi/__tests__/values.spec.ts b/examples/napi/__tests__/values.spec.ts index c3c805b1..47a710a6 100644 --- a/examples/napi/__tests__/values.spec.ts +++ b/examples/napi/__tests__/values.spec.ts @@ -126,7 +126,11 @@ import { createObjWithProperty, receiveObjectOnlyFromJs, dateToNumber, - chronoDateToMillis, + chronoUtcDateToMillis, + chronoLocalDateToMillis, + chronoDateWithTimezoneToMillis, + chronoDateFixtureReturn1, + chronoDateFixtureReturn2, derefUint8Array, chronoDateAdd1Minute, bufferPassThrough, @@ -1235,13 +1239,22 @@ Napi5Test('Date test', (t) => { Napi5Test('Date to chrono test', (t) => { const fixture = new Date('2022-02-09T19:31:55.396Z') - t.is(chronoDateToMillis(fixture), fixture.getTime()) + t.is(chronoUtcDateToMillis(fixture), fixture.getTime()) + t.is(chronoLocalDateToMillis(fixture), fixture.getTime()) + t.is(chronoDateWithTimezoneToMillis(fixture), fixture.getTime()) t.deepEqual( chronoDateAdd1Minute(fixture), new Date(fixture.getTime() + 60 * 1000), ) }) +Napi5Test('Get date', (t) => { + const fixture1 = new Date('2024-02-07T18:28:18-0800') + t.deepEqual(chronoDateFixtureReturn1(), fixture1) + const fixture2 = new Date('2024-02-07T18:28:18+0530') + t.deepEqual(chronoDateFixtureReturn2(), fixture2) +}) + Napi5Test('Class with getter setter closures', (t) => { const instance = new GetterSetterWithClosures() // @ts-expect-error diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index e4fd00d3..def2f34e 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -438,9 +438,16 @@ module.exports.callLongThreadsafeFunction = nativeBinding.callLongThreadsafeFunc module.exports.callThreadsafeFunction = nativeBinding.callThreadsafeFunction module.exports.captureErrorInCallback = nativeBinding.captureErrorInCallback module.exports.chronoDateAdd1Minute = nativeBinding.chronoDateAdd1Minute -module.exports.chronoDateToMillis = nativeBinding.chronoDateToMillis +module.exports.chronoDateFixtureReturn1 = nativeBinding.chronoDateFixtureReturn1 +module.exports.chronoDateFixtureReturn2 = nativeBinding.chronoDateFixtureReturn2 +module.exports.chronoDateWithTimezoneReturn = nativeBinding.chronoDateWithTimezoneReturn +module.exports.chronoDateWithTimezoneToMillis = nativeBinding.chronoDateWithTimezoneToMillis +module.exports.chronoLocalDateReturn = nativeBinding.chronoLocalDateReturn +module.exports.chronoLocalDateToMillis = nativeBinding.chronoLocalDateToMillis module.exports.chronoNativeDateTime = nativeBinding.chronoNativeDateTime module.exports.chronoNativeDateTimeReturn = nativeBinding.chronoNativeDateTimeReturn +module.exports.chronoUtcDateReturn = nativeBinding.chronoUtcDateReturn +module.exports.chronoUtcDateToMillis = nativeBinding.chronoUtcDateToMillis module.exports.concatLatin1 = nativeBinding.concatLatin1 module.exports.concatStr = nativeBinding.concatStr module.exports.concatUtf16 = nativeBinding.concatUtf16 diff --git a/examples/napi/index.d.cts b/examples/napi/index.d.cts index e3b775c4..b62656eb 100644 --- a/examples/napi/index.d.cts +++ b/examples/napi/index.d.cts @@ -333,12 +333,26 @@ export function captureErrorInCallback(cb1: () => void, cb2: (arg0: Error) => vo export function chronoDateAdd1Minute(input: Date): Date -export function chronoDateToMillis(input: Date): number +export function chronoDateFixtureReturn1(): Date + +export function chronoDateFixtureReturn2(): Date + +export function chronoDateWithTimezoneReturn(): Date | null + +export function chronoDateWithTimezoneToMillis(input: Date): number + +export function chronoLocalDateReturn(): Date | null + +export function chronoLocalDateToMillis(input: Date): number export function chronoNativeDateTime(date: Date): number export function chronoNativeDateTimeReturn(): Date | null +export function chronoUtcDateReturn(): Date | null + +export function chronoUtcDateToMillis(input: Date): number + export function concatLatin1(s: string): string export function concatStr(s: string): string @@ -387,7 +401,7 @@ export const enum CustomNumEnum { export function customStatusCode(): void -export interface Dates { +export interface DatesWithTimeZone { start: Date end?: Date } @@ -495,6 +509,11 @@ export const enum Kind { export function listObjKeys(obj: object): Array +export interface LocalDates { + start: Date + end?: Date +} + export function mapOption(val?: number | undefined | null): number | null export function mutateExternal(external: ExternalObject, newVal: number): void @@ -688,6 +707,11 @@ export interface UseNullableStruct { nullableStringField: string | null } +export interface UtcDates { + start: Date + end?: Date +} + export function validateArray(arr: Array): number export function validateBigint(input: bigint): bigint diff --git a/examples/napi/src/date.rs b/examples/napi/src/date.rs index e0566d2e..ada4c3d0 100644 --- a/examples/napi/src/date.rs +++ b/examples/napi/src/date.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use chrono::{Duration, Utc}; +use chrono::{Duration, FixedOffset, Local, TimeZone, Utc}; use napi::bindgen_prelude::*; #[napi] @@ -9,7 +9,17 @@ fn date_to_number(input: Date) -> Result { } #[napi] -fn chrono_date_to_millis(input: chrono::DateTime) -> i64 { +fn chrono_utc_date_to_millis(input: chrono::DateTime) -> i64 { + input.timestamp_millis() +} + +#[napi] +fn chrono_local_date_to_millis(input: chrono::DateTime) -> i64 { + input.timestamp_millis() +} + +#[napi] +fn chrono_date_with_timezone_to_millis(input: chrono::DateTime) -> i64 { input.timestamp_millis() } @@ -19,11 +29,23 @@ fn chrono_date_add_1_minute(input: chrono::DateTime) -> chrono::DateTime, pub end: Option>, } +#[napi(object)] +pub struct LocalDates { + pub start: chrono::DateTime, + pub end: Option>, +} + +#[napi(object)] +pub struct DatesWithTimeZone { + pub start: chrono::DateTime, + pub end: Option>, +} + #[napi] pub fn chrono_native_date_time(date: chrono::NaiveDateTime) -> i64 { date.and_utc().timestamp_millis() @@ -33,3 +55,38 @@ pub fn chrono_native_date_time(date: chrono::NaiveDateTime) -> i64 { pub fn chrono_native_date_time_return() -> Option { chrono::NaiveDateTime::from_str("2016-12-23T15:25:59.325").ok() } + +#[napi] +pub fn chrono_utc_date_return() -> Option> { + chrono::DateTime::::from_str("2016-12-23T15:25:59.325").ok() +} + +#[napi] +pub fn chrono_local_date_return() -> Option> { + chrono::DateTime::::from_str("2016-12-23T15:25:59.325").ok() +} + +#[napi] +pub fn chrono_date_with_timezone_return() -> Option> { + chrono::DateTime::::from_str("2016-12-23T15:25:59.325").ok() +} + +#[napi] +pub fn chrono_date_fixture_return1() -> chrono::DateTime { + // Pacific Standard Time: UTC-08:00 + let pst = FixedOffset::west_opt(8 * 3600).unwrap(); + pst + .with_ymd_and_hms(2024, 2, 7, 18, 28, 18) + .single() + .unwrap() +} + +#[napi] +pub fn chrono_date_fixture_return2() -> chrono::DateTime { + // Indian Standard Time: UTC+05:30 + let ist = FixedOffset::east_opt(5 * 3600 + 30 * 60).unwrap(); + ist + .with_ymd_and_hms(2024, 2, 7, 18, 28, 18) + .single() + .unwrap() +}