From a95b6ff4e8b209b27a1a5d9c1f9bf4fa3be50ad1 Mon Sep 17 00:00:00 2001 From: Dmitrii Korolev Date: Sun, 21 Jul 2024 23:31:56 +0200 Subject: [PATCH] some other tries --- .../DapperInterceptorGenerator.cs | 2 ++ .../InGeneration/DapperHelpers.cs | 2 ++ .../Dapper.AOT.Test.Integration.csproj | 2 +- .../InterceptionExecutables/DbString.cs | 31 +++++++++++-------- .../InterceptedCodeExecutionTestsBase.cs | 31 +++++++++++++++++-- .../TestCommon/RoslynTestHelpers.cs | 6 +++- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs b/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs index 702feb0..49c8b37 100644 --- a/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs +++ b/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs @@ -312,6 +312,8 @@ internal void Generate(in GenerateState ctx) resultType = grp.First().ResultType!; sb.Append("// returns data: ").Append(resultType).NewLine(); } + + sb.Append($"throw new global::System.Exception(\"my test\");").NewLine(); // assertions var commandTypeMode = flags & (OperationFlags.Text | OperationFlags.StoredProcedure | OperationFlags.TableDirect); diff --git a/src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs b/src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs index dbc9e5c..b7fb5ce 100644 --- a/src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs +++ b/src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs @@ -12,6 +12,8 @@ public static void ConfigureDbStringDbParameter( global::System.Data.Common.DbParameter dbParameter, global::Dapper.DbString? dbString) { + throw new global::System.Exception("qwe"); + if (dbString is null) { dbParameter.Value = global::System.DBNull.Value; diff --git a/test/Dapper.AOT.Test.Integration/Dapper.AOT.Test.Integration.csproj b/test/Dapper.AOT.Test.Integration/Dapper.AOT.Test.Integration.csproj index 89f817e..b750aae 100644 --- a/test/Dapper.AOT.Test.Integration/Dapper.AOT.Test.Integration.csproj +++ b/test/Dapper.AOT.Test.Integration/Dapper.AOT.Test.Integration.csproj @@ -1,7 +1,7 @@  - net6.0;net48 + net8.0;net48 Dapper.AOT.Test.Integration $(DefineConstants);DAPPERAOT_INTERNAL diff --git a/test/Dapper.AOT.Test.Integration/InterceptionExecutables/DbString.cs b/test/Dapper.AOT.Test.Integration/InterceptionExecutables/DbString.cs index 76d009f..8db744f 100644 --- a/test/Dapper.AOT.Test.Integration/InterceptionExecutables/DbString.cs +++ b/test/Dapper.AOT.Test.Integration/InterceptionExecutables/DbString.cs @@ -18,19 +18,24 @@ public static Poco Execute(IDbConnection dbConnection) public static async Task ExecuteAsync(IDbConnection dbConnection) { - var results = await dbConnection.QueryAsync("select * from dbStringTestsTable where id = @Id and Name = @Name", new - { - Name = new DbString - { - Value = "me testing!", - IsFixedLength = false, - Length = 11 - }, - - Id = 1, - }); - - return results.First(); + var a = GetValue(); + return new Poco() { Id = 1, Name = a }; + + // var results = await dbConnection.QueryAsync("select * from dbStringTestsTable where id = @Id and Name = @Name", new + // { + // Name = new DbString + // { + // Value = "me testing!", + // IsFixedLength = false, + // Length = 11 + // }, + // + // Id = 1, + // }); + // + // return results.First(); } + + public static string GetValue() => "my-data"; } } \ No newline at end of file diff --git a/test/Dapper.AOT.Test.Integration/Setup/InterceptedCodeExecutionTestsBase.cs b/test/Dapper.AOT.Test.Integration/Setup/InterceptedCodeExecutionTestsBase.cs index 9b60163..871d7b2 100644 --- a/test/Dapper.AOT.Test.Integration/Setup/InterceptedCodeExecutionTestsBase.cs +++ b/test/Dapper.AOT.Test.Integration/Setup/InterceptedCodeExecutionTestsBase.cs @@ -68,13 +68,40 @@ protected T BuildAndExecuteInterceptedUserCode( Log($$""" Generated code: --- - {{results.GeneratedSources.First().SourceText}} + {{results.GeneratedSources.FirstOrDefault().SourceText}} --- """); + + compilation = compilation.AddSyntaxTrees(RoslynTestHelpers.CreateSyntaxTree(""" + namespace System.Runtime.CompilerServices + { + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public sealed class InterceptsLocationAttribute(string path, int lineNumber, int columnNumber) : Attribute + { + } + } + """, "attribute.cs")); + + compilation = compilation.AddSyntaxTrees(RoslynTestHelpers.CreateSyntaxTree($$""" + using System; + + namespace Dapper.AOT + { + public static class D + { + [System.Runtime.CompilerServices.InterceptsLocation(path: "Program.cs", lineNumber: 21, columnNumber: 21)] + internal static string GetValue() + { + return "changed-string"; + } + } + + } + """, "intercepted!")); Assert.NotNull(compilation); Assert.True(errorCount == 0, $"Compilation errors: {diagnosticsOutputStringBuilder}"); - + var assembly = Compile(compilation!); var type = assembly.GetTypes().Single(t => t.FullName == $"InterceptionExecutables.{className}"); var mainMethod = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); diff --git a/test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs b/test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs index cd76f1b..0fd73a3 100644 --- a/test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs +++ b/test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs @@ -14,6 +14,7 @@ using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; +using Docker.DotNet.Models; namespace Dapper.TestCommon; @@ -44,7 +45,10 @@ public static class RoslynTestHelpers "RELEASE", #endif }) - .WithFeatures([ DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair ]); + .WithFeatures([ + new KeyValuePair(DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespaces, $"{DapperInterceptorGenerator.FeatureKeys.CodegenNamespace};InterceptionExecutables"), + new KeyValuePair("LangVersion", "preview"), + ]); public static SyntaxTree CreateSyntaxTree(string source, string fileName) => CSharpSyntaxTree.ParseText(source, ParseOptionsLatestLangVer, encoding: Encoding.UTF8).WithFilePath(fileName);