AsyncQueryableTests.Generated.tt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. // See the LICENSE file in the project root for more information.
  4. <#@ template debug="false" hostspecific="false" language="C#" #>
  5. <#@ assembly name="System.Core" #>
  6. <#@ assembly name="System.Runtime" #>
  7. <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Threading.Tasks.Extensions.dll" #>
  8. <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Linq.Async.dll" #>
  9. <#@ import namespace="System.Linq" #>
  10. <#@ import namespace="System.Text" #>
  11. <#@ import namespace="System.Threading" #>
  12. <#@ import namespace="System.Threading.Tasks" #>
  13. <#@ import namespace="System.Collections.Generic" #>
  14. <#@ output extension=".cs" #>
  15. <#
  16. var nullableParameterNames = new[] { "comparer" };
  17. var failing = new string[0];
  18. var exclude = new[] { "ForEach", "ForEachAsync", "ToEnumerable", "ToAsyncEnumerable", "ToObservable", "AsAsyncEnumerable", "WithCancellation" };
  19. var toQuotedImpl = default(Func<Type, int, bool, string>);
  20. toQuotedImpl = (t, i, b) =>
  21. {
  22. var name = t.Name;
  23. if (t.IsGenericType)
  24. {
  25. var genDef = t.GetGenericTypeDefinition();
  26. name = genDef.Name.Substring(0, genDef.Name.LastIndexOf('`'));
  27. var genArgs = "<" + string.Join(", ", t.GetGenericArguments().Select(a => toQuotedImpl(a, i, false))) + ">";
  28. if (b)
  29. {
  30. if (name == "Func" || name == "Action")
  31. {
  32. name = "Expression<" + name + genArgs + ">";
  33. }
  34. else if (name == "IAsyncEnumerable" && i == 0)
  35. {
  36. name = "IAsyncQueryable" + genArgs;
  37. }
  38. else if (name == "IOrderedAsyncEnumerable" && i == 0)
  39. {
  40. name = "IOrderedAsyncQueryable" + genArgs;
  41. }
  42. else
  43. {
  44. name += genArgs;
  45. }
  46. }
  47. else
  48. {
  49. if (name == "Nullable")
  50. {
  51. name = genArgs.Substring(1, genArgs.Length - 2) + "?";
  52. }
  53. else
  54. {
  55. name += genArgs;
  56. }
  57. }
  58. }
  59. else if (t.IsArray)
  60. {
  61. var elem = toQuotedImpl(t.GetElementType(), i, b);
  62. name = elem + "[]";
  63. }
  64. else
  65. {
  66. if (t == typeof(int))
  67. {
  68. name = "int";
  69. }
  70. else if (t == typeof(long))
  71. {
  72. name = "long";
  73. }
  74. else if (t == typeof(float))
  75. {
  76. name = "float";
  77. }
  78. else if (t == typeof(double))
  79. {
  80. name = "double";
  81. }
  82. else if (t == typeof(decimal))
  83. {
  84. name = "decimal";
  85. }
  86. else if (t == typeof(bool))
  87. {
  88. name = "bool";
  89. }
  90. else if (t == typeof(object))
  91. {
  92. name = "object";
  93. }
  94. }
  95. return name;
  96. };
  97. var toQuoted = new Func<Type, int, string>((t, i) => toQuotedImpl(t, i, true));
  98. var index = new Dictionary<string, int>();
  99. #>
  100. using System;
  101. using System.Collections.Generic;
  102. using System.Linq;
  103. using System.Linq.Expressions;
  104. using System.Threading;
  105. using System.Threading.Tasks;
  106. using Xunit;
  107. namespace Tests
  108. {
  109. public class AsyncQueryableExTests
  110. {
  111. <#
  112. // NOTE: Just including extension methods
  113. foreach (var m in typeof(AsyncEnumerable).GetMethods()
  114. .Where(m => m.IsStatic)
  115. .Where(m => !exclude.Contains(m.Name))
  116. .Where(m => m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true))
  117. .Where(m =>
  118. {
  119. var p0 = m.GetParameters()[0].ParameterType;
  120. if (p0.IsGenericType)
  121. {
  122. var p0d = p0.GetGenericTypeDefinition();
  123. return p0d == typeof(IAsyncEnumerable<>) || p0d == typeof(IOrderedAsyncEnumerable<>);
  124. }
  125. return false;
  126. })
  127. .OrderBy(m => m.Name)
  128. .ThenBy(m => m.IsGenericMethod ? m.GetGenericArguments().Length : 0)
  129. .ThenBy(m => m.GetParameters().Length)
  130. .ThenBy(m => string.Join(", ", m.GetParameters().Select(p => p.Name))))
  131. {
  132. var genArgs = m.GetGenericArguments();
  133. var ret = toQuoted(m.ReturnType, 0);
  134. var name = m.Name;
  135. if (genArgs.Length > 0)
  136. {
  137. name += "<" + string.Join(", ", genArgs.Select(a => a.Name)) + ">";
  138. }
  139. var isParams = false;
  140. var parCount = m.GetParameters().Length;
  141. if (parCount != 0)
  142. {
  143. if (m.GetParameters().Last().IsDefined(typeof(ParamArrayAttribute), true))
  144. {
  145. isParams = true;
  146. }
  147. }
  148. var pars = string.Join(", ", m.GetParameters().Select((p, i) => (i == parCount - 1 && isParams ? "params " : "") + toQuoted(p.ParameterType, i) + " " + p.Name));
  149. if (m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true))
  150. {
  151. pars = "this " + pars;
  152. }
  153. var mtd = "(MethodInfo)MethodBase.GetCurrentMethod()";
  154. if (m.IsGenericMethod)
  155. {
  156. mtd = "(" + mtd + ").MakeGenericMethod(" + string.Join(", ", genArgs.Select(a => "typeof(" + a.Name + ")")) + ")";
  157. }
  158. var provider = m.GetParameters()[0].Name + ".Provider";
  159. var factory = "";
  160. var rem = "";
  161. var cast = "";
  162. var quotedArgs = new List<string>();
  163. if (m.ReturnType.IsGenericType)
  164. {
  165. var td = m.ReturnType.GetGenericTypeDefinition();
  166. if (td.Name.EndsWith("Task`1")) // NB: Covers Task and ValueTask
  167. {
  168. factory = "ExecuteAsync<" + toQuotedImpl(m.ReturnType.GetGenericArguments()[0], -1, false) + ">";
  169. var last = m.GetParameters().Last();
  170. if (last.ParameterType == typeof(CancellationToken))
  171. {
  172. rem = ", " + last.Name;
  173. }
  174. else
  175. {
  176. rem = ", CancellationToken.None";
  177. }
  178. }
  179. else if (td == typeof(IAsyncEnumerable<>) || td == typeof(IOrderedAsyncEnumerable<>))
  180. {
  181. factory = "CreateQuery<" + toQuotedImpl(m.ReturnType.GetGenericArguments()[0], -1, false) + ">";
  182. if (td == typeof(IOrderedAsyncEnumerable<>))
  183. {
  184. cast = "(" + toQuoted(m.ReturnType, 0) + ")";
  185. }
  186. }
  187. }
  188. var n = 0;
  189. foreach (var p in m.GetParameters())
  190. {
  191. var pt = p.ParameterType;
  192. var add = false;
  193. if (pt.IsGenericType)
  194. {
  195. var ptd = pt.GetGenericTypeDefinition();
  196. if (ptd == typeof(IAsyncEnumerable<>) || ptd == typeof(IOrderedAsyncEnumerable<>))
  197. {
  198. if (n == 0)
  199. {
  200. quotedArgs.Add(p.Name + ".Expression");
  201. }
  202. else
  203. {
  204. quotedArgs.Add("GetSourceExpression(" + p.Name + ")");
  205. }
  206. add = true;
  207. }
  208. else if (ptd.Name.StartsWith("Func") || ptd.Name.StartsWith("Action"))
  209. {
  210. quotedArgs.Add(p.Name);
  211. add = true;
  212. }
  213. }
  214. if (!add)
  215. {
  216. quotedArgs.Add("Expression.Constant(" + p.Name + ", typeof(" + toQuoted(pt, -1) + "))");
  217. }
  218. n++;
  219. }
  220. var expr = "Expression.Call(" + mtd + ", " + string.Join(", ", quotedArgs) + ")";
  221. var testName = m.Name;
  222. var num = 0;
  223. if (!index.TryGetValue(testName, out num))
  224. {
  225. index[testName] = 0;
  226. }
  227. index[testName] = num + 1;
  228. testName += (num + 1);
  229. #>
  230. [Fact]
  231. public void <#=testName#>()
  232. {
  233. <#
  234. var indexes = new List<int>();
  235. var j = 0;
  236. foreach (var p in m.GetParameters())
  237. {
  238. if (!p.ParameterType.IsValueType && !p.ParameterType.IsGenericParameter && !nullableParameterNames.Contains(p.Name))
  239. {
  240. indexes.Add(j);
  241. }
  242. j++;
  243. }
  244. var tm = m;
  245. if (tm.IsGenericMethodDefinition)
  246. {
  247. tm = m.MakeGenericMethod(m.GetGenericArguments().Select(a =>
  248. {
  249. var cs = a.GetGenericParameterConstraints();
  250. if (cs.Length > 0)
  251. {
  252. var bc = cs.FirstOrDefault(c => c.IsClass);
  253. if (bc != null)
  254. {
  255. return bc;
  256. }
  257. }
  258. return typeof(int);
  259. }).ToArray());
  260. }
  261. var opName = tm.Name;
  262. if (tm.IsGenericMethod)
  263. {
  264. opName += "<" + string.Join(", ", tm.GetGenericArguments().Select(a => toQuotedImpl(a, -1, false))) + ">";
  265. }
  266. var getVal = default(Func<Type, int, string>);
  267. getVal = (tp, pos) =>
  268. {
  269. if (tp.IsGenericType)
  270. {
  271. var tpd = tp.GetGenericTypeDefinition();
  272. if (tpd == typeof(IAsyncEnumerable<>))
  273. {
  274. var tpa = tp.GetGenericArguments()[0];
  275. var et = toQuotedImpl(tpa, -1, false);
  276. var res = "new " + et + "[] { default(" + et + ") }.ToAsyncEnumerable()";
  277. if (pos == 0)
  278. {
  279. res += ".AsAsyncQueryable()";
  280. }
  281. return res;
  282. }
  283. else if (tpd == typeof(IOrderedAsyncEnumerable<>))
  284. {
  285. var tpa = tp.GetGenericArguments()[0];
  286. var res = "new " + toQuotedImpl(tpa, -1, false) + "[0].ToAsyncEnumerable()";
  287. if (pos == 0)
  288. {
  289. res += ".AsAsyncQueryable()";
  290. }
  291. return res + ".OrderBy(x => x)";
  292. }
  293. else if (tpd.Name.StartsWith("Func"))
  294. {
  295. var inv = tp.GetMethod("Invoke");
  296. var largs = string.Join(", ", inv.GetParameters().Select((lp, lpi) => toQuoted(lp.ParameterType, -1) + " arg" + lpi).ToArray());
  297. var lret = "default(" + toQuoted(inv.ReturnType, -1) + ")";
  298. if (inv.ReturnType.IsGenericType)
  299. {
  300. if (inv.ReturnType.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>))
  301. {
  302. var tpa = inv.ReturnType.GetGenericArguments()[0];
  303. var et = toQuotedImpl(tpa, -1, false);
  304. lret = "new " + et + "[] { default(" + et + ") }.ToAsyncEnumerable()";
  305. }
  306. }
  307. else if (inv.ReturnType == typeof(bool))
  308. {
  309. lret = "true";
  310. }
  311. return "(" + largs + ") => " + lret;
  312. }
  313. else if (tpd.Name.StartsWith("Action"))
  314. {
  315. var inv = tp.GetMethod("Invoke");
  316. var largs = string.Join(", ", inv.GetParameters().Select((lp, lpi) => toQuoted(lp.ParameterType, -1) + " arg" + lpi).ToArray());
  317. var lret = "Console.WriteLine()";
  318. return "(" + largs + ") => " + lret;
  319. }
  320. else if (tpd == typeof(IEqualityComparer<>))
  321. {
  322. var tpa = tp.GetGenericArguments()[0];
  323. return "EqualityComparer<" + toQuotedImpl(tpa, -1, false) + ">.Default";
  324. }
  325. else if (tpd == typeof(IComparer<>))
  326. {
  327. var tpa = tp.GetGenericArguments()[0];
  328. return "Comparer<" + toQuotedImpl(tpa, -1, false) + ">.Default";
  329. }
  330. else if (tpd == typeof(IObserver<>))
  331. {
  332. var tpa = tp.GetGenericArguments()[0];
  333. return "new NopObserver<" + toQuotedImpl(tpa, -1, false) + ">()";
  334. }
  335. }
  336. else if (tp == typeof(CancellationToken))
  337. {
  338. return "CancellationToken.None";
  339. }
  340. else if (tp == typeof(Action))
  341. {
  342. return "() => { }";
  343. }
  344. else if (tp.IsArray)
  345. {
  346. var tpa = tp.GetElementType();
  347. var et = toQuotedImpl(tpa, -1, false);
  348. return "new " + et + "[] { default(" + et + ") }";
  349. }
  350. else if (tp == typeof(int))
  351. {
  352. return "1";
  353. }
  354. return "default(" + toQuoted(tp, pos) + ")";
  355. };
  356. var vals = tm.GetParameters().Select((p, i) => getVal(p.ParameterType, i)).ToArray();
  357. var nulls = tm.GetParameters().Select((p, i) => "default(" + toQuoted(p.ParameterType, i) + ")").ToArray();
  358. var len = vals.Length;
  359. if (indexes.Count != 0)
  360. {
  361. foreach (var idx in indexes)
  362. {
  363. var args = string.Join(", ", Enumerable.Range(0, len).Select(k => k == idx ? nulls[k] : vals[k]).ToArray());
  364. var nullArg = tm.GetParameters()[idx].Name;
  365. #>
  366. AssertEx.Throws<ArgumentNullException>(() => AsyncQueryable.<#=opName#>(<#=args#>), ane => ane.ParamName == "<#=nullArg#>");
  367. <#
  368. }
  369. #>
  370. <#
  371. }
  372. {
  373. var args = string.Join(", ", vals);
  374. #>
  375. var res = AsyncQueryable.<#=opName#>(<#=args#>);
  376. <#
  377. var tmRet = tm.ReturnType;
  378. if (tmRet.IsGenericType)
  379. {
  380. tmRet = tmRet.GetGenericTypeDefinition();
  381. }
  382. if (tm.Name == "Repeat" || tm.Name == "Expand")
  383. {
  384. #>
  385. res = res.Take(5);
  386. <#
  387. }
  388. if (!failing.Contains(tm.Name))
  389. {
  390. if (tmRet.Name.EndsWith("Task`1")) // NB: Covers Task and ValueTask
  391. {
  392. #>
  393. AssertEx.SucceedOrFailProper(() => res.Wait());
  394. <#
  395. }
  396. else if (tmRet == typeof(IAsyncEnumerable<>))
  397. {
  398. #>
  399. var task = res.ForEachAsync(_ => { });
  400. AssertEx.SucceedOrFailProper(() => task.Wait());
  401. <#
  402. }
  403. }
  404. else
  405. {
  406. #>
  407. // TODO: investigate test hang
  408. <#
  409. }
  410. }
  411. #>
  412. }
  413. <#
  414. }
  415. #>
  416. }
  417. }