mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-12 23:01:44 +01:00
Include all generated files in unit test binary schemas
This commit is contained in:
parent
2ac731eec6
commit
cb69c52cc7
@ -22,9 +22,9 @@ namespace CapnpC.CSharp.Generator.Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Test01NestedClash()
|
public void Test01NestedClash()
|
||||||
{
|
{
|
||||||
var run = LoadAndGenerate("UnitTest1.capnp.bin");
|
var (model, codegen) = LoadAndGenerate("UnitTest1.capnp.bin");
|
||||||
var structFoo = GetTypeDef(0x93db6ba5509bac24, run.Model);
|
var structFoo = GetTypeDef(0x93db6ba5509bac24, model);
|
||||||
var names = run.CodeGen.GetNames();
|
var names = codegen.GetNames();
|
||||||
var fieldName = names.GetCodeIdentifier(structFoo.Fields[0]).ToString();
|
var fieldName = names.GetCodeIdentifier(structFoo.Fields[0]).ToString();
|
||||||
Assert.AreEqual("Foo", structFoo.Name);
|
Assert.AreEqual("Foo", structFoo.Name);
|
||||||
Assert.AreNotEqual(structFoo.Name, fieldName);
|
Assert.AreNotEqual(structFoo.Name, fieldName);
|
||||||
@ -51,12 +51,12 @@ namespace CapnpC.CSharp.Generator.Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Test10ImportedNamespaces()
|
public void Test10ImportedNamespaces()
|
||||||
{
|
{
|
||||||
var run = LoadAndGenerate("UnitTest10.capnp.bin");
|
var (model, codegen) = LoadAndGenerate("UnitTest10.capnp.bin");
|
||||||
var outerTypeDef = run.FirstFile.NestedTypes.First();
|
var outerTypeDef = GetGeneratedFile("UnitTest10.capnp", model).NestedTypes.First();
|
||||||
var outerType = Model.Types.FromDefinition(outerTypeDef);
|
var outerType = Model.Types.FromDefinition(outerTypeDef);
|
||||||
var innerType = outerTypeDef.Fields[0].Type;
|
var innerType = outerTypeDef.Fields[0].Type;
|
||||||
var innerTypeDef = innerType.Definition;
|
var innerTypeDef = innerType.Definition;
|
||||||
var names = run.CodeGen.GetNames();
|
var names = codegen.GetNames();
|
||||||
var outerNameSyntax = names.GetQName(outerType, outerTypeDef);
|
var outerNameSyntax = names.GetQName(outerType, outerTypeDef);
|
||||||
var innerNameSyntax = names.GetQName(innerType, outerTypeDef);
|
var innerNameSyntax = names.GetQName(innerType, outerTypeDef);
|
||||||
string[] outerNamespace = { "Foo", "Bar", "Baz" };
|
string[] outerNamespace = { "Foo", "Bar", "Baz" };
|
||||||
@ -93,26 +93,22 @@ namespace CapnpC.CSharp.Generator.Tests
|
|||||||
LoadAndGenerate("schema-with-offsets.capnp.bin");
|
LoadAndGenerate("schema-with-offsets.capnp.bin");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Run
|
static (Model.SchemaModel, CodeGen.CodeGenerator) LoadAndGenerate(string inputName)
|
||||||
{
|
{
|
||||||
public Model.SchemaModel Model;
|
var model = Load(inputName);
|
||||||
public CodeGen.CodeGenerator CodeGen;
|
var codegen = new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions());
|
||||||
public Model.GenFile FirstFile;
|
|
||||||
public string Code;
|
var code = model.FilesToGenerate.Select(f => codegen.Transform(f)).ToArray();
|
||||||
|
Assert.IsTrue(Util.InlineAssemblyCompiler.TryCompileCapnp(code), "Compilation was not successful");
|
||||||
|
|
||||||
|
return (model, codegen);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CodeGen.CodeGenerator NewGeneratorFor(Model.SchemaModel model)
|
static Model.GenFile GetGeneratedFile(string name, Model.SchemaModel model)
|
||||||
=> new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions());
|
|
||||||
|
|
||||||
Run LoadAndGenerate(string inputName)
|
|
||||||
{
|
{
|
||||||
var run = new Run();
|
var file = model.FilesToGenerate.SingleOrDefault(f => f.Name.EndsWith(name));
|
||||||
run.Model = Load(inputName);
|
Assert.IsNotNull(file, $"Could not find '{name}' in generated files");
|
||||||
run.CodeGen = NewGeneratorFor(run.Model);
|
return file;
|
||||||
run.FirstFile = run.Model.FilesToGenerate.First();
|
|
||||||
run.Code = run.CodeGen.Transform(run.FirstFile);
|
|
||||||
Assert.IsTrue(Util.InlineAssemblyCompiler.TryCompileCapnp(run.Code), "Compilation was not successful");
|
|
||||||
return run;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Model.TypeDefinition GetTypeDef(ulong id, Model.SchemaModel model)
|
static Model.TypeDefinition GetTypeDef(ulong id, Model.SchemaModel model)
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -11,21 +11,22 @@ namespace CapnpC.CSharp.Generator.Tests.Util
|
|||||||
class InlineAssemblyCompiler
|
class InlineAssemblyCompiler
|
||||||
{
|
{
|
||||||
public static bool TryCompileCapnp(string code)
|
public static bool TryCompileCapnp(string code)
|
||||||
|
{
|
||||||
|
return TryCompileCapnp(new[] {code});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool TryCompileCapnp(string[] code)
|
||||||
{
|
{
|
||||||
var options = new CSharpCompilationOptions(
|
var options = new CSharpCompilationOptions(
|
||||||
OutputKind.DynamicallyLinkedLibrary,
|
OutputKind.DynamicallyLinkedLibrary,
|
||||||
optimizationLevel: OptimizationLevel.Debug);
|
optimizationLevel: OptimizationLevel.Debug);
|
||||||
|
|
||||||
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
|
|
||||||
|
|
||||||
string assemblyRoot = Path.GetDirectoryName(typeof(object).Assembly.Location);
|
string assemblyRoot = Path.GetDirectoryName(typeof(object).Assembly.Location);
|
||||||
|
|
||||||
string capnpRuntimePath = Path.GetFullPath(Path.Combine(
|
string capnpRuntimePath = Path.GetFullPath(Path.Combine(
|
||||||
Assembly.GetExecutingAssembly().Location,
|
Assembly.GetExecutingAssembly().Location,
|
||||||
@"..\..\..\..\..\Capnp.Net.Runtime\bin\Debug\netcoreapp2.1\Capnp.Net.Runtime.dll"));
|
@"..\..\..\..\..\Capnp.Net.Runtime\bin\Debug\netcoreapp2.1\Capnp.Net.Runtime.dll"));
|
||||||
|
|
||||||
var capnpRuntimeMetadataRef = MetadataReference.CreateFromFile(capnpRuntimePath);
|
|
||||||
|
|
||||||
var compilation = CSharpCompilation.Create(
|
var compilation = CSharpCompilation.Create(
|
||||||
"CompilationTestAssembly",
|
"CompilationTestAssembly",
|
||||||
options: options,
|
options: options,
|
||||||
@ -35,8 +36,8 @@ namespace CapnpC.CSharp.Generator.Tests.Util
|
|||||||
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Core.dll")),
|
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Core.dll")),
|
||||||
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Runtime.dll")),
|
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Runtime.dll")),
|
||||||
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Private.CoreLib.dll")),
|
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Private.CoreLib.dll")),
|
||||||
capnpRuntimeMetadataRef },
|
MetadataReference.CreateFromFile(capnpRuntimePath) },
|
||||||
syntaxTrees: new SyntaxTree[] { syntaxTree });
|
syntaxTrees: Array.ConvertAll(code, new Converter<string, SyntaxTree>(c => CSharpSyntaxTree.ParseText(c))));
|
||||||
|
|
||||||
using (var stream = new MemoryStream())
|
using (var stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
@ -46,11 +47,14 @@ namespace CapnpC.CSharp.Generator.Tests.Util
|
|||||||
Console.WriteLine($"{diag}");
|
Console.WriteLine($"{diag}");
|
||||||
|
|
||||||
if (!emitResult.Success)
|
if (!emitResult.Success)
|
||||||
|
{
|
||||||
|
foreach (var c in code)
|
||||||
{
|
{
|
||||||
string path = Path.ChangeExtension(Path.GetTempFileName(), ".capnp.cs");
|
string path = Path.ChangeExtension(Path.GetTempFileName(), ".capnp.cs");
|
||||||
File.WriteAllText(path, code);
|
File.WriteAllText(path, c);
|
||||||
Console.WriteLine($"[See {path} for generated code]");
|
Console.WriteLine($"[See {path} for generated code]");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return emitResult.Success;
|
return emitResult.Success;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user