Exemplo n.º 1
0
        public void WrappedDtoType_Ctr_CreatesObject()
        {
            Compilation outputCompilation = CreateWrappedTypeCompilation();
            var         assembly          = SourceBuilder.EmitToAssembly(outputCompilation);

            var sut = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto2Wrapped").GetConstructor(new Type[0]).Invoke(null);

            Assert.NotNull(sut);
        }
        public void ApplyPatch_PropertyToDelete_SetsNullOnTarget(
            string sourceCode,
            bool hasTargetParent,
            bool hasTargetSub,
            string parentStringPropertyInput,
            string parentStringPropertyExpected,
            int numberPropInput,
            int numberPropExpected,
            DateTime?nullableDateTimePropertyInput,
            DateTime?nullableDateTimePropertyExpected,
            double camelCasePropertyInput,
            double camelCasePropertyExpected,
            int[] valuesInput,
            int[] valuesExpected,
            string jsonInput)
        {
            var compilation    = CreateInputOutputCompilation(sourceCode);
            var outputAssembly = SourceBuilder.EmitToAssembly(compilation);

            var parentDtoWrappedMetadata = outputAssembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.ParentDtoWrapped");
            var targetParentMetadata     = outputAssembly.GetType("TestCode.ParentDto");
            var targetSubMetadata        = outputAssembly.GetType("TestCode.SubDto");

            var sut = JsonSerializer.Deserialize(jsonInput, parentDtoWrappedMetadata);

            object targetParent = null;

            if (hasTargetParent)
            {
                var ctor = targetParentMetadata.GetConstructors().OrderByDescending(x => x.GetParameters().Count()).First();
                targetParent = ctor.Invoke(new object[ctor.GetParameters().Count()]);
                targetParentMetadata.GetProperty("ParentStringProperty").SetValue(targetParent, parentStringPropertyInput);
                targetParentMetadata.GetProperty("Values").SetValue(targetParent, valuesInput);
            }
            if (hasTargetSub)
            {
                var ctor      = targetSubMetadata.GetConstructors().OrderByDescending(x => x.GetParameters().Count()).First();
                var targetSub = ctor.Invoke(new object[ctor.GetParameters().Count()]);
                targetParentMetadata.GetProperty("OtherDto").SetValue(targetParent, targetSub);
                targetSubMetadata.GetProperty("NumberProp").SetValue(targetSub, numberPropInput);
                targetSubMetadata.GetProperty("NullableDateTimeProperty").SetValue(targetSub, nullableDateTimePropertyInput);
                targetSubMetadata.GetProperty("CamelCaseProperty").SetValue(targetSub, camelCasePropertyInput);
            }

            var patchedParent = parentDtoWrappedMetadata.GetMethod("ApplyPatch").Invoke(sut, new[] { targetParent });
            var patchedSub    = targetParentMetadata.GetProperty("OtherDto").GetValue(patchedParent);

            Assert.Equal(parentStringPropertyExpected, targetParentMetadata.GetProperty("ParentStringProperty").GetValue(patchedParent));
            Assert.Equal(valuesExpected, targetParentMetadata.GetProperty("Values").GetValue(patchedParent));
            Assert.Equal(numberPropExpected, targetSubMetadata.GetProperty("NumberProp").GetValue(patchedSub));
            Assert.Equal(nullableDateTimePropertyExpected, targetSubMetadata.GetProperty("NullableDateTimeProperty").GetValue(patchedSub));
            Assert.Equal(camelCasePropertyExpected, targetSubMetadata.GetProperty("CamelCaseProperty").GetValue(patchedSub));
        }
Exemplo n.º 3
0
        public void NullArgument_ApplyPatch_ReturnsObject()
        {
            Compilation outputCompilation = CreateWrappedTypeCompilation();
            var         assembly          = SourceBuilder.EmitToAssembly(outputCompilation);

            var wrappedSubDtoMetadata = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto1Wrapped");
            var sut = wrappedSubDtoMetadata.GetConstructor(new Type[0]).Invoke(null);

            var result = wrappedSubDtoMetadata.GetMethod("ApplyPatch").Invoke(sut, new object[] { null });

            Assert.NotNull(result);
        }
Exemplo n.º 4
0
        public void WrappedSubDtoType_Sets_GeneratedProperties()
        {
            Compilation outputCompilation = CreateWrappedTypeCompilation();
            var         assembly          = SourceBuilder.EmitToAssembly(outputCompilation);

            var wrappedTypeMetadata = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto1Wrapped");
            var sut = wrappedTypeMetadata.GetConstructor(new Type[0]).Invoke(null);

            wrappedTypeMetadata.GetProperty("NumberProp").SetValue(sut, 100);

            Assert.Equal(100, wrappedTypeMetadata.GetProperty("NumberProp").GetValue(sut));
        }
Exemplo n.º 5
0
        public void WrappedDtoType_Sets_SubDtoProperty()
        {
            Compilation outputCompilation = CreateWrappedTypeCompilation();
            var         assembly          = SourceBuilder.EmitToAssembly(outputCompilation);

            var wrappedDtoMetadata    = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto2Wrapped");
            var wrappedSubDtoMetadata = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto1Wrapped");
            var sut    = wrappedDtoMetadata.GetConstructor(new Type[0]).Invoke(null);
            var subDto = wrappedSubDtoMetadata.GetConstructor(new Type[0]).Invoke(null);

            wrappedDtoMetadata.GetProperty("OtherDto").SetValue(sut, subDto);

            Assert.Same(subDto, wrappedDtoMetadata.GetProperty("OtherDto").GetValue(sut));
        }
Exemplo n.º 6
0
        public void ApplyPatch_DoesNotSet_ReferencePropertyWithoutValue()
        {
            var compilation    = CreateWrappedTypeCompilation();
            var outputAssembly = SourceBuilder.EmitToAssembly(compilation);

            var wrappedTypeMetadata = outputAssembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto2Wrapped");
            var sut = wrappedTypeMetadata.GetConstructor(new Type[0]).Invoke(null);

            var targetTypeMetadata = outputAssembly.GetType("TestCode.Dto2");
            var targetObject       = targetTypeMetadata.GetConstructor(new Type[0]).Invoke(null);

            targetTypeMetadata.GetProperty("Property").SetValue(targetObject, "hello world");

            wrappedTypeMetadata.GetMethod("ApplyPatch").Invoke(sut, new[] { targetObject });

            Assert.Equal("hello world", targetTypeMetadata.GetProperty("Property").GetValue(targetObject));
        }
Exemplo n.º 7
0
        public void ApplyPatch_SetsPropertiesWithValues_ToTargetObject()
        {
            var compilation    = CreateWrappedTypeCompilation();
            var outputAssembly = SourceBuilder.EmitToAssembly(compilation);

            var wrappedTypeMetadata = outputAssembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto1Wrapped");
            var sut = wrappedTypeMetadata.GetConstructor(new Type[0]).Invoke(null);

            wrappedTypeMetadata.GetProperty("NumberProp").SetValue(sut, 100);

            var targetTypeMetadata = outputAssembly.GetType("TestCode.Dto1");
            var targetObject       = targetTypeMetadata.GetConstructor(new Type[0]).Invoke(null);

            wrappedTypeMetadata.GetMethod("ApplyPatch").Invoke(sut, new[] { targetObject });

            Assert.Equal(100, targetTypeMetadata.GetProperty("NumberProp").GetValue(targetObject));
        }
        public void ApplyPatch_PropertyToDelete_SetsNullOnTarget(
            string source,
            object input,
            object expected,
            string jsonInput)
        {
            var compilation    = CreateWrappedTypeCompilation(source);
            var outputAssembly = SourceBuilder.EmitToAssembly(compilation);

            var dtoWrappedMetadata = outputAssembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.DtoWrapped");
            var targetMetadata     = outputAssembly.GetType("TestCode.Dto");
            var target             = targetMetadata.GetConstructor(new Type[0]).Invoke(null);

            targetMetadata.GetProperty("Values").SetValue(target, input);

            var sut           = JsonSerializer.Deserialize(jsonInput, dtoWrappedMetadata);
            var patchedParent = dtoWrappedMetadata.GetMethod("ApplyPatch").Invoke(sut, new[] { target });

            Assert.Equal(expected, targetMetadata.GetProperty("Values").GetValue(patchedParent));
        }
Exemplo n.º 9
0
        public void ApplyPatch_CallsApplyPath_OnSubDto()
        {
            Compilation outputCompilation = CreateWrappedTypeCompilation();
            var         assembly          = SourceBuilder.EmitToAssembly(outputCompilation);

            var wrappedDtoMetadata    = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto2Wrapped");
            var wrappedSubDtoMetadata = assembly.GetType("LaDeak.JsonMergePatch.Generated.SafeTestCode.Dto1Wrapped");
            var sut    = wrappedDtoMetadata.GetConstructor(new Type[0]).Invoke(null);
            var subDto = wrappedSubDtoMetadata.GetConstructor(new Type[0]).Invoke(null);

            wrappedDtoMetadata.GetProperty("OtherDto").SetValue(sut, subDto);
            wrappedSubDtoMetadata.GetProperty("NumberProp").SetValue(subDto, 100);

            var result               = wrappedDtoMetadata.GetMethod("ApplyPatch").Invoke(sut, new object[] { null });
            var targetDtoMetadata    = assembly.GetType("TestCode.Dto2");
            var targetSubDtoMetadata = assembly.GetType("TestCode.Dto1");
            var subDtoResult         = targetDtoMetadata.GetProperty("OtherDto").GetValue(result);

            Assert.NotNull(subDtoResult);
            Assert.Equal(100, targetSubDtoMetadata.GetProperty("NumberProp").GetValue(subDtoResult));
        }