ParameterBindingMethodCacheTests.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. #nullable enable
  4. using System.Globalization;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using Microsoft.Extensions.Internal;
  8. namespace Microsoft.AspNetCore.Http.Extensions.Tests;
  9. public class ParameterBindingMethodCacheTests
  10. {
  11. [Theory]
  12. [InlineData(typeof(int))]
  13. [InlineData(typeof(double))]
  14. [InlineData(typeof(float))]
  15. [InlineData(typeof(Half))]
  16. [InlineData(typeof(short))]
  17. [InlineData(typeof(long))]
  18. [InlineData(typeof(IntPtr))]
  19. [InlineData(typeof(sbyte))]
  20. [InlineData(typeof(ushort))]
  21. [InlineData(typeof(uint))]
  22. [InlineData(typeof(ulong))]
  23. public void FindTryParseStringMethod_ReturnsTheExpectedTryParseMethodWithInvariantCulture(Type type)
  24. {
  25. var methodFound = new ParameterBindingMethodCache().FindTryParseMethod(@type);
  26. Assert.NotNull(methodFound);
  27. var call = methodFound!(Expression.Variable(type, "parsedValue"), Expression.Constant(CultureInfo.InvariantCulture)) as MethodCallExpression;
  28. Assert.NotNull(call);
  29. var parameters = call!.Method.GetParameters();
  30. Assert.Equal(4, parameters.Length);
  31. Assert.Equal(typeof(string), parameters[0].ParameterType);
  32. Assert.Equal(typeof(NumberStyles), parameters[1].ParameterType);
  33. Assert.Equal(typeof(IFormatProvider), parameters[2].ParameterType);
  34. Assert.True(parameters[3].IsOut);
  35. }
  36. [Theory]
  37. [InlineData(typeof(DateTime))]
  38. [InlineData(typeof(DateOnly))]
  39. [InlineData(typeof(DateTimeOffset))]
  40. [InlineData(typeof(TimeOnly))]
  41. [InlineData(typeof(TimeSpan))]
  42. public void FindTryParseStringMethod_ReturnsTheExpectedTryParseMethodWithInvariantCultureDateType(Type type)
  43. {
  44. var methodFound = new ParameterBindingMethodCache().FindTryParseMethod(@type);
  45. Assert.NotNull(methodFound);
  46. var call = methodFound!(Expression.Variable(type, "parsedValue"), Expression.Constant(CultureInfo.InvariantCulture)) as MethodCallExpression;
  47. Assert.NotNull(call);
  48. var parameters = call!.Method.GetParameters();
  49. if (@type == typeof(TimeSpan))
  50. {
  51. Assert.Equal(3, parameters.Length);
  52. Assert.Equal(typeof(string), parameters[0].ParameterType);
  53. Assert.Equal(typeof(IFormatProvider), parameters[1].ParameterType);
  54. Assert.True(parameters[2].IsOut);
  55. }
  56. else
  57. {
  58. Assert.Equal(4, parameters.Length);
  59. Assert.Equal(typeof(string), parameters[0].ParameterType);
  60. Assert.Equal(typeof(IFormatProvider), parameters[1].ParameterType);
  61. Assert.Equal(typeof(DateTimeStyles), parameters[2].ParameterType);
  62. Assert.True(parameters[3].IsOut);
  63. }
  64. }
  65. [Theory]
  66. [InlineData(typeof(TryParseStringRecord))]
  67. [InlineData(typeof(TryParseStringStruct))]
  68. [InlineData(typeof(TryParseInheritClassWithFormatProvider))]
  69. [InlineData(typeof(TryParseFromInterfaceWithFormatProvider))]
  70. public void FindTryParseStringMethod_ReturnsTheExpectedTryParseMethodWithInvariantCultureCustomType(Type type)
  71. {
  72. var methodFound = new ParameterBindingMethodCache().FindTryParseMethod(@type);
  73. Assert.NotNull(methodFound);
  74. var call = methodFound!(Expression.Variable(type, "parsedValue"), Expression.Constant(CultureInfo.InvariantCulture)) as MethodCallExpression;
  75. Assert.NotNull(call);
  76. var parameters = call!.Method.GetParameters();
  77. Assert.Equal(3, parameters.Length);
  78. Assert.Equal(typeof(string), parameters[0].ParameterType);
  79. Assert.Equal(typeof(IFormatProvider), parameters[1].ParameterType);
  80. Assert.True(parameters[2].IsOut);
  81. Assert.True(((call.Arguments[1] as ConstantExpression)!.Value as CultureInfo)!.Equals(CultureInfo.InvariantCulture));
  82. }
  83. [Theory]
  84. [InlineData(typeof(TryParseNoFormatProviderRecord))]
  85. [InlineData(typeof(TryParseNoFormatProviderStruct))]
  86. [InlineData(typeof(TryParseInheritClass))]
  87. [InlineData(typeof(TryParseFromInterface))]
  88. [InlineData(typeof(TryParseFromGrandparentInterface))]
  89. [InlineData(typeof(TryParseDirectlyAndFromInterface))]
  90. [InlineData(typeof(TryParseFromClassAndInterface))]
  91. public void FindTryParseMethod_WithNoFormatProvider(Type type)
  92. {
  93. var methodFound = new ParameterBindingMethodCache().FindTryParseMethod(@type);
  94. Assert.NotNull(methodFound);
  95. var call = methodFound!(Expression.Variable(type, "parsedValue"), Expression.Constant(CultureInfo.InvariantCulture)) as MethodCallExpression;
  96. Assert.NotNull(call);
  97. var parameters = call!.Method.GetParameters();
  98. Assert.Equal(2, parameters.Length);
  99. Assert.Equal(typeof(string), parameters[0].ParameterType);
  100. Assert.True(parameters[1].IsOut);
  101. }
  102. public static IEnumerable<object[]> TryParseStringParameterInfoData
  103. {
  104. get
  105. {
  106. return new[]
  107. {
  108. new[]
  109. {
  110. GetFirstParameter((TryParseStringRecord arg) => TryParseStringRecordMethod(arg)),
  111. },
  112. new[]
  113. {
  114. GetFirstParameter((TryParseStringStruct arg) => TryParseStringStructMethod(arg)),
  115. },
  116. new[]
  117. {
  118. GetFirstParameter((TryParseStringStruct? arg) => TryParseStringNullableStructMethod(arg)),
  119. },
  120. };
  121. }
  122. }
  123. [Theory]
  124. [MemberData(nameof(TryParseStringParameterInfoData))]
  125. public void HasTryParseStringMethod_ReturnsTrueWhenMethodExists(ParameterInfo parameterInfo)
  126. {
  127. Assert.True(new ParameterBindingMethodCache().HasTryParseMethod(parameterInfo.ParameterType));
  128. }
  129. [Fact]
  130. public void FindTryParseStringMethod_WorksForEnums()
  131. {
  132. var type = typeof(Choice);
  133. var methodFound = new ParameterBindingMethodCache().FindTryParseMethod(type);
  134. Assert.NotNull(methodFound);
  135. var call = methodFound!(Expression.Variable(type, "parsedValue"), Expression.Constant(CultureInfo.InvariantCulture)) as MethodCallExpression;
  136. Assert.NotNull(call);
  137. var method = call!.Method;
  138. var parameters = method.GetParameters();
  139. // By default, we use Enum.TryParse<T>
  140. Assert.True(method.IsGenericMethod);
  141. Assert.Equal(2, parameters.Length);
  142. Assert.Equal(typeof(string), parameters[0].ParameterType);
  143. Assert.True(parameters[1].IsOut);
  144. }
  145. [Fact]
  146. public void FindTryParseStringMethod_WorksForEnumsWhenNonGenericEnumParseIsUsed()
  147. {
  148. var type = typeof(Choice);
  149. var cache = new ParameterBindingMethodCache(preferNonGenericEnumParseOverload: true);
  150. var methodFound = cache.FindTryParseMethod(type);
  151. Assert.NotNull(methodFound);
  152. var parsedValue = Expression.Variable(type, "parsedValue");
  153. var block = methodFound!(parsedValue, Expression.Constant(CultureInfo.InvariantCulture)) as BlockExpression;
  154. Assert.NotNull(block);
  155. Assert.Equal(typeof(bool), block!.Type);
  156. var parseEnum = Expression.Lambda<Func<string, Choice>>(Expression.Block(new[] { parsedValue },
  157. block,
  158. parsedValue), ParameterBindingMethodCache.TempSourceStringExpr).Compile();
  159. Assert.Equal(Choice.One, parseEnum("One"));
  160. Assert.Equal(Choice.Two, parseEnum("Two"));
  161. Assert.Equal(Choice.Three, parseEnum("Three"));
  162. }
  163. [Fact]
  164. public async Task FindBindAsyncMethod_FindsCorrectMethodOnClass()
  165. {
  166. var type = typeof(BindAsyncRecord);
  167. var cache = new ParameterBindingMethodCache();
  168. var parameter = new MockParameterInfo(type, "bindAsyncRecord");
  169. var methodFound = cache.FindBindAsyncMethod(parameter);
  170. Assert.NotNull(methodFound.Expression);
  171. Assert.Equal(2, methodFound.ParamCount);
  172. var parsedValue = Expression.Variable(type, "parsedValue");
  173. var parseHttpContext = Expression.Lambda<Func<HttpContext, ValueTask<object>>>(
  174. Expression.Block(new[] { parsedValue }, methodFound.Expression!),
  175. ParameterBindingMethodCache.HttpContextExpr).Compile();
  176. var httpContext = new DefaultHttpContext
  177. {
  178. Request =
  179. {
  180. Headers =
  181. {
  182. ["ETag"] = "42",
  183. },
  184. },
  185. };
  186. Assert.Equal(new BindAsyncRecord(42), await parseHttpContext(httpContext));
  187. }
  188. [Fact]
  189. public async Task FindBindAsyncMethod_FindsSingleArgBindAsync()
  190. {
  191. var type = typeof(BindAsyncSingleArgStruct);
  192. var cache = new ParameterBindingMethodCache();
  193. var parameter = new MockParameterInfo(type, "bindAsyncSingleArgStruct");
  194. var methodFound = cache.FindBindAsyncMethod(parameter);
  195. Assert.NotNull(methodFound.Expression);
  196. Assert.Equal(1, methodFound.ParamCount);
  197. var parsedValue = Expression.Variable(type, "parsedValue");
  198. var parseHttpContext = Expression.Lambda<Func<HttpContext, ValueTask<object>>>(
  199. Expression.Block(new[] { parsedValue }, methodFound.Expression!),
  200. ParameterBindingMethodCache.HttpContextExpr).Compile();
  201. var httpContext = new DefaultHttpContext
  202. {
  203. Request =
  204. {
  205. Headers =
  206. {
  207. ["ETag"] = "42",
  208. },
  209. },
  210. };
  211. Assert.Equal(new BindAsyncSingleArgStruct(42), await parseHttpContext(httpContext));
  212. }
  213. public static IEnumerable<object[]> BindAsyncParameterInfoData
  214. {
  215. get
  216. {
  217. return new[]
  218. {
  219. new[]
  220. {
  221. GetFirstParameter((BindAsyncRecord arg) => BindAsyncRecordMethod(arg)),
  222. },
  223. new[]
  224. {
  225. GetFirstParameter((BindAsyncStruct arg) => BindAsyncStructMethod(arg)),
  226. },
  227. new[]
  228. {
  229. GetFirstParameter((BindAsyncSingleArgRecord arg) => BindAsyncSingleArgRecordMethod(arg)),
  230. },
  231. new[]
  232. {
  233. GetFirstParameter((BindAsyncSingleArgStruct arg) => BindAsyncSingleArgStructMethod(arg)),
  234. },
  235. new[]
  236. {
  237. GetFirstParameter((InheritBindAsync arg) => InheritBindAsyncMethod(arg))
  238. },
  239. new[]
  240. {
  241. GetFirstParameter((InheritBindAsyncWithParameterInfo arg) => InheritBindAsyncWithParameterInfoMethod(arg))
  242. },
  243. new[]
  244. {
  245. GetFirstParameter((BindAsyncFromInterface arg) => BindAsyncFromInterfaceMethod(arg))
  246. },
  247. new[]
  248. {
  249. GetFirstParameter((BindAsyncFromGrandparentInterface arg) => BindAsyncFromGrandparentInterfaceMethod(arg))
  250. },
  251. new[]
  252. {
  253. GetFirstParameter((BindAsyncDirectlyAndFromInterface arg) => BindAsyncDirectlyAndFromInterfaceMethod(arg))
  254. },
  255. new[]
  256. {
  257. GetFirstParameter((BindAsyncFromClassAndInterface arg) => BindAsyncFromClassAndInterfaceMethod(arg))
  258. },
  259. new[]
  260. {
  261. GetFirstParameter((BindAsyncFromInterfaceWithParameterInfo arg) => BindAsyncFromInterfaceWithParameterInfoMethod(arg))
  262. },
  263. };
  264. }
  265. }
  266. [Theory]
  267. [MemberData(nameof(BindAsyncParameterInfoData))]
  268. public void HasBindAsyncMethod_ReturnsTrueWhenMethodExists(ParameterInfo parameterInfo)
  269. {
  270. Assert.True(new ParameterBindingMethodCache().HasBindAsyncMethod(parameterInfo));
  271. }
  272. [Fact]
  273. public void HasBindAsyncMethod_ReturnsTrueForNullableReturningBindAsyncStructMethod()
  274. {
  275. var parameterInfo = GetFirstParameter((NullableReturningBindAsyncStruct arg) => NullableReturningBindAsyncStructMethod(arg));
  276. Assert.True(new ParameterBindingMethodCache().HasBindAsyncMethod(parameterInfo));
  277. }
  278. [Fact]
  279. public void FindBindAsyncMethod_FindsNonNullableReturningBindAsyncMethodGivenNullableType()
  280. {
  281. var parameterInfo = GetFirstParameter((BindAsyncStruct? arg) => BindAsyncNullableStructMethod(arg));
  282. Assert.True(new ParameterBindingMethodCache().HasBindAsyncMethod(parameterInfo));
  283. }
  284. [Fact]
  285. public async Task FindBindAsyncMethod_FindsFallbackMethodWhenPreferredMethodsReturnTypeIsWrong()
  286. {
  287. var parameterInfo = GetFirstParameter((BindAsyncFallsBack? arg) => BindAsyncFallbackMethod(arg));
  288. var cache = new ParameterBindingMethodCache();
  289. Assert.True(cache.HasBindAsyncMethod(parameterInfo));
  290. var methodFound = cache.FindBindAsyncMethod(parameterInfo);
  291. var parseHttpContext = Expression.Lambda<Func<HttpContext, ValueTask<object>>>(methodFound.Expression!,
  292. ParameterBindingMethodCache.HttpContextExpr).Compile();
  293. var httpContext = new DefaultHttpContext();
  294. Assert.Null(await parseHttpContext(httpContext));
  295. }
  296. [Fact]
  297. public async Task FindBindAsyncMethod_FindsFallbackMethodFromInheritedWhenPreferredMethodIsInvalid()
  298. {
  299. var parameterInfo = GetFirstParameter((BindAsyncBadMethod? arg) => BindAsyncBadMethodMethod(arg));
  300. var cache = new ParameterBindingMethodCache();
  301. Assert.True(cache.HasBindAsyncMethod(parameterInfo));
  302. var methodFound = cache.FindBindAsyncMethod(parameterInfo);
  303. var parseHttpContext = Expression.Lambda<Func<HttpContext, ValueTask<object>>>(methodFound.Expression!,
  304. ParameterBindingMethodCache.HttpContextExpr).Compile();
  305. var httpContext = new DefaultHttpContext();
  306. Assert.Null(await parseHttpContext(httpContext));
  307. }
  308. [Theory]
  309. [InlineData(typeof(ClassWithParameterlessConstructor))]
  310. [InlineData(typeof(RecordClassParameterlessConstructor))]
  311. [InlineData(typeof(StructWithParameterlessConstructor))]
  312. [InlineData(typeof(RecordStructWithParameterlessConstructor))]
  313. public void FindConstructor_FindsParameterlessConstructor_WhenExplicitlyDeclared(Type type)
  314. {
  315. var cache = new ParameterBindingMethodCache();
  316. var (constructor, parameters) = cache.FindConstructor(type);
  317. Assert.NotNull(constructor);
  318. Assert.True(parameters.Length == 0);
  319. }
  320. [Theory]
  321. [InlineData(typeof(ClassWithDefaultConstructor))]
  322. [InlineData(typeof(RecordClassWithDefaultConstructor))]
  323. public void FindConstructor_FindsDefaultConstructor_WhenNotExplictlyDeclared(Type type)
  324. {
  325. var cache = new ParameterBindingMethodCache();
  326. var (constructor, parameters) = cache.FindConstructor(type);
  327. Assert.NotNull(constructor);
  328. Assert.True(parameters.Length == 0);
  329. }
  330. [Theory]
  331. [InlineData(typeof(ClassWithParameterizedConstructor))]
  332. [InlineData(typeof(RecordClassParameterizedConstructor))]
  333. [InlineData(typeof(StructWithParameterizedConstructor))]
  334. [InlineData(typeof(RecordStructParameterizedConstructor))]
  335. public void FindConstructor_FindsParameterizedConstructor_WhenExplictlyDeclared(Type type)
  336. {
  337. var cache = new ParameterBindingMethodCache();
  338. var (constructor, parameters) = cache.FindConstructor(type);
  339. Assert.NotNull(constructor);
  340. Assert.True(parameters.Length == 1);
  341. }
  342. [Theory]
  343. [InlineData(typeof(StructWithDefaultConstructor))]
  344. [InlineData(typeof(RecordStructWithDefaultConstructor))]
  345. public void FindConstructor_ReturnNullForStruct_WhenNotExplictlyDeclared(Type type)
  346. {
  347. var cache = new ParameterBindingMethodCache();
  348. var (constructor, parameters) = cache.FindConstructor(type);
  349. Assert.Null(constructor);
  350. Assert.True(parameters.Length == 0);
  351. }
  352. [Theory]
  353. [InlineData(typeof(StructWithMultipleConstructors))]
  354. [InlineData(typeof(RecordStructWithMultipleConstructors))]
  355. public void FindConstructor_ReturnNullForStruct_WhenMultipleParameterizedConstructorsDeclared(Type type)
  356. {
  357. var cache = new ParameterBindingMethodCache();
  358. var (constructor, parameters) = cache.FindConstructor(type);
  359. Assert.Null(constructor);
  360. Assert.True(parameters.Length == 0);
  361. }
  362. public static TheoryData<Type> InvalidTryParseStringTypesData
  363. {
  364. get
  365. {
  366. return new TheoryData<Type>
  367. {
  368. typeof(InvalidVoidReturnTryParseStruct),
  369. typeof(InvalidVoidReturnTryParseClass),
  370. typeof(InvalidWrongTypeTryParseStruct),
  371. typeof(InvalidWrongTypeTryParseClass),
  372. typeof(InvalidTryParseNullableStruct),
  373. typeof(InvalidTooFewArgsTryParseStruct),
  374. typeof(InvalidTooFewArgsTryParseClass),
  375. typeof(InvalidNonStaticTryParseStruct),
  376. typeof(InvalidNonStaticTryParseClass),
  377. typeof(TryParseWrongTypeInheritClass),
  378. typeof(TryParseWrongTypeFromInterface),
  379. };
  380. }
  381. }
  382. [Theory]
  383. [MemberData(nameof(InvalidTryParseStringTypesData))]
  384. public void FindTryParseMethod_ThrowsIfInvalidTryParseOnType(Type type)
  385. {
  386. var ex = Assert.Throws<InvalidOperationException>(
  387. () => new ParameterBindingMethodCache().FindTryParseMethod(type));
  388. Assert.StartsWith($"TryParse method found on {TypeNameHelper.GetTypeDisplayName(type, fullName: false)} with incorrect format. Must be a static method with format", ex.Message);
  389. Assert.Contains($"bool TryParse(string, IFormatProvider, out {TypeNameHelper.GetTypeDisplayName(type, fullName: false)})", ex.Message);
  390. Assert.Contains($"bool TryParse(string, out {TypeNameHelper.GetTypeDisplayName(type, fullName: false)})", ex.Message);
  391. }
  392. [Theory]
  393. [MemberData(nameof(InvalidTryParseStringTypesData))]
  394. public void FindTryParseMethod_DoesNotThrowIfInvalidTryParseOnType_WhenThrowOnInvalidFalse(Type type)
  395. {
  396. Assert.Null(new ParameterBindingMethodCache(throwOnInvalidMethod: false).FindTryParseMethod(type));
  397. }
  398. [Fact]
  399. public void FindTryParseMethod_ThrowsIfMultipleInterfacesMatch()
  400. {
  401. var ex = Assert.Throws<InvalidOperationException>(
  402. () => new ParameterBindingMethodCache().FindTryParseMethod(typeof(TryParseFromMultipleInterfaces)));
  403. Assert.Equal("TryParseFromMultipleInterfaces implements multiple interfaces defining a static Boolean TryParse(System.String, TryParseFromMultipleInterfaces ByRef) method causing ambiguity.", ex.Message);
  404. }
  405. [Fact]
  406. public void FindTryParseMethod_DoesNotThrowIfMultipleInterfacesMatch_WhenThrowOnInvalidFalse()
  407. {
  408. Assert.Null(new ParameterBindingMethodCache(throwOnInvalidMethod: false).FindTryParseMethod(typeof(TryParseFromMultipleInterfaces)));
  409. }
  410. [Theory]
  411. [InlineData(typeof(TryParseClassWithGoodAndBad))]
  412. [InlineData(typeof(TryParseStructWithGoodAndBad))]
  413. public void FindTryParseMethod_IgnoresInvalidTryParseIfGoodOneFound(Type type)
  414. {
  415. var method = new ParameterBindingMethodCache().FindTryParseMethod(type);
  416. Assert.NotNull(method);
  417. }
  418. public static TheoryData<Type> InvalidBindAsyncTypesData
  419. {
  420. get
  421. {
  422. return new TheoryData<Type>
  423. {
  424. typeof(InvalidWrongReturnBindAsyncStruct),
  425. typeof(InvalidWrongReturnBindAsyncClass),
  426. typeof(InvalidWrongParamBindAsyncStruct),
  427. typeof(InvalidWrongParamBindAsyncClass),
  428. typeof(BindAsyncWrongTypeInherit),
  429. typeof(BindAsyncWithParameterInfoWrongTypeInherit),
  430. typeof(BindAsyncWrongTypeFromInterface),
  431. typeof(BindAsyncBothBadMethods),
  432. };
  433. }
  434. }
  435. [Theory]
  436. [MemberData(nameof(InvalidBindAsyncTypesData))]
  437. public void FindBindAsyncMethod_ThrowsIfInvalidBindAsyncOnType(Type type)
  438. {
  439. var cache = new ParameterBindingMethodCache();
  440. var parameter = new MockParameterInfo(type, "anything");
  441. var ex = Assert.Throws<InvalidOperationException>(
  442. () => cache.FindBindAsyncMethod(parameter));
  443. Assert.StartsWith($"BindAsync method found on {TypeNameHelper.GetTypeDisplayName(type, fullName: false)} with incorrect format. Must be a static method with format", ex.Message);
  444. Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}> BindAsync(HttpContext context, ParameterInfo parameter)", ex.Message);
  445. Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}> BindAsync(HttpContext context)", ex.Message);
  446. Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}?> BindAsync(HttpContext context, ParameterInfo parameter)", ex.Message);
  447. Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}?> BindAsync(HttpContext context)", ex.Message);
  448. }
  449. [Theory]
  450. [MemberData(nameof(InvalidBindAsyncTypesData))]
  451. public void FindBindAsyncMethod_DoesNotThrowIfInvalidBindAsyncOnType_WhenThrowOnInvalidFalse(Type type)
  452. {
  453. var cache = new ParameterBindingMethodCache(throwOnInvalidMethod: false);
  454. var parameter = new MockParameterInfo(type, "anything");
  455. var (expression, _) = cache.FindBindAsyncMethod(parameter);
  456. Assert.Null(expression);
  457. }
  458. [Fact]
  459. public void FindBindAsyncMethod_ThrowsIfMultipleInterfacesMatch()
  460. {
  461. var cache = new ParameterBindingMethodCache();
  462. var parameter = new MockParameterInfo(typeof(BindAsyncFromMultipleInterfaces), "anything");
  463. var ex = Assert.Throws<InvalidOperationException>(() => cache.FindBindAsyncMethod(parameter));
  464. Assert.Equal("BindAsyncFromMultipleInterfaces implements multiple interfaces defining a static System.Threading.Tasks.ValueTask`1[Microsoft.AspNetCore.Http.Extensions.Tests.ParameterBindingMethodCacheTests+BindAsyncFromMultipleInterfaces] BindAsync(Microsoft.AspNetCore.Http.HttpContext) method causing ambiguity.", ex.Message);
  465. }
  466. [Fact]
  467. public void FindBindAsyncMethod_DoesNotThrowIfMultipleInterfacesMatch_WhenThrowOnInvalidFalse()
  468. {
  469. var cache = new ParameterBindingMethodCache(throwOnInvalidMethod: false);
  470. var parameter = new MockParameterInfo(typeof(BindAsyncFromMultipleInterfaces), "anything");
  471. var (expression, _) = cache.FindBindAsyncMethod(parameter);
  472. Assert.Null(expression);
  473. }
  474. [Theory]
  475. [InlineData(typeof(BindAsyncStructWithGoodAndBad))]
  476. [InlineData(typeof(BindAsyncClassWithGoodAndBad))]
  477. public void FindBindAsyncMethod_IgnoresInvalidBindAsyncIfGoodOneFound(Type type)
  478. {
  479. var cache = new ParameterBindingMethodCache();
  480. var parameter = new MockParameterInfo(type, "anything");
  481. var (expression, _) = cache.FindBindAsyncMethod(parameter);
  482. Assert.NotNull(expression);
  483. }
  484. private class ClassWithInternalConstructor
  485. {
  486. internal ClassWithInternalConstructor()
  487. { }
  488. }
  489. private record RecordWithInternalConstructor
  490. {
  491. internal RecordWithInternalConstructor()
  492. { }
  493. }
  494. [Theory]
  495. [InlineData(typeof(ClassWithInternalConstructor))]
  496. [InlineData(typeof(RecordWithInternalConstructor))]
  497. public void FindConstructor_ThrowsIfNoPublicConstructors(Type type)
  498. {
  499. var cache = new ParameterBindingMethodCache();
  500. var ex = Assert.Throws<InvalidOperationException>(() => cache.FindConstructor(type));
  501. Assert.Equal($"No public parameterless constructor found for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'.", ex.Message);
  502. }
  503. [Theory]
  504. [InlineData(typeof(AbstractClass))]
  505. [InlineData(typeof(AbstractRecord))]
  506. public void FindConstructor_ThrowsIfAbstract(Type type)
  507. {
  508. var cache = new ParameterBindingMethodCache();
  509. var ex = Assert.Throws<InvalidOperationException>(() => cache.FindConstructor(type));
  510. Assert.Equal($"The abstract type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}' is not supported.", ex.Message);
  511. }
  512. [Theory]
  513. [InlineData(typeof(ClassWithMultipleConstructors))]
  514. [InlineData(typeof(RecordWithMultipleConstructors))]
  515. public void FindConstructor_ThrowsIfMultipleParameterizedConstructors(Type type)
  516. {
  517. var cache = new ParameterBindingMethodCache();
  518. var ex = Assert.Throws<InvalidOperationException>(() => cache.FindConstructor(type));
  519. Assert.Equal($"Only a single public parameterized constructor is allowed for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'.", ex.Message);
  520. }
  521. [Theory]
  522. [InlineData(typeof(ClassWithInvalidConstructors))]
  523. [InlineData(typeof(RecordClassWithInvalidConstructors))]
  524. [InlineData(typeof(RecordStructWithInvalidConstructors))]
  525. [InlineData(typeof(StructWithInvalidConstructors))]
  526. public void FindConstructor_ThrowsIfParameterizedConstructorIncludeNoMatchingArguments(Type type)
  527. {
  528. var cache = new ParameterBindingMethodCache();
  529. var ex = Assert.Throws<InvalidOperationException>(() => cache.FindConstructor(type));
  530. Assert.Equal(
  531. $"The public parameterized constructor must contain only parameters that match the declared public properties for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'.",
  532. ex.Message);
  533. }
  534. enum Choice
  535. {
  536. One,
  537. Two,
  538. Three
  539. }
  540. private static void TryParseStringRecordMethod(TryParseStringRecord arg) { }
  541. private static void TryParseStringStructMethod(TryParseStringStruct arg) { }
  542. private static void TryParseStringNullableStructMethod(TryParseStringStruct? arg) { }
  543. private static void BindAsyncRecordMethod(BindAsyncRecord arg) { }
  544. private static void BindAsyncStructMethod(BindAsyncStruct arg) { }
  545. private static void BindAsyncNullableStructMethod(BindAsyncStruct? arg) { }
  546. private static void NullableReturningBindAsyncStructMethod(NullableReturningBindAsyncStruct arg) { }
  547. private static void BindAsyncSingleArgRecordMethod(BindAsyncSingleArgRecord arg) { }
  548. private static void BindAsyncSingleArgStructMethod(BindAsyncSingleArgStruct arg) { }
  549. private static void InheritBindAsyncMethod(InheritBindAsync arg) { }
  550. private static void InheritBindAsyncWithParameterInfoMethod(InheritBindAsyncWithParameterInfo args) { }
  551. private static void BindAsyncFromInterfaceMethod(BindAsyncFromInterface arg) { }
  552. private static void BindAsyncFromGrandparentInterfaceMethod(BindAsyncFromGrandparentInterface arg) { }
  553. private static void BindAsyncDirectlyAndFromInterfaceMethod(BindAsyncDirectlyAndFromInterface arg) { }
  554. private static void BindAsyncFromClassAndInterfaceMethod(BindAsyncFromClassAndInterface arg) { }
  555. private static void BindAsyncFromInterfaceWithParameterInfoMethod(BindAsyncFromInterfaceWithParameterInfo args) { }
  556. private static void BindAsyncFallbackMethod(BindAsyncFallsBack? arg) { }
  557. private static void BindAsyncBadMethodMethod(BindAsyncBadMethod? arg) { }
  558. private static ParameterInfo GetFirstParameter<T>(Expression<Action<T>> expr)
  559. {
  560. var mc = (MethodCallExpression)expr.Body;
  561. return mc.Method.GetParameters()[0];
  562. }
  563. private record TryParseStringRecord(int Value)
  564. {
  565. public static bool TryParse(string? value, IFormatProvider formatProvider, out TryParseStringRecord? result)
  566. {
  567. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  568. {
  569. result = null;
  570. return false;
  571. }
  572. result = new TryParseStringRecord(val);
  573. return true;
  574. }
  575. }
  576. private record struct TryParseStringStruct(int Value)
  577. {
  578. public static bool TryParse(string? value, IFormatProvider formatProvider, out TryParseStringStruct result)
  579. {
  580. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  581. {
  582. result = default;
  583. return false;
  584. }
  585. result = new TryParseStringStruct(val);
  586. return true;
  587. }
  588. }
  589. private record struct InvalidVoidReturnTryParseStruct(int Value)
  590. {
  591. public static void TryParse(string? value, IFormatProvider formatProvider, out InvalidVoidReturnTryParseStruct result)
  592. {
  593. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  594. {
  595. result = default;
  596. return;
  597. }
  598. result = new InvalidVoidReturnTryParseStruct(val);
  599. return;
  600. }
  601. }
  602. private record struct InvalidWrongTypeTryParseStruct(int Value)
  603. {
  604. public static bool TryParse(string? value, IFormatProvider formatProvider, out InvalidVoidReturnTryParseStruct result)
  605. {
  606. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  607. {
  608. result = default;
  609. return false;
  610. }
  611. result = new InvalidVoidReturnTryParseStruct(val);
  612. return true;
  613. }
  614. }
  615. private record struct InvalidTryParseNullableStruct(int Value)
  616. {
  617. public static bool TryParse(string? value, IFormatProvider formatProvider, out InvalidTryParseNullableStruct? result)
  618. {
  619. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  620. {
  621. result = default;
  622. return false;
  623. }
  624. result = new InvalidTryParseNullableStruct(val);
  625. return true;
  626. }
  627. }
  628. private record struct InvalidTooFewArgsTryParseStruct(int Value)
  629. {
  630. public static bool TryParse(out InvalidTooFewArgsTryParseStruct result)
  631. {
  632. result = default;
  633. return false;
  634. }
  635. }
  636. private struct TryParseStructWithGoodAndBad
  637. {
  638. public static bool TryParse(string? value, out TryParseStructWithGoodAndBad result)
  639. {
  640. result = new();
  641. return false;
  642. }
  643. public static void TryParse(out TryParseStructWithGoodAndBad result)
  644. {
  645. result = new();
  646. }
  647. }
  648. private record struct InvalidNonStaticTryParseStruct(int Value)
  649. {
  650. public bool TryParse(string? value, IFormatProvider formatProvider, out InvalidVoidReturnTryParseStruct result)
  651. {
  652. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  653. {
  654. result = default;
  655. return false;
  656. }
  657. result = new InvalidVoidReturnTryParseStruct(val);
  658. return true;
  659. }
  660. }
  661. private class InvalidVoidReturnTryParseClass
  662. {
  663. public static void TryParse(string? value, IFormatProvider formatProvider, out InvalidVoidReturnTryParseClass result)
  664. {
  665. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  666. {
  667. result = new();
  668. return;
  669. }
  670. result = new();
  671. }
  672. }
  673. private class InvalidWrongTypeTryParseClass
  674. {
  675. public static bool TryParse(string? value, IFormatProvider formatProvider, out InvalidVoidReturnTryParseClass result)
  676. {
  677. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  678. {
  679. result = new();
  680. return false;
  681. }
  682. result = new();
  683. return true;
  684. }
  685. }
  686. private class InvalidTooFewArgsTryParseClass
  687. {
  688. public static bool TryParse(out InvalidTooFewArgsTryParseClass result)
  689. {
  690. result = new();
  691. return false;
  692. }
  693. }
  694. private class TryParseClassWithGoodAndBad
  695. {
  696. public static bool TryParse(string? value, out TryParseClassWithGoodAndBad result)
  697. {
  698. result = new();
  699. return false;
  700. }
  701. public static bool TryParse(out TryParseClassWithGoodAndBad result)
  702. {
  703. result = new();
  704. return false;
  705. }
  706. }
  707. private class InvalidNonStaticTryParseClass
  708. {
  709. public bool TryParse(string? value, IFormatProvider formatProvider, out InvalidNonStaticTryParseClass result)
  710. {
  711. if (!int.TryParse(value, NumberStyles.Integer, formatProvider, out var val))
  712. {
  713. result = new();
  714. return false;
  715. }
  716. result = new();
  717. return true;
  718. }
  719. }
  720. private record TryParseNoFormatProviderRecord(int Value)
  721. {
  722. public static bool TryParse(string? value, out TryParseNoFormatProviderRecord? result)
  723. {
  724. if (!int.TryParse(value, out var val))
  725. {
  726. result = null;
  727. return false;
  728. }
  729. result = new TryParseNoFormatProviderRecord(val);
  730. return true;
  731. }
  732. }
  733. private record struct TryParseNoFormatProviderStruct(int Value)
  734. {
  735. public static bool TryParse(string? value, out TryParseNoFormatProviderStruct result)
  736. {
  737. if (!int.TryParse(value, out var val))
  738. {
  739. result = default;
  740. return false;
  741. }
  742. result = new TryParseNoFormatProviderStruct(val);
  743. return true;
  744. }
  745. }
  746. private class BaseTryParseClass<T>
  747. {
  748. public static bool TryParse(string? value, out T? result)
  749. {
  750. result = default(T);
  751. return false;
  752. }
  753. }
  754. private class TryParseInheritClass : BaseTryParseClass<TryParseInheritClass>
  755. {
  756. }
  757. // using wrong T on purpose
  758. private class TryParseWrongTypeInheritClass : BaseTryParseClass<TryParseInheritClass>
  759. {
  760. }
  761. private class BaseTryParseClassWithFormatProvider<T>
  762. {
  763. public static bool TryParse(string? value, IFormatProvider formatProvider, out T? result)
  764. {
  765. result = default(T);
  766. return false;
  767. }
  768. }
  769. private class TryParseInheritClassWithFormatProvider : BaseTryParseClassWithFormatProvider<TryParseInheritClassWithFormatProvider>
  770. {
  771. }
  772. private interface ITryParse<T>
  773. {
  774. static bool TryParse(string? value, out T? result)
  775. {
  776. result = default(T);
  777. return false;
  778. }
  779. }
  780. private interface ITryParse2<T>
  781. {
  782. static bool TryParse(string? value, out T? result)
  783. {
  784. result = default(T);
  785. return false;
  786. }
  787. }
  788. private interface IImplementITryParse<T> : ITryParse<T>
  789. {
  790. }
  791. private class TryParseFromInterface : ITryParse<TryParseFromInterface>
  792. {
  793. }
  794. private class TryParseFromGrandparentInterface : IImplementITryParse<TryParseFromGrandparentInterface>
  795. {
  796. }
  797. private class TryParseDirectlyAndFromInterface : ITryParse<TryParseDirectlyAndFromInterface>
  798. {
  799. static bool TryParse(string? value, out TryParseDirectlyAndFromInterface? result)
  800. {
  801. result = null;
  802. return false;
  803. }
  804. }
  805. private class TryParseFromClassAndInterface
  806. : BaseTryParseClass<TryParseFromClassAndInterface>,
  807. ITryParse<TryParseFromClassAndInterface>
  808. {
  809. }
  810. private class TryParseFromMultipleInterfaces
  811. : ITryParse<TryParseFromMultipleInterfaces>,
  812. ITryParse2<TryParseFromMultipleInterfaces>
  813. {
  814. }
  815. // using wrong T on purpose
  816. private class TryParseWrongTypeFromInterface : ITryParse<TryParseFromInterface>
  817. {
  818. }
  819. private interface ITryParseWithFormatProvider<T>
  820. {
  821. public static bool TryParse(string? value, IFormatProvider formatProvider, out T? result)
  822. {
  823. result = default(T);
  824. return false;
  825. }
  826. }
  827. private class TryParseFromInterfaceWithFormatProvider : ITryParseWithFormatProvider<TryParseFromInterfaceWithFormatProvider>
  828. {
  829. }
  830. private record BindAsyncRecord(int Value)
  831. {
  832. public static ValueTask<BindAsyncRecord?> BindAsync(HttpContext context, ParameterInfo parameter)
  833. {
  834. Assert.Equal(typeof(BindAsyncRecord), parameter.ParameterType);
  835. Assert.Equal("bindAsyncRecord", parameter.Name);
  836. if (!int.TryParse(context.Request.Headers.ETag, out var val))
  837. {
  838. return new(result: null);
  839. }
  840. return new(result: new(val));
  841. }
  842. }
  843. private record struct BindAsyncStruct(int Value)
  844. {
  845. public static ValueTask<BindAsyncStruct> BindAsync(HttpContext context, ParameterInfo parameter)
  846. {
  847. Assert.Equal(typeof(BindAsyncStruct), parameter.ParameterType);
  848. Assert.Equal("bindAsyncStruct", parameter.Name);
  849. if (!int.TryParse(context.Request.Headers.ETag, out var val))
  850. {
  851. throw new BadHttpRequestException("The request is missing the required ETag header.");
  852. }
  853. return new(result: new(val));
  854. }
  855. }
  856. private record struct NullableReturningBindAsyncStruct(int Value)
  857. {
  858. public static ValueTask<NullableReturningBindAsyncStruct?> BindAsync(HttpContext context, ParameterInfo parameter) =>
  859. throw new NotImplementedException();
  860. }
  861. private record BindAsyncSingleArgRecord(int Value)
  862. {
  863. public static ValueTask<BindAsyncSingleArgRecord?> BindAsync(HttpContext context)
  864. {
  865. if (!int.TryParse(context.Request.Headers.ETag, out var val))
  866. {
  867. return new(result: null);
  868. }
  869. return new(result: new(val));
  870. }
  871. }
  872. private record struct BindAsyncSingleArgStruct(int Value)
  873. {
  874. public static ValueTask<BindAsyncSingleArgStruct> BindAsync(HttpContext context)
  875. {
  876. if (!int.TryParse(context.Request.Headers.ETag, out var val))
  877. {
  878. throw new BadHttpRequestException("The request is missing the required ETag header.");
  879. }
  880. return new(result: new(val));
  881. }
  882. }
  883. private record struct InvalidWrongReturnBindAsyncStruct(int Value)
  884. {
  885. public static Task<InvalidWrongReturnBindAsyncStruct> BindAsync(HttpContext context, ParameterInfo parameter) =>
  886. throw new NotImplementedException();
  887. }
  888. private class InvalidWrongReturnBindAsyncClass
  889. {
  890. public static Task<InvalidWrongReturnBindAsyncClass> BindAsync(HttpContext context, ParameterInfo parameter) =>
  891. throw new NotImplementedException();
  892. }
  893. private record struct InvalidWrongParamBindAsyncStruct(int Value)
  894. {
  895. public static ValueTask<InvalidWrongParamBindAsyncStruct> BindAsync(ParameterInfo parameter) =>
  896. throw new NotImplementedException();
  897. }
  898. private class InvalidWrongParamBindAsyncClass
  899. {
  900. public static Task<InvalidWrongParamBindAsyncClass> BindAsync(ParameterInfo parameter) =>
  901. throw new NotImplementedException();
  902. }
  903. private record struct BindAsyncStructWithGoodAndBad(int Value)
  904. {
  905. public static ValueTask<BindAsyncStructWithGoodAndBad> BindAsync(HttpContext context, ParameterInfo parameter) =>
  906. throw new NotImplementedException();
  907. public static ValueTask<BindAsyncStructWithGoodAndBad> BindAsync(ParameterInfo parameter) =>
  908. throw new NotImplementedException();
  909. }
  910. private class BindAsyncClassWithGoodAndBad
  911. {
  912. public static ValueTask<BindAsyncClassWithGoodAndBad> BindAsync(HttpContext context, ParameterInfo parameter) =>
  913. throw new NotImplementedException();
  914. public static ValueTask<BindAsyncClassWithGoodAndBad> BindAsync(ParameterInfo parameter) =>
  915. throw new NotImplementedException();
  916. }
  917. private class BaseBindAsync<T>
  918. {
  919. public static ValueTask<T?> BindAsync(HttpContext context)
  920. {
  921. return new(default(T));
  922. }
  923. }
  924. private class InheritBindAsync : BaseBindAsync<InheritBindAsync>
  925. {
  926. }
  927. // Using wrong T on purpose
  928. private class BindAsyncWrongTypeInherit : BaseBindAsync<InheritBindAsync>
  929. {
  930. }
  931. private class BaseBindAsyncWithParameterInfo<T>
  932. {
  933. public static ValueTask<T?> BindAsync(HttpContext context, ParameterInfo parameter)
  934. {
  935. return new(default(T));
  936. }
  937. }
  938. private class InheritBindAsyncWithParameterInfo : BaseBindAsyncWithParameterInfo<InheritBindAsyncWithParameterInfo>
  939. {
  940. }
  941. // Using wrong T on purpose
  942. private class BindAsyncWithParameterInfoWrongTypeInherit : BaseBindAsyncWithParameterInfo<InheritBindAsync>
  943. {
  944. }
  945. private interface IBindAsync<T>
  946. {
  947. static ValueTask<T?> BindAsync(HttpContext context)
  948. {
  949. return new(default(T));
  950. }
  951. }
  952. private interface IBindAsync2<T>
  953. {
  954. static ValueTask<T?> BindAsync(HttpContext context)
  955. {
  956. return new(default(T));
  957. }
  958. }
  959. private interface IImeplmentIBindAsync<T> : IBindAsync<T>
  960. {
  961. }
  962. private class BindAsyncFromInterface : IBindAsync<BindAsyncFromInterface>
  963. {
  964. }
  965. private class BindAsyncFromGrandparentInterface : IImeplmentIBindAsync<BindAsyncFromGrandparentInterface>
  966. {
  967. }
  968. private class BindAsyncDirectlyAndFromInterface : IBindAsync<BindAsyncDirectlyAndFromInterface>
  969. {
  970. static ValueTask<BindAsyncFromInterface?> BindAsync(HttpContext context)
  971. {
  972. return new(result: null);
  973. }
  974. }
  975. private class BindAsyncFromClassAndInterface
  976. : BaseBindAsync<BindAsyncFromClassAndInterface>,
  977. IBindAsync<BindAsyncFromClassAndInterface>
  978. {
  979. }
  980. private class BindAsyncFromMultipleInterfaces
  981. : IBindAsync<BindAsyncFromMultipleInterfaces>,
  982. IBindAsync2<BindAsyncFromMultipleInterfaces>
  983. {
  984. }
  985. // using wrong T on purpose
  986. private class BindAsyncWrongTypeFromInterface : IBindAsync<BindAsyncFromInterface>
  987. {
  988. }
  989. private interface IBindAsyncWithParameterInfo<T>
  990. {
  991. static ValueTask<T?> BindAsync(HttpContext context, ParameterInfo parameter)
  992. {
  993. return new(default(T));
  994. }
  995. }
  996. private class BindAsyncFromInterfaceWithParameterInfo : IBindAsync<BindAsyncFromInterfaceWithParameterInfo>
  997. {
  998. }
  999. private class BindAsyncFallsBack
  1000. {
  1001. public static void BindAsync(HttpContext context, ParameterInfo parameter)
  1002. => throw new NotImplementedException();
  1003. public static ValueTask<BindAsyncFallsBack?> BindAsync(HttpContext context)
  1004. {
  1005. return new(result: null);
  1006. }
  1007. }
  1008. private class BindAsyncBadMethod : IBindAsyncWithParameterInfo<BindAsyncBadMethod>
  1009. {
  1010. public static void BindAsync(HttpContext context, ParameterInfo parameter)
  1011. => throw new NotImplementedException();
  1012. }
  1013. private class BindAsyncBothBadMethods
  1014. {
  1015. public static void BindAsync(HttpContext context, ParameterInfo parameter)
  1016. => throw new NotImplementedException();
  1017. public static void BindAsync(HttpContext context)
  1018. => throw new NotImplementedException();
  1019. }
  1020. public class ClassWithParameterizedConstructor
  1021. {
  1022. public int Foo { get; set; }
  1023. public ClassWithParameterizedConstructor(int foo)
  1024. {
  1025. }
  1026. }
  1027. public record RecordClassParameterizedConstructor(int Foo);
  1028. public record struct RecordStructParameterizedConstructor(int Foo);
  1029. public struct StructWithParameterizedConstructor
  1030. {
  1031. public int Foo { get; set; }
  1032. public StructWithParameterizedConstructor(int foo)
  1033. {
  1034. Foo = foo;
  1035. }
  1036. }
  1037. public class ClassWithParameterlessConstructor
  1038. {
  1039. public ClassWithParameterlessConstructor()
  1040. {
  1041. }
  1042. public ClassWithParameterlessConstructor(int foo)
  1043. {
  1044. }
  1045. }
  1046. public record RecordClassParameterlessConstructor
  1047. {
  1048. public RecordClassParameterlessConstructor()
  1049. {
  1050. }
  1051. public RecordClassParameterlessConstructor(int foo)
  1052. {
  1053. }
  1054. }
  1055. public struct StructWithParameterlessConstructor
  1056. {
  1057. public StructWithParameterlessConstructor()
  1058. {
  1059. }
  1060. public StructWithParameterlessConstructor(int foo)
  1061. {
  1062. }
  1063. }
  1064. public record struct RecordStructWithParameterlessConstructor
  1065. {
  1066. public RecordStructWithParameterlessConstructor()
  1067. {
  1068. }
  1069. public RecordStructWithParameterlessConstructor(int foo)
  1070. {
  1071. }
  1072. }
  1073. public class ClassWithDefaultConstructor
  1074. { }
  1075. public record RecordClassWithDefaultConstructor
  1076. { }
  1077. public struct StructWithDefaultConstructor
  1078. { }
  1079. public record struct RecordStructWithDefaultConstructor
  1080. { }
  1081. public struct StructWithMultipleConstructors
  1082. {
  1083. public StructWithMultipleConstructors(int foo)
  1084. {
  1085. }
  1086. public StructWithMultipleConstructors(int foo, int bar)
  1087. {
  1088. }
  1089. }
  1090. public record struct RecordStructWithMultipleConstructors(int Foo)
  1091. {
  1092. public RecordStructWithMultipleConstructors(int foo, int bar)
  1093. : this(foo)
  1094. {
  1095. }
  1096. }
  1097. private abstract class AbstractClass { }
  1098. private abstract record AbstractRecord();
  1099. private class ClassWithMultipleConstructors
  1100. {
  1101. public ClassWithMultipleConstructors(int foo)
  1102. { }
  1103. public ClassWithMultipleConstructors(int foo, int bar)
  1104. { }
  1105. }
  1106. private record RecordWithMultipleConstructors
  1107. {
  1108. public RecordWithMultipleConstructors(int foo)
  1109. { }
  1110. public RecordWithMultipleConstructors(int foo, int bar)
  1111. { }
  1112. }
  1113. private class ClassWithInvalidConstructors
  1114. {
  1115. public int Foo { get; set; }
  1116. public ClassWithInvalidConstructors(int foo, int bar)
  1117. { }
  1118. }
  1119. private record RecordClassWithInvalidConstructors
  1120. {
  1121. public int Foo { get; set; }
  1122. public RecordClassWithInvalidConstructors(int foo, int bar)
  1123. { }
  1124. }
  1125. private struct StructWithInvalidConstructors
  1126. {
  1127. public int Foo { get; set; }
  1128. public StructWithInvalidConstructors(int foo, int bar)
  1129. {
  1130. Foo = foo;
  1131. }
  1132. }
  1133. private record struct RecordStructWithInvalidConstructors
  1134. {
  1135. public int Foo { get; set; }
  1136. public RecordStructWithInvalidConstructors(int foo, int bar)
  1137. {
  1138. Foo = foo;
  1139. }
  1140. }
  1141. private class MockParameterInfo : ParameterInfo
  1142. {
  1143. public MockParameterInfo(Type type, string name)
  1144. {
  1145. ClassImpl = type;
  1146. NameImpl = name;
  1147. }
  1148. }
  1149. }