public void CircularReferenceInclude() { string baseFbs = "include \"A.fbs\"; namespace Foo; table BaseTable (PrecompiledSerializer) { OtherTable:OtherTable; }"; var includes = new Dictionary <string, string> { { "A.fbs", "include \"root.fbs\"; namespace Foo; table OtherTable (PrecompiledSerializer) { Foo:BaseTable; }" }, }; string cSharp = FlatSharpCompiler.TestHookCreateCSharp(baseFbs, includes); Assert.IsTrue(!string.IsNullOrEmpty(cSharp)); Assert.IsTrue(cSharp.Contains("public static ISerializer<Foo.BaseTable> Serializer")); Assert.IsFalse(cSharp.Contains("public static ISerializer<Foo.OtherTable> Serializer")); BaseSchemaMember member = FlatSharpCompiler.TestHookParseSyntax(baseFbs, includes); Assert.IsTrue(member is RootNodeDefinition); Assert.AreEqual(1, member.Children.Count); NamespaceDefinition nsDef = (NamespaceDefinition)member.Children.Single().Value; Assert.AreEqual("Foo", nsDef.Name); Assert.AreEqual(2, nsDef.Children.Count); var basetable = nsDef.Children["BaseTable"]; var otherTable = nsDef.Children["OtherTable"]; Assert.IsNotNull(basetable); Assert.IsNotNull(otherTable); Assert.AreEqual("root.fbs", Path.GetFileName(basetable.DeclaringFile)); Assert.AreEqual("A.fbs", Path.GetFileName(otherTable.DeclaringFile)); }
public void SimpleInclude() { string baseFbs = "include \"OtherTable.fbs\"; namespace Foo; table BaseTable { OtherTable:OtherTable; Int:int; }"; var includes = new Dictionary <string, string> { { "OtherTable.fbs", "namespace Foo; table OtherTable { Foo:string; }" } }; string cSharp = FlatSharpCompiler.TestHookCreateCSharp(baseFbs, includes); Assert.IsTrue(!string.IsNullOrEmpty(cSharp)); BaseSchemaMember member = FlatSharpCompiler.TestHookParseSyntax(baseFbs, includes); Assert.IsTrue(member is RootNodeDefinition); Assert.AreEqual(1, member.Children.Count); NamespaceDefinition nsDef = (NamespaceDefinition)member.Children.Single().Value; Assert.AreEqual("Foo", nsDef.Name); Assert.AreEqual(2, nsDef.Children.Count); var basetable = nsDef.Children["BaseTable"]; var otherTable = nsDef.Children["OtherTable"]; Assert.IsNotNull(basetable); Assert.IsNotNull(otherTable); Assert.AreEqual("root.fbs", Path.GetFileName(basetable.DeclaringFile)); Assert.AreEqual("OtherTable.fbs", Path.GetFileName(otherTable.DeclaringFile)); }
public void DuplicateTypesDifferentFiles() { string baseFbs = "include \"A.fbs\"; namespace Foo; table BaseTable (PrecompiledSerializer) { BaseTable:BaseTable; }"; var includes = new Dictionary <string, string> { { "A.fbs", "namespace Foo; table BaseTable { BaseTable:BaseTable; }" }, }; var ex = Assert.ThrowsException <InvalidFbsFileException>(() => FlatSharpCompiler.TestHookCreateCSharp(baseFbs, includes)); Assert.IsTrue(ex.Message.Contains("Duplicate member name")); }
public void NoPrecompiledSerializer() { string schema = $@" namespace NoPrecompiledSerializer; rpc_service RouteGuide {{ GetFeature(Point):Point; }} table Point {{ latitude:int32; longitude:int32; }}"; Assert.ThrowsException <InvalidFbsFileException>(() => FlatSharpCompiler.TestHookCreateCSharp(schema, new())); }
public void RpcReturnsAStruct() { string schema = $@" namespace RpcReturnsAStruct; rpc_service RouteGuide {{ GetFeature(Point):Point; }} struct Point ({MetadataKeys.SerializerKind}:lazy) {{ latitude:int32; longitude:int32; }}"; Assert.ThrowsException <InvalidFbsFileException>(() => FlatSharpCompiler.TestHookCreateCSharp(schema, new())); }
public void RpcUnknownStreamingType() { string schema = $@" namespace RpcUnknownStreamingType; rpc_service RouteGuide {{ GetFeature(Point):Point (streaming:banana); }} table Point ({MetadataKeys.SerializerKind}:lazy) {{ latitude:int32; longitude:int32; }}"; Assert.ThrowsException <InvalidFbsFileException>(() => FlatSharpCompiler.TestHookCreateCSharp(schema, new())); }
public void RpcReturnsAStruct() { string schema = @" namespace RpcReturnsAStruct; rpc_service RouteGuide { GetFeature(Point):Point; } struct Point (PrecompiledSerializer:lazy) { latitude:int32; longitude:int32; }"; Assert.ThrowsException <InvalidFbsFileException>(() => FlatSharpCompiler.TestHookCreateCSharp(schema)); }
public void RpcUnknownStreamingType() { string schema = @" namespace RpcUnknownStreamingType; rpc_service RouteGuide { GetFeature(Point):Point (streaming:banana); } table Point (PrecompiledSerializer:lazy) { latitude:int32; longitude:int32; }"; Assert.ThrowsException <InvalidFbsFileException>(() => FlatSharpCompiler.TestHookCreateCSharp(schema)); }
public void RouteGuideTest() { string schema = @" namespace routeguide; rpc_service RouteGuide { GetFeature(Point):Feature; ListFeatures(Rectangle):Feature (streaming:server); RecordRoute(Point):RouteSummary (streaming:client); RouteChat(RouteNote):RouteNote (streaming:duplex); } table Point (PrecompiledSerializer:lazy) { latitude:int32; longitude:int32; } table Rectangle (PrecompiledSerializer:lazy) { lo:Point; hi:Point; } table Feature (PrecompiledSerializer:lazy) { // The name of the feature. name:string; // The point where the feature is detected. location:Point; } table RouteNote (PrecompiledSerializer:lazy) { location:Point; message:string; } table RouteSummary (PrecompiledSerializer:greedymutable) { point_count:int; feature_count:int; distance:int; elapsed_time:int; }"; string cSharp = FlatSharpCompiler.TestHookCreateCSharp(schema); Assembly compiled = FlatSharpCompiler.CompileAndLoadAssembly( schema, additionalReferences: new[] { typeof(Grpc.Core.AsyncClientStreamingCall <,>).Assembly }); var rpcType = compiled.GetType("routeguide.RouteGuide"); Assert.IsNotNull(rpcType); var serverBaseClass = rpcType.GetNestedType("RouteGuideServerBase"); Assert.IsTrue(serverBaseClass.IsAbstract); var attribute = serverBaseClass.GetCustomAttribute <Grpc.Core.BindServiceMethodAttribute>(); Assert.IsNotNull(attribute); var bindServiceMethod = rpcType.GetMethod("BindService", new [] { serverBaseClass }); Assert.IsNotNull(bindServiceMethod); Assert.AreEqual(bindServiceMethod.Name, attribute.BindMethodName); Assert.AreEqual(rpcType, attribute.BindType); Assert.IsTrue(bindServiceMethod.IsPublic); Assert.IsTrue(bindServiceMethod.IsStatic); var bindServiceOverload = rpcType.GetMethod("BindService", new[] { typeof(Grpc.Core.ServiceBinderBase), serverBaseClass }); Assert.IsNotNull(bindServiceOverload); Assert.AreEqual(bindServiceOverload.Name, attribute.BindMethodName); Assert.AreEqual(rpcType, attribute.BindType); Assert.IsTrue(bindServiceOverload.IsPublic); Assert.IsTrue(bindServiceOverload.IsStatic); Assert.AreEqual(bindServiceOverload.ReturnType, typeof(void)); var clientClass = rpcType.GetNestedType("RouteGuideClient"); Assert.IsFalse(clientClass.IsAbstract); Assert.AreEqual(clientClass.BaseType.GetGenericTypeDefinition(), typeof(Grpc.Core.ClientBase <>)); }