|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Globalization; |
| 5 | +using Microsoft.AspNetCore.Components.Endpoints.FormMapping; |
| 6 | +using Microsoft.Extensions.Primitives; |
| 7 | + |
| 8 | +namespace Microsoft.AspNetCore.Components.Endpoints.Tests.FormMapping; |
| 9 | + |
| 10 | +public class NullableConverterTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void TryConvertValue_ForDateOnlyReturnsTrueWithNullForEmptyString() |
| 14 | + { |
| 15 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 16 | + |
| 17 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 18 | + var reader = new FormDataReader(default, culture, default); |
| 19 | + |
| 20 | + var returnValue = nullableConverter.TryConvertValue(ref reader, string.Empty, out var result); |
| 21 | + |
| 22 | + Assert.True(returnValue); |
| 23 | + Assert.Null(result); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void TryConvertValue_ForDateOnlyReturnsTrueWithDateForRealDateValue() |
| 28 | + { |
| 29 | + var date = new DateOnly(2023, 11, 30); |
| 30 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 31 | + |
| 32 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 33 | + var reader = new FormDataReader(default, culture, default); |
| 34 | + |
| 35 | + var returnValue = nullableConverter.TryConvertValue(ref reader, date.ToString(culture), out var result); |
| 36 | + |
| 37 | + Assert.True(returnValue); |
| 38 | + Assert.Equal(date, result); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void TryConvertValue_ForDateOnlyReturnsFalseWithNullForBadDateValue() |
| 43 | + { |
| 44 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 45 | + |
| 46 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 47 | + var reader = new FormDataReader(default, culture, default) |
| 48 | + { |
| 49 | + ErrorHandler = (_, __, ___) => { } |
| 50 | + }; |
| 51 | + |
| 52 | + var returnValue = nullableConverter.TryConvertValue(ref reader, "bad date", out var result); |
| 53 | + |
| 54 | + Assert.False(returnValue); |
| 55 | + Assert.Null(result); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void TryRead_ForDateOnlyReturnsFalseWithNullForNoValue() |
| 60 | + { |
| 61 | + const string prefixName = "field"; |
| 62 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 63 | + |
| 64 | + var dictionary = new Dictionary<FormKey, StringValues>(); |
| 65 | + var buffer = prefixName.ToCharArray().AsMemory(); |
| 66 | + var reader = new FormDataReader(dictionary, culture, buffer); |
| 67 | + reader.PushPrefix(prefixName); |
| 68 | + |
| 69 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 70 | + |
| 71 | + var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); |
| 72 | + |
| 73 | + Assert.False(found); |
| 74 | + Assert.True(returnValue); |
| 75 | + Assert.Null(result); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void TryRead_ForDateOnlyReturnsTrueWithNullForEmptyString() |
| 80 | + { |
| 81 | + const string prefixName = "field"; |
| 82 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 83 | + |
| 84 | + var dictionary = new Dictionary<FormKey, StringValues>() |
| 85 | + { |
| 86 | + { new FormKey(prefixName.AsMemory()), (StringValues)string.Empty } |
| 87 | + }; |
| 88 | + var buffer = prefixName.ToCharArray().AsMemory(); |
| 89 | + var reader = new FormDataReader(dictionary, culture, buffer); |
| 90 | + reader.PushPrefix(prefixName); |
| 91 | + |
| 92 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 93 | + |
| 94 | + var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); |
| 95 | + |
| 96 | + Assert.True(found); |
| 97 | + Assert.True(returnValue); |
| 98 | + Assert.Null(result); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void TryRead_ForDateOnlyReturnsTrueWithDateForRealDateValue() |
| 103 | + { |
| 104 | + const string prefixName = "field"; |
| 105 | + var date = new DateOnly(2023, 11, 30); |
| 106 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 107 | + |
| 108 | + var dictionary = new Dictionary<FormKey, StringValues>() |
| 109 | + { |
| 110 | + { new FormKey(prefixName.AsMemory()), (StringValues)date.ToString(culture) } |
| 111 | + }; |
| 112 | + var buffer = prefixName.ToCharArray().AsMemory(); |
| 113 | + var reader = new FormDataReader(dictionary, culture, buffer); |
| 114 | + reader.PushPrefix(prefixName); |
| 115 | + |
| 116 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 117 | + |
| 118 | + var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); |
| 119 | + |
| 120 | + Assert.True(found); |
| 121 | + Assert.True(returnValue); |
| 122 | + Assert.Equal(date, result); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void TryRead_ForDateOnlyReturnsFalseWithNullForBadDateValue() |
| 127 | + { |
| 128 | + const string prefixName = "field"; |
| 129 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 130 | + |
| 131 | + var dictionary = new Dictionary<FormKey, StringValues>() |
| 132 | + { |
| 133 | + { new FormKey(prefixName.AsMemory()), (StringValues)"bad date" } |
| 134 | + }; |
| 135 | + var buffer = prefixName.ToCharArray().AsMemory(); |
| 136 | + var reader = new FormDataReader(dictionary, culture, buffer) |
| 137 | + { |
| 138 | + ErrorHandler = (_, __, ___) => { } |
| 139 | + }; |
| 140 | + reader.PushPrefix(prefixName); |
| 141 | + |
| 142 | + var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); |
| 143 | + |
| 144 | + var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); |
| 145 | + |
| 146 | + Assert.True(found); |
| 147 | + Assert.False(returnValue); |
| 148 | + Assert.Null(result); |
| 149 | + } |
| 150 | +} |
0 commit comments