|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Diagnostics.CodeAnalysis; |
4 | 5 | using System.Globalization;
|
5 | 6 | using Microsoft.AspNetCore.Components.Endpoints.FormMapping;
|
6 | 7 | using Microsoft.Extensions.Primitives;
|
@@ -147,4 +148,93 @@ public void TryRead_ForDateOnlyReturnsFalseWithNullForBadDateValue()
|
147 | 148 | Assert.False(returnValue);
|
148 | 149 | Assert.Null(result);
|
149 | 150 | }
|
| 151 | + |
| 152 | + [Fact] |
| 153 | + public void TryConvertValue_ForCustomParsableStruct_UsesParsableImplementation_ForEmptyString() |
| 154 | + { |
| 155 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 156 | + |
| 157 | + var nullableConverter = new NullableConverter<ParsableTestStruct>(new ParsableConverter<ParsableTestStruct>()); |
| 158 | + var reader = new FormDataReader(default, culture, default); |
| 159 | + |
| 160 | + var returnValue = nullableConverter.TryConvertValue(ref reader, string.Empty, out var result); |
| 161 | + |
| 162 | + Assert.True(returnValue); |
| 163 | + Assert.NotNull(result); |
| 164 | + Assert.True(result.Value.WasEmptyOrNull); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public void TryConvertValue_ForCustomParsableStruct_UsesParsableImplementation_ForNull() |
| 169 | + { |
| 170 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 171 | + |
| 172 | + var nullableConverter = new NullableConverter<ParsableTestStruct>(new ParsableConverter<ParsableTestStruct>()); |
| 173 | + var reader = new FormDataReader(default, culture, default); |
| 174 | + |
| 175 | + var returnValue = nullableConverter.TryConvertValue(ref reader, null, out var result); |
| 176 | + |
| 177 | + Assert.True(returnValue); |
| 178 | + Assert.NotNull(result); |
| 179 | + Assert.True(result.Value.WasEmptyOrNull); |
| 180 | + } |
| 181 | + |
| 182 | + [Fact] |
| 183 | + public void TryConvertValue_ForCustomParsableStruct_UsesParsableImplementation_ForGoodValue() |
| 184 | + { |
| 185 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 186 | + |
| 187 | + var nullableConverter = new NullableConverter<ParsableTestStruct>(new ParsableConverter<ParsableTestStruct>()); |
| 188 | + var reader = new FormDataReader(default, culture, default) |
| 189 | + { |
| 190 | + ErrorHandler = (_, __, ___) => { } |
| 191 | + }; |
| 192 | + |
| 193 | + var returnValue = nullableConverter.TryConvertValue(ref reader, "good value", out var result); |
| 194 | + |
| 195 | + Assert.True(returnValue); |
| 196 | + Assert.False(result.Value.WasEmptyOrNull); |
| 197 | + } |
| 198 | + |
| 199 | + [Fact] |
| 200 | + public void TryConvertValue_ForCustomParsableStruct_UsesParsableImplementation_ForBadValue() |
| 201 | + { |
| 202 | + var culture = CultureInfo.GetCultureInfo("en-US"); |
| 203 | + |
| 204 | + var nullableConverter = new NullableConverter<ParsableTestStruct>(new ParsableConverter<ParsableTestStruct>()); |
| 205 | + var reader = new FormDataReader(default, culture, default) |
| 206 | + { |
| 207 | + ErrorHandler = (_, __, ___) => { } |
| 208 | + }; |
| 209 | + |
| 210 | + var returnValue = nullableConverter.TryConvertValue(ref reader, "bad value", out var result); |
| 211 | + |
| 212 | + Assert.False(returnValue); |
| 213 | + } |
| 214 | + |
| 215 | + private struct ParsableTestStruct : IParsable<ParsableTestStruct> |
| 216 | + { |
| 217 | + public bool WasEmptyOrNull { get; set; } |
| 218 | + |
| 219 | + public static ParsableTestStruct Parse(string s, IFormatProvider provider) => throw new NotImplementedException(); |
| 220 | + |
| 221 | + public static bool TryParse([NotNullWhen(true)] string s, IFormatProvider provider, [MaybeNullWhen(false)] out ParsableTestStruct result) |
| 222 | + { |
| 223 | + if (string.IsNullOrEmpty(s)) |
| 224 | + { |
| 225 | + result = new ParsableTestStruct { WasEmptyOrNull = true }; |
| 226 | + return true; |
| 227 | + } |
| 228 | + else if (s == "good value") |
| 229 | + { |
| 230 | + result = new ParsableTestStruct { WasEmptyOrNull = false }; |
| 231 | + return true; |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + result = new(); |
| 236 | + return false; |
| 237 | + } |
| 238 | + } |
| 239 | + } |
150 | 240 | }
|
0 commit comments