| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 | <#@ template debug="false" hostspecific="false" language="C#" #><#@ assembly name="System.Core" #><#@ assembly name="$(ProjectDir)\..\packages\Ix-Async.1.2.5\lib\net45\System.Interactive.Async.dll" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Threading" #><#@ import namespace="System.Threading.Tasks" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".cs" #><#var failing = new[] { "TakeLast" };var exclude = new[] { "ForEach", "ForEachAsync", "ToEnumerable", "ToAsyncEnumerable", "ToObservable", "AsAsyncEnumerable" };var toQuotedImpl = default(Func<Type, int, bool, string>);toQuotedImpl = (t, i, b) =>{var name = t.Name;if (t.IsGenericType){	var genDef = t.GetGenericTypeDefinition();	name = genDef.Name.Substring(0, genDef.Name.LastIndexOf('`'));	var genArgs = "<" + string.Join(", ", t.GetGenericArguments().Select(a => toQuotedImpl(a, i, false))) + ">";	if (b)	{		if (name == "Func" || name == "Action")		{			name = "Expression<" + name + genArgs + ">";		}		else if (name == "IAsyncEnumerable" && i == 0)		{			name = "IAsyncQueryable" + genArgs;		}		else if (name == "IOrderedAsyncEnumerable" && i == 0)		{			name = "IOrderedAsyncQueryable" + genArgs;		}		else		{			name += genArgs;		}	}	else	{		if (name == "Nullable")		{			name = genArgs.Substring(1, genArgs.Length - 2) + "?";		}		else		{			name += genArgs;		}	}}else if (t.IsArray){	var elem = toQuotedImpl(t.GetElementType(), i, b);	name = elem + "[]";}else{	if (t == typeof(int))	{		name = "int";	}	else if (t == typeof(long))	{		name = "long";	}	else if (t == typeof(float))	{		name = "float";	}	else if (t == typeof(double))	{		name = "double";	}	else if (t == typeof(decimal))	{		name = "decimal";	}	else if (t == typeof(bool))	{		name = "bool";	}	else if (t == typeof(object))	{		name = "object";	}}return name;};var toQuoted = new Func<Type, int, string>((t, i) => toQuotedImpl(t, i, true));var index = new Dictionary<string, int>();#>using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Threading;using Xunit;namespace Tests{		public class AsyncQueryableTests	{<#// NOTE: Just including extension methodsforeach (var m in typeof(AsyncEnumerable).GetMethods()						.Where(m => m.IsStatic)						.Where(m => !exclude.Contains(m.Name))						.Where(m => m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true))						.Where(m =>						{							var p0 = m.GetParameters()[0].ParameterType;							if (p0.IsGenericType)							{								var p0d = p0.GetGenericTypeDefinition();								return p0d == typeof(IAsyncEnumerable<>) || p0d == typeof(IOrderedAsyncEnumerable<>);							}							return false;						})						.OrderBy(m => m.Name)						.ThenBy(m => m.GetParameters().Length)){	var genArgs = m.GetGenericArguments();	var ret = toQuoted(m.ReturnType, 0);	var name = m.Name;	if (genArgs.Length > 0)	{		name += "<" + string.Join(", ", genArgs.Select(a => a.Name)) + ">";	}	var isParams = false;	var parCount = m.GetParameters().Length;	if (parCount != 0)	{		if (m.GetParameters().Last().IsDefined(typeof(ParamArrayAttribute), true))		{			isParams = true;		}	}	var pars = string.Join(", ", m.GetParameters().Select((p, i) => (i == parCount - 1 && isParams ? "params " : "") + toQuoted(p.ParameterType, i) + " " + p.Name));	if (m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true))	{		pars = "this " + pars;	}	var mtd = "(MethodInfo)MethodBase.GetCurrentMethod()";	if (m.IsGenericMethod)	{		mtd = "(" + mtd + ").MakeGenericMethod(" + string.Join(", ", genArgs.Select(a => "typeof(" + a.Name + ")")) + ")";	}	var provider = m.GetParameters()[0].Name + ".Provider";	var factory = "";	var rem = "";	var cast = "";	var quotedArgs = new List<string>();	if (m.ReturnType.IsGenericType)	{		var td = m.ReturnType.GetGenericTypeDefinition();		if (td == typeof(Task<>))		{			factory = "ExecuteAsync<" + toQuotedImpl(m.ReturnType.GetGenericArguments()[0], -1, false) + ">";			var last = m.GetParameters().Last();			if (last.ParameterType == typeof(CancellationToken))			{				rem = ", " + last.Name;			}			else			{				rem = ", CancellationToken.None";			}		}		else if (td == typeof(IAsyncEnumerable<>) || td == typeof(IOrderedAsyncEnumerable<>))		{			factory = "CreateQuery<" + toQuotedImpl(m.ReturnType.GetGenericArguments()[0], -1, false) + ">";			if (td == typeof(IOrderedAsyncEnumerable<>))			{				cast = "(" + toQuoted(m.ReturnType, 0) + ")";			}		}	}	var n = 0;	foreach (var p in m.GetParameters())	{		var pt = p.ParameterType;		var add = false;		if (pt.IsGenericType)		{			var ptd = pt.GetGenericTypeDefinition();			if (ptd == typeof(IAsyncEnumerable<>) || ptd == typeof(IOrderedAsyncEnumerable<>))			{				if (n == 0)				{					quotedArgs.Add(p.Name + ".Expression");				}				else				{					quotedArgs.Add("GetSourceExpression(" + p.Name + ")");				}				add = true;			}			else if (ptd.Name.StartsWith("Func") || ptd.Name.StartsWith("Action"))			{				quotedArgs.Add(p.Name);				add = true;			}		}				if (!add)		{			quotedArgs.Add("Expression.Constant(" + p.Name + ", typeof(" + toQuoted(pt, -1) + "))");		}		n++;	}	var expr = "Expression.Call(" + mtd + ", " + string.Join(", ", quotedArgs) + ")";	var testName = m.Name;	var num = 0;	if (!index.TryGetValue(testName, out num))	{		index[testName] = 0;	}	index[testName] = num + 1;	testName += (num + 1);#>		[Fact]		public void <#=testName#>()		{<#var indexes = new List<int>();var j = 0;foreach (var p in m.GetParameters()){	if (!p.ParameterType.IsValueType && !p.ParameterType.IsGenericParameter)	{		indexes.Add(j);	}	j++;}var tm = m;if (tm.IsGenericMethodDefinition){	tm = m.MakeGenericMethod(m.GetGenericArguments().Select(a =>	{		var cs = a.GetGenericParameterConstraints();		if (cs.Length > 0)		{			var bc = cs.FirstOrDefault(c => c.IsClass);			if (bc != null)			{				return bc;			}		}		return typeof(int);	}).ToArray());}var opName = tm.Name;if (tm.IsGenericMethod){	opName += "<" + string.Join(", ", tm.GetGenericArguments().Select(a => toQuotedImpl(a, -1, false))) + ">";}var getVal = default(Func<Type, int, string>);getVal = (tp, pos) =>{	if (tp.IsGenericType)	{		var tpd = tp.GetGenericTypeDefinition();		if (tpd == typeof(IAsyncEnumerable<>))		{			var tpa = tp.GetGenericArguments()[0];			var et = toQuotedImpl(tpa, -1, false);			var res = "new " + et + "[] { default(" + et + ") }.ToAsyncEnumerable()";			if (pos == 0)			{				res += ".AsAsyncQueryable()";			}			return res;		}		else if (tpd == typeof(IOrderedAsyncEnumerable<>))		{			var tpa = tp.GetGenericArguments()[0];			var res = "new " + toQuotedImpl(tpa, -1, false) + "[0].ToAsyncEnumerable()";			if (pos == 0)			{				res += ".AsAsyncQueryable()";			}			return res + ".OrderBy(x => x)";		}		else if (tpd.Name.StartsWith("Func"))		{			var inv = tp.GetMethod("Invoke");							var largs = string.Join(", ", inv.GetParameters().Select((lp, lpi) => toQuoted(lp.ParameterType, -1) + " arg" + lpi).ToArray());			var lret = "default(" + toQuoted(inv.ReturnType, -1) + ")";			if (inv.ReturnType.IsGenericType)			{				if (inv.ReturnType.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>))				{					var tpa = inv.ReturnType.GetGenericArguments()[0];					var et = toQuotedImpl(tpa, -1, false);					lret = "new " + et + "[] { default(" + et + ") }.ToAsyncEnumerable()";				}			}			else if (inv.ReturnType == typeof(bool))			{				lret = "true";			}			return "(" + largs + ") => " + lret;		}		else if (tpd.Name.StartsWith("Action"))		{			var inv = tp.GetMethod("Invoke");							var largs = string.Join(", ", inv.GetParameters().Select((lp, lpi) => toQuoted(lp.ParameterType, -1) + " arg" + lpi).ToArray());			var lret = "Console.WriteLine()";			return "(" + largs + ") => " + lret;		}		else if (tpd == typeof(IEqualityComparer<>))		{			var tpa = tp.GetGenericArguments()[0];			return "EqualityComparer<" + toQuotedImpl(tpa, -1, false) + ">.Default";		}		else if (tpd == typeof(IComparer<>))		{			var tpa = tp.GetGenericArguments()[0];			return "Comparer<" + toQuotedImpl(tpa, -1, false) + ">.Default";		}		else if (tpd == typeof(IObserver<>))		{			var tpa = tp.GetGenericArguments()[0];			return "new NopObserver<" + toQuotedImpl(tpa, -1, false) + ">()";		}	}	else if (tp == typeof(CancellationToken))	{		return "CancellationToken.None";	}	else if (tp == typeof(Action))	{		return "() => { }";	}	else if (tp.IsArray)	{		var tpa = tp.GetElementType();		var et = toQuotedImpl(tpa, -1, false);		return "new " + et + "[] { default(" + et + ") }";	}	else if (tp == typeof(int))	{		return "1";	}	return "default(" + toQuoted(tp, pos) + ")";};var vals = tm.GetParameters().Select((p, i) => getVal(p.ParameterType, i)).ToArray();var nulls = tm.GetParameters().Select((p, i) => "default(" + toQuoted(p.ParameterType, i) + ")").ToArray();var len = vals.Length;if (indexes.Count != 0){	foreach (var idx in indexes)	{		var args = string.Join(", ", Enumerable.Range(0, len).Select(k => k == idx ? nulls[k] : vals[k]).ToArray());		var nullArg = tm.GetParameters()[idx].Name;#>			AssertEx.Throws<ArgumentNullException>(() => AsyncQueryable.<#=opName#>(<#=args#>), ane => ane.ParamName == "<#=nullArg#>");<#	}#><#}{	var args = string.Join(", ", vals);#>			var res = AsyncQueryable.<#=opName#>(<#=args#>);<#	var tmRet = tm.ReturnType;	if (tmRet.IsGenericType)	{		tmRet = tmRet.GetGenericTypeDefinition();	}	if (tm.Name == "Repeat" || tm.Name == "Expand")	{#>			res = res.Take(5);<#	}	if (!failing.Contains(tm.Name))	{		if (tmRet == typeof(Task<>))		{#>			AssertEx.SucceedOrFailProper(() => res.Wait());<#		}		else if (tmRet == typeof(IAsyncEnumerable<>))		{#>			var task = res.ForEachAsync(_ => { });			AssertEx.SucceedOrFailProper(() => task.Wait());<#		}	}	else	{#>			// TODO: investigate test hang<#	}}#>		}<#}#>	}}
 |