Skip to content

Commit 0406137

Browse files
Vignesh-SF3580rmarinho
authored andcommitted
added unit test
1 parent bacffc7 commit 0406137

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

src/Controls/src/Core/DateTimeTypeConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace Microsoft.Maui.Controls;
99
internal class DateTimeTypeConverter : TypeConverter
1010
{
1111
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
12-
=> sourceType == typeof(DateTime) || sourceType == typeof(DateOnly);
12+
=> sourceType == typeof(DateOnly);
1313

1414
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
15-
=> destinationType == typeof(DateTime) || destinationType == typeof(DateOnly);
15+
=> destinationType == typeof(DateTime);
1616

1717
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
1818
{

src/Controls/src/Core/TimeSpanTypeConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace Microsoft.Maui.Controls;
99
internal class TimeSpanTypeConverter : TypeConverter
1010
{
1111
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
12-
=> sourceType == typeof(string) || sourceType == typeof(TimeOnly) || sourceType == typeof(TimeSpan);
12+
=> sourceType == typeof(TimeOnly);
1313

1414
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
15-
=> destinationType == typeof(string) || destinationType == typeof(TimeSpan) || destinationType == typeof(TimeOnly);
15+
=> destinationType == typeof(TimeSpan);
1616

1717
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
1818
{

src/Controls/tests/Core.UnitTests/DateOnlyTypeConverterTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,24 @@ public void DateTimeToDateOnlyConversion()
3535

3636
Assert.Equal(expectedDateOnly, actualDateOnly);
3737
}
38+
39+
[Fact]
40+
public void DateOnlyToDatePickerBinding()
41+
{
42+
var datePicker = new DatePicker();
43+
var source = new Issue20438DatePickerViewModel
44+
{
45+
SelectedDate = new DateOnly(2025, 3, 15)
46+
};
47+
datePicker.BindingContext = source;
48+
datePicker.SetBinding(DatePicker.DateProperty, "SelectedDate");
49+
var expectedDateTime = new DateTime(2025, 3, 15);
50+
Assert.Equal(expectedDateTime, datePicker.Date);
51+
}
52+
53+
public class Issue20438DatePickerViewModel
54+
{
55+
public DateOnly SelectedDate { get; set; }
56+
}
3857
}
3958
#endif

src/Controls/tests/Core.UnitTests/TimeOnlyTypeConverterTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,24 @@ public void TimeSpanToTimeOnlyConversion()
3434

3535
Assert.Equal(expectedTimeOnly, actualTimeOnly);
3636
}
37+
38+
[Fact]
39+
public void TimeOnlyToTimePickerBinding()
40+
{
41+
var timePicker = new TimePicker();
42+
var source = new Issue20438TimePickerViewModel
43+
{
44+
SelectedTime = new TimeOnly(14, 30, 0)
45+
};
46+
timePicker.BindingContext = source;
47+
timePicker.SetBinding(TimePicker.TimeProperty, "SelectedTime");
48+
var expectedTimeSpan = new TimeSpan(14, 30, 0);
49+
Assert.Equal(expectedTimeSpan, timePicker.Time);
50+
}
51+
52+
public class Issue20438TimePickerViewModel
53+
{
54+
public TimeOnly SelectedTime { get; set; }
55+
}
3756
}
3857
#endif

0 commit comments

Comments
 (0)