Skip to content

Instantly share code, notes, and snippets.

@nros
Last active November 9, 2020 16:54
Show Gist options
  • Save nros/558ba0d0afb638dea20d5821bb411f98 to your computer and use it in GitHub Desktop.
Save nros/558ba0d0afb638dea20d5821bb411f98 to your computer and use it in GitHub Desktop.
Unit test for issue 145 of project Reinforced.Typings - https://github.com/reinforced/Reinforced.Typings/issues/145
#nullable enable
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Namotion.Reflection;
using Reinforced.Typings;
using Reinforced.Typings.Fluent;
using Xunit;
using Xunit.Abstractions;
namespace TypeScriptExporter.Test.Exporter
{
public class TestTypeScriptExporterPlainLibrary
{
private readonly ITestOutputHelper _testOutputHelper;
public TestTypeScriptExporterPlainLibrary(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
private interface INullableProperty
{
public string? Name { get; set; }
public string NameNotNullable { get; set; }
}
[Fact]
public async Task TestNullablePropertyIsExportedAsNullableAsync()
{
const string expectedExport = "export interface INullableProperty\n" +
"{\n" +
"\tName?: string;\n" +
"\tNameNotNullable: string;\n" +
"}\n";
ExportContext ec = new ExportContext(Array.Empty<Assembly>())
{
ConfigurationMethod = configBuilder =>
{
configBuilder.Global(config =>
config
.UseModules()
.DontWriteWarningComment()
);
configBuilder.ExportAsInterfaces(
new []{typeof(INullableProperty)},
config => config.WithPublicProperties()
);
},
Hierarchical = false,
};
var typeScriptCode = await PerformExportAsync(ec);
_testOutputHelper.WriteLine(typeScriptCode);
Assert.Equal(expectedExport, typeScriptCode);
}
[Fact]
public async Task TestNullablePropertyIsExportedAsNullableWithExternalLibraryAsync()
{
const string expectedExport = "export interface INullableProperty\n" +
"{\n" +
"\tName?: string;\n" +
"\tNameNotNullable: string;\n" +
"}\n";
ExportContext ec = new ExportContext(Array.Empty<Assembly>())
{
ConfigurationMethod = configBuilder =>
{
configBuilder.Global(config =>
config
.UseModules()
.DontWriteWarningComment()
);
configBuilder.ExportAsInterfaces(
new []{typeof(INullableProperty)},
config => config.WithPublicProperties(propBuilder =>
{
ContextualPropertyInfo contextualPropertyInfo = propBuilder.Member.ToContextualProperty();
var isNullable = propBuilder.Member.PropertyType.IsNullable()
|| contextualPropertyInfo.Nullability == Nullability.Nullable;
propBuilder.ForceNullable(isNullable);
})
);
},
Hierarchical = false,
};
var typeScriptCode = await PerformExportAsync(ec);
_testOutputHelper.WriteLine(typeScriptCode);
Assert.Equal(expectedExport, typeScriptCode);
}
private async Task<string> PerformExportAsync(ExportContext ec)
{
ec.TargetFile = Path.GetTempFileName();
TsExporter te = new TsExporter(ec);
te.Export();
await using FileStream fs = new FileStream(
ec.TargetFile,
FileMode.Open, FileAccess.ReadWrite, FileShare.None,
4096,
FileOptions.Asynchronous | FileOptions.SequentialScan | FileOptions.DeleteOnClose
);
fs.ConfigureAwait(false);
using var reader = new StreamReader(fs);
return (await reader.ReadToEndAsync().ConfigureAwait(false));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment