|
|
@@ -0,0 +1,494 @@
|
|
|
+// Tests for runner/src/intl_shim_js.inc - the minimal `Intl.DateTimeFormat`
|
|
|
+// the engine core installs into every context.
|
|
|
+//
|
|
|
+// WHY a file of its own rather than more cases in host_prelude_test.cpp: the
|
|
|
+// prelude test stands fake recorders in for the host bindings, which is exactly
|
|
|
+// right for "was the argument checked before the host was reached" and exactly
|
|
|
+// wrong here. What has to be asserted about a date formatter is that it prints
|
|
|
+// the right local time, and the only thing that knows that is the tz database.
|
|
|
+//
|
|
|
+// So this file links functions/timezone.cpp (pure, no database client) and
|
|
|
+// builds the shim's zone backend out of REAL `zonePartsAt` answers, injected as
|
|
|
+// a lookup table. The Budapest instants below straddle both DST transitions, so
|
|
|
+// a wrong offset cannot pass.
|
|
|
+//
|
|
|
+// The other half of the point is the LOAD-TIME path: five of six real functions
|
|
|
+// failed to register because they built a formatter at module scope and
|
|
|
+// quickjs-ng has no Intl. That is asserted through readExport(), which is the
|
|
|
+// call registration actually makes, and which installs no host bindings at all.
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+#include <string>
|
|
|
+#include <utility>
|
|
|
+#include <vector>
|
|
|
+
|
|
|
+#include "functions/timezone.hpp"
|
|
|
+#include "runner/js_runtime.hpp"
|
|
|
+
|
|
|
+using svapi::runner::JsBytecode;
|
|
|
+using svapi::runner::JsResult;
|
|
|
+using svapi::runner::JsRuntime;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+// 2026-01-15 12:00:00Z -> 13:00 CET in Budapest, a Thursday.
|
|
|
+constexpr int64_t kWinter = 1768478400000LL;
|
|
|
+// 2026-07-15 12:00:00Z -> 14:00 CEST, a Wednesday.
|
|
|
+constexpr int64_t kSummer = 1784116800000LL;
|
|
|
+// The spring-forward minute: 00:59Z is 01:59 CET, 01:00Z is already 03:00 CEST.
|
|
|
+constexpr int64_t kBeforeSpringForward = 1774745940000LL;
|
|
|
+constexpr int64_t kAfterSpringForward = 1774746000000LL;
|
|
|
+// The repeated hour in autumn: both of these are 02:30 local, one CEST, one CET.
|
|
|
+constexpr int64_t kFirstRepeatedHalfTwo = 1792888200000LL;
|
|
|
+constexpr int64_t kSecondRepeatedHalfTwo = 1792891800000LL;
|
|
|
+
|
|
|
+const char* const kZone = "Europe/Budapest";
|
|
|
+
|
|
|
+/// The shim asks the host exactly one question - `parts(epochMs, zone)` - so a
|
|
|
+/// table of real answers is a complete backend. Built from the tz database, not
|
|
|
+/// from constants, so the test cannot drift from what a cron schedule or
|
|
|
+/// ctx.time would compute for the same instant.
|
|
|
+std::string partsTableJs() {
|
|
|
+ const std::pair<int64_t, const char*> wanted[] = {
|
|
|
+ {kWinter, kZone}, {kSummer, kZone},
|
|
|
+ {kBeforeSpringForward, kZone}, {kAfterSpringForward, kZone},
|
|
|
+ {kFirstRepeatedHalfTwo, kZone}, {kSecondRepeatedHalfTwo, kZone},
|
|
|
+ {kWinter, "America/New_York"},
|
|
|
+ };
|
|
|
+ nlohmann::json table = nlohmann::json::object();
|
|
|
+ for (const auto& [ms, zone] : wanted) {
|
|
|
+ auto p = svapi::functions::zonePartsAt(zone, static_cast<std::time_t>(ms / 1000));
|
|
|
+ if (!p) continue; // no tzdata for this zone on this host; the case skips below
|
|
|
+ table[std::to_string(ms) + "@" + zone] = nlohmann::json{
|
|
|
+ {"year", p->year}, {"month", p->month}, {"day", p->day},
|
|
|
+ {"hour", p->hour}, {"minute", p->minute}, {"second", p->second},
|
|
|
+ {"weekday", p->weekday}, {"offsetSec", p->offsetSec},
|
|
|
+ {"dst", p->dst}, {"abbrev", p->abbrev}};
|
|
|
+ }
|
|
|
+ return table.dump();
|
|
|
+}
|
|
|
+
|
|
|
+/// Install the real zone backend through the shim's own hook, then run `body`.
|
|
|
+/// `run()` is called with no host bindings, so the hook is still in place -
|
|
|
+/// which is also how a module behaves while its descriptor is read.
|
|
|
+const char* kHarness = R"JS(
|
|
|
+const __parts = %TABLE%;
|
|
|
+
|
|
|
+export default function () {
|
|
|
+ globalThis.__svapiBindIntlTime(function (op, payload) {
|
|
|
+ if (op !== 'parts') throw new Error('the shim asked for an unexpected op: ' + op);
|
|
|
+ const key = String(payload.epochMs) + '@' + payload.zone;
|
|
|
+ const p = __parts[key];
|
|
|
+ if (p === undefined) throw new Error('no recorded parts for ' + key);
|
|
|
+ return p;
|
|
|
+ });
|
|
|
+
|
|
|
+ let value = null;
|
|
|
+ let threw = null;
|
|
|
+ try {
|
|
|
+ value = (function () { %BODY% })();
|
|
|
+ } catch (e) {
|
|
|
+ threw = { name: e.name, code: e.code === undefined ? null : e.code,
|
|
|
+ message: String(e.message) };
|
|
|
+ }
|
|
|
+ return { value: value === undefined ? null : value, threw: threw };
|
|
|
+}
|
|
|
+)JS";
|
|
|
+
|
|
|
+void substitute(std::string& src, const std::string& token, const std::string& with) {
|
|
|
+ src.replace(src.find(token), token.size(), with);
|
|
|
+}
|
|
|
+
|
|
|
+nlohmann::json runIntl(const std::string& body) {
|
|
|
+ static const std::string table = partsTableJs();
|
|
|
+ std::string src = kHarness;
|
|
|
+ substitute(src, "%TABLE%", table);
|
|
|
+ substitute(src, "%BODY%", body);
|
|
|
+
|
|
|
+ JsRuntime rt;
|
|
|
+ std::string error;
|
|
|
+ auto bc = rt.compile(src, "intl_shim_test.js", error);
|
|
|
+ EXPECT_TRUE(bc.has_value()) << "compile failed: " << error;
|
|
|
+ if (!bc) return nlohmann::json::object();
|
|
|
+ JsResult result = rt.run(*bc, nlohmann::json::object(), 0, nullptr);
|
|
|
+ EXPECT_TRUE(result.ok) << result.error;
|
|
|
+ return result.value;
|
|
|
+}
|
|
|
+
|
|
|
+/// The value of an expression that was expected to succeed.
|
|
|
+nlohmann::json value(const std::string& body) {
|
|
|
+ const nlohmann::json out = runIntl(body);
|
|
|
+ EXPECT_TRUE(out.contains("threw") && out.at("threw").is_null())
|
|
|
+ << body << " threw: " << out.dump();
|
|
|
+ return out.at("value");
|
|
|
+}
|
|
|
+
|
|
|
+bool haveBudapest() {
|
|
|
+ return svapi::functions::zonePartsAt(kZone, static_cast<std::time_t>(kWinter / 1000))
|
|
|
+ .has_value();
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+// --- it exists, and it is shaped like the real one --------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, IntlAndDateTimeFormatAreGlobalButNumberFormatIsAbsent) {
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "return { intl: typeof Intl, dtf: typeof Intl.DateTimeFormat,"
|
|
|
+ " number: typeof Intl.NumberFormat, plural: typeof Intl.PluralRules,"
|
|
|
+ " supported: typeof Intl.supportedValuesOf,"
|
|
|
+ " supportedLocalesOf: typeof Intl.DateTimeFormat.supportedLocalesOf };");
|
|
|
+ EXPECT_EQ(v.at("intl"), "object");
|
|
|
+ EXPECT_EQ(v.at("dtf"), "function");
|
|
|
+ EXPECT_EQ(v.at("supportedLocalesOf"), "function");
|
|
|
+ // ABSENT rather than present-and-throwing, so `typeof Intl.NumberFormat ===
|
|
|
+ // 'undefined'` works as a feature test and a polyfill can install its own.
|
|
|
+ EXPECT_EQ(v.at("number"), "undefined");
|
|
|
+ EXPECT_EQ(v.at("plural"), "undefined");
|
|
|
+ EXPECT_EQ(v.at("supported"), "undefined");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, ConstructsWithAndWithoutNew) {
|
|
|
+ // The real Intl.DateTimeFormat is callable both ways, and real code uses
|
|
|
+ // both. A shim that only works with `new` breaks half of it.
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const a = new Intl.DateTimeFormat('en');"
|
|
|
+ "const b = Intl.DateTimeFormat('en');"
|
|
|
+ "return { a: typeof a.format, b: typeof b.format,"
|
|
|
+ " instance: b instanceof Intl.DateTimeFormat,"
|
|
|
+ " same: a.format(0) === b.format(0) };");
|
|
|
+ EXPECT_EQ(v.at("a"), "function");
|
|
|
+ EXPECT_EQ(v.at("b"), "function");
|
|
|
+ EXPECT_EQ(v.at("instance"), true);
|
|
|
+ EXPECT_EQ(v.at("same"), true);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, FormatSurvivesBeingDetachedFromItsFormatter) {
|
|
|
+ // `dates.map(fmt.format)` is ordinary code; the real API's format is a
|
|
|
+ // bound function, so it must not need its receiver.
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en').format;"
|
|
|
+ "return [0, 86400000].map(f);");
|
|
|
+ ASSERT_TRUE(v.is_array());
|
|
|
+ EXPECT_EQ(v.at(0), "1/1/1970");
|
|
|
+ EXPECT_EQ(v.at(1), "1/2/1970");
|
|
|
+}
|
|
|
+
|
|
|
+// --- locales ----------------------------------------------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, ALocaleArrayTakesTheFirstSupportedEntry) {
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "return { picked: new Intl.DateTimeFormat(['fr-FR', 'hu-HU', 'en'])"
|
|
|
+ " .resolvedOptions().locale,"
|
|
|
+ " none: new Intl.DateTimeFormat(['fr', 'de-AT']).resolvedOptions().locale,"
|
|
|
+ " regional: new Intl.DateTimeFormat('hu-HU').resolvedOptions().locale,"
|
|
|
+ " undef: new Intl.DateTimeFormat().resolvedOptions().locale };");
|
|
|
+ EXPECT_EQ(v.at("picked"), "hu");
|
|
|
+ // An unsupported locale RESOLVES to the default. Throwing is what the real
|
|
|
+ // API does for a malformed tag, not for one it cannot match - and throwing
|
|
|
+ // at module load is the failure this shim exists to remove.
|
|
|
+ EXPECT_EQ(v.at("none"), "en");
|
|
|
+ // Only the language subtag is honoured, so the resolved tag is the base
|
|
|
+ // language: reporting 'hu-HU' would promise regional data there is none of.
|
|
|
+ EXPECT_EQ(v.at("regional"), "hu");
|
|
|
+ EXPECT_EQ(v.at("undef"), "en");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, AnUnsupportedLocaleStillFormatsInsteadOfThrowing) {
|
|
|
+ const nlohmann::json out = runIntl(
|
|
|
+ "return new Intl.DateTimeFormat('ja-JP', { year: 'numeric', month: 'long',"
|
|
|
+ " day: 'numeric' }).format(0);");
|
|
|
+ ASSERT_TRUE(out.at("threw").is_null()) << out.dump();
|
|
|
+ EXPECT_EQ(out.at("value"), "January 1, 1970");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, SupportedLocalesOfReportsOnlyWhatTheNamesCover) {
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "return Intl.DateTimeFormat.supportedLocalesOf(['fr', 'hu-HU', 'en-GB', 'hu']);");
|
|
|
+ EXPECT_EQ(v, nlohmann::json({"hu", "en"}));
|
|
|
+}
|
|
|
+
|
|
|
+// --- real zones, real DST ---------------------------------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, FormatsABudapestInstantInWinterAndInSummer) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en', { timeZone: 'Europe/Budapest',"
|
|
|
+ " year: 'numeric', month: 'short', day: 'numeric',"
|
|
|
+ " hour: 'numeric', minute: '2-digit' });"
|
|
|
+ "return { winter: f.format(1768478400000), summer: f.format(1784116800000) };");
|
|
|
+ // The same UTC wall clock, an hour apart in local time: that hour IS the
|
|
|
+ // offset change, and it is the whole reason this shim may not guess.
|
|
|
+ EXPECT_EQ(v.at("winter"), "Jan 15, 2026, 1:00 PM");
|
|
|
+ EXPECT_EQ(v.at("summer"), "Jul 15, 2026, 2:00 PM");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, SpringForwardSkipsAnHourWithinOneMinuteOfUtc) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en', { timeZone: 'Europe/Budapest',"
|
|
|
+ " hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short' });"
|
|
|
+ "return { before: f.format(1774745940000), after: f.format(1774746000000) };");
|
|
|
+ EXPECT_EQ(v.at("before"), "01:59 CET");
|
|
|
+ EXPECT_EQ(v.at("after"), "03:00 CEST");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, TheRepeatedAutumnHourPrintsTwiceWithDifferentZoneNames) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en', { timeZone: 'Europe/Budapest',"
|
|
|
+ " hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short' });"
|
|
|
+ "return { first: f.format(1792888200000), second: f.format(1792891800000) };");
|
|
|
+ // Two different instants, one hour apart, both 02:30 local. Only the zone
|
|
|
+ // name tells them apart, which is why timeZoneName is worth having.
|
|
|
+ EXPECT_EQ(v.at("first"), "02:30 CEST");
|
|
|
+ EXPECT_EQ(v.at("second"), "02:30 CET");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, HungarianUsesHungarianFieldOrderAndNames) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const long_ = new Intl.DateTimeFormat('hu', { timeZone: 'Europe/Budapest',"
|
|
|
+ " year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' });"
|
|
|
+ "const numeric = new Intl.DateTimeFormat('hu-HU', { timeZone: 'Europe/Budapest',"
|
|
|
+ " year: 'numeric', month: '2-digit', day: '2-digit',"
|
|
|
+ " hour: '2-digit', minute: '2-digit' });"
|
|
|
+ "return { long_: long_.format(1768478400000), numeric: numeric.format(1768478400000) };");
|
|
|
+ // The weekday goes last in Hungarian and the month name is lowercase.
|
|
|
+ EXPECT_EQ(v.at("long_"), "2026. január 15., csütörtök");
|
|
|
+ // 24-hour by default for hu, because 1 PM is not how a Hungarian clock
|
|
|
+ // reads.
|
|
|
+ EXPECT_EQ(v.at("numeric"), "2026. 01. 15. 13:00");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, Hour12IsHonouredExplicitlyInBothDirections) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "function at(opts) {"
|
|
|
+ " return new Intl.DateTimeFormat('en',"
|
|
|
+ " Object.assign({ timeZone: 'Europe/Budapest', hour: 'numeric',"
|
|
|
+ " minute: '2-digit' }, opts)).format(1768478400000);"
|
|
|
+ "}"
|
|
|
+ "return { dflt: at({}), off: at({ hour12: false }), on: at({ hour12: true }),"
|
|
|
+ " cycle23: at({ hourCycle: 'h23' }),"
|
|
|
+ " hu: new Intl.DateTimeFormat('hu', { timeZone: 'Europe/Budapest',"
|
|
|
+ " hour: 'numeric', minute: '2-digit', hour12: true })"
|
|
|
+ " .format(1768478400000) };");
|
|
|
+ EXPECT_EQ(v.at("dflt"), "1:00 PM");
|
|
|
+ EXPECT_EQ(v.at("off"), "13:00");
|
|
|
+ EXPECT_EQ(v.at("on"), "1:00 PM");
|
|
|
+ EXPECT_EQ(v.at("cycle23"), "13:00");
|
|
|
+ // Hungarian's day period, and after the time rather than before it.
|
|
|
+ EXPECT_EQ(v.at("hu"), "1:00 du.");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, MidnightIsTwelveAmNotZeroAm) {
|
|
|
+ // The classic off-by-twelve. 1970-01-01T00:00:00Z is midnight UTC.
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en', { hour: 'numeric', minute: '2-digit' });"
|
|
|
+ "return { midnight: f.format(0), noon: f.format(12 * 3600 * 1000) };");
|
|
|
+ EXPECT_EQ(v.at("midnight"), "12:00 AM");
|
|
|
+ EXPECT_EQ(v.at("noon"), "12:00 PM");
|
|
|
+}
|
|
|
+
|
|
|
+// --- formatToParts ----------------------------------------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, FormatToPartsIsTheSameRenderingSplitUp) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en', { timeZone: 'Europe/Budapest',"
|
|
|
+ " weekday: 'long', year: 'numeric', month: 'long', day: 'numeric',"
|
|
|
+ " hour: 'numeric', minute: '2-digit' });"
|
|
|
+ "const parts = f.formatToParts(1768478400000);"
|
|
|
+ "return { parts: parts, joined: parts.map(function (p) { return p.value; }).join(''),"
|
|
|
+ " formatted: f.format(1768478400000) };");
|
|
|
+ EXPECT_EQ(v.at("joined"), v.at("formatted"))
|
|
|
+ << "format() must be exactly the concatenation of formatToParts()";
|
|
|
+ EXPECT_EQ(v.at("formatted"), "Thursday, January 15, 2026, 1:00 PM");
|
|
|
+
|
|
|
+ const nlohmann::json expected = nlohmann::json::array({
|
|
|
+ {{"type", "weekday"}, {"value", "Thursday"}},
|
|
|
+ {{"type", "literal"}, {"value", ", "}},
|
|
|
+ {{"type", "month"}, {"value", "January"}},
|
|
|
+ {{"type", "literal"}, {"value", " "}},
|
|
|
+ {{"type", "day"}, {"value", "15"}},
|
|
|
+ {{"type", "literal"}, {"value", ", "}},
|
|
|
+ {{"type", "year"}, {"value", "2026"}},
|
|
|
+ {{"type", "literal"}, {"value", ", "}},
|
|
|
+ {{"type", "hour"}, {"value", "1"}},
|
|
|
+ {{"type", "literal"}, {"value", ":"}},
|
|
|
+ {{"type", "minute"}, {"value", "00"}},
|
|
|
+ {{"type", "literal"}, {"value", " "}},
|
|
|
+ {{"type", "dayPeriod"}, {"value", "PM"}},
|
|
|
+ });
|
|
|
+ EXPECT_EQ(v.at("parts"), expected);
|
|
|
+}
|
|
|
+
|
|
|
+// --- resolvedOptions --------------------------------------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, ResolvedOptionsReportsTheZoneThatWasActuallyUsed) {
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "return { given: new Intl.DateTimeFormat('en', { timeZone: 'Europe/Budapest' })"
|
|
|
+ " .resolvedOptions(),"
|
|
|
+ " dflt: new Intl.DateTimeFormat('en').resolvedOptions(),"
|
|
|
+ " gmt: new Intl.DateTimeFormat('en', { timeZone: 'gmt' })"
|
|
|
+ " .resolvedOptions().timeZone,"
|
|
|
+ " withHour: new Intl.DateTimeFormat('hu', { hour: 'numeric' })"
|
|
|
+ " .resolvedOptions() };");
|
|
|
+ EXPECT_EQ(v.at("given").at("timeZone"), "Europe/Budapest");
|
|
|
+ EXPECT_EQ(v.at("given").at("calendar"), "gregory");
|
|
|
+ // No system zone is reachable from this engine, so the default is UTC and
|
|
|
+ // says so rather than pretending to be the host's local zone.
|
|
|
+ EXPECT_EQ(v.at("dflt").at("timeZone"), "UTC");
|
|
|
+ EXPECT_EQ(v.at("gmt"), "UTC");
|
|
|
+ // The real API reports the default date components when none were asked for.
|
|
|
+ EXPECT_EQ(v.at("dflt").at("year"), "numeric");
|
|
|
+ EXPECT_EQ(v.at("dflt").at("month"), "numeric");
|
|
|
+ EXPECT_EQ(v.at("dflt").at("day"), "numeric");
|
|
|
+ // hour12/hourCycle only when an hour is being rendered, as in the real API.
|
|
|
+ EXPECT_FALSE(v.at("dflt").contains("hour12"));
|
|
|
+ EXPECT_EQ(v.at("withHour").at("hour12"), false);
|
|
|
+ EXPECT_EQ(v.at("withHour").at("hourCycle"), "h23");
|
|
|
+}
|
|
|
+
|
|
|
+// --- argument handling ------------------------------------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, FormatAcceptsADateANumberOrNothing) {
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const f = new Intl.DateTimeFormat('en');"
|
|
|
+ "return { date: f.format(new Date(0)), number: f.format(0),"
|
|
|
+ " now: typeof f.format(), invalid: (function () {"
|
|
|
+ " try { f.format(new Date('nonsense')); return 'no throw'; }"
|
|
|
+ " catch (e) { return e.name; } })() };");
|
|
|
+ EXPECT_EQ(v.at("date"), "1/1/1970");
|
|
|
+ EXPECT_EQ(v.at("number"), "1/1/1970");
|
|
|
+ EXPECT_EQ(v.at("now"), "string");
|
|
|
+ EXPECT_EQ(v.at("invalid"), "RangeError");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, AnOptionValueOutsideTheEnumerationIsARangeError) {
|
|
|
+ // The real API throws for this, and it is a typo in a literal rather than
|
|
|
+ // data: being lenient would print "1" where the author wrote "01".
|
|
|
+ const nlohmann::json out = runIntl(
|
|
|
+ "return new Intl.DateTimeFormat('en', { month: '2digit' }).format(0);");
|
|
|
+ ASSERT_FALSE(out.at("threw").is_null());
|
|
|
+ EXPECT_EQ(out.at("threw").at("name"), "RangeError");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, AnUnknownZoneIsRefusedByTheHostNotGuessedAt) {
|
|
|
+ // The zone is NOT validated at construction: the host is the only thing
|
|
|
+ // that can judge a zone name, and a formatter is often built where the host
|
|
|
+ // is out of reach. It surfaces at format() instead - never as a silent
|
|
|
+ // fallback to UTC, which would move somebody's report by an hour.
|
|
|
+ const nlohmann::json out = runIntl(
|
|
|
+ "const f = new Intl.DateTimeFormat('en', { timeZone: 'Mars/Olympus' });"
|
|
|
+ "return f.format(1768478400000);");
|
|
|
+ ASSERT_FALSE(out.at("threw").is_null()) << out.dump();
|
|
|
+}
|
|
|
+
|
|
|
+// --- Date.prototype.toLocale* ----------------------------------------------
|
|
|
+
|
|
|
+TEST(IntlShim, DateToLocaleMethodsGoThroughTheSameFormatter) {
|
|
|
+ if (!haveBudapest()) GTEST_SKIP() << "no tzdata for " << kZone;
|
|
|
+ const nlohmann::json v = value(
|
|
|
+ "const d = new Date(1768478400000);"
|
|
|
+ "const z = { timeZone: 'Europe/Budapest' };"
|
|
|
+ "return { date: d.toLocaleDateString('en', z),"
|
|
|
+ " time: d.toLocaleTimeString('en', z),"
|
|
|
+ " both: d.toLocaleString('hu', z),"
|
|
|
+ " narrowed: d.toLocaleTimeString('en', { timeZone: 'Europe/Budapest',"
|
|
|
+ " hour: '2-digit', hour12: false }),"
|
|
|
+ " invalid: new Date('nonsense').toLocaleDateString('en') };");
|
|
|
+ EXPECT_EQ(v.at("date"), "1/15/2026");
|
|
|
+ EXPECT_EQ(v.at("time"), "1:00:00 PM");
|
|
|
+ EXPECT_EQ(v.at("both"), "2026. 01. 15. 13:00:00");
|
|
|
+ // Asking for an hour drops the default minutes and seconds, as the real API
|
|
|
+ // does: otherwise {hour:'2-digit'} would still print a full clock time.
|
|
|
+ EXPECT_EQ(v.at("narrowed"), "13");
|
|
|
+ EXPECT_EQ(v.at("invalid"), "Invalid Date");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, AToLocaleCallThatTheShimCannotServeFallsBackToTheEngine) {
|
|
|
+ // The fallback is what keeps this change from breaking a function that
|
|
|
+ // already registers and already calls toLocaleDateString(): an unknown zone
|
|
|
+ // returns the engine's own answer rather than throwing.
|
|
|
+ const nlohmann::json out = runIntl(
|
|
|
+ "return typeof new Date(0).toLocaleDateString('en', { timeZone: 'Mars/Olympus' });");
|
|
|
+ ASSERT_TRUE(out.at("threw").is_null()) << out.dump();
|
|
|
+ EXPECT_EQ(out.at("value"), "string");
|
|
|
+}
|
|
|
+
|
|
|
+// --- the load-time path, which is the whole point ---------------------------
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+/// Compile a module and read one export, exactly as registration does: no host
|
|
|
+/// bindings, so a formatter built at module scope has no zone data behind it.
|
|
|
+nlohmann::json readExportOf(const std::string& source, const std::string& name,
|
|
|
+ std::string& error) {
|
|
|
+ JsRuntime rt;
|
|
|
+ auto bc = rt.compile(source, "intl_load_time.js", error);
|
|
|
+ if (!bc) return nlohmann::json();
|
|
|
+ auto out = rt.readExport(*bc, name, error);
|
|
|
+ return out.value_or(nlohmann::json());
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+TEST(IntlShim, AModuleThatBuildsAFormatterAtLoadTimeCanBeRegistered) {
|
|
|
+ // This is the bug. Five of six real functions did exactly this and failed
|
|
|
+ // to register with compile_failed, because `Intl` did not exist.
|
|
|
+ const std::string source =
|
|
|
+ "const FMT = new Intl.DateTimeFormat('hu-HU', {\n"
|
|
|
+ " timeZone: 'Europe/Budapest', year: 'numeric', month: '2-digit', day: '2-digit',\n"
|
|
|
+ " hour: '2-digit', minute: '2-digit'\n"
|
|
|
+ "});\n"
|
|
|
+ "export const on = { function_id: 'report', http: { method: 'GET', path: '/r' } };\n"
|
|
|
+ "export default function (ctx) { return { at: FMT.format(Date.now()) }; }\n";
|
|
|
+ std::string error;
|
|
|
+ const nlohmann::json on = readExportOf(source, "on", error);
|
|
|
+ ASSERT_TRUE(error.empty()) << error;
|
|
|
+ EXPECT_EQ(on.at("function_id"), "report");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, AUtcFormatterEvenFormatsAtLoadTime) {
|
|
|
+ // UTC needs no zone database, so the shim answers it on its own. That is
|
|
|
+ // what makes a module which stamps a build time at load time registrable.
|
|
|
+ const std::string source =
|
|
|
+ "const FMT = new Intl.DateTimeFormat('en', { timeZone: 'UTC',\n"
|
|
|
+ " year: 'numeric', month: '2-digit', day: '2-digit' });\n"
|
|
|
+ "export const on = { stamp: FMT.format(1768478400000) };\n"
|
|
|
+ "export default function () { return null; }\n";
|
|
|
+ std::string error;
|
|
|
+ const nlohmann::json on = readExportOf(source, "on", error);
|
|
|
+ ASSERT_TRUE(error.empty()) << error;
|
|
|
+ EXPECT_EQ(on.at("stamp"), "01/15/2026");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, FormattingANonUtcZoneAtLoadTimeSaysWhyItCannot) {
|
|
|
+ // Constructing is fine; formatting is not, because the tz database is
|
|
|
+ // reached through the host and there is no host while a descriptor is read.
|
|
|
+ // The message has to say that, or the author's next move is to file a bug
|
|
|
+ // about a formatter that works in the handler and not above it.
|
|
|
+ const std::string source =
|
|
|
+ "const FMT = new Intl.DateTimeFormat('en', { timeZone: 'Europe/Budapest' });\n"
|
|
|
+ "export const on = { stamp: FMT.format(0) };\n"
|
|
|
+ "export default function () { return null; }\n";
|
|
|
+ std::string error;
|
|
|
+ readExportOf(source, "on", error);
|
|
|
+ ASSERT_FALSE(error.empty());
|
|
|
+ EXPECT_NE(error.find("while a module is being loaded"), std::string::npos) << error;
|
|
|
+}
|
|
|
+
|
|
|
+TEST(IntlShim, AModuleCallingToLocaleDateStringAtLoadTimeStillRegisters) {
|
|
|
+ // The regression that mattered most: these methods already worked (via the
|
|
|
+ // engine's own implementation), so routing them through the shim may not
|
|
|
+ // make a module that was registering stop.
|
|
|
+ const std::string source =
|
|
|
+ "export const on = { stamp: typeof new Date(0).toLocaleDateString() };\n"
|
|
|
+ "export default function () { return null; }\n";
|
|
|
+ std::string error;
|
|
|
+ const nlohmann::json on = readExportOf(source, "on", error);
|
|
|
+ ASSERT_TRUE(error.empty()) << error;
|
|
|
+ EXPECT_EQ(on.at("stamp"), "string");
|
|
|
+}
|