Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Common/tests/System/Collections/IList.NonGeneric.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ protected virtual object CreateT(int seed)

protected virtual Type IList_NonGeneric_Item_InvalidIndex_ThrowType => typeof(ArgumentOutOfRangeException);

protected virtual bool IList_NonGeneric_RemoveNonExistent_Throws => false;

#endregion

#region ICollection Helper Methods
Expand Down Expand Up @@ -866,12 +868,20 @@ public void IList_NonGeneric_Remove_NonNullNotContainedInCollection(int count)
if (!IsReadOnly && !ExpectedFixedSize)
{
int seed = count * 251;
IList collection = NonGenericIListFactory(count);
IList list = NonGenericIListFactory(count);
object value = CreateT(seed++);
while (collection.Contains(value) || Enumerable.Contains(InvalidValues, value))
while (list.Contains(value) || Enumerable.Contains(InvalidValues, value))
value = CreateT(seed++);
collection.Remove(value);
Assert.Equal(count, collection.Count);
list.Remove(value);

if (IList_NonGeneric_RemoveNonExistent_Throws)
{
Assert.Throws<ArgumentException>(() => list.Remove(value));
}
else
{
Assert.Equal(count, list.Count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.CodeDom.Tests
{
public class CodeAttributeArgumentCollectionTests : CodeCollectionTestBase<CodeAttributeArgumentCollection, CodeAttributeArgument>
{
public override CodeAttributeArgumentCollection Ctor() => new CodeAttributeArgumentCollection();
public override CodeAttributeArgumentCollection CtorArray(CodeAttributeArgument[] array) => new CodeAttributeArgumentCollection(array);
public override CodeAttributeArgumentCollection CtorCollection(CodeAttributeArgumentCollection collection) => new CodeAttributeArgumentCollection(collection);

public override int Count(CodeAttributeArgumentCollection collection) => collection.Count;

public override CodeAttributeArgument GetItem(CodeAttributeArgumentCollection collection, int index) => collection[index];
public override void SetItem(CodeAttributeArgumentCollection collection, int index, CodeAttributeArgument value) => collection[index] = value;

public override void AddRange(CodeAttributeArgumentCollection collection, CodeAttributeArgument[] array) => collection.AddRange(array);
public override void AddRange(CodeAttributeArgumentCollection collection, CodeAttributeArgumentCollection value) => collection.AddRange(value);

public override object Add(CodeAttributeArgumentCollection collection, CodeAttributeArgument obj) => collection.Add(obj);

public override void Insert(CodeAttributeArgumentCollection collection, int index, CodeAttributeArgument value) => collection.Insert(index, value);

public override void Remove(CodeAttributeArgumentCollection collection, CodeAttributeArgument value) => collection.Remove(value);

public override int IndexOf(CodeAttributeArgumentCollection collection, CodeAttributeArgument value) => collection.IndexOf(value);
public override bool Contains(CodeAttributeArgumentCollection collection, CodeAttributeArgument value) => collection.Contains(value);

public override void CopyTo(CodeAttributeArgumentCollection collection, CodeAttributeArgument[] array, int index) => collection.CopyTo(array, index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.CodeDom.Tests
{
public class CodeAttributeDeclarationCollectionTests : CodeCollectionTestBase<CodeAttributeDeclarationCollection, CodeAttributeDeclaration>
{
public override CodeAttributeDeclarationCollection Ctor() => new CodeAttributeDeclarationCollection();
public override CodeAttributeDeclarationCollection CtorArray(CodeAttributeDeclaration[] array) => new CodeAttributeDeclarationCollection(array);
public override CodeAttributeDeclarationCollection CtorCollection(CodeAttributeDeclarationCollection collection) => new CodeAttributeDeclarationCollection(collection);

public override int Count(CodeAttributeDeclarationCollection collection) => collection.Count;

public override CodeAttributeDeclaration GetItem(CodeAttributeDeclarationCollection collection, int index) => collection[index];
public override void SetItem(CodeAttributeDeclarationCollection collection, int index, CodeAttributeDeclaration value) => collection[index] = value;

public override void AddRange(CodeAttributeDeclarationCollection collection, CodeAttributeDeclaration[] array) => collection.AddRange(array);
public override void AddRange(CodeAttributeDeclarationCollection collection, CodeAttributeDeclarationCollection value) => collection.AddRange(value);

public override object Add(CodeAttributeDeclarationCollection collection, CodeAttributeDeclaration obj) => collection.Add(obj);

public override void Insert(CodeAttributeDeclarationCollection collection, int index, CodeAttributeDeclaration value) => collection.Insert(index, value);

public override void Remove(CodeAttributeDeclarationCollection collection, CodeAttributeDeclaration value) => collection.Remove(value);

public override int IndexOf(CodeAttributeDeclarationCollection collection, CodeAttributeDeclaration value) => collection.IndexOf(value);
public override bool Contains(CodeAttributeDeclarationCollection collection, CodeAttributeDeclaration value) => collection.Contains(value);

public override void CopyTo(CodeAttributeDeclarationCollection collection, CodeAttributeDeclaration[] array, int index) => collection.CopyTo(array, index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.CodeDom.Tests
{
public class CodeCatchClauseCollectionTests : CodeCollectionTestBase<CodeCatchClauseCollection, CodeCatchClause>
{
public override CodeCatchClauseCollection Ctor() => new CodeCatchClauseCollection();
public override CodeCatchClauseCollection CtorArray(CodeCatchClause[] array) => new CodeCatchClauseCollection(array);
public override CodeCatchClauseCollection CtorCollection(CodeCatchClauseCollection collection) => new CodeCatchClauseCollection(collection);

public override int Count(CodeCatchClauseCollection collection) => collection.Count;

public override CodeCatchClause GetItem(CodeCatchClauseCollection collection, int index) => collection[index];
public override void SetItem(CodeCatchClauseCollection collection, int index, CodeCatchClause value) => collection[index] = value;

public override void AddRange(CodeCatchClauseCollection collection, CodeCatchClause[] array) => collection.AddRange(array);
public override void AddRange(CodeCatchClauseCollection collection, CodeCatchClauseCollection value) => collection.AddRange(value);

public override object Add(CodeCatchClauseCollection collection, CodeCatchClause obj) => collection.Add(obj);

public override void Insert(CodeCatchClauseCollection collection, int index, CodeCatchClause value) => collection.Insert(index, value);

public override void Remove(CodeCatchClauseCollection collection, CodeCatchClause value) => collection.Remove(value);

public override int IndexOf(CodeCatchClauseCollection collection, CodeCatchClause value) => collection.IndexOf(value);
public override bool Contains(CodeCatchClauseCollection collection, CodeCatchClause value) => collection.Contains(value);

public override void CopyTo(CodeCatchClauseCollection collection, CodeCatchClause[] array, int index) => collection.CopyTo(array, index);
}
}
221 changes: 221 additions & 0 deletions src/System.CodeDom/tests/CodeCollections/CodeCollectionTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Xunit;

namespace System.CodeDom.Tests
{
public abstract class CodeCollectionTestBase<TCollection, TItem> where TCollection: class where TItem : class, new()
{
public abstract TCollection Ctor();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Do all of the collections have a parameterless ctor? If so, we could use a new() constraint on TCollection as well to avoid needing this abstract method.

public abstract TCollection CtorArray(TItem[] array);
public abstract TCollection CtorCollection(TCollection collection);

public abstract int Count(TCollection collection);

public abstract TItem GetItem(TCollection collection, int index);
public abstract void SetItem(TCollection collection, int index, TItem value);

public abstract void AddRange(TCollection collection, TItem[] array);
public abstract void AddRange(TCollection collection, TCollection value);

public abstract object Add(TCollection collection, TItem seed);

public abstract void Insert(TCollection collection, int index, TItem value);
public abstract void Remove(TCollection collection, TItem value);

public abstract int IndexOf(TCollection collection, TItem value);
public abstract bool Contains(TCollection collection, TItem value);

public abstract void CopyTo(TCollection collection, TItem[] array, int index);

[Fact]
public void Ctor_Empty()
{
var collection = Ctor();
Assert.Equal(0, Count(collection));
}

public static IEnumerable<object[]> AddRange_TestData()
{
yield return new object[] { new TItem[0] };
yield return new object[] { new TItem[] { new TItem() } };
yield return new object[] { new TItem[] { new TItem(), new TItem() } };
}

[Theory]
[MemberData(nameof(AddRange_TestData))]
public void Ctor_Array_Works(TItem[] value)
{
var collection = CtorArray(value);
VerifyCollection(collection, value);
}

[Theory]
[MemberData(nameof(AddRange_TestData))]
public void Ctor_CodeStatementCollection_Works(TItem[] value)
{
var collection = CtorCollection(CtorArray(value));
VerifyCollection(collection, value);
}

[Theory]
[MemberData(nameof(AddRange_TestData))]
public void AddRange_CodeStatementArray_Works(TItem[] value)
{
var collection = Ctor();
AddRange(collection, value);
VerifyCollection(collection, value);
}

[Theory]
[MemberData(nameof(AddRange_TestData))]
public void AddRange_CodeStatementCollection_Works(TItem[] value)
{
var collection = Ctor();
AddRange(collection, CtorCollection(CtorArray(value)));
VerifyCollection(collection, value);
}

[Fact]
public void AddRange_Null_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("value", () => CtorArray(null));
Assert.Throws<ArgumentNullException>("value", () => CtorCollection(null));

var collection = Ctor();
Assert.Throws<ArgumentNullException>("value", () => AddRange(collection, (TItem[])null));
Assert.Throws<ArgumentNullException>("value", () => AddRange(collection, (TCollection)null));
}

[Fact]
public void AddRange_NullObjectInValue_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("value", () => CtorArray(new TItem[] { null }));

var collection = Ctor();
Assert.Throws<ArgumentNullException>("value", () => AddRange(collection, new TItem[] { null }));
}

[Fact]
public void Add_CodeStatement_Insert_Remove()
{
var collection = Ctor();

var value1 = new TItem();
Assert.Equal(0, Add(collection, value1));
Assert.Equal(1, Count(collection));
Assert.Equal(value1, GetItem(collection, 0));

var value2 = new TItem();
Insert(collection, 0, value2);
Assert.Equal(2, Count(collection));
Assert.Same(value2, GetItem(collection, 0));

Remove(collection, value1);
Assert.Equal(1, Count(collection));

Remove(collection, value2);
Assert.Equal(0, Count(collection));
}

[Fact]
public void Add_Null_ThrowsArgumentNullException()
{
var collection = Ctor();
Assert.Throws<ArgumentNullException>("value", () => Add(collection, null));
}

[Theory]
[InlineData(-1)]
[InlineData(1)]
public void Insert_InvalidIndex_ThrowsArgumentOutOfRangeException(int index)
{
var collection = Ctor();
Assert.Throws<ArgumentOutOfRangeException>("index", () => Insert(collection, index, new TItem()));
}

[Fact]
public void Insert_Null_ThrowsArgumentNullException()
{
var collection = Ctor();
Assert.Throws<ArgumentNullException>("value", () => Insert(collection, 0, null));
}

[Fact]
public void Remove_Null_ThrowsArgumentNullException()
{
var collection = Ctor();
Assert.Throws<ArgumentNullException>("value", () => Remove(collection, null));
}

[Fact]
public void Remove_NoSuchObject_ThrowsArgumentException()
{
var collection = Ctor();
Assert.Throws<ArgumentException>(null, () => Remove(collection, new TItem()));
}

[Fact]
public void Contains_NoSuchObject_ReturnsMinusOne()
{
var collection = Ctor();
Assert.False(Contains(collection, null));
Assert.False(Contains(collection, new TItem()));
}

[Fact]
public void IndexOf_NoSuchObject_ReturnsMinusOne()
{
var collection = Ctor();
Assert.Equal(-1, IndexOf(collection, null));
Assert.Equal(-1, IndexOf(collection, new TItem()));
}

[Theory]
[InlineData(-1)]
[InlineData(0)]
public void Item_InvalidIndex_ThrowsArgumentOutOfRangeException(int index)
{
var collection = Ctor();
Assert.Throws<ArgumentOutOfRangeException>("index", () => GetItem(collection, index));
Assert.Throws<ArgumentOutOfRangeException>("index", () => SetItem(collection, index, new TItem()));
}

[Fact]
public void ItemSet_Get_ReturnsExpected()
{
var value1 = new TItem();
var value2 = new TItem();
var collection = Ctor();
Add(collection, value1);

SetItem(collection, 0, value2);
Assert.Equal(1, Count(collection));
Assert.Same(value2, GetItem(collection, 0));
}

private void VerifyCollection(TCollection collection, TItem[] contents)
{
Assert.Equal(contents.Length, Count(collection));
for (int i = 0; i < contents.Length; i++)
{
TItem content = GetItem(collection, i);
Assert.Equal(i, IndexOf(collection, content));
Assert.True(Contains(collection, content));
Assert.Same(content, GetItem(collection, i));
}

const int Index = 1;
var copy = new TItem[contents.Length + Index];
CopyTo(collection, copy, Index);
Assert.Null(copy[0]);
for (int i = Index; i < copy.Length; i++)
{
Assert.Same(contents[i - Index], copy[i]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.CodeDom.Tests
{
public class CodeCommentStatementCollectionTests : CodeCollectionTestBase<CodeCommentStatementCollection, CodeCommentStatement>
{
public override CodeCommentStatementCollection Ctor() => new CodeCommentStatementCollection();
public override CodeCommentStatementCollection CtorArray(CodeCommentStatement[] array) => new CodeCommentStatementCollection(array);
public override CodeCommentStatementCollection CtorCollection(CodeCommentStatementCollection collection) => new CodeCommentStatementCollection(collection);

public override int Count(CodeCommentStatementCollection collection) => collection.Count;

public override CodeCommentStatement GetItem(CodeCommentStatementCollection collection, int index) => collection[index];
public override void SetItem(CodeCommentStatementCollection collection, int index, CodeCommentStatement value) => collection[index] = value;

public override void AddRange(CodeCommentStatementCollection collection, CodeCommentStatement[] array) => collection.AddRange(array);
public override void AddRange(CodeCommentStatementCollection collection, CodeCommentStatementCollection value) => collection.AddRange(value);

public override object Add(CodeCommentStatementCollection collection, CodeCommentStatement obj) => collection.Add(obj);

public override void Insert(CodeCommentStatementCollection collection, int index, CodeCommentStatement value) => collection.Insert(index, value);

public override void Remove(CodeCommentStatementCollection collection, CodeCommentStatement value) => collection.Remove(value);

public override int IndexOf(CodeCommentStatementCollection collection, CodeCommentStatement value) => collection.IndexOf(value);
public override bool Contains(CodeCommentStatementCollection collection, CodeCommentStatement value) => collection.Contains(value);

public override void CopyTo(CodeCommentStatementCollection collection, CodeCommentStatement[] array, int index) => collection.CopyTo(array, index);
}
}
Loading