1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16type FormatToPartsResult = [string | undefined, Intl.DateTimeFormatOptions | undefined, Intl.DateTimeFormatPart[]] 17 18const FORMAT_RESULTS = new Array<FormatToPartsResult>() 19 20class FmtPart implements Intl.DateTimeFormatPart { 21 private _type: Intl.DateTimeFormatPartTypes 22 private _value: String 23 24 constructor() { 25 this._type = "literal" 26 this._value = "" 27 } 28 29 constructor(type: Intl.DateTimeFormatPartTypes, value: string) { 30 this._type = type 31 this._value = value 32 } 33 34 get type(): Intl.DateTimeFormatPartTypes { 35 return this._type 36 } 37 38 set type(value: Intl.DateTimeFormatPartTypes) { 39 this._type = value 40 } 41 42 get value(): string { 43 return this._value 44 } 45 46 set value(value: string) { 47 this._value = value 48 } 49 50 static $_invoke(type: Intl.DateTimeFormatPartTypes, value: string):FmtPart { 51 return new FmtPart(type, value) 52 } 53} 54 55function addFormatToPartsResult(locale: string | undefined, options: Intl.DateTimeFormatOptions | undefined, result: Intl.DateTimeFormatPart[]): void { 56 const res: [string | undefined, Intl.DateTimeFormatOptions | undefined, Intl.DateTimeFormatPart[]] = [locale, options, result] 57 FORMAT_RESULTS.push(res) 58} 59 60const TEST_DATE = new Date("2023-10-11T00:20:00Z") 61 62function initFormatResults(): void { 63 if (FORMAT_RESULTS.length > 0) { 64 return 65 } 66 67 const result001:Intl.DateTimeFormatPart[] = [FmtPart("year", "2023"), FmtPart("literal", "/"), FmtPart("month", "10"), FmtPart("literal", "/"), FmtPart("day", "11")] 68 addFormatToPartsResult("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit" }, result001) 69} 70 71function verifyResults(actual: Intl.DateTimeFormatPart[], expected: Intl.DateTimeFormatPart[]): boolean { 72 if (actual.length != expected.length) { 73 return false 74 } 75 76 for (let i = 0; i < actual.length; i++) { 77 if (actual[i].type != expected[i].type || actual[i].value != expected[i].value) { 78 return false 79 } 80 } 81 82 return true 83} 84 85function dateFormatToPartsWithOptions(): void { 86 FORMAT_RESULTS.forEach((res: FormatToPartsResult) => { 87 const locale = res[0] 88 const options = res[1] 89 90 const formatter = new Intl.DateTimeFormat(locale, options) 91 const actualResult = formatter.formatToParts(TEST_DATE) 92 const expectedResult = res[2] 93 94 const failMsg = `Unexpected DateTimeFormat.formatToParts(${locale}, ${options}) result` 95 assertTrue(verifyResults(actualResult, expectedResult), failMsg) 96 }) 97} 98 99function main(): int { 100 initFormatResults() 101 102 const suite = new ArkTestsuite("Intl.DateTimeFormat.formatToParts()") 103 suite.addTest("with formatting options", dateFormatToPartsWithOptions) 104 return suite.run() 105} 106