Repository navigation
Add System.CodeDom collections tests - #12679
Conversation
|
Seems like I've fluffed up the formatting again, will fix soon |
Thanks, @hughbe. Please use spaces rather than tabs. |
| { | ||
| ICollection collection = NonGenericICollectionFactory(count); | ||
| Assert.Null(collection.SyncRoot); | ||
| } |
There was a problem hiding this comment.
Rather than introducing a new test, I think I'd prefer if they were a single test that verified the SyncRoot was what it was supposed to be, e.g.
public void ICollection_NonGeneric_SyncRoot_NullOrNonNull(int count)
{
if (!ICollection_NonGeneric_SupportsSyncRoot) return;
ICollection collection = NonGenericICollectionFactory(count);
Assert.Equal(ICollection_NonGeneric_HasNullSyncRoot, collection.SyncRoot == null);
}| [MemberData(nameof(ValidCollectionSizes))] | ||
| public void ICollection_NonGeneric_SyncRootConsistent(int count) | ||
| { | ||
| if (ICollection_NonGeneric_SupportsSyncRoot) |
There was a problem hiding this comment.
Is this change needed? Looks like the test will work even if the collection returns null from SyncRoot. If we're going to run the test anyway, we may as well do something with it.
| [MemberData(nameof(ValidCollectionSizes))] | ||
| public void ICollection_NonGeneric_SyncRoot_ThrowsNotSupportedException(int count) | ||
| { | ||
| if (!ICollection_NonGeneric_SupportsSyncRoot) |
There was a problem hiding this comment.
Rather than having this be a separate test, I'd similarly rather have it be in the single test that verifies the SyncRoot is what we expect it to be: null, non-null, or not even supported. I realize you didn't introduce this particular test. I'm just cognizant of the fact that every new test we add here gets multiplied significantly, as it's a theory that's then pulled into a bunch of different tests for a bunch of different instantiations for a bunch of different collection types. If a test really is verifying something unique, then it's fine to stand alone. Here, though, this and the other two earlier tests are really all checking the same thing: does SyncRoot return what we expect (if we expect anything).
| [MemberData(nameof(ValidCollectionSizes))] | ||
| public void IList_NonGeneric_Remove_NonNullNotContainedInCollection_Throws(int count) | ||
| { | ||
| if (IList_NonGeneric_RemoveNonExistent_Throws) |
There was a problem hiding this comment.
Same comment as earlier. I'd prefer we not introduce a new test for this and just add a conditional to the previous one, e.g.
if (IList_NonGeneric_RemoveNonExistent_Throws)
{
Assert.Throws<ArgumentException>(() => list.Remove(value));
}
else
{
list.Remove(value);
Assert.Equal(count, list.Count);
}We otherwise end up significantly increasing the number of test cases, but half of them end up being nops for a given collection.
| _basePath = Path.Combine( | ||
| string.IsNullOrEmpty(TempDir) ? Path.GetTempPath() : TempDir, | ||
| Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); | ||
| string full = Path.GetFullPath(_basePath); |
There was a problem hiding this comment.
Good spot on the dead code. This had been part of a permission demand, which was removed, and I neglected to remove the code that was providing the input to the demand.
| catch (IOException) | ||
| { | ||
| retryCount--; | ||
| if (retryCount == 0) |
There was a problem hiding this comment.
Given how you found this, I think we actually need to put back the second half of the condition as well. If a non-existent directory is provided, we don't want to try 5000 different files, all of which will fail, before actually failing: we want to fail immediately. But we also can't put it back exactly as it was, as it won't work cross-platform, due to different platform-level error code values. I suggest this:
if (retryCount == 0 || e is DirectoryNotFoundException)
{
throw;
}It's not 100% the same, but it addresses the most likely case (unfortunately there's no strongly-typed "file already exists" exception we can special case). If you can think of other exceptions that we'd get here, we can add those, too.
There was a problem hiding this comment.
(Thanks for finding the issue, btw.)
| public static IEnumerable<object[]> AddRange_TestData() | ||
| { | ||
| yield return new object[] { new CodeAttributeArgument[0] }; | ||
| yield return new object[] { new CodeAttributeArgument[] { new CodeAttributeArgument() } }; |
There was a problem hiding this comment.
Since this is about adding a range, let's have at least three cases: 0, 1, and more than 1. (If you've added similar tests later in the PR, same feedback applies).
How about defining your own test base class that provides virtual methods for Add, Remove, Ctor, CtorWithArray, CtorWithCollection, etc., and then write the tests in terms of those and have each collection override those methods? Then most of the collections should be able to share the same set of tests. |
|
Also, could you separate out the TempFileCollection fix and associated tests into its own PR? That'll make it easier to squash the general collection test additions while keeping the fix separate. |
| File.Delete(filePath); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I may have missed it, but is there a test that verifies TempFileCollection will delete multiple files added to it? If not, could you add one while you're at it?
|
Thanks for adding these, @hughbe. I reviewed the TempFileCollection change and tests, as well as one of the other collection's tests... I'll hold off on reviewing more until you decide what to do about all of the duplication. |
|
This PR now relies on #12680. I'll work on trying to avoid duplication in private then push when that PR is merged :) |
|
You were right - I managed to get rid of bunch of duplicated code. thanks for the suggestion! |
| { | ||
| public abstract class CodeCollectionTestBase<TCollection, TItem> where TCollection: class where TItem : class, new() | ||
| { | ||
| public abstract TCollection Ctor(); |
There was a problem hiding this comment.
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.
|
Looks good, @hughbe, and glad to see much of the duplication go away. Thanks for adding these. |
Commit migrated from dotnet/corefx@9a664e2
Part of #12179
/cc @stephentoub