public void ExpressionTypeAllowListScanner_Generic1() { var ws = new ExpressionTypeAllowListScanner { Types = { typeof(List <int>), typeof(HashSet <>), typeof(Func <>), } }; foreach (var e in new Expression[] { (Expression <Func <List <int> > >)(() => new List <int>()), (Expression <Func <HashSet <int> > >)(() => new HashSet <int>()), (Expression <Func <HashSet <string> > >)(() => new HashSet <string>()), }) { Assert.AreSame(e, ws.Visit(e)); } foreach (var e in new Expression[] { (Expression <Func <List <string> > >)(() => new List <string>()), }) { Assert.ThrowsException <NotSupportedException>(() => ws.Visit(e)); } }
public void ExpressionTypeAllowListScanner_Simple() { var ws = new ExpressionTypeAllowListScanner { Types = { typeof(string), typeof(Func <string,string>), typeof(int), } }; foreach (var e in new Expression[] { (Expression <Func <string, string> >)(s => s.Substring(0, 1).ToUpper()), }) { Assert.AreSame(e, ws.Visit(e)); } foreach (var e in new Expression[] { (Expression <Func <string, char> >)(s => s[0]), }) { Assert.ThrowsException <NotSupportedException>(() => ws.Visit(e)); } }
public void ExpressionTypeAllowListScanner_Generic1() { var ws = new ExpressionTypeAllowListScanner { Types = { typeof(List <int>), typeof(HashSet <>), typeof(Func <>), } }; foreach (var e in new Expression[] { #pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.) (Expression <Func <List <int> > >)(() => new List <int>()), (Expression <Func <HashSet <int> > >)(() => new HashSet <int>()), (Expression <Func <HashSet <string> > >)(() => new HashSet <string>()), #pragma warning restore IDE0004 }) { Assert.AreSame(e, ws.Visit(e)); } foreach (var e in new Expression[] { #pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.) (Expression <Func <List <string> > >)(() => new List <string>()), #pragma warning restore IDE0004 }) { Assert.ThrowsException <NotSupportedException>(() => ws.Visit(e)); } }
public void ExpressionTypeAllowListScanner_Errors() { var ws = new ExpressionTypeAllowListScanner(); Assert.ThrowsException <NotSupportedException>(() => ((IEnumerable)ws.Types).GetEnumerator()); Assert.ThrowsException <NotSupportedException>(() => ((IEnumerable <Type>)ws.Types).GetEnumerator()); AssertEx.ThrowsException <ArgumentNullException>(() => ws.Types.Add(type: null), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => ws.Types.Add(type: null, includeBase: false), ex => Assert.AreEqual("type", ex.ParamName)); }
public void ExpressionTypeAllowListScanner_Generic2() { var ws = new ExpressionTypeAllowListScanner { Types = { typeof(IEnumerable <>), typeof(int), typeof(bool), typeof(string), typeof(Func <, >), } }; foreach (var e in new Expression[] { (Expression <Func <IEnumerable <int>, IEnumerable <string> > >)(xs => from x in xs where x > 0 select x.ToString()), }) { Assert.AreSame(e, ws.Visit(e)); } }