Skip to content

Commit eaa21eb

Browse files
committed
correctly generate type with generic enum constraint
1 parent 9ba8da4 commit eaa21eb

6 files changed

Lines changed: 75 additions & 25 deletions

File tree

TypeScript.ContractGenerator.Tests/CliTest.cs

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Linq;
25

36
using FluentAssertions;
47

@@ -13,28 +16,24 @@ public class CliTest
1316
[Test]
1417
public void CliGenerated()
1518
{
16-
RunDotnetCommand($"{pathToSlnDirectory}/TypeScript.ContractGenerator.Cli/bin/{configuration}/{targetFramework}/SkbKontur.TypeScript.ContractGenerator.Cli.dll " +
17-
$"-a {pathToSlnDirectory}/AspNetCoreExample.Api/bin/{configuration}/{targetFramework}/AspNetCoreExample.Api.dll " +
18-
$"-o {TestContext.CurrentContext.TestDirectory}/cliOutput " +
19-
"--nullabilityMode NullableReference " +
20-
"--lintMode TsLint");
21-
22-
var expectedDirectory = $"{pathToSlnDirectory}/AspNetCoreExample.Generator/output";
23-
var actualDirectory = $"{TestContext.CurrentContext.TestDirectory}/cliOutput";
24-
TestBase.CheckDirectoriesEquivalenceInner(expectedDirectory, actualDirectory, generatedOnly : true);
19+
var output = Path.Combine(TestContext.CurrentContext.TestDirectory, "cliOutput");
20+
21+
RunDotnetCommand($"{Tool()} -a {Assembly()} -o {output} --nullabilityMode NullableReference --lintMode TsLint");
22+
23+
var expectedDirectory = $"{repoRoot}/AspNetCoreExample.Generator/output";
24+
TestBase.CheckDirectoriesEquivalenceInner(expectedDirectory, output, generatedOnly : true);
2525
}
2626

2727
[Test]
2828
public void RoslynCliGenerated()
2929
{
30-
RunDotnetCommand($"{pathToSlnDirectory}/TypeScript.ContractGenerator.Cli/bin/{configuration}/{targetFramework}/SkbKontur.TypeScript.ContractGenerator.Cli.dll " +
31-
$"-d {pathToSlnDirectory}/AspNetCoreExample.Api " +
32-
$"-a {typeof(ControllerBase).Assembly.Location} " +
33-
$"-o {TestContext.CurrentContext.TestDirectory}/roslynCliOutput " +
34-
"--nullabilityMode NullableReference " +
35-
"--lintMode TsLint");
36-
37-
var expectedDirectory = $"{pathToSlnDirectory}/AspNetCoreExample.Generator/output";
30+
var project = Path.Combine(repoRoot, "AspNetCoreExample.Api");
31+
var assembly = typeof(ControllerBase).Assembly.Location;
32+
var output = Path.Combine(TestContext.CurrentContext.TestDirectory, "roslynCliOutput");
33+
34+
RunDotnetCommand($"{Tool()} -d {project} -a {assembly} -o {output} --nullabilityMode NullableReference --lintMode TsLint");
35+
36+
var expectedDirectory = $"{repoRoot}/AspNetCoreExample.Generator/output";
3837
var actualDirectory = $"{TestContext.CurrentContext.TestDirectory}/roslynCliOutput";
3938
TestBase.CheckDirectoriesEquivalenceInner(expectedDirectory, actualDirectory, generatedOnly : true);
4039
}
@@ -55,12 +54,47 @@ private static void RunDotnetCommand(string command)
5554
process.Start();
5655
process.WaitForExit();
5756

58-
process.ExitCode.Should().Be(0);
59-
process.StandardOutput.ReadToEnd().Trim().Should().Be("Generating TypeScript");
6057
process.StandardError.ReadToEnd().Trim().Should().BeEmpty();
58+
process.StandardOutput.ReadToEnd().Trim().Should().Be("Generating TypeScript");
59+
process.ExitCode.Should().Be(0);
60+
}
61+
62+
private static string Tool()
63+
{
64+
return Path.Combine(
65+
repoRoot,
66+
"TypeScript.ContractGenerator.Cli",
67+
"bin",
68+
configuration,
69+
targetFramework,
70+
"SkbKontur.TypeScript.ContractGenerator.Cli.dll"
71+
);
72+
}
73+
74+
private static string Assembly()
75+
{
76+
return Path.Combine(
77+
repoRoot,
78+
"AspNetCoreExample.Api",
79+
"bin",
80+
configuration,
81+
targetFramework,
82+
"AspNetCoreExample.Api.dll"
83+
);
84+
}
85+
86+
private static string RootDirectory(DirectoryInfo? current = null)
87+
{
88+
current ??= new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
89+
while (current != null && current.EnumerateFiles().All(x => x.Name != "TypeScript.ContractGenerator.sln"))
90+
{
91+
current = current.Parent;
92+
}
93+
94+
return current?.FullName ?? throw new InvalidOperationException("Cannot find root folder");
6195
}
6296

63-
private static readonly string pathToSlnDirectory = $"{TestContext.CurrentContext.TestDirectory}/../../../../";
97+
private static readonly string repoRoot = RootDirectory();
6498

6599
private const string targetFramework = "net8.0";
66100

TypeScript.ContractGenerator.Tests/EndToEndTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class EndToEndTests<TTypesProvider> : TestBase
2424
[TestCase(typeof(ComplexRootType), "complex-types")]
2525
[TestCase(typeof(GenericRootType<>), "generic-root")]
2626
[TestCase(typeof(GenericContainingRootType), "generic-types")]
27+
[TestCase(typeof(GenericInheritingType<>), "generic-inheriting-types")]
2728
[TestCase(typeof(ArrayRootType), "array-types")]
2829
[TestCase(typeof(NotNullRootType), "notnull-types")]
2930
[TestCase(typeof(NonDefaultConstructorRootType), "non-default-constructor")]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
export type GenericInheritingType<TType> = {
3+
type: TType;
4+
};

TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net48;net6.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>net48;net8.0</TargetFrameworks>
55
<AssemblyName>SkbKontur.TypeScript.ContractGenerator.Tests</AssemblyName>
66
<RootNamespace>SkbKontur.TypeScript.ContractGenerator.Tests</RootNamespace>
77
</PropertyGroup>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace SkbKontur.TypeScript.ContractGenerator.Tests.Types
4+
{
5+
public class GenericInheritingType<TType>
6+
where TType : Enum
7+
{
8+
[NotNull]
9+
public TType Type { get; set; }
10+
}
11+
}

TypeScript.ContractGenerator/TypeScriptGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeIn
102102
if (NullableTypeBuildingContext.Accept(typeInfo))
103103
return new NullableTypeBuildingContext(typeInfo);
104104

105+
if (typeInfo.IsGenericParameter)
106+
return new GenericParameterTypeBuildingContext(typeInfo);
107+
105108
if (typeInfo.IsEnum)
106109
return new TypeScriptEnumTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo);
107110

108111
if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
109112
return new GenericTypeTypeBuildingContext(typeInfo);
110113

111-
if (typeInfo.IsGenericParameter)
112-
return new GenericParameterTypeBuildingContext(typeInfo);
113-
114114
if (typeInfo.IsGenericTypeDefinition)
115115
return new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo);
116116

0 commit comments

Comments
 (0)