コード例 #1
0
        public async Task Returns_ReadJsonPatchAsync_Types()
        {
            string code = @"
using LaDeak.JsonMergePatch.Http;
public class Controller
{
    public class SomeType { }
    public async System.Threading.Tasks.Task TestMethodAsync()
    {
        var client = new System.Net.Http.HttpClient();
        var response = await client.GetAsync(""https://test.com"");
        await response.Content.ReadJsonPatchAsync<SomeType>();
    }
}";

            var compilation = SourceBuilder.Compile(code, new[] {
                MetadataReference.CreateFromFile(typeof(PatchParameterWalkerTests).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Patch <>).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(HttpClient).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(HttpContentExtensions).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(JsonSerializerOptions).Assembly.Location)
            });

            var sut    = new PatchParametersWalker();
            var result = sut.Process(await compilation.Tree.GetRootAsync(), compilation.Compilation.GetSemanticModel(compilation.Tree));

            Assert.Equal("SomeType", result.First().Name);
        }
コード例 #2
0
        public void MultipleInnerType_Generate_ReturnsWrappedMultipleInnerType()
        {
            (Compilation inputCompilation, SyntaxTree tree) = SourceBuilder.Compile(SimpleTestCode);
            var walker = Substitute.For <IPatchParametersWalker>();

            walker.Process(Arg.Any <SyntaxNode>(), Arg.Any <SemanticModel>()).Returns(new[] { Substitute.For <ITypeSymbol>() });
            var typeBuilder = Substitute.For <ITypeBuilder>();

            typeBuilder.BuildWrapperType(Arg.Any <ITypeSymbol>(), Arg.Any <string>())
            .Returns(
                new GeneratedWrapper()
            {
                ToProcessTypes = new List <ITypeSymbol>()
                {
                    Substitute.For <ITypeSymbol>(), Substitute.For <ITypeSymbol>()
                }
            },
                new GeneratedWrapper()
            {
                ToProcessTypes = new List <ITypeSymbol>()
            },
                new GeneratedWrapper()
            {
                ToProcessTypes = new List <ITypeSymbol>()
            });
            var sut = new MultiTypeBuilder(new[] { tree }, inputCompilation, typeBuilder, walker);

            var results = sut.Generate();

            Assert.Equal(3, results.Count());
        }
コード例 #3
0
        private static Compilation CreateInputOutputCompilation(string sourceCode)
        {
            Compilation inputCompilation            = SourceBuilder.Compile(sourceCode).Compilation;
            JsonMergePatchSourceGenerator generator = new JsonMergePatchSourceGenerator();
            GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

            driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics);
            return(outputCompilation);
        }
コード例 #4
0
        public void WalkerReturnsNoFinding_Generate_ReturnsEmptyResults()
        {
            (Compilation inputCompilation, SyntaxTree tree) = SourceBuilder.Compile(SimpleTestCode);
            var walker = Substitute.For <IPatchParametersWalker>();

            walker.Process(Arg.Any <SyntaxNode>(), Arg.Any <SemanticModel>()).Returns(Enumerable.Empty <ITypeSymbol>());
            var typeBuilder = Substitute.For <ITypeBuilder>();
            var sut         = new MultiTypeBuilder(new[] { tree }, inputCompilation, typeBuilder, walker);

            var results = sut.Generate();

            Assert.Empty(results);
            typeBuilder.DidNotReceive().BuildWrapperType(Arg.Any <ITypeSymbol>(), Arg.Any <string>());
        }
コード例 #5
0
        public async Task No_PatchedType_EmptyResult()
        {
            string code = @"
public class Controller
{
    public void TestMethod1(SomeType input) { }
}";

            var compilation = SourceBuilder.Compile(code, new[] { MetadataReference.CreateFromFile(typeof(PatchParameterWalkerTests).Assembly.Location), MetadataReference.CreateFromFile(typeof(Patch <>).Assembly.Location) });

            var sut    = new PatchParametersWalker();
            var result = sut.Process(await compilation.Tree.GetRootAsync(), compilation.Compilation.GetSemanticModel(compilation.Tree));

            Assert.Empty(result);
        }
コード例 #6
0
        public async Task ReturnsGeneric_TypeOf_Patch()
        {
            string code = @"
public class Controller
{
    public class SomeType { }
    public void TestMethod(LaDeak.JsonMergePatch.Abstractions.Patch<SomeType> input) { }
}";

            var compilation = SourceBuilder.Compile(code, new[] { MetadataReference.CreateFromFile(typeof(PatchParameterWalkerTests).Assembly.Location), MetadataReference.CreateFromFile(typeof(Patch <>).Assembly.Location) });

            var sut    = new PatchParametersWalker();
            var result = sut.Process(await compilation.Tree.GetRootAsync(), compilation.Compilation.GetSemanticModel(compilation.Tree));

            Assert.Equal("SomeType", result.Single().Name);
        }
コード例 #7
0
        public async Task RecordTypeWithPatchableArgument_Patch_ReturnsType()
        {
            string code = @"
public class Controller
{
    [LaDeak.JsonMergePatch.Abstractions.Patchable]
    public record SomeType(int Param);

    public record SomeOtherType(int Param);
}";

            var compilation = SourceBuilder.Compile(code, new[] { MetadataReference.CreateFromFile(typeof(PatchParameterWalkerTests).Assembly.Location), MetadataReference.CreateFromFile(typeof(Patch <>).Assembly.Location) });

            var sut    = new PatchParametersWalker();
            var result = sut.Process(await compilation.Tree.GetRootAsync(), compilation.Compilation.GetSemanticModel(compilation.Tree));

            Assert.Equal("SomeType", result.Single().Name);
        }
コード例 #8
0
        public async Task Returns_Multiple_PatchTypes()
        {
            string code = @"
public class Controller
{
    public class SomeType { }
    public void TestMethod1(LaDeak.JsonMergePatch.Abstractions.Patch<SomeType> input) { }
    public void TestMethod2(LaDeak.JsonMergePatch.Abstractions.Patch<bool> input) { }
}";

            var compilation = SourceBuilder.Compile(code, new[] { MetadataReference.CreateFromFile(typeof(PatchParameterWalkerTests).Assembly.Location), MetadataReference.CreateFromFile(typeof(Patch <>).Assembly.Location) });

            var sut    = new PatchParametersWalker();
            var result = sut.Process(await compilation.Tree.GetRootAsync(), compilation.Compilation.GetSemanticModel(compilation.Tree));

            Assert.Equal("SomeType", result.First().Name);
            Assert.Equal("Boolean", result.Last().Name);
            Assert.Equal("System", result.Last().ContainingNamespace.Name);
        }
コード例 #9
0
        public void GeneratedWrappersSource_SameAsReturnedByTypeBuilder()
        {
            (Compilation inputCompilation, SyntaxTree tree) = SourceBuilder.Compile(SimpleTestCode);
            var walker = Substitute.For <IPatchParametersWalker>();

            walker.Process(Arg.Any <SyntaxNode>(), Arg.Any <SemanticModel>()).Returns(new[] { Substitute.For <ITypeSymbol>() });
            var typeBuilder       = Substitute.For <ITypeBuilder>();
            var typeBuilderResult = new GeneratedWrapper()
            {
                ToProcessTypes = new List <ITypeSymbol>(), FileName = "file.cs", SourceCode = "namespace TestNameSpace { }", SourceTypeFullName = "Source.FullName", TargetTypeFullName = "Target.FullName"
            };

            typeBuilder.BuildWrapperType(Arg.Any <ITypeSymbol>(), Arg.Any <string>()).Returns(typeBuilderResult);
            var sut = new MultiTypeBuilder(new[] { tree }, inputCompilation, typeBuilder, walker);

            var result = sut.Generate().Single();

            Assert.Equal(typeBuilderResult.FileName, result.FileName);
            Assert.Equal(typeBuilderResult.SourceCode, result.SourceCode);
            Assert.Equal(typeBuilderResult.SourceTypeFullName, result.SourceTypeFullName);
            Assert.Equal(typeBuilderResult.TargetTypeFullName, result.TargetTypeFullName);
        }
コード例 #10
0
 private static Compilation CreateCompilation(string source) => SourceBuilder.Compile(source).Compilation;