Skip to content

Commit

Permalink
some other tries
Browse files Browse the repository at this point in the history
  • Loading branch information
DeagleGross committed Jul 21, 2024
1 parent 44e37d0 commit a95b6ff
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 17 in src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 17 in src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 17 in src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 17 in src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected
{
dbParameter.Value = global::System.DBNull.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<RootNamespace>Dapper.AOT.Test.Integration</RootNamespace>
<DefineConstants>$(DefineConstants);DAPPERAOT_INTERNAL</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ public static Poco Execute(IDbConnection dbConnection)

public static async Task<Poco> ExecuteAsync(IDbConnection dbConnection)
{
var results = await dbConnection.QueryAsync<Poco>("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<Poco>("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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,40 @@ protected T BuildAndExecuteInterceptedUserCode<T>(
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);
Expand Down
6 changes: 5 additions & 1 deletion test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Docker.DotNet.Models;

namespace Dapper.TestCommon;

Expand Down Expand Up @@ -44,7 +45,10 @@ public static class RoslynTestHelpers
"RELEASE",
#endif
})
.WithFeatures([ DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair ]);
.WithFeatures([
new KeyValuePair<string, string>(DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespaces, $"{DapperInterceptorGenerator.FeatureKeys.CodegenNamespace};InterceptionExecutables"),
new KeyValuePair<string, string>("LangVersion", "preview"),
]);

public static SyntaxTree CreateSyntaxTree(string source, string fileName)
=> CSharpSyntaxTree.ParseText(source, ParseOptionsLatestLangVer, encoding: Encoding.UTF8).WithFilePath(fileName);
Expand Down

0 comments on commit a95b6ff

Please sign in to comment.