Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add TryAdd and Clear regression tests #32407

Merged
merged 7 commits into from
Sep 25, 2018
Merged

Conversation

A-And
Copy link

@A-And A-And commented Sep 22, 2018

Added regression tests for changes which were rolled back in dotnet/coreclr#17096 .
@jkotas @sergiy-k

[Fact]
public void Clear_OnEmptyCollection_DoesNotInvalidateEnumerator()
{
if (PlatformDetection.IsFullFramework)
Copy link

Choose a reason for hiding this comment

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

use [SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)]
Can use please also add the reason why we are skipping this test for .net core

Copy link
Author

Choose a reason for hiding this comment

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

The implementation of Clear was changed recently to bring it in line with the behavior of Remove and not invalidate the enumerator. The if() clause here is wrong.

PlatformDetection is definitely unnecessary here. Instead the test should probably rely on expected behavior. Moved test to Dictionary_IDictionary_NonGeneric_Tests and instead inspect ModifyEnumeratorSucceeds for expected behavior.

Assert.Empty(dictionary);
valuesEnum.MoveNext();
}

Copy link

Choose a reason for hiding this comment

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

nit :extra line

{
if (PlatformDetection.IsFullFramework)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
Copy link
Member

Choose a reason for hiding this comment

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

nit: left hand side can be just var (coding guidelines)

if (PlatformDetection.IsFullFramework)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
var valuesEnum = dictionary.GetEnumerator();
Copy link
Member

Choose a reason for hiding this comment

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

nit: concrete type on the left hand side instead of var (coding guidelines)

[Fact]
public void Unsuccessful_TryAdd_DoesNotInvalidateEnumerator()
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
Copy link
Member

Choose a reason for hiding this comment

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

nit: again var on the left hand side.

[Fact]
public void Clear_OnEmptyCollection_DoesNotInvalidateEnumerator()
{
if(ModifyEnumeratorAllowed.HasFlag(ModifyOperation.Clear))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add space between if and (

var dictionary = new Dictionary<string, string>();
dictionary.Add("a", "b");

var valuesEnum = dictionary.GetEnumerator();
Copy link

Choose a reason for hiding this comment

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

nit : change var here to IEnumerator

Copy link

@Anipik Anipik left a comment

Choose a reason for hiding this comment

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

LGTM

@A-And
Copy link
Author

A-And commented Sep 24, 2018

@Anipik - thanks for the patience. I need to get my linter in order.

@A-And
Copy link
Author

A-And commented Sep 25, 2018

Seeing some Microsoft.VisualBasic.Tests failures. @Anipik - is this expected?

@Anipik
Copy link

Anipik commented Sep 25, 2018

yes the failures were expected earlier in the morning today but they were reverted back by this #32439
These should be fine now

@Anipik
Copy link

Anipik commented Sep 25, 2018

@dotnet-bot test this please

@@ -250,6 +264,18 @@ public void Remove_NonExistentEntries_DoesNotPreventEnumeration()
}
}

[Fact]
public void Unsuccessful_TryAdd_DoesNotInvalidateEnumerator()
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'd suggest a name more like "TryAdd_ItemAlreadyExists_DoesNotInvalidEnumerator".

IEnumerator valuesEnum = dictionary.GetEnumerator();
Assert.False(dictionary.TryAdd("a", "c"));

valuesEnum.MoveNext();
Copy link
Member

Choose a reason for hiding this comment

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

Assert the expected result of MoveNext?


dictionary.Clear();
Assert.Empty(dictionary);
valuesEnum.MoveNext();
Copy link
Member

Choose a reason for hiding this comment

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

Assert the expected result of MoveNext?

Copy link
Author

Choose a reason for hiding this comment

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

In this case MoveNext will always be false or thrown an InvalidOperationException.

It's probably better to be explicit, but wouldn't an Assert.False() here be a bit confusing? I.e. whether or not the enumerator advances or not is an implementation detail of IEnumerator we don't necessarily want to test with this.

Copy link
Member

@stephentoub stephentoub Sep 25, 2018

Choose a reason for hiding this comment

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

whether or not the enumerator advances or not is an implementation detail of IEnumerator we don't necessarily want to test with this.

How is it an implementation detail? Regardless of implementation, it should always return false, no?

Copy link
Author

@A-And A-And Sep 25, 2018

Choose a reason for hiding this comment

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

Absolutely true.

I suppose my confusion here stems from the fact that "invalidate the enumerator" here means not changing the state of the collection (and not throwing an InvalidOperationException). The functionality of Clear is already covered by other tests. Whether or not MoveNext is false or not doesn't necessarily matter for the purpose of this test.

Copy link
Author

Choose a reason for hiding this comment

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

Either way, I'm deferring to you - added.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

A few nits, otherwise LGTM.

@A-And A-And merged commit d8a0778 into dotnet:master Sep 25, 2018
@A-And A-And deleted the DictRegressionTests branch September 25, 2018 18:12
@karelz karelz added this to the 3.0 milestone Oct 8, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Add TryAdd and Clear regression tests

* Add Run Condition on Clear()

* Address PR Feedback

* Address PR Feedback dotnet/corefx#2

*  Address PR Feedback dotnet/corefx#3

* Remove Extra Line

* Add MoveNext Result Asserts


Commit migrated from dotnet/corefx@d8a0778
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants