Skip to content

Commit

Permalink
correctly generate type with generic enum constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
fakefeik committed Mar 1, 2024
1 parent 9ba8da4 commit eaa21eb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
76 changes: 55 additions & 21 deletions TypeScript.ContractGenerator.Tests/CliTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

using FluentAssertions;

Expand All @@ -13,28 +16,24 @@ public class CliTest
[Test]
public void CliGenerated()
{
RunDotnetCommand($"{pathToSlnDirectory}/TypeScript.ContractGenerator.Cli/bin/{configuration}/{targetFramework}/SkbKontur.TypeScript.ContractGenerator.Cli.dll " +
$"-a {pathToSlnDirectory}/AspNetCoreExample.Api/bin/{configuration}/{targetFramework}/AspNetCoreExample.Api.dll " +
$"-o {TestContext.CurrentContext.TestDirectory}/cliOutput " +
"--nullabilityMode NullableReference " +
"--lintMode TsLint");

var expectedDirectory = $"{pathToSlnDirectory}/AspNetCoreExample.Generator/output";
var actualDirectory = $"{TestContext.CurrentContext.TestDirectory}/cliOutput";
TestBase.CheckDirectoriesEquivalenceInner(expectedDirectory, actualDirectory, generatedOnly : true);
var output = Path.Combine(TestContext.CurrentContext.TestDirectory, "cliOutput");

RunDotnetCommand($"{Tool()} -a {Assembly()} -o {output} --nullabilityMode NullableReference --lintMode TsLint");

var expectedDirectory = $"{repoRoot}/AspNetCoreExample.Generator/output";
TestBase.CheckDirectoriesEquivalenceInner(expectedDirectory, output, generatedOnly : true);
}

[Test]
public void RoslynCliGenerated()
{
RunDotnetCommand($"{pathToSlnDirectory}/TypeScript.ContractGenerator.Cli/bin/{configuration}/{targetFramework}/SkbKontur.TypeScript.ContractGenerator.Cli.dll " +
$"-d {pathToSlnDirectory}/AspNetCoreExample.Api " +
$"-a {typeof(ControllerBase).Assembly.Location} " +
$"-o {TestContext.CurrentContext.TestDirectory}/roslynCliOutput " +
"--nullabilityMode NullableReference " +
"--lintMode TsLint");

var expectedDirectory = $"{pathToSlnDirectory}/AspNetCoreExample.Generator/output";
var project = Path.Combine(repoRoot, "AspNetCoreExample.Api");
var assembly = typeof(ControllerBase).Assembly.Location;
var output = Path.Combine(TestContext.CurrentContext.TestDirectory, "roslynCliOutput");

RunDotnetCommand($"{Tool()} -d {project} -a {assembly} -o {output} --nullabilityMode NullableReference --lintMode TsLint");

var expectedDirectory = $"{repoRoot}/AspNetCoreExample.Generator/output";
var actualDirectory = $"{TestContext.CurrentContext.TestDirectory}/roslynCliOutput";
TestBase.CheckDirectoriesEquivalenceInner(expectedDirectory, actualDirectory, generatedOnly : true);
}
Expand All @@ -55,12 +54,47 @@ private static void RunDotnetCommand(string command)
process.Start();
process.WaitForExit();

process.ExitCode.Should().Be(0);
process.StandardOutput.ReadToEnd().Trim().Should().Be("Generating TypeScript");
process.StandardError.ReadToEnd().Trim().Should().BeEmpty();
process.StandardOutput.ReadToEnd().Trim().Should().Be("Generating TypeScript");
process.ExitCode.Should().Be(0);
}

private static string Tool()
{
return Path.Combine(
repoRoot,
"TypeScript.ContractGenerator.Cli",
"bin",
configuration,
targetFramework,
"SkbKontur.TypeScript.ContractGenerator.Cli.dll"
);
}

private static string Assembly()
{
return Path.Combine(
repoRoot,
"AspNetCoreExample.Api",
"bin",
configuration,
targetFramework,
"AspNetCoreExample.Api.dll"
);
}

private static string RootDirectory(DirectoryInfo? current = null)
{
current ??= new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
while (current != null && current.EnumerateFiles().All(x => x.Name != "TypeScript.ContractGenerator.sln"))
{
current = current.Parent;
}

return current?.FullName ?? throw new InvalidOperationException("Cannot find root folder");
}

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

private const string targetFramework = "net8.0";

Expand Down
1 change: 1 addition & 0 deletions TypeScript.ContractGenerator.Tests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class EndToEndTests<TTypesProvider> : TestBase
[TestCase(typeof(ComplexRootType), "complex-types")]
[TestCase(typeof(GenericRootType<>), "generic-root")]
[TestCase(typeof(GenericContainingRootType), "generic-types")]
[TestCase(typeof(GenericInheritingType<>), "generic-inheriting-types")]
[TestCase(typeof(ArrayRootType), "array-types")]
[TestCase(typeof(NotNullRootType), "notnull-types")]
[TestCase(typeof(NonDefaultConstructorRootType), "non-default-constructor")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export type GenericInheritingType<TType> = {
type: TType;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net48;net8.0</TargetFrameworks>
<AssemblyName>SkbKontur.TypeScript.ContractGenerator.Tests</AssemblyName>
<RootNamespace>SkbKontur.TypeScript.ContractGenerator.Tests</RootNamespace>
</PropertyGroup>
Expand Down
11 changes: 11 additions & 0 deletions TypeScript.ContractGenerator.Tests/Types/GenericInheritingType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SkbKontur.TypeScript.ContractGenerator.Tests.Types
{
public class GenericInheritingType<TType>
where TType : Enum
{
[NotNull]
public TType Type { get; set; }
}
}
6 changes: 3 additions & 3 deletions TypeScript.ContractGenerator/TypeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeIn
if (NullableTypeBuildingContext.Accept(typeInfo))
return new NullableTypeBuildingContext(typeInfo);

if (typeInfo.IsGenericParameter)
return new GenericParameterTypeBuildingContext(typeInfo);

if (typeInfo.IsEnum)
return new TypeScriptEnumTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo);

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

if (typeInfo.IsGenericParameter)
return new GenericParameterTypeBuildingContext(typeInfo);

if (typeInfo.IsGenericTypeDefinition)
return new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo);

Expand Down

0 comments on commit eaa21eb

Please sign in to comment.