2019-08-29 01:36:47 -04:00
|
|
|
using capnpc_csharp.Tests.Properties;
|
|
|
|
using Capnp;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2019-08-29 12:01:08 -04:00
|
|
|
using System;
|
2019-08-29 01:36:47 -04:00
|
|
|
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>();
|
2019-08-29 01:36:47 -04:00
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void Test00Enumerant()
|
|
|
|
{
|
|
|
|
var model = Load(Resources.UnitTest1_capnp);
|
|
|
|
Assert.AreEqual("@byte", GetTypeDef(0xc8461867c409f5d4, model).Enumerants[0].Literal);
|
|
|
|
}
|
|
|
|
|
2019-08-29 10:28:53 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void Test01NestedClash()
|
|
|
|
{
|
2019-08-30 13:54:47 -04:00
|
|
|
var run = LoadAndGenerate(Resources.UnitTest1_capnp, 1);
|
|
|
|
var structFoo = GetTypeDef(0x93db6ba5509bac24, run.Model);
|
|
|
|
var names = run.CodeGen.GetNames();
|
2019-08-29 10:28:53 -04:00
|
|
|
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-08-30 13:54:47 -04:00
|
|
|
LoadAndGenerate(Resources.UnitTest2_capnp, 2);
|
2019-08-29 12:38:13 -04:00
|
|
|
// Should not throw
|
|
|
|
}
|
|
|
|
|
2019-08-29 10:29:21 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void Test03NonGeneratedNodeSkip()
|
|
|
|
{
|
2019-08-30 13:54:47 -04:00
|
|
|
LoadAndGenerate(Resources.UnitTest3_capnp, 3);
|
2019-08-29 10:29:21 -04:00
|
|
|
// Should not throw
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:01:08 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void Test10ImportedNamespaces()
|
|
|
|
{
|
2019-08-30 13:54:47 -04:00
|
|
|
var run = LoadAndGenerate(Resources.UnitTest10_capnp, 10);
|
|
|
|
var outerTypeDef = run.FirstFile.NestedTypes.First();
|
2019-08-29 12:01:08 -04:00
|
|
|
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();
|
2019-08-29 12:01:08 -04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-08-30 11:23:44 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void Test11ImportedConst()
|
|
|
|
{
|
2019-08-30 13:54:47 -04:00
|
|
|
LoadAndGenerate(Resources.UnitTest11_capnp, 11);
|
2019-08-30 11:23:44 -04:00
|
|
|
// Should not throw
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:57:37 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void Test20AnnotationAndConst()
|
|
|
|
{
|
2019-08-30 13:54:47 -04:00
|
|
|
LoadAndGenerate(Resources.UnitTest20_capnp, 20);
|
2019-08-29 11:57:37 -04:00
|
|
|
// Should not throw
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:02:32 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void Test30SchemaCapnp()
|
|
|
|
{
|
2019-08-30 13:54:47 -04:00
|
|
|
LoadAndGenerate(Resources.schema_with_offsets_capnp);
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-29 10:28:53 -04:00
|
|
|
static Generator.CodeGenerator NewGeneratorFor(Model.SchemaModel model)
|
|
|
|
=> new Generator.CodeGenerator(model, new Generator.GeneratorOptions());
|
|
|
|
|
2019-08-30 13:54:47 -04:00
|
|
|
Run LoadAndGenerate(byte[] input, int? testNum = null)
|
|
|
|
{
|
|
|
|
var run = new Run();
|
|
|
|
run.Model = Load(input);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:36:47 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Model.SchemaModel Load(byte[] data)
|
|
|
|
{
|
|
|
|
WireFrame segments;
|
|
|
|
var input = new MemoryStream(data);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|