Add more StringValues tests
This commit is contained in:
parent
4e0f0c79ec
commit
20e534a570
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
|
@ -10,10 +11,76 @@ namespace Microsoft.Framework.Primitives
|
|||
{
|
||||
public class StringValuesTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsReadOnly_True()
|
||||
public static TheoryData<StringValues> DefaultOrNullStringValues
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<StringValues>
|
||||
{
|
||||
new StringValues(),
|
||||
new StringValues((string)null),
|
||||
new StringValues((string[])null),
|
||||
(string)null,
|
||||
(string[])null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static TheoryData<StringValues> EmptyStringValues
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<StringValues>
|
||||
{
|
||||
StringValues.Empty,
|
||||
new StringValues(new string[0]),
|
||||
new string[0]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static TheoryData<StringValues> FilledStringValues
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<StringValues>
|
||||
{
|
||||
new StringValues("abc"),
|
||||
new StringValues(new[] { "abc" }),
|
||||
new StringValues(new[] { "abc", "bcd" }),
|
||||
new StringValues(new[] { "abc", "bcd", "foo" }),
|
||||
"abc",
|
||||
new[] { "abc" },
|
||||
new[] { "abc", "bcd" },
|
||||
new[] { "abc", "bcd", "foo" }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static TheoryData<StringValues, string[]> FilledStringValuesWithExpected
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<StringValues, string[]>
|
||||
{
|
||||
{ new StringValues("abc"), new[] { "abc" } },
|
||||
{ new StringValues(new[] { "abc" }), new[] { "abc" } },
|
||||
{ new StringValues(new[] { "abc", "bcd" }), new[] { "abc", "bcd" } },
|
||||
{ new StringValues(new[] { "abc", "bcd", "foo" }), new[] { "abc", "bcd", "foo" } },
|
||||
{ "abc", new[] { "abc" } },
|
||||
{ new[] { "abc" }, new[] { "abc" } },
|
||||
{ new[] { "abc", "bcd" }, new[] { "abc", "bcd" } },
|
||||
{ new[] { "abc", "bcd", "foo" }, new[] { "abc", "bcd", "foo" } }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DefaultOrNullStringValues))]
|
||||
[MemberData(nameof(EmptyStringValues))]
|
||||
[MemberData(nameof(FilledStringValues))]
|
||||
public void IsReadOnly_True(StringValues stringValues)
|
||||
{
|
||||
var stringValues = new StringValues();
|
||||
Assert.True(((IList<string>)stringValues).IsReadOnly);
|
||||
Assert.Throws<NotSupportedException>(() => ((IList<string>)stringValues)[0] = string.Empty);
|
||||
Assert.Throws<NotSupportedException>(() => ((ICollection<string>)stringValues).Add(string.Empty));
|
||||
|
|
@ -23,51 +90,34 @@ namespace Microsoft.Framework.Primitives
|
|||
Assert.Throws<NotSupportedException>(() => ((ICollection<string>)stringValues).Clear());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultConstructor_ExpectedValues()
|
||||
[Theory]
|
||||
[MemberData(nameof(DefaultOrNullStringValues))]
|
||||
public void DefaultOrNull_ExpectedValues(StringValues stringValues)
|
||||
{
|
||||
Assert.Null((string[])stringValues);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DefaultOrNullStringValues))]
|
||||
[MemberData(nameof(EmptyStringValues))]
|
||||
public void DefaultNullOrEmpty_ExpectedValues(StringValues stringValues)
|
||||
{
|
||||
var stringValues = new StringValues();
|
||||
Assert.Equal(0, stringValues.Count);
|
||||
Assert.Null((string)stringValues);
|
||||
Assert.Equal((string)null, stringValues);
|
||||
Assert.Equal(string.Empty, stringValues.ToString());
|
||||
Assert.Equal(new string[0], stringValues.ToArray());
|
||||
|
||||
Assert.True(StringValues.IsNullOrEmpty(stringValues));
|
||||
Assert.Throws<IndexOutOfRangeException>(() => stringValues[0]);
|
||||
Assert.Throws<IndexOutOfRangeException>(() => ((IList<string>)stringValues)[0]);
|
||||
Assert.Equal(string.Empty, stringValues.ToString());
|
||||
Assert.Equal(-1, ((IList<string>)stringValues).IndexOf(null));
|
||||
Assert.Equal(-1, ((IList<string>)stringValues).IndexOf(string.Empty));
|
||||
Assert.Equal(0, stringValues.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullStringValue_ExpectedValues()
|
||||
{
|
||||
var stringValues = new StringValues((string)null);
|
||||
Assert.Equal(0, stringValues.Count);
|
||||
Assert.Null((string)stringValues);
|
||||
Assert.Equal(string.Empty, stringValues.ToString());
|
||||
Assert.Null((string[])stringValues);
|
||||
Assert.Equal(new string[0], stringValues.ToArray());
|
||||
|
||||
Assert.True(StringValues.IsNullOrEmpty(stringValues));
|
||||
Assert.Throws<IndexOutOfRangeException>(() => stringValues[0]);
|
||||
Assert.Equal(-1, ((IList<string>)stringValues).IndexOf(string.Empty));
|
||||
Assert.Equal(0, stringValues.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullStringArray_ExpectedValues()
|
||||
{
|
||||
var stringValues = new StringValues((string[])null);
|
||||
Assert.Equal(0, stringValues.Count);
|
||||
Assert.Null((string)stringValues);
|
||||
Assert.Equal(string.Empty, stringValues.ToString());
|
||||
Assert.Null((string[])stringValues);
|
||||
Assert.Equal(new string[0], stringValues.ToArray());
|
||||
|
||||
Assert.True(StringValues.IsNullOrEmpty(stringValues));
|
||||
Assert.Throws<IndexOutOfRangeException>(() => stringValues[0]);
|
||||
Assert.Equal(string.Empty, stringValues.ToString());
|
||||
Assert.Equal(-1, ((IList<string>)stringValues).IndexOf(string.Empty));
|
||||
Assert.Equal(-1, ((IList<string>)stringValues).IndexOf("not there"));
|
||||
Assert.False(((ICollection<string>)stringValues).Contains(null));
|
||||
Assert.False(((ICollection<string>)stringValues).Contains(string.Empty));
|
||||
Assert.False(((ICollection<string>)stringValues).Contains("not there"));
|
||||
Assert.Equal(0, stringValues.Count());
|
||||
}
|
||||
|
||||
|
|
@ -85,6 +135,7 @@ namespace Microsoft.Framework.Primitives
|
|||
Assert.Equal(1, stringValues.Count);
|
||||
Assert.Equal(aString, stringValues);
|
||||
Assert.Equal(aString, stringValues[0]);
|
||||
Assert.Equal(aString, ((IList<string>)stringValues)[0]);
|
||||
Assert.Equal<string[]>(new string[] { aString }, stringValues);
|
||||
}
|
||||
|
||||
|
|
@ -103,6 +154,7 @@ namespace Microsoft.Framework.Primitives
|
|||
Assert.Equal(1, stringValues.Count);
|
||||
Assert.Equal(aString, stringValues);
|
||||
Assert.Equal(aString, stringValues[0]);
|
||||
Assert.Equal(aString, ((IList<string>)stringValues)[0]);
|
||||
Assert.Equal<string[]>(aStringArray, stringValues);
|
||||
|
||||
aString = "abc";
|
||||
|
|
@ -113,5 +165,117 @@ namespace Microsoft.Framework.Primitives
|
|||
Assert.Equal("abc,bcd", stringValues);
|
||||
Assert.Equal<string[]>(aStringArray, stringValues);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DefaultOrNullStringValues))]
|
||||
[MemberData(nameof(EmptyStringValues))]
|
||||
public void DefaultNullOrEmpty_Enumerator(StringValues stringValues)
|
||||
{
|
||||
var e1 = ((IEnumerable<string>)stringValues).GetEnumerator();
|
||||
Assert.Null(e1.Current);
|
||||
Assert.False(e1.MoveNext());
|
||||
Assert.Null(e1.Current);
|
||||
Assert.False(e1.MoveNext());
|
||||
Assert.False(e1.MoveNext());
|
||||
Assert.False(e1.MoveNext());
|
||||
|
||||
var e2 = ((IEnumerable)stringValues).GetEnumerator();
|
||||
Assert.Null(e2.Current);
|
||||
Assert.False(e2.MoveNext());
|
||||
Assert.Null(e2.Current);
|
||||
Assert.False(e2.MoveNext());
|
||||
Assert.False(e2.MoveNext());
|
||||
Assert.False(e2.MoveNext());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FilledStringValuesWithExpected))]
|
||||
public void Enumerator(StringValues stringValues, string[] expected)
|
||||
{
|
||||
var e1 = ((IEnumerable<string>)stringValues).GetEnumerator();
|
||||
Assert.Null(e1.Current);
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
Assert.True(e1.MoveNext());
|
||||
Assert.Equal(expected[i], e1.Current);
|
||||
}
|
||||
Assert.False(e1.MoveNext());
|
||||
Assert.False(e1.MoveNext());
|
||||
Assert.False(e1.MoveNext());
|
||||
|
||||
var e2 = ((IEnumerable)stringValues).GetEnumerator();
|
||||
Assert.Null(e2.Current);
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
Assert.True(e2.MoveNext());
|
||||
Assert.Equal(expected[i], e2.Current);
|
||||
}
|
||||
Assert.False(e2.MoveNext());
|
||||
Assert.False(e2.MoveNext());
|
||||
Assert.False(e2.MoveNext());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FilledStringValuesWithExpected))]
|
||||
public void IndexOf(StringValues stringValues, string[] expected)
|
||||
{
|
||||
IList<string> list = stringValues;
|
||||
Assert.Equal(-1, list.IndexOf("not there"));
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
Assert.Equal(i, list.IndexOf(expected[i]));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FilledStringValuesWithExpected))]
|
||||
public void Contains(StringValues stringValues, string[] expected)
|
||||
{
|
||||
ICollection<string> collection = stringValues;
|
||||
Assert.False(collection.Contains("not there"));
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
Assert.True(collection.Contains(expected[i]));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FilledStringValuesWithExpected))]
|
||||
public void CopyTo(StringValues stringValues, string[] expected)
|
||||
{
|
||||
ICollection<string> collection = stringValues;
|
||||
string[] actual = new string[expected.Length];
|
||||
collection.CopyTo(actual, 0);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DefaultOrNullStringValues))]
|
||||
[MemberData(nameof(EmptyStringValues))]
|
||||
public void DefaultNullOrEmpty_Concat(StringValues stringValues)
|
||||
{
|
||||
string[] expected = new[] { "abc", "bcd", "foo" };
|
||||
Assert.Equal(expected, StringValues.Concat(stringValues, new StringValues(expected)));
|
||||
Assert.Equal(expected, StringValues.Concat(new StringValues(expected), stringValues));
|
||||
|
||||
string[] empty = new string[0];
|
||||
Assert.Equal(empty, StringValues.Concat(stringValues, StringValues.Empty));
|
||||
Assert.Equal(empty, StringValues.Concat(StringValues.Empty, stringValues));
|
||||
Assert.Equal(empty, StringValues.Concat(stringValues, new StringValues()));
|
||||
Assert.Equal(empty, StringValues.Concat(new StringValues(), stringValues));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FilledStringValuesWithExpected))]
|
||||
public void Concat(StringValues stringValues, string[] array)
|
||||
{
|
||||
string[] filled = new[] { "abc", "bcd", "foo" };
|
||||
|
||||
string[] expectedPrepended = array.Concat(filled).ToArray();
|
||||
Assert.Equal(expectedPrepended, StringValues.Concat(stringValues, new StringValues(filled)));
|
||||
|
||||
string[] expectedAppended = filled.Concat(array).ToArray();
|
||||
Assert.Equal(expectedAppended, StringValues.Concat(new StringValues(filled), stringValues));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue