Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea for immutability for TestNode + PropertyBag #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
98 changes: 7 additions & 91 deletions src/Platform/Microsoft.Testing.Platform/Messages/PropertyBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,102 +23,14 @@ public sealed partial class PropertyBag
internal /* for testing */ Property? _property;
#pragma warning restore SA1401 // Fields should be private

public PropertyBag()
{
}

public PropertyBag(params IProperty[] properties)
internal PropertyBag(IEnumerable<IProperty> properties, TestNodeStateProperty testNodeStateProperty)
{
ArgumentGuard.IsNotNull(properties);
ArgumentGuard.IsNotNull(testNodeStateProperty);

if (properties.Length == 0)
{
return;
}

for (int i = 0; i < properties.Length; i++)
{
if (properties[i] is TestNodeStateProperty testNodeStateProperty)
{
if (_testNodeStateProperty is not null)
{
ThrowDuplicatedPropertyType(properties[i]);
}

_testNodeStateProperty = testNodeStateProperty;
}
else
{
if (_property is null)
{
_property = new(properties[i]);
}
else
{
if (_property.Contains(properties[i]))
{
ThrowDuplicatedPropertyInstance(properties[i]);
}

_property = new(properties[i], _property);
}
}
}
}

public PropertyBag(IEnumerable<IProperty> properties)
{
ArgumentGuard.IsNotNull(properties);
_testNodeStateProperty = testNodeStateProperty;

foreach (IProperty property in properties)
{
if (property is TestNodeStateProperty testNodeStateProperty)
{
if (_testNodeStateProperty is not null)
{
ThrowDuplicatedPropertyType(property);
}

_testNodeStateProperty = testNodeStateProperty;
}
else
{
if (_property is null)
{
_property = new(property);
}
else
{
if (_property.Contains(property))
{
ThrowDuplicatedPropertyInstance(property);
}

_property = new(property, _property);
}
}
}
}

public int Count => _property is null
? _testNodeStateProperty is null ? 0 : 1
: _property.Count + (_testNodeStateProperty is not null ? 1 : 0);

public void Add(IProperty property)
{
ArgumentGuard.IsNotNull(property);

// Optimized access to the TestNodeStateProperty, it's one of the most used property.
if (property is TestNodeStateProperty testNodeStateProperty)
{
if (_testNodeStateProperty is not null)
{
ThrowDuplicatedPropertyType(property);
}

_testNodeStateProperty = testNodeStateProperty;
}
else
{
if (_property is null)
{
Expand All @@ -136,6 +48,10 @@ public void Add(IProperty property)
}
}

public int Count => _property is null
? _testNodeStateProperty is null ? 0 : 1
: _property.Count + (_testNodeStateProperty is not null ? 1 : 0);

public bool Any<TProperty>()
where TProperty : IProperty
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Testing.Platform.Extensions.Messages;

public class PropertyBagBuilder : List<IProperty>
{
public new PropertyBagBuilder Add(IProperty property)
{
base.Add(property);
return this;
}

public PropertyBag ToImmutablePropertyBag(TestNodeStateProperty currentState)
{
IEnumerable<IProperty> nonTestStateProperties = this.Where(x => x is not TestNodeStateProperty);

return new PropertyBag(nonTestStateProperties, currentState);
}
}
13 changes: 10 additions & 3 deletions src/Platform/Microsoft.Testing.Platform/Messages/TestNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ namespace Microsoft.Testing.Platform.Extensions.Messages;

public class TestNode
{
public required TestNodeUid Uid { get; init; }
internal TestNode(TestNodeUid uid, string displayName, PropertyBag properties)
{
Uid = uid;
DisplayName = displayName;
Properties = properties;
}

public TestNodeUid Uid { get; }

public required string DisplayName { get; init; }
public string DisplayName { get; }

public PropertyBag Properties { get; init; } = new();
public PropertyBag Properties { get; }

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Testing.Platform.Extensions.Messages;

public class TestNodeBuilder
{
public required TestNodeUid Uid { get; init; }

public required string DisplayName { get; init; }

public PropertyBagBuilder Properties { get; init; } = new();

public TestNodeBuilder WithProperty(IProperty property)
{
Properties.Add(property);
return this;
}

public TestNode ToImmutableTestNode(TestNodeStateProperty currentState) =>
new(Uid, DisplayName, Properties.ToImmutablePropertyBag(currentState));
}