public async Task GenerateStructAsync_GeneratesStruct_WithJsonConverter()
        {
            // Arrange
            var context = new EntityIdGenerationContext(
                SyntaxFactory.StructDeclaration("EntityId"),
                typeConverter: false,
                jsonNetConverter: true);

            // Act
            var @struct = await Int32EntityIdSyntaxGenerator.GenerateStructAsync(context);

            // Assert
            Assert.NotNull(@struct);
            Assert.Equal("EntityId", @struct.Identifier.ValueText);

            AssertHasModifier(@struct, SyntaxKind.ReadOnlyKeyword);
            AssertHasAttribute(@struct, "System.Diagnostics.DebuggerDisplay",
                               attribute =>
            {
                Assert.Equal("(\"{DebuggerToString(),nq}\")", attribute.ArgumentList.ToString());
            }
                               );
            AssertDoesNotHaveAttribute(@struct, "System.ComponentModel.TypeConverter");
            AssertHasAttribute(@struct, "Newtonsoft.Json.JsonConverter",
                               attribute =>
            {
                Assert.Equal("(typeof(EntityIdJsonConverter))", attribute.ArgumentList.ToString());
            }
                               );
        }
        public async Task GenerateStructAsync_GeneratesStruct_WithEmptyField()
        {
            // Arrange
            var context = new EntityIdGenerationContext(
                SyntaxFactory.StructDeclaration("EntityId"),
                typeConverter: false,
                jsonNetConverter: false);

            // Act
            var @struct = await Int32EntityIdSyntaxGenerator.GenerateStructAsync(context);

            // Assert
            AssertHasField(
                @struct,
                name: "Empty",
                type: "EntityId",
                (member, variable) =>
            {
                AssertHasModifier(member, SyntaxKind.PublicKeyword);
                AssertHasModifier(member, SyntaxKind.StaticKeyword);
                AssertHasModifier(member, SyntaxKind.ReadOnlyKeyword);

                Assert.NotNull(variable.Initializer);
                Assert.Equal("= new EntityId()", variable.Initializer.ToString());
            });
        }
示例#3
0
        private async Task <MemberDeclarationSyntax> GetSyntaxAsync(StructDeclarationSyntax @struct)
        {
            var context = new EntityIdGenerationContext(
                @struct,
                _backingType,
                typeConverter: _generateTypeConverter,
                jsonNetConverter: _generateJsonNetConverter);

            EntityIdSyntaxGeneratorBase generator = null;

            if (_backingType == typeof(int))
            {
                generator = new Int32EntityIdSyntaxGenerator();
            }

            if (generator is object)
            {
                return(await generator.GenerateSyntaxAsync(context));
            }

            throw new InvalidOperationException($"The backing type {_backingType} is not supported for entity ID generation.");
        }
        public async Task GenerateStructAsync_GeneratesStruct_WithValueProperty()
        {
            // Arrange
            var context = new EntityIdGenerationContext(
                SyntaxFactory.StructDeclaration("EntityId"),
                typeConverter: false,
                jsonNetConverter: false);

            // Act
            var @struct = await Int32EntityIdSyntaxGenerator.GenerateStructAsync(context);

            // Assert
            AssertHasProperty(
                @struct,
                name: "Value",
                type: "int",
                (member) =>
            {
                AssertHasModifier(member, SyntaxKind.PublicKeyword);

                AssertHasAccessor(member, SyntaxKind.GetAccessorDeclaration);
                AssertDoesNotHaveAccessor(member, SyntaxKind.SetAccessorDeclaration);
            });
        }