public void ListStorage_RemoveAt_RearrangesInnerArray()
        {
            // Arrange
            var dict = new DispatcherValueCollection();

            dict.Add("key", "value");
            dict.Add("key2", "value2");
            dict.Add("key3", "value3");

            // Assert 1
            var storage = Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);

            Assert.Equal(3, storage.Count);

            // Act
            dict.Remove("key2");

            // Assert 2
            Assert.Equal(2, storage.Count);
            Assert.Equal("key", storage[0].Key);
            Assert.Equal("value", storage[0].Value);
            Assert.Equal("key3", storage[1].Key);
            Assert.Equal("value3", storage[1].Value);

            Assert.Throws <ArgumentOutOfRangeException>(() => storage[2]);
        }
示例#2
0
        internal RoutePatternBinder(
            UrlEncoder urlEncoder,
            ObjectPool <UriBuildingContext> pool,
            RoutePattern pattern,
            DispatcherValueCollection defaults)
        {
            if (urlEncoder == null)
            {
                throw new ArgumentNullException(nameof(urlEncoder));
            }

            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            _urlEncoder = urlEncoder;
            _pool       = pool;
            _pattern    = pattern;
            _defaults   = defaults;

            // Any default that doesn't have a corresponding parameter is a 'filter' and if a value
            // is provided for that 'filter' it must match the value in defaults.
            _filters = new DispatcherValueCollection(_defaults);
            foreach (var parameter in _pattern.Parameters)
            {
                _filters.Remove(parameter.Name);
            }
        }
        public void Remove_EmptyStorage()
        {
            // Arrange
            var dict = new DispatcherValueCollection();

            // Act
            var result = dict.Remove("key");

            // Assert
            Assert.False(result);
            Assert.IsType <DispatcherValueCollection.EmptyStorage>(dict._storage);
        }
        public void Remove_PropertyStorage_True_CaseInsensitive()
        {
            // Arrange
            var dict = new DispatcherValueCollection(new { key = "value" });

            // Act
            var result = dict.Remove("kEy");

            // Assert
            Assert.True(result);
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);
        }
        public void Remove_PropertyStorage_False()
        {
            // Arrange
            var dict = new DispatcherValueCollection(new { key = "value" });

            // Act
            var result = dict.Remove("other");

            // Assert
            Assert.False(result);
            Assert.Collection(dict, kvp => { Assert.Equal("key", kvp.Key); Assert.Equal("value", kvp.Value); });
            Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);
        }
        public void Remove_PropertyStorage_Empty()
        {
            // Arrange
            var dict = new DispatcherValueCollection(new { });

            // Act
            var result = dict.Remove("other");

            // Assert
            Assert.False(result);
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.PropertyStorage>(dict._storage);
        }
        public void Remove_ListStorage_True()
        {
            // Arrange
            var dict = new DispatcherValueCollection()
            {
                { "key", "value" },
            };

            // Act
            var result = dict.Remove("key");

            // Assert
            Assert.True(result);
            Assert.Empty(dict);
            Assert.IsType <DispatcherValueCollection.ListStorage>(dict._storage);
        }
示例#8
0
        private string BindValues(UriBuildingContext context, DispatcherValueCollection acceptedValues)
        {
            for (var i = 0; i < _pattern.PathSegments.Count; i++)
            {
                Debug.Assert(context.BufferState == SegmentState.Beginning);
                Debug.Assert(context.UriState == SegmentState.Beginning);

                var segment = _pattern.PathSegments[i];

                for (var j = 0; j < segment.Parts.Count; j++)
                {
                    var part = segment.Parts[j];

                    if (part.IsLiteral)
                    {
                        if (!context.Accept(_urlEncoder, ((RoutePatternLiteral)part).Content))
                        {
                            return(null);
                        }
                    }
                    else if (part.IsSeparator)
                    {
                        if (!context.Accept(_urlEncoder, ((RoutePatternSeparator)part).Content))
                        {
                            return(null);
                        }
                    }
                    else if (part.IsParameter && part is RoutePatternParameter parameter)
                    {
                        // If it's a parameter, get its value
                        object value;
                        var    hasValue = acceptedValues.TryGetValue(parameter.Name, out value);
                        if (hasValue)
                        {
                            acceptedValues.Remove(parameter.Name);
                        }

                        var    isSameAsDefault = false;
                        object defaultValue;
                        if (_defaults != null && _defaults.TryGetValue(parameter.Name, out defaultValue))
                        {
                            if (RoutePartsEqual(value, defaultValue))
                            {
                                isSameAsDefault = true;
                            }
                        }

                        var converted = Convert.ToString(value, CultureInfo.InvariantCulture);
                        if (isSameAsDefault)
                        {
                            // If the accepted value is the same as the default value buffer it since
                            // we won't necessarily add it to the URI we generate.
                            if (!context.Buffer(_urlEncoder, converted))
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            // If the value is not accepted, it is null or empty value in the
                            // middle of the segment. We accept this if the parameter is an
                            // optional parameter and it is preceded by an optional seperator.
                            // I this case, we need to remove the optional seperator that we
                            // have added to the URI
                            // Example: template = {id}.{format?}. parameters: id=5
                            // In this case after we have generated "5.", we wont find any value
                            // for format, so we remove '.' and generate 5.
                            if (!context.Accept(_urlEncoder, converted))
                            {
                                if (j != 0 && parameter.IsOptional && segment.Parts[j - 1].IsSeparator)
                                {
                                    context.Remove(((RoutePatternSeparator)segment.Parts[j - 1]).Content);
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                        }
                    }
                }

                context.EndSegment();
            }

            // Generate the query string from the remaining values
            var wroteFirst = false;

            foreach (var kvp in acceptedValues)
            {
                if (_defaults != null && _defaults.ContainsKey(kvp.Key))
                {
                    // This value is a 'filter' we don't need to put it in the query string.
                    continue;
                }

                var values = kvp.Value as IEnumerable;
                if (values != null && !(values is string))
                {
                    foreach (var value in values)
                    {
                        wroteFirst |= AddParameterToContext(context, kvp.Key, value, wroteFirst);
                    }
                }
                else
                {
                    wroteFirst |= AddParameterToContext(context, kvp.Key, kvp.Value, wroteFirst);
                }
            }
            return(context.ToString());
        }