Skip to content

Conversation

@Krishanthaudayakumara
Copy link
Owner

Fix Culture-Dependent Date Format Test Failures

Description

This PR fixes test failures that occur when running the test suite on systems with non-US locales (e.g., Danish culture da-DK). The issue was caused by hardcoded date format expectations in tests that didn't account for culture-specific date formatting differences.

Problem

When the system culture was set to Danish (da-DK), several date-related tests failed because:

  • Danish culture uses hyphens (-) as date separators instead of slashes (/)
  • Tests expected hardcoded US date formats (e.g., "05/04/2012") but got culture-specific formats (e.g., "05-04-2012")
  • Unit tests used CultureInfo.InvariantCulture for expectations while the actual implementation used CultureInfo.CurrentCulture

Root Cause

The DateTagHelper implementation correctly uses CultureInfo.CurrentCulture when no specific culture is provided:

CultureInfo culture = !string.IsNullOrWhiteSpace(Culture) ? CultureInfo.CreateSpecificCulture(Culture) : CultureInfo.CurrentCulture;

However, tests were expecting either:

  1. Invariant culture formatting (in unit tests)
  2. Hardcoded US date formats (in integration tests)

Changes Made

Unit Tests (DateTagHelperFixture.cs)

Changed test expectations from CultureInfo.InvariantCulture to CultureInfo.CurrentCulture to match the actual implementation behavior

Integration Tests

Replaced hardcoded date strings with dynamic culture-aware formatting using TestConstants.DateTimeValue.ToString() with appropriate format strings and CultureInfo.CurrentCulture

AllFieldTagHelpersFixture.cs - Changed from hardcoded TestConstants.DateFieldValue to dynamic formatting using TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture)

ViewFieldsBindingFixture.cs - Added culture-aware date separator replacement: "12/12/2019".Replace("/", CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator)

RichTextFieldTagHelperFixture.cs - Applied the same date separator fix as ViewFieldsBindingFixture.cs

This comment was marked as outdated.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Fixes test failures that occur when running tests on systems with non-US locales by making date formatting culture-aware instead of using hardcoded US date formats or invariant culture expectations.

  • Updated unit tests to expect CultureInfo.CurrentCulture formatting instead of CultureInfo.InvariantCulture
  • Replaced hardcoded date strings in integration tests with dynamic culture-aware formatting
  • Added date separator replacement logic to handle culture-specific separators (e.g., hyphens vs slashes)

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
DateTagHelperFixture.cs Changed unit test expectations from InvariantCulture to CurrentCulture
RichTextFieldTagHelperFixture.cs Added culture-aware date separator replacement for hardcoded date string
DateFieldTagHelperFixture.cs Replaced hardcoded date strings with dynamic formatting using TestConstants
AllFieldTagHelpersFixture.cs Updated date field assertion to use dynamic formatting instead of hardcoded constant
ViewFieldsBindingFixture.cs Added culture-aware date separator replacement for hardcoded date string

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

.Should().Be(TestConstants.AllFieldsImageValue);
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div6", StringComparison.OrdinalIgnoreCase)).InnerHtml
.Should().Be(TestConstants.DateFieldValue);
.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
Copy link

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded format string "MM/dd/yyyy" contradicts the culture-aware approach. Consider using a culture-neutral format or the culture's standard short date pattern via CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.

Suggested change
.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
.Should().Be(TestConstants.DateTimeValue.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture));

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this change and it causes the test to fail because the actual component output uses a hardcoded "MM/dd/yyyy" format (producing "05/04/2012"), while ShortDatePattern produces "5/4/2012" without leading zeros.

// Assert
sectionNode.ChildNodes[1].InnerHtml.Should().Be("05/04/2012");
sectionNode.ChildNodes[3].InnerHtml.Should().Be("05/04/2012 00:00:00");
sectionNode.ChildNodes[1].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
Copy link

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using hardcoded format "MM/dd/yyyy" with CurrentCulture may still produce incorrect results in non-US locales. Some cultures don't use MM/dd/yyyy format even when explicitly specified. Consider using the culture's native short date pattern.

Suggested change
sectionNode.ChildNodes[1].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
sectionNode.ChildNodes[1].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture));

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, I tested this change and it causes the test to fail because the actual component output uses a hardcoded "MM/dd/yyyy" format (producing "05/04/2012"), while ShortDatePattern produces "5/4/2012" without leading zeros.

sectionNode.ChildNodes[1].InnerHtml.Should().Be("05/04/2012");
sectionNode.ChildNodes[3].InnerHtml.Should().Be("05/04/2012 00:00:00");
sectionNode.ChildNodes[1].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
sectionNode.ChildNodes[3].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.CurrentCulture));
Copy link

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded format "MM/dd/yyyy HH:mm:ss" with CurrentCulture may still fail in cultures that use different date/time patterns. Consider using culture-appropriate format patterns or a more flexible matching approach.

Suggested change
sectionNode.ChildNodes[3].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.CurrentCulture));
sectionNode.ChildNodes[3].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture));

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go with the CultureInfo.CurrentCulture because it expect the date format regarding that specific culture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants