Skip to content

Commit 3c338e0

Browse files
add tests scaffolding
1 parent 377adb9 commit 3c338e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1697
-1480
lines changed

.github/workflows/legacy-tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ jobs:
7474
Where-Object { $_.FullName -like '*Legacy*' } |
7575
ForEach-Object { nuget restore $_.FullName }
7676
nuget restore ./EndToEndCommon/EndToEndCommon.csproj
77-
nuget restore ./LegacyEndToEndTests/LegacyEndToEndTests.csproj
78-
msbuild.exe ./LegacyEndToEndTests/LegacyEndToEndTests.csproj -p:Configuration=Release -p:FrameworkVersion=v4.7.2
77+
nuget restore ./EndToEndTestsLegacy/EndToEndTestsLegacy.csproj
78+
msbuild.exe ./EndToEndTestsLegacy/EndToEndTestsLegacy.csproj -p:Configuration=Release -p:FrameworkVersion=v4.7.2
7979
return $LASTEXITCODE
8080
}
8181
@@ -108,4 +108,4 @@ jobs:
108108
run: |
109109
$path = vswhere -latest -products * -requires Microsoft.VisualStudio.Workload.ManagedDesktop Microsoft.VisualStudio.Workload.Web -requiresAny -property installationPath
110110
$path = join-path $path 'Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe'
111-
& $path ./LegacyEndToEndTests/bin/Release/net472/LegacyEndToEndTests.dll
111+
& $path ./EndToEndTestsLegacy/bin/Release/net472/EndToEndTestsLegacy.dll

CodeGenerator/CodeGenerator.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ private static CompilationUnitSyntax AddResharperDisables(CompilationUnitSyntax
160160
private ClassDeclarationSyntax GetClassDeclaration(string className,
161161
IEnumerable<MemberDeclarationSyntax> classMembers)
162162
{
163-
var optionalDapperConfig = Options.UseDapper ? Environment.NewLine + " Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;" : "";
163+
var optionalDapperConfig = Options.UseDapper
164+
? Environment.NewLine + " Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;"
165+
: "";
164166
var classDeclaration = (ClassDeclarationSyntax)ParseMemberDeclaration(
165167
$$"""
166168
class {{className}}

EndToEndScaffold/Config.cs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
3+
namespace EndToEndScaffold;
4+
5+
public readonly record struct ClassGenConfig(
6+
string TestNamespace,
7+
string LegacyTestNamespace,
8+
HashSet<KnownTestType> TestTypes
9+
);
10+
11+
public enum KnownTestType
12+
{
13+
One,
14+
Many,
15+
Exec,
16+
ExecRows,
17+
}
18+
19+
internal static class Config
20+
{
21+
public static Dictionary<string, ClassGenConfig> FilesToGenerate { get; } =
22+
new()
23+
{
24+
{
25+
"MySqlConnectorTester", new ClassGenConfig
26+
{
27+
TestNamespace = "MySqlConnectorExampleGen",
28+
LegacyTestNamespace = "MySqlConnectorLegacyExampleGen",
29+
TestTypes = [KnownTestType.One, KnownTestType.Many, KnownTestType.Exec, KnownTestType.ExecRows]
30+
}
31+
},
32+
{
33+
"MySqlConnectorDapperTester", new ClassGenConfig
34+
{
35+
TestNamespace = "MySqlConnectorDapperExampleGen",
36+
LegacyTestNamespace = "MySqlConnectorDapperLegacyExampleGen",
37+
TestTypes = [KnownTestType.One, KnownTestType.Many, KnownTestType.Exec, KnownTestType.ExecRows]
38+
}
39+
},
40+
{
41+
"NpgsqlTester", new ClassGenConfig
42+
{
43+
TestNamespace = "NpgsqlExampleGen",
44+
LegacyTestNamespace = "NpgsqlLegacyExampleGen",
45+
TestTypes = [KnownTestType.One, KnownTestType.Many, KnownTestType.Exec, KnownTestType.ExecRows]
46+
}
47+
},
48+
{
49+
"NpgsqlDapperTester", new ClassGenConfig
50+
{
51+
TestNamespace = "NpgsqlDapperExampleGen",
52+
LegacyTestNamespace = "NpgsqlDapperLegacyExampleGen",
53+
TestTypes = [KnownTestType.One, KnownTestType.Many, KnownTestType.Exec, KnownTestType.ExecRows]
54+
}
55+
},
56+
{
57+
"SqliteTester", new ClassGenConfig
58+
{
59+
TestNamespace = "SqliteExampleGen",
60+
LegacyTestNamespace = "SqliteLegacyExampleGen",
61+
TestTypes = [KnownTestType.One, KnownTestType.Many, KnownTestType.Exec, KnownTestType.ExecRows]
62+
}
63+
},
64+
{
65+
"SqliteDapperTester", new ClassGenConfig
66+
{
67+
TestNamespace = "SqliteDapperExampleGen",
68+
LegacyTestNamespace = "SqliteDapperLegacyExampleGen",
69+
TestTypes = [KnownTestType.One, KnownTestType.Many, KnownTestType.Exec, KnownTestType.ExecRows]
70+
}
71+
},
72+
};
73+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
10+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\PluginOptions\PluginOptions.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

EndToEndScaffold/Program.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.CodeAnalysis;
2+
using SqlcGenCsharp;
3+
using System;
4+
using System.Linq;
5+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
6+
7+
namespace EndToEndScaffold;
8+
9+
public static class Program
10+
{
11+
public static void Main()
12+
{
13+
var testClassName = Environment.GetEnvironmentVariable("TEST_CLASS_NAME") ??
14+
throw new Exception("env TEST_CLASS_NAME not set");
15+
var isLegacyDotnet = Environment.GetEnvironmentVariable("IS_LEGACY") == "true";
16+
var contents = GetFileContents(testClassName, isLegacyDotnet);
17+
Console.WriteLine(contents);
18+
}
19+
20+
private static string GetFileContents(string testClassName, bool isLegacyDotnet)
21+
{
22+
if (!Config.FilesToGenerate.TryGetValue(testClassName, out var config))
23+
throw new ArgumentException($"No code to generate for input {testClassName}");
24+
25+
var testsImplementation = string.Join("\n", config.TestTypes
26+
.Select(t =>
27+
{
28+
var testGen = Templates.TestImplementations[t];
29+
return isLegacyDotnet ? testGen.Legacy : testGen.Modern;
30+
})
31+
.ToList());
32+
var namespaceToTest = isLegacyDotnet ? config.LegacyTestNamespace : config.TestNamespace;
33+
return ParseCompilationUnit(
34+
$$"""
35+
using {{namespaceToTest}};
36+
using NUnit.Framework;
37+
using NUnit.Framework.Legacy;
38+
using System;
39+
using System.Collections.Generic;
40+
using System.Linq;
41+
using System.Threading.Tasks;
42+
43+
namespace SqlcGenCsharpTests
44+
{
45+
[TestFixture]
46+
public partial class {{testClassName}}
47+
{
48+
{{testsImplementation}}
49+
}
50+
}
51+
""")
52+
.NormalizeWhitespace()
53+
.ToFullString();
54+
}
55+
}

0 commit comments

Comments
 (0)