public void TestAfterAnalyzation() { var readFile = GetMethodName(o => o.ReadFile); var callReadFile = GetMethodName(o => o.CallReadFile); var callCallReadFile = GetMethodName(o => o.CallCallReadFile); var read = GetMethodName(() => SimpleFile.Read()); var readAsync = GetMethodName(() => SimpleFile.ReadAsync()); var generator = new AsyncCodeGenerator(); Action <IProjectAnalyzationResult> afterAnalyzationFn = result => { Assert.AreEqual(1, result.Documents.Count); Assert.AreEqual(1, result.Documents[0].Namespaces.Count); Assert.AreEqual(1, result.Documents[0].Namespaces[0].Types.Count); Assert.AreEqual(3, result.Documents[0].Namespaces[0].Types[0].Methods.Count); var methods = result.Documents[0].Namespaces[0].Types[0].Methods.ToDictionary(o => o.Symbol.Name); Assert.IsTrue(methods[readFile].OmitAsync); // Check InvokedBy Assert.AreEqual(1, methods[readFile].InvokedBy.Count); Assert.AreEqual(methods[callReadFile], methods[readFile].InvokedBy[0]); Assert.AreEqual(1, methods[callReadFile].InvokedBy.Count); Assert.AreEqual(0, methods[callCallReadFile].InvokedBy.Count); // Check MethodReferences Assert.AreEqual(1, methods[readFile].MethodReferences.Count); var methodReference = methods[readFile].MethodReferences[0]; Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.IsFalse(methodReference.Ignore); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsNull(methodReference.ReferenceFunctionData); Assert.AreEqual(read, methodReference.ReferenceSymbol.Name); Assert.AreEqual(1, methodReference.ReferenceAsyncSymbols.Count); Assert.AreEqual(readAsync, methodReference.ReferenceAsyncSymbols[0].Name); // Check Conversion Assert.AreEqual(MethodConversion.ToAsync, methods[readFile].Conversion); Assert.AreEqual(MethodConversion.ToAsync, methods[callReadFile].Conversion); Assert.AreEqual(MethodConversion.ToAsync, methods[callCallReadFile].Conversion); }; var methodConversions = new[] { MethodConversion.ToAsync, MethodConversion.Smart }; foreach (var methodConversion in methodConversions) { var config = Configure(p => p .ConfigureAnalyzation(a => a .MethodConversion(symbol => { return(symbol.Name == readFile ? methodConversion : MethodConversion.Unknown); }) .Callbacks(c => c.AfterAnalyzation(afterAnalyzationFn)) ) ); Assert.DoesNotThrowAsync(async() => await generator.GenerateAsync(config)); } }
public async Task TestAfterAnalyzation() { var readFile = GetMethodName(o => o.ReadFile); var callReadFile = GetMethodName(o => o.CallReadFile); var callCallReadFile = GetMethodName(o => o.CallCallReadFile); var read = GetMethodName(() => SimpleFile.Read()); var readAsync = GetMethodName(() => SimpleFile.ReadAsync()); void AfterAnalyzation(IProjectAnalyzationResult result) { Assert.AreEqual(1, result.Documents.Count); Assert.AreEqual(1, result.Documents[0].GlobalNamespace.NestedNamespaces.Count); Assert.AreEqual(1, result.Documents[0].GlobalNamespace.NestedNamespaces[0].Types.Count); Assert.AreEqual(3, result.Documents[0].GlobalNamespace.NestedNamespaces[0].Types[0].Methods.Count); var methods = result.Documents[0].GlobalNamespace.NestedNamespaces[0].Types[0].Methods.ToDictionary(o => o.Symbol.Name); Assert.IsTrue(methods[readFile].OmitAsync); // Check InvokedBy Assert.AreEqual(1, methods[readFile].ReferencedBy.Count()); Assert.AreEqual(methods[callReadFile], methods[readFile].ReferencedBy.First()); Assert.AreEqual(1, methods[callReadFile].ReferencedBy.Count()); Assert.AreEqual(0, methods[callCallReadFile].ReferencedBy.Count()); // Check MethodReferences Assert.AreEqual(1, methods[readFile].BodyFunctionReferences.Count()); var methodReference = methods[readFile].BodyFunctionReferences.First(); Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.AreEqual(ReferenceConversion.ToAsync, methodReference.GetConversion()); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsNull(methodReference.ReferenceFunction); Assert.AreEqual(read, methodReference.ReferenceSymbol.Name); Assert.AreEqual(1, methodReference.ReferenceAsyncSymbols.Count); Assert.AreEqual(readAsync, methodReference.ReferenceAsyncSymbols[0].Name); // Check Conversion Assert.AreEqual(MethodConversion.ToAsync, methods[readFile].Conversion); Assert.AreEqual(MethodConversion.ToAsync, methods[callReadFile].Conversion); Assert.AreEqual(MethodConversion.ToAsync, methods[callCallReadFile].Conversion); } var methodConversions = new[] { MethodConversion.ToAsync, MethodConversion.Smart }; foreach (var methodConversion in methodConversions) { await ReadonlyTest(p => p .ConfigureAnalyzation(a => a .MethodConversion(symbol => symbol.Name == readFile ? methodConversion : MethodConversion.Unknown) .AfterAnalyzation(AfterAnalyzation) ) ); } }
public virtual async Task <bool> ReadAsync() { await SimpleFile.ReadAsync(); return(true); }
public async Task TestCancellationTokensAfterAnalyzation() { var readFile = GetMethodName(o => o.ReadFile); var callReadFile = GetMethodName(o => o.CallReadFile); var callCallReadFile = GetMethodName(o => o.CallCallReadFile); var read = GetMethodName(() => SimpleFile.Read()); var readAsync = GetMethodName(() => SimpleFile.ReadAsync()); void AfterAnalyzation(IProjectAnalyzationResult result) { Assert.AreEqual(1, result.Documents.Count); Assert.AreEqual(1, result.Documents[0].GlobalNamespace.NestedNamespaces.Count); Assert.AreEqual(1, result.Documents[0].GlobalNamespace.NestedNamespaces[0].Types.Count); Assert.AreEqual(3, result.Documents[0].GlobalNamespace.NestedNamespaces[0].Types[0].Methods.Count); var methods = result.Documents[0].GlobalNamespace.NestedNamespaces[0].Types[0].Methods.ToDictionary(o => o.Symbol.Name); // Check conversions CheckMethodsConversion(methods.Values); //Check token Assert.IsTrue(methods[readFile].CancellationTokenRequired); Assert.IsTrue(methods[callReadFile].CancellationTokenRequired); Assert.IsTrue(methods[callCallReadFile].CancellationTokenRequired); Assert.IsTrue(methods[readFile].OmitAsync); // Check MethodReferences Assert.AreEqual(1, methods[readFile].BodyFunctionReferences.Count()); var methodReference = methods[readFile].BodyFunctionReferences.First(); Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.AreEqual(ReferenceConversion.ToAsync, methodReference.GetConversion()); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsTrue(methodReference.PassCancellationToken); Assert.IsNull(methodReference.ReferenceFunction); Assert.AreEqual(read, methodReference.ReferenceSymbol.Name); Assert.AreEqual(2, methodReference.ReferenceAsyncSymbols.Count); Assert.AreEqual(readAsync, methodReference.ReferenceAsyncSymbols[0].Name); Assert.AreEqual(readAsync, methodReference.ReferenceAsyncSymbols[1].Name); methodReference = methods[callReadFile].BodyFunctionReferences.First(); Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.AreEqual(ReferenceConversion.ToAsync, methodReference.GetConversion()); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsTrue(methodReference.PassCancellationToken); methodReference = methods[callCallReadFile].BodyFunctionReferences.First(); Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.AreEqual(ReferenceConversion.ToAsync, methodReference.GetConversion()); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsTrue(methodReference.PassCancellationToken); } var methodConversions = new Func <IMethodSymbol, MethodConversion>[] { s => s.Name == readFile ? MethodConversion.ToAsync : MethodConversion.Unknown, s => s.Name == readFile ? MethodConversion.Smart : MethodConversion.Unknown, s => s.Name == callCallReadFile ? MethodConversion.ToAsync : MethodConversion.Smart }; foreach (var methodConversion in methodConversions) { await ReadonlyTest(p => p .ConfigureAnalyzation(a => a .MethodConversion(methodConversion) .CancellationTokens(true) .AfterAnalyzation(AfterAnalyzation) ) ); } }
public virtual async Task <bool> ReadAsync(CancellationToken cancellationToken) { await SimpleFile.ReadAsync(cancellationToken); return(true); }
public void TestUseCancellationTokenOverloadAfterAnalyzation() { var readFile = GetMethodName(o => o.ReadFile); var callReadFile = GetMethodName(o => o.CallReadFile); var callCallReadFile = GetMethodName(o => o.CallCallReadFile); var read = GetMethodName(() => SimpleFile.Read()); var readAsync = GetMethodName(() => SimpleFile.ReadAsync()); var generator = new AsyncCodeGenerator(); Action <IProjectAnalyzationResult> afterAnalyzationFn = result => { Assert.AreEqual(1, result.Documents.Count); Assert.AreEqual(1, result.Documents[0].Namespaces.Count); Assert.AreEqual(1, result.Documents[0].Namespaces[0].Types.Count); Assert.AreEqual(3, result.Documents[0].Namespaces[0].Types[0].Methods.Count); var methods = result.Documents[0].Namespaces[0].Types[0].Methods.ToDictionary(o => o.Symbol.Name); // Check conversions CheckMethodsConversion(methods.Values); //Check token Assert.IsTrue(methods[readFile].CancellationTokenRequired); Assert.IsTrue(methods[callReadFile].CancellationTokenRequired); Assert.IsTrue(methods[callCallReadFile].CancellationTokenRequired); Assert.IsTrue(methods[readFile].OmitAsync); // Check MethodReferences Assert.AreEqual(1, methods[readFile].MethodReferences.Count); var methodReference = methods[readFile].MethodReferences[0]; Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.IsFalse(methodReference.Ignore); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsTrue(methodReference.CancellationTokenRequired); Assert.IsNull(methodReference.ReferenceFunctionData); Assert.AreEqual(read, methodReference.ReferenceSymbol.Name); Assert.AreEqual(2, methodReference.ReferenceAsyncSymbols.Count); Assert.AreEqual(readAsync, methodReference.ReferenceAsyncSymbols[0].Name); Assert.AreEqual(readAsync, methodReference.ReferenceAsyncSymbols[1].Name); methodReference = methods[callReadFile].MethodReferences[0]; Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.IsFalse(methodReference.Ignore); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsTrue(methodReference.CancellationTokenRequired); methodReference = methods[callCallReadFile].MethodReferences[0]; Assert.AreEqual(SyntaxKind.InvocationExpression, methodReference.ReferenceNode.Kind()); Assert.IsFalse(methodReference.Ignore); Assert.IsFalse(methodReference.AwaitInvocation); Assert.IsTrue(methodReference.UseAsReturnValue); Assert.IsTrue(methodReference.CancellationTokenRequired); }; var methodConversions = new Func <IMethodSymbol, MethodConversion>[] { s => s.Name == readFile ? MethodConversion.ToAsync : MethodConversion.Unknown, s => s.Name == readFile ? MethodConversion.Smart : MethodConversion.Unknown, s => s.Name == callCallReadFile ? MethodConversion.ToAsync : MethodConversion.Smart }; foreach (var methodConversion in methodConversions) { var config = Configure(p => p .ConfigureAnalyzation(a => a .MethodConversion(methodConversion) .UseCancellationTokenOverload(true) .Callbacks(c => c.AfterAnalyzation(afterAnalyzationFn)) ) ); Assert.DoesNotThrowAsync(async() => await generator.GenerateAsync(config)); Console.WriteLine("Passed"); } }
public Task ReadAsync() { return(SimpleFile.ReadAsync()); }
public void ReadAsync() { SimpleFile.ReadAsync().GetAwaiter().GetResult(); }
public IAsyncEnumerable <string> GetDataAsync() { SimpleFile.ReadAsync(); return(AsyncEnumerable.Empty <string>()); }