예제 #1
0
    public void TryAdd_ReplacesExistingPropertyValue()
    {
        // Arrange
        var     adapter = new DynamicObjectAdapter();
        dynamic target  = new DynamicTestObject();

        target.List = new List <int>()
        {
            1, 2, 3
        };
        var value = new List <string>()
        {
            "stringValue1", "stringValue2"
        };
        var segment  = "List";
        var resolver = new DefaultContractResolver();

        // Act
        var status = adapter.TryAdd(target, segment, resolver, value, out string errorMessage);

        // Assert
        Assert.True(status);
        Assert.Null(errorMessage);
        Assert.Equal(value, target.List);
    }
예제 #2
0
        public void TryRemove_ThrowsPathNotFoundException_ForNonExistingProperty()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();
            var     segment = "NewProperty";
            var     options = new JsonSerializerOptions();

            // Act
            var removeStatus = adapter.TryRemove(target, typeof(DynamicTestObject), segment, options, out string removeErrorMessage);

            // Assert
            Assert.False(removeStatus);
            Assert.Equal($"The target location specified by path segment '{segment}' was not found.", removeErrorMessage);
        }
예제 #3
0
    public void TryReplace_ThrowsPathNotFoundException_ForNonExistingProperty()
    {
        // Arrange
        var     adapter  = new DynamicObjectAdapter();
        dynamic target   = new DynamicTestObject();
        var     segment  = "NewProperty";
        var     resolver = new DefaultContractResolver();

        // Act
        var status = adapter.TryReplace(target, segment, resolver, "test", out string errorMessage);

        // Assert
        Assert.False(status);
        Assert.Equal($"The target location specified by path segment '{segment}' was not found.", errorMessage);
    }
예제 #4
0
        public void TryAdd_AddsNewProperty()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();
            var     segment = "NewProperty";
            var     options = new JsonSerializerOptions();

            // Act
            var status = adapter.TryAdd(target, typeof(DynamicTestObject), segment, options, "new", out string errorMessage);

            // Assert
            Assert.True(status);
            Assert.Null(errorMessage);
            Assert.Equal("new", target.NewProperty);
        }
예제 #5
0
    public void TryAdd_AddsNewProperty()
    {
        // Arrange
        var     adapter  = new DynamicObjectAdapter();
        dynamic target   = new DynamicTestObject();
        var     segment  = "NewProperty";
        var     resolver = new DefaultContractResolver();

        // Act
        var status = adapter.TryAdd(target, segment, resolver, "new", out string errorMessage);

        // Assert
        Assert.True(status);
        Assert.Null(errorMessage);
        Assert.Equal("new", target.NewProperty);
    }
예제 #6
0
    public void TryReplace_ThrowsPropertyInvalidException_IfNewValueIsNotTheSameTypeAsInitialValue()
    {
        // Arrange
        var     adapter = new DynamicObjectAdapter();
        dynamic target  = new DynamicTestObject();

        target.NewProperty = 1;
        var segment  = "NewProperty";
        var resolver = new DefaultContractResolver();

        // Act
        var status = adapter.TryReplace(target, segment, resolver, "test", out string errorMessage);

        // Assert
        Assert.False(status);
        Assert.Equal($"The value 'test' is invalid for target location.", errorMessage);
    }
예제 #7
0
        public void TryReplace_ThrowsPropertyInvalidException_IfNewValueIsNotTheSameTypeAsInitialValue()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NewProperty = 1;
            var segment = "NewProperty";
            var options = new JsonSerializerOptions();

            // Act
            var status = adapter.TryReplace(target, typeof(DynamicTestObject), segment, options, "test", out string errorMessage);

            // Assert
            Assert.False(status);
            Assert.Equal("The value 'test' is invalid for target location.", errorMessage);
        }
예제 #8
0
        public void TryReplace_RemovesExistingValue_BeforeAddingNewValue()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new WriteOnceDynamicTestObject();

            target.NewProperty = new object();
            var segment = "NewProperty";
            var options = new JsonSerializerOptions();

            // Act
            var status = adapter.TryReplace(target, typeof(WriteOnceDynamicTestObject), segment, options, "new", out string errorMessage);

            // Assert
            Assert.True(status);
            Assert.Null(errorMessage);
            Assert.Equal("new", target.NewProperty);
        }
예제 #9
0
        public void TryTest_ThrowsJsonPatchException_IfTestFails()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NewProperty = "Joana";
            var segment = "NewProperty";
            var expectedErrorMessage = $"The current value 'Joana' at path '{segment}' is not equal to the test value 'John'.";
            var options = new JsonSerializerOptions();

            // Act
            var testStatus = adapter.TryTest(target, typeof(DynamicTestObject), segment, options, "John", out string errorMessage);

            // Assert
            Assert.False(testStatus);
            Assert.Equal(expectedErrorMessage, errorMessage);
        }
예제 #10
0
    public void TryReplace_RemovesExistingValue_BeforeAddingNewValue()
    {
        // Arrange
        var     adapter = new DynamicObjectAdapter();
        dynamic target  = new WriteOnceDynamicTestObject();

        target.NewProperty = new object();
        var segment  = "NewProperty";
        var resolver = new DefaultContractResolver();

        // Act
        var status = adapter.TryReplace(target, segment, resolver, "new", out string errorMessage);

        // Assert
        Assert.True(status);
        Assert.Null(errorMessage);
        Assert.Equal("new", target.NewProperty);
    }
예제 #11
0
    public void TryTraverse_FindsNextTarget()
    {
        // Arrange
        var     adapter = new DynamicObjectAdapter();
        dynamic target  = new DynamicTestObject();

        target.NestedObject             = new DynamicTestObject();
        target.NestedObject.NewProperty = "A";
        var segment  = "NestedObject";
        var resolver = new DefaultContractResolver();

        // Act
        var status = adapter.TryTraverse(target, segment, resolver, out object nextTarget, out string errorMessage);

        // Assert
        Assert.True(status);
        Assert.Null(errorMessage);
        Assert.Equal(target.NestedObject, nextTarget);
    }
예제 #12
0
        public void TryTraverse_FindsNextTarget()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NestedObject             = new DynamicTestObject();
            target.NestedObject.NewProperty = "A";
            var segment = "NestedObject";
            var options = new JsonSerializerOptions();

            // Act
            var status = adapter.TryTraverse(target, typeof(DynamicTestObject), segment, options, out object nextTarget, out string errorMessage);

            // Assert
            Assert.True(status);
            Assert.Null(errorMessage);
            Assert.Equal(target.NestedObject, nextTarget);
        }
예제 #13
0
    public void TryReplace_UsesCustomConverter()
    {
        // Arrange
        var     adapter = new DynamicObjectAdapter();
        dynamic target  = new WriteOnceDynamicTestObject();

        target.NewProperty = new Rectangle();
        var segment  = "NewProperty";
        var resolver = new RectangleContractResolver();

        // Act
        var status = adapter.TryReplace(target, segment, resolver, "new", out string errorMessage);

        // Assert
        Assert.True(status);
        Assert.Null(errorMessage);
        Assert.True(target.NewProperty is Rectangle);
        var rect = (Rectangle)target.NewProperty;

        Assert.Equal("new", rect.RectangleProperty);
    }
예제 #14
0
        public void TryAdd_ReplacesExistingPropertyValue()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.List = new List <int> {
                1, 2, 3
            };
            var value = new List <string> {
                "stringValue1", "stringValue2"
            };
            var segment = "List";
            var options = new JsonSerializerOptions();

            // Act
            var status = adapter.TryAdd(target, typeof(DynamicTestObject), segment, options, value, out string errorMessage);

            // Assert
            Assert.True(status);
            Assert.Null(errorMessage);
            Assert.Equal(value, target.List);
        }
예제 #15
0
        public void TryTest_DoesNotThrowException_IfTestSuccessful()
        {
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();
            var     value   = new List <object>
            {
                "Joana",
                2,
                new Customer("Joana", 25)
            };

            target.NewProperty = value;
            var segment = "NewProperty";
            var options = new JsonSerializerOptions();

            // Act
            var testStatus = adapter.TryTest(target, typeof(DynamicTestObject), segment, options, value, out string errorMessage);

            // Assert
            Assert.Equal(value, target.NewProperty);
            Assert.True(testStatus);
            Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
        }
예제 #16
0
        public void TryGet_GetsPropertyValue_ForExistingProperty()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();
            var     segment = "NewProperty";
            var     options = new JsonSerializerOptions();

            // Act 1
            var addStatus = adapter.TryAdd(target, typeof(DynamicTestObject), segment, options, "new", out string errorMessage);

            // Assert 1
            Assert.True(addStatus);
            Assert.Null(errorMessage);
            Assert.Equal("new", target.NewProperty);

            // Act 2
            var getStatus = adapter.TryGet(target, typeof(DynamicTestObject), segment, options, out object getValue, out string getErrorMessage);

            // Assert 2
            Assert.True(getStatus);
            Assert.Null(getErrorMessage);
            Assert.Equal(getValue, target.NewProperty);
        }
예제 #17
0
    public void TryRemove_SetsPropertyToDefaultOrNull(object value, object expectedValue)
    {
        // Arrange
        var     adapter  = new DynamicObjectAdapter();
        dynamic target   = new DynamicTestObject();
        var     segment  = "NewProperty";
        var     resolver = new DefaultContractResolver();

        // Act 1
        var addStatus = adapter.TryAdd(target, segment, resolver, value, out string errorMessage);

        // Assert 1
        Assert.True(addStatus);
        Assert.Null(errorMessage);
        Assert.Equal(value, target.NewProperty);

        // Act 2
        var removeStatus = adapter.TryRemove(target, segment, resolver, out string removeErrorMessage);

        // Assert 2
        Assert.True(removeStatus);
        Assert.Null(removeErrorMessage);
        Assert.Equal(expectedValue, target.NewProperty);
    }
예제 #18
0
    public void TryGet_GetsPropertyValue_ForExistingProperty()
    {
        // Arrange
        var     adapter  = new DynamicObjectAdapter();
        dynamic target   = new DynamicTestObject();
        var     segment  = "NewProperty";
        var     resolver = new DefaultContractResolver();

        // Act 1
        var addStatus = adapter.TryAdd(target, segment, resolver, "new", out string errorMessage);

        // Assert 1
        Assert.True(addStatus);
        Assert.Null(errorMessage);
        Assert.Equal("new", target.NewProperty);

        // Act 2
        var getStatus = adapter.TryGet(target, segment, resolver, out object getValue, out string getErrorMessage);

        // Assert 2
        Assert.True(getStatus);
        Assert.Null(getErrorMessage);
        Assert.Equal(getValue, target.NewProperty);
    }
예제 #19
0
        public void TryRemove_SetsPropertyToDefaultOrNull(object value, object expectedValue)
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();
            var     segment = "NewProperty";
            var     options = new JsonSerializerOptions();

            // Act 1
            var addStatus = adapter.TryAdd(target, typeof(DynamicTestObject), segment, options, value, out string errorMessage);

            // Assert 1
            Assert.True(addStatus);
            Assert.Null(errorMessage);
            Assert.Equal(value, target.NewProperty);

            // Act 2
            var removeStatus = adapter.TryRemove(target, typeof(DynamicTestObject), segment, options, out string removeErrorMessage);

            // Assert 2
            Assert.True(removeStatus);
            Assert.Null(removeErrorMessage);
            Assert.Equal(expectedValue, target.NewProperty);
        }