示例#1
0
    public void GetKeysFromPrefix_ReturnsSubKeysThatStartWithPrefix()
    {
        // Arrange
        var keys      = new[] { "foo[0].name", "foo.age", "foo[1].name", "food[item].spice" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.GetKeysFromPrefix("foo");

        // Assert
        Assert.Collection(result.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase),
                          item =>
        {
            Assert.Equal("0", item.Key);
            Assert.Equal("foo[0]", item.Value);
        },
                          item =>
        {
            Assert.Equal("1", item.Key);
            Assert.Equal("foo[1]", item.Value);
        },
                          item =>
        {
            Assert.Equal("age", item.Key);
            Assert.Equal("foo.age", item.Value);
        });
    }
示例#2
0
    public void GetKeysFromPrefix_ReturnsUniqueTopLevelEntries_WhenPrefixIsEmpty()
    {
        // Arrange
        var keys      = new[] { "[0].name", "[0].address.street", "[item1].name", "[item1].age", "foo", "foo.bar" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.GetKeysFromPrefix(prefix: string.Empty);

        // Assert
        Assert.Collection(result.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase),
                          item =>
        {
            Assert.Equal("0", item.Key);
            Assert.Equal("[0]", item.Value);
        },
                          item =>
        {
            Assert.Equal("foo", item.Key);
            Assert.Equal("foo", item.Value);
        },
                          item =>
        {
            Assert.Equal("item1", item.Key);
            Assert.Equal("[item1]", item.Value);
        });
    }
        public void ContainsPrefix_EmptyCollectionReturnsFalse()
        {
            // Arrange
            var container = new PrefixContainer(new string[0]);

            // Act & Assert
            Assert.False(container.ContainsPrefix(""));
        }
        public void ContainsPrefix_ExactMatch()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "Hello" });

            // Act & Assert
            Assert.True(container.ContainsPrefix("Hello"));
        }
        public void ContainsPrefix_MatchIsCaseInsensitive()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "Hello" });

            // Act & Assert
            Assert.True(container.ContainsPrefix("hello"));
        }
        public void ContainsPrefix_MatchIsNotSimpleSubstringMatch()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "Hello" });

            // Act & Assert
            Assert.False(container.ContainsPrefix("He"));
        }
        public void ContainsPrefix_GuardClauses()
        {
            // Arrange
            var container = new PrefixContainer(new string[0]);

            // Act & assert
            Assert.ThrowsArgumentNull(() => container.ContainsPrefix(null), "prefix");
        }
        public void ContainsPrefix_NonEmptyCollectionReturnsTrueIfPrefixIsEmptyString()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "Hello" });

            // Act & Assert
            Assert.True(container.ContainsPrefix(""));
        }
示例#9
0
        public virtual IDictionary <string, string> GetKeysFromPrefix(string prefix)
        {
            if (prefix == null)
            {
                throw Error.ArgumentNull("prefix");
            }

            return(PrefixContainer.GetKeysFromPrefix(prefix));
        }
示例#10
0
            /// <inheritdoc />
            public override bool ContainsPrefix(string key)
            {
                if (_prefixContainer == null)
                {
                    _prefixContainer = new PrefixContainer(values.Keys);
                }

                return(_prefixContainer.ContainsPrefix(key));
            }
        private PrefixContainer GetOrCreatePrefixContainer()
        {
            if (_prefixContainer == null)
            {
                _prefixContainer = new PrefixContainer(_values.Keys);
            }

            return _prefixContainer;
        }
        public void ContainsPrefix_PrefixBoundaries()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "Hello.There[0]" });

            // Act & Assert
            Assert.True(container.ContainsPrefix("hello"));
            Assert.True(container.ContainsPrefix("hello.there"));
            Assert.True(container.ContainsPrefix("hello.there[0]"));
            Assert.False(container.ContainsPrefix("hello.there.0"));
        }
示例#13
0
    public void ContainsPrefix_HasEntries_NoMatch(string prefix)
    {
        // Arrange
        var keys      = new string[] { "ax", "bx", "cx", "dx" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(prefix);

        // Assert
        Assert.False(result);
    }
示例#14
0
    public void GetKeysFromPrefix_ReturnsEmptyDictionaryWhenNoKeysStartWithPrefix()
    {
        // Arrange
        var keys      = new[] { "foo[0].name", "foo.age", "[1].name", "[item].age" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.GetKeysFromPrefix("baz");

        // Assert
        Assert.Empty(result);
    }
示例#15
0
    public void ContainsPrefix_HasEntries_PrefixMatch_WithSquareBrace(string prefix)
    {
        // Arrange
        var keys      = new string[] { "a[x]", "d[x].y", "e.a.b[foo].bar" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(prefix);

        // Assert
        Assert.True(result);
    }
示例#16
0
    public void ContainsPrefix_HasEntries_EmptyString_True()
    {
        // Arrange
        var keys      = new string[] { "some.prefix" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(string.Empty);

        // Assert
        Assert.True(result);
    }
示例#17
0
    public void GetKeysFromPrefix_ReturnsEmptySequenceWhenContainerIsEmpty(string prefix)
    {
        // Arrange
        var keys      = new string[0];
        var container = new PrefixContainer(keys);

        // Act
        var result = container.GetKeysFromPrefix(prefix);

        // Assert
        Assert.Empty(result);
    }
示例#18
0
    public void ContainsPrefix_ReturnsFalse_IfPrefixDoesNotMatch(string prefix)
    {
        // Arrange
        var keys      = new string[] { "a.b", "a.bc", "a.b[c]", "a.b[0]" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(prefix);

        // Assert
        Assert.False(result);
    }
示例#19
0
    public void ContainsPrefix_HasEntries_PrefixMatch_WithDot(string prefix)
    {
        // Arrange
        var keys      = new string[] { "a.x", "b.xy", "c.x.y", "d.x.z[0]" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(prefix);

        // Assert
        Assert.True(result);
    }
示例#20
0
    public void ContainsPrefix_EmptyCollection_EmptyString_False()
    {
        // Arrange
        var keys      = new string[] { };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(string.Empty);

        // Assert
        Assert.False(result);
    }
示例#21
0
        public bool ContainsPrefix(string prefix)
        {
            foreach (string key in Keys)
            {
                if (PrefixContainer.IsPrefixMatch(prefix, key))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#22
0
    public void ContainsPrefix_ReturnsTrue_IfTheContainerHasAnExactMatch(string prefix)
    {
        // Arrange
        var keys      = new string[] { "bc", "a", "abc", "d" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix(prefix);

        // Assert
        Assert.True(result);
    }
        internal CookieValueProvider(HttpCookieCollection collection, HttpCookieCollection unvalidatedCollection, CultureInfo culture) {
            if (collection == null) {
                throw new ArgumentNullException("collection");
            }

            _culture = culture;
            _prefixes = new PrefixContainer(collection.Keys.Cast<string>());
            _validatedCollection = collection;
            _unvalidatedCollection = unvalidatedCollection ?? collection;

            foreach (string key in collection) {
                if (key != null) {
                    _values[key] = new ValueProviderResultPlaceholder(key, this);
                }
            }
        }
示例#24
0
    public void ContainsPrefix_HasEntries_PartialAndPrefixMatch_WithSquareBrace(int partialMatches)
    {
        // Arrange
        var keys = new string[partialMatches + 1];

        for (var i = 0; i < partialMatches; i++)
        {
            keys[i] = $"aa[{i}]";
        }
        keys[partialMatches] = "a[0]"; // Sorted after all "aa" keys.
        var container = new PrefixContainer(keys);

        // Act
        var result = container.ContainsPrefix("a");

        // Assert
        Assert.True(result);
    }
示例#25
0
    public void GetKeysFromPrefix_ReturnsSubKeysThatStartWithPrefix_ForNestedSubKeys()
    {
        // Arrange
        var keys      = new[] { "person[0].address[0].street", "person[0].address[1].street", "person[0].address[1].zip" };
        var container = new PrefixContainer(keys);

        // Act
        var result = container.GetKeysFromPrefix("person[0].address");

        // Assert
        Assert.Collection(result.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase),
                          item =>
        {
            Assert.Equal("0", item.Key);
            Assert.Equal("person[0].address[0]", item.Value);
        },
                          item =>
        {
            Assert.Equal("1", item.Key);
            Assert.Equal("person[0].address[1]", item.Value);
        });
    }
示例#26
0
 public virtual bool ContainsPrefix(string prefix)
 {
     return(PrefixContainer.ContainsPrefix(prefix));
 }
        public void ContainsPrefix_ShouldIdentifyCollectionWhenNonCollectionPropertyDoesNotOccurOnBinarySearchBoundary()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "foo.a", "foo.b", "foo.c", "foo.d", "foo.esSomethingElse", "foo.es[0].a", "foo.es[0].b", "foo.es[0].c" });

            // Act & Assert
            Assert.True(container.ContainsPrefix("foo.es"));
        }
 private async Task<PrefixContainer> GetPrefixContainerAsync()
 {
     if (_prefixContainer == null)
     {
         // Initialization race is OK providing data remains read-only and object identity is not significant
         var collection = await GetValueCollectionAsync();
         _prefixContainer = new PrefixContainer(collection.Keys);
     }
     return _prefixContainer;
 }
示例#29
0
 /// <inheritdoc />
 public bool ContainsPrefix(string key)
 {
     return(PrefixContainer.ContainsPrefix(key));
 }
        public void GetKeysFromPrefix_PrefixNotFound()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "foo[bar]", "something[other]", "foo.baz", "foot[hello]", "fo[nothing]", "foo" });
            string prefix = "notfound";

            // Act
            IDictionary<string, string> result = container.GetKeysFromPrefix(prefix);

            // Assert
            Assert.Empty(result);
        }
        public void GetKeysFromPrefix_AllValues()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "foo[bar]baz", "something[other]", "foo.baz", "foot[hello]", "fo[nothing]", "foo" });
            string prefix = "";

            // Act
            IDictionary<string, string> result = container.GetKeysFromPrefix(prefix);

            // Assert
            Assert.Equal(4, result.Count());
            Assert.Equal("foo", result["foo"]);
            Assert.Equal("something", result["something"]);
            Assert.Equal("foot", result["foot"]);
            Assert.Equal("fo", result["fo"]);
        }
        public void GetKeysFromPrefix_MixedDotsAndBrackets()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "foo[bar]baz", "something[other]", "foo.baz", "foot[hello]", "fo[nothing]", "foo" });
            string prefix = "foo";

            // Act
            IDictionary<string, string> result = container.GetKeysFromPrefix(prefix);

            // Assert
            Assert.Equal(2, result.Count());
            Assert.True(result.ContainsKey("bar"));
            Assert.True(result.ContainsKey("baz"));
            Assert.Equal("foo[bar]", result["bar"]);
            Assert.Equal("foo.baz", result["baz"]);
        }
        public void GetKeysFromPrefix_DotsNotation()
        {
            // Arrange
            var container = new PrefixContainer(new[] { "foo.bar.baz", "something.other", "foo.baz", "foot.hello", "fo.nothing", "foo" });
            string prefix = "foo";

            // Act
            IDictionary<string, string> result = container.GetKeysFromPrefix(prefix);

            // Assert
            Assert.Equal(2, result.Count());
            Assert.True(result.ContainsKey("bar"));
            Assert.True(result.ContainsKey("baz"));
            Assert.Equal("foo.bar", result["bar"]);
            Assert.Equal("foo.baz", result["baz"]);
        }
示例#34
0
 public bool ContainsPrefix(string prefix)
 {
     return(PrefixContainer.IsPrefixMatch(prefix, Name));
 }
        public void ContainsPrefix_PositiveTests(string testValue)
        {
            // Arrange
            var container = new PrefixContainer(new[] { "a.b", "c.b.a", "a[d]", "a.c" });

            // Act & Assert
            Assert.True(container.ContainsPrefix(testValue));
        }
        private async Task<PrefixContainer> GetPrefixContainerAsync()
        {
            if (_prefixContainer == null)
            {
                var dictionary = await GetDictionary();

                // Initialization race is OK providing data remains read-only and object identity is not significant.
                _prefixContainer = new PrefixContainer(dictionary.Keys);
            }

            return _prefixContainer;
        }