Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions PolyShim.Tests/NetCore20/DictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.NetCore20;

public class DictionaryTests
{
[Fact]
public void TryAdd_Test()
{
// Arrange
var dictionary = new Dictionary<string, int> { ["apple"] = 1, ["banana"] = 2 };

// Act
var result = dictionary.TryAdd("cherry", 3);

// Assert
result.Should().BeTrue();
dictionary.Should().ContainKey("cherry");
dictionary["cherry"].Should().Be(3);
dictionary.Should().HaveCount(3);
}

[Fact]
public void TryAdd_Exists_Test()
{
// Arrange
var dictionary = new Dictionary<string, int> { ["apple"] = 1, ["banana"] = 2 };

// Act
var result = dictionary.TryAdd("apple", 99);

// Assert
result.Should().BeFalse();
dictionary["apple"].Should().Be(1);
dictionary.Should().HaveCount(2);
}
}
2 changes: 1 addition & 1 deletion PolyShim/List-Signatures.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ foreach ($file in $codeFiles) {
}

# Extract extension members
$extensionPattern = 'extension(?:<[^>]+>)?\(([^)]+)\)\s*\{'
$extensionPattern = 'extension(?:<[^>]+>)?\(([^)]+)\)(?:\s+where[^{]*)?\s*\{'
$extensionMatches = [regex]::Matches($content, $extensionPattern)

foreach ($match in $extensionMatches) {
Expand Down
25 changes: 25 additions & 0 deletions PolyShim/NetCore20/Dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if (NETCOREAPP && !NETCOREAPP2_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD && !NETSTANDARD2_1_OR_GREATER)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.Collections.Generic;

internal static partial class PolyfillExtensions
{
extension<TKey, TValue>(Dictionary<TKey, TValue> dictionary) where TKey : notnull
{
// https://learn.microsoft.com/dotnet/api/system.collections.generic.dictionary-2.tryadd
public bool TryAdd(TKey key, TValue value)
{
if (dictionary.ContainsKey(key))
return false;

dictionary.Add(key, value);
return true;
}
}
}
#endif
6 changes: 4 additions & 2 deletions PolyShim/Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 231
- **Total:** 232
- **Types:** 62
- **Members:** 169
- **Members:** 170

___

Expand All @@ -28,6 +28,8 @@ ___
- [`DateTime UnixEpoch`](https://learn.microsoft.com/dotnet/api/system.datetime.unixepoch) <sup><sub>.NET Core 2.1</sub></sup>
- `DateTimeOffset`
- [`DateTimeOffset UnixEpoch`](https://learn.microsoft.com/dotnet/api/system.datetimeoffset.unixepoch) <sup><sub>.NET Core 2.1</sub></sup>
- `Dictionary<TKey, TValue>`
- [`bool TryAdd(TKey, TValue)`](https://learn.microsoft.com/dotnet/api/system.collections.generic.dictionary-2.tryadd) <sup><sub>.NET Core 2.0</sub></sup>
- `DictionaryEntry`
- [`void Deconstruct(out object, out object?)`](https://learn.microsoft.com/dotnet/api/system.collections.dictionaryentry.deconstruct) <sup><sub>.NET Core 2.0</sub></sup>
- `DisallowNullAttribute`
Expand Down
Loading