feat: implement mock TimeZoneInfo in testing package#1049
Merged
Conversation
Adds the mock implementation of `ITimeZoneInfo` to `MockTimeSystem`, making the local time zone deterministically controllable in tests. - Implement `TimeZoneInfoMock` and expose it via `MockTimeSystem.TimeZoneInfo`. - Add `LocalTimeZone` plus a seeded, overridable time-zone registry (`RegisterTimeZone` / `FindSystemTimeZoneById` / `GetSystemTimeZones`) to `ITimeProvider`; the registry is seeded from the host and can be extended. - `TimeProviderFactory`: `Now`/`Use` default the local time zone to `TimeZoneInfo.Local`, `Random` picks from a curated, cross-platform-stable set (incl. a daylight-saving zone), and a new `Use(time, localTimeZone)` overload allows an explicit zone. - Route `DateTimeMock.Now`/`Today` through the configured local time zone and resolve the `ToTimeProvider().LocalTimeZone` bridge (closes #1039). - `MockTimeSystem.DateTimeOffset` is temporarily stubbed; the mock is added in the follow-up `DateTimeOffset` change. - Reset `BuildScope` to `Default` and reference the `10.3.0-pre.1` interface pre-release.
…ption` - Move the runtime-agnostic `ITimeZoneInfo` tests into `Testably.Abstractions.Tests` so they run against the real and the mock implementation and are verified to behave identically. - `FindSystemTimeZoneById` for an unknown id now verifies the exact exception message and HResult. - Centralize the `TimeZoneNotFoundException` in `ExceptionFactory` with an English message (matching the real BCL) instead of throwing inline. - Force the test run to `InvariantCulture` so localized exception messages (on .NET Framework) stay deterministic and English. - Document the local time zone configuration in the time-system docs.
`SetTo` stored the value as-is, so an `Unspecified`-kind value was later treated as host-local, disagreeing with the constructor (`Use`), which treats `Unspecified` as UTC. Normalize `SetTo` the same way (Unspecified -> UTC, otherwise convert to UTC) so the simulated instant is canonical UTC and the local-time-zone conversions read a true UTC input. `Utc`/`Local` inputs are unaffected; only the `Unspecified` case changes, aligning it with the documented `Use` semantics.
9754280 to
b75d476
Compare
Test Results 84 files + 83 84 suites +83 1h 59m 23s ⏱️ + 1h 59m 20s For more details on these failures, see this check. Results for commit b75d476. ± Comparison against base commit 43a4cb6. |
Verify (against both the real and the mock time system) that `DateTime.Now` and `DateTime.UtcNow` represent the same instant - i.e. `Now.ToUniversalTime()` falls between two surrounding `UtcNow` reads - and that `Now` has `DateTimeKind.Local`.
Defer allocation and host seeding of the TimeProviderMock time-zone registry until FindSystemTimeZoneById/GetSystemTimeZones/RegisterTimeZone is actually used, so mocks that only read the time pay no allocation and never enumerate the host time zones. Add a Dictionary.TryAdd polyfill for netstandard2.0. Also: - Guard TimeProviderFactory.Use(DateTime, TimeZoneInfo) against a null time zone and cover it with a test. - Trim the thread-aware provider docs example to the members relevant to the per-thread clock. - Restyle the time tests and tighten the NowAndUtcNow tolerance.
6b03723 to
7d9ad75
Compare
|
|
This is addressed in release v7.0.0. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Adds the mock implementation of
ITimeZoneInfotoMockTimeSystem, making the local time zone deterministically controllable in tests.TimeZoneInfoMockand expose it viaMockTimeSystem.TimeZoneInfo.LocalTimeZoneplus a seeded, overridable time-zone registry (RegisterTimeZone/FindSystemTimeZoneById/GetSystemTimeZones) toITimeProvider; the registry is seeded from the host and can be extended.TimeProviderFactory:Now/Usedefault the local time zone toTimeZoneInfo.Local,Randompicks from a curated, cross-platform-stable set (incl. a daylight-saving zone), and a newUse(time, localTimeZone)overload allows an explicit zone.DateTimeMock.Now/Todaythrough the configured local time zone and resolve theToTimeProvider().LocalTimeZonebridge.MockTimeSystem.DateTimeOffsetis temporarily stubbed; the mock is added in the follow-upDateTimeOffsetchange.BuildScopetoDefaultand reference the10.3.0-pre.1interface pre-release.DateTimeOffsetsupport toITimeSystem#1039