public void TypeBasedExpressionRewriter_NoChange() { foreach (var e in new Expression[] { Expression.Constant(1L, typeof(long)), (Expression <Func <DateTimeOffset> >)(() => DateTimeOffset.Now), (Expression <Func <IEnumerable <long>, IEnumerable <string> > >)(xs => from x in xs where x % 2 == 0 select x.ToString()) }) { var rewriter = new TestRewriter(); Assert.AreSame(e, rewriter.Visit(e)); } }
public void TypeBasedExpressionRewriter_NoChange() { foreach (var e in new Expression[] { Expression.Constant(1L, typeof(long)), #pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.) (Expression <Func <DateTimeOffset> >)(() => DateTimeOffset.Now), #pragma warning restore IDE0004 (Expression <Func <IEnumerable <long>, IEnumerable <string> > >)(xs => from x in xs where x % 2 == 0 select x.ToString()) }) { var rewriter = new TestRewriter(); Assert.AreSame(e, rewriter.Visit(e)); } }
public void TypeBasedExpressionRewriter_Simple() { var f = new Foo { Value = 42 }; foreach (var e in new Dictionary <Expression, Expression> { { Expression.Constant(f, typeof(Foo)), Expression.Convert(Expression.Constant(f, typeof(Foo)), typeof(Bar)) }, { (Expression <Func <Foo, Foo> >)(x => x.Other), (Expression <Func <Foo, Foo> >)(x => (Bar)((Bar)x).Other) }, { (Expression <Func <Foo, Foo> >)(x => x), (Expression <Func <Foo, Foo> >)(x => (Bar)x) }, #pragma warning disable IDE0004 // Remove Unnecessary Cast. (Keeping inside expression tree.) { (Expression <Func <List <int>, IEnumerable <int> > >)(x => (IEnumerable <int>)x), (Expression <Func <List <int>, IEnumerable <int> > >)(x => (IEnumerable <int>)(IEnumerable <int>) x) }, #pragma warning restore IDE0004 }) { var comparer = new ExpressionEqualityComparer(); var rewriter = new TestRewriter(); Assert.IsTrue(comparer.Equals(rewriter.Visit(e.Key), e.Value)); } }
private Tuple <JsClass, MockErrorReporter> Compile(string source, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var er = new MockErrorReporter(!expectErrors); var n = new Namer(); var references = new[] { Mscorlib, QUnit }; var compilation = PreparedCompilation.CreateCompilation("Test", new[] { sourceFile }, references, null); var s = new AttributeStore(compilation.Compilation, er); s.RunAttributeCode(); var md = new MetadataImporter(er, compilation.Compilation, s, new CompilerOptions()); var rtl = new RuntimeLibrary(md, er, compilation.Compilation, n, s); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Compiler(md, n, rtl, er); var result = compiler.Compile(compilation).ToList(); Assert.That(result, Has.Count.EqualTo(1), "Should compile exactly one type"); Assert.That(er.AllMessages, Is.Empty, "Compile should not generate errors"); result = new TestRewriter(er, rtl, s).Rewrite(result).ToList(); Assert.That(result, Has.Count.EqualTo(1), "Should have one type after rewrite"); Assert.That(result[0], Is.InstanceOf <JsClass>(), "Compiled type should be a class after rewrite"); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty); } else { Assert.That(er.AllMessages, Is.Empty); } return(Tuple.Create((JsClass)result[0], er)); }