155 lines
5.2 KiB
C#
Raw Normal View History

2019-09-03 12:08:39 -04:00
using capnpc_csharp.Tests;
using Capnp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace CapnpC
{
[TestClass]
public class UnitTests
{
2019-08-30 13:54:47 -04:00
static readonly Dictionary<int, string> GeneratedCode = new Dictionary<int, string>();
[TestMethod]
public void Test00Enumerant()
{
2019-09-03 12:08:39 -04:00
var model = Load("UnitTest1.capnp.bin");
Assert.AreEqual("@byte", GetTypeDef(0xc8461867c409f5d4, model).Enumerants[0].Literal);
}
[TestMethod]
public void Test01NestedClash()
{
2019-09-03 12:08:39 -04:00
var run = LoadAndGenerate("UnitTest1.capnp.bin", 1);
2019-08-30 13:54:47 -04:00
var structFoo = GetTypeDef(0x93db6ba5509bac24, run.Model);
var names = run.CodeGen.GetNames();
var fieldName = names.GetCodeIdentifier(structFoo.Fields[0]).ToString();
Assert.AreEqual("Foo", structFoo.Name);
Assert.AreNotEqual(structFoo.Name, fieldName);
}
2019-08-29 12:38:13 -04:00
[TestMethod]
public void Test02ForwardInheritance()
{
2019-09-03 12:08:39 -04:00
LoadAndGenerate("UnitTest2.capnp.bin", 2);
2019-08-29 12:38:13 -04:00
// Should not throw
}
[TestMethod]
public void Test03NonGeneratedNodeSkip()
{
2019-09-03 12:08:39 -04:00
LoadAndGenerate("UnitTest3.capnp.bin", 3);
// Should not throw
}
[TestMethod]
public void Test04MutualDependencies()
{
2019-09-03 12:08:39 -04:00
LoadAndGenerate("UnitTest4.capnp.bin", 4);
// Should not throw
}
[TestMethod]
public void Test10ImportedNamespaces()
{
2019-09-03 12:08:39 -04:00
var run = LoadAndGenerate("UnitTest10.capnp.bin", 10);
2019-08-30 13:54:47 -04:00
var outerTypeDef = run.FirstFile.NestedTypes.First();
var outerType = Model.Types.FromDefinition(outerTypeDef);
var innerType = outerTypeDef.Fields[0].Type;
var innerTypeDef = innerType.Definition;
2019-08-30 13:54:47 -04:00
var names = run.CodeGen.GetNames();
var outerNameSyntax = names.GetQName(outerType, outerTypeDef);
var innerNameSyntax = names.GetQName(innerType, outerTypeDef);
string[] outerNamespace = { "Foo", "Bar", "Baz" };
string[] innerNamespace = { "Foo", "Garf", "Snarf" };
CollectionAssert.AreEqual(outerNamespace, (outerTypeDef.DeclaringElement as Model.GenFile).Namespace);
CollectionAssert.AreEqual(innerNamespace, (innerType.Definition.DeclaringElement as Model.GenFile).Namespace);
string outerNSStr = String.Join('.', outerNamespace);
string innerNSStr = String.Join('.', innerNamespace);
Assert.AreEqual($"{outerNSStr}.Outer", outerNameSyntax.ToString());
Assert.AreEqual($"{innerNSStr}.Inner", innerNameSyntax.ToString());
}
[TestMethod]
public void Test11ImportedConst()
{
2019-09-03 12:08:39 -04:00
LoadAndGenerate("UnitTest11.capnp.bin", 11);
// Should not throw
}
[TestMethod]
public void Test20AnnotationAndConst()
{
2019-09-03 12:08:39 -04:00
LoadAndGenerate("UnitTest20.capnp.bin", 20);
// Should not throw
}
2019-08-29 13:02:32 -04:00
[TestMethod]
public void Test30SchemaCapnp()
{
2019-09-03 12:08:39 -04:00
LoadAndGenerate("schema-with-offsets.capnp.bin");
2019-08-29 13:02:32 -04:00
// Should not throw
}
2019-08-30 13:54:47 -04:00
struct Run
{
public Model.SchemaModel Model;
public Generator.CodeGenerator CodeGen;
public Model.GenFile FirstFile;
public string Code;
}
static Generator.CodeGenerator NewGeneratorFor(Model.SchemaModel model)
=> new Generator.CodeGenerator(model, new Generator.GeneratorOptions());
2019-09-03 12:08:39 -04:00
Run LoadAndGenerate(string inputName, int? testNum = null)
2019-08-30 13:54:47 -04:00
{
var run = new Run();
2019-09-03 12:08:39 -04:00
run.Model = Load(inputName);
2019-08-30 13:54:47 -04:00
run.CodeGen = NewGeneratorFor(run.Model);
run.FirstFile = run.Model.FilesToGenerate.First();
run.Code = run.CodeGen.Transform(run.FirstFile);
if (testNum is int num)
GeneratedCode[num] = run.Code;
return run;
}
static Model.TypeDefinition GetTypeDef(ulong id, Model.SchemaModel model)
{
foreach (var defs in model.FilesToGenerate.Select(f => f.NestedTypes))
{
if (GetTypeDef(id, defs) is Model.TypeDefinition def) return def;
}
return null;
}
static Model.TypeDefinition GetTypeDef(ulong id, IEnumerable<Model.TypeDefinition> defs)
{
foreach (var def in defs)
{
if (def.Id == id) return def;
var sub = GetTypeDef(id, def.NestedTypes);
if (sub != null) return sub;
}
return null;
}
2019-09-03 12:08:39 -04:00
static Model.SchemaModel Load(string inputName)
{
WireFrame segments;
2019-09-03 12:08:39 -04:00
var input = CodeGeneratorSteps.LoadResource(inputName);
using (input)
{
segments = Framing.ReadSegments(input);
}
var dec = DeserializerState.CreateRoot(segments);
var reader = Schema.CodeGeneratorRequest.Reader.Create(dec);
var model = Model.SchemaModel.Create(reader);
return model;
}
}
}