Ejemplo n.º 1
0
    public void Replace_NonExistingKey_Fails()
    {
        // Arrange
        var nameKey           = "Name";
        var dictionary        = new Dictionary <string, object>(StringComparer.Ordinal);
        var dictionaryAdapter = new DictionaryAdapter <string, object>();
        var resolver          = new DefaultContractResolver();

        // Act
        var replaceStatus = dictionaryAdapter.TryReplace(dictionary, nameKey, resolver, "Mike", out var message);

        // Assert
        Assert.False(replaceStatus);
        Assert.Equal("The target location specified by path segment 'Name' was not found.", message);
        Assert.Empty(dictionary);
    }
Ejemplo n.º 2
0
        public void Replace_NonExistingKey_Fails()
        {
            // Arrange
            var nameKey           = "Name";
            var dictionary        = new Dictionary <string, object>(StringComparer.Ordinal);
            var dictionaryAdapter = new DictionaryAdapter <string, object>();
            var options           = new JsonSerializerOptions();

            // Act
            var replaceStatus = dictionaryAdapter.TryReplace(dictionary, typeof(Dictionary <string, object>), nameKey, options, "Mike", out var message);

            // Assert
            Assert.False(replaceStatus);
            Assert.Equal("The target location specified by path segment 'Name' was not found.", message);
            Assert.Empty(dictionary);
        }
Ejemplo n.º 3
0
    public void ReplacingWithInvalidValue_ThrowsInvalidValueForPropertyException()
    {
        // Arrange
        var guidKey    = new Guid();
        var dictionary = new Dictionary <Guid, int>();

        dictionary.Add(guidKey, 5);
        var dictionaryAdapter = new DictionaryAdapter <Guid, int>();
        var resolver          = new DefaultContractResolver();

        // Act
        var replaceStatus = dictionaryAdapter.TryReplace(dictionary, guidKey.ToString(), resolver, "test", out var message);

        // Assert
        Assert.False(replaceStatus);
        Assert.Equal("The value 'test' is invalid for target location.", message);
        Assert.Equal(5, dictionary[guidKey]);
    }
Ejemplo n.º 4
0
    public void ReplacingExistingItem_WithGuidKey()
    {
        // Arrange
        var guidKey    = new Guid();
        var dictionary = new Dictionary <Guid, object>();

        dictionary.Add(guidKey, "Mike");
        var dictionaryAdapter = new DictionaryAdapter <Guid, object>();
        var resolver          = new DefaultContractResolver();

        // Act
        var replaceStatus = dictionaryAdapter.TryReplace(dictionary, guidKey.ToString(), resolver, "James", out var message);

        // Assert
        Assert.True(replaceStatus);
        Assert.True(string.IsNullOrEmpty(message), "Expected no error message");
        Assert.Single(dictionary);
        Assert.Equal("James", dictionary[guidKey]);
    }
Ejemplo n.º 5
0
    public void ReplacingExistingItem()
    {
        // Arrange
        var nameKey    = "Name";
        var dictionary = new Dictionary <string, object>(StringComparer.Ordinal);

        dictionary.Add(nameKey, "Mike");
        var dictionaryAdapter = new DictionaryAdapter <string, object>();
        var resolver          = new DefaultContractResolver();

        // Act
        var replaceStatus = dictionaryAdapter.TryReplace(dictionary, nameKey, resolver, "James", out var message);

        // Assert
        Assert.True(replaceStatus);
        Assert.True(string.IsNullOrEmpty(message), "Expected no error message");
        Assert.Single(dictionary);
        Assert.Equal("James", dictionary[nameKey]);
    }
Ejemplo n.º 6
0
        public void ReplacingExistingItem_WithGuidKey()
        {
            // Arrange
            var guidKey    = new Guid();
            var dictionary = new Dictionary <Guid, object>
            {
                { guidKey, "Mike" }
            };
            var dictionaryAdapter = new DictionaryAdapter <Guid, object>();
            var options           = new JsonSerializerOptions();

            // Act
            var replaceStatus = dictionaryAdapter.TryReplace(dictionary, typeof(Dictionary <Guid, object>), guidKey.ToString(), options, "James", out var message);

            // Assert
            Assert.True(replaceStatus);
            Assert.True(string.IsNullOrEmpty(message), "Expected no error message");
            Assert.Single(dictionary);
            Assert.Equal("James", dictionary[guidKey]);
        }
Ejemplo n.º 7
0
        public void ReplacingExistingItem()
        {
            // Arrange
            var nameKey    = "Name";
            var dictionary = new Dictionary <string, object>(StringComparer.Ordinal)
            {
                { nameKey, "Mike" }
            };
            var dictionaryAdapter = new DictionaryAdapter <string, object>();
            var options           = new JsonSerializerOptions();

            // Act
            var replaceStatus = dictionaryAdapter.TryReplace(dictionary, typeof(Dictionary <string, object>), nameKey, options, "James", out var message);

            // Assert
            Assert.True(replaceStatus);
            Assert.True(string.IsNullOrEmpty(message), "Expected no error message");
            Assert.Single(dictionary);
            Assert.Equal("James", dictionary[nameKey]);
        }
Ejemplo n.º 8
0
        public void ReplacingWithInvalidValue_ThrowsInvalidValueForPropertyException()
        {
            // Arrange
            var guidKey    = new Guid();
            var dictionary = new Dictionary <Guid, int>
            {
                {
                    guidKey, 5
                }
            };
            var dictionaryAdapter = new DictionaryAdapter <Guid, int>();
            var options           = new JsonSerializerOptions();

            // Act
            var replaceStatus = dictionaryAdapter.TryReplace(dictionary, typeof(Dictionary <Guid, int>), guidKey.ToString(), options, "test", out var message);

            // Assert
            Assert.False(replaceStatus);
            Assert.Equal("The value 'test' is invalid for target location.", message);
            Assert.Equal(5, dictionary[guidKey]);
        }
Ejemplo n.º 9
0
    public void Replace_UsesCustomConverter()
    {
        // Arrange
        var nameKey    = "Name";
        var dictionary = new Dictionary <string, Rectangle>(StringComparer.Ordinal);

        dictionary.Add(nameKey, new Rectangle()
        {
            RectangleProperty = "Mike"
        });
        var dictionaryAdapter = new DictionaryAdapter <string, Rectangle>();
        var resolver          = new RectangleContractResolver();

        // Act
        var replaceStatus = dictionaryAdapter.TryReplace(dictionary, nameKey, resolver, "James", out var message);

        // Assert
        Assert.True(replaceStatus);
        Assert.True(string.IsNullOrEmpty(message), "Expected no error message");
        Assert.Single(dictionary);
        Assert.Equal("James", dictionary[nameKey].RectangleProperty);
    }