Skip to content

Commit 97bf915

Browse files
committed
VersionBump : v0.10.0
1 parent b33038f commit 97bf915

File tree

8 files changed

+64
-6
lines changed

8 files changed

+64
-6
lines changed

src/CodeOfChaos.Types.DataSeeder/CodeOfChaos.Types.DataSeeder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- Main package name -->
1010
<PackageId>CodeOfChaos.Types.DataSeeder</PackageId>
11-
<Version>0.9.0</Version>
11+
<Version>0.10.0</Version>
1212
<Authors>Anna Sas</Authors>
1313
<Description>A small library housing DataSeeder typings</Description>
1414
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>

src/CodeOfChaos.Types.TypedValueStore/CodeOfChaos.Types.TypedValueStore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- Main package name -->
1010
<PackageId>CodeOfChaos.Types.TypedValueStore</PackageId>
11-
<Version>0.9.0</Version>
11+
<Version>0.10.0</Version>
1212
<Authors>Anna Sas</Authors>
1313
<Description>A small library housing TypedValueStore typings</Description>
1414
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>

src/CodeOfChaos.Types.TypedValueStore/TypedValueStore.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ public bool TryAdd<T>(TKey key, T value) where T : notnull => _storage
109109
public void AddOrUpdate<T>(TKey key, T value) where T : notnull => _storage
110110
.AddOrUpdate(key, new ValueContainer<T>(value), updateValueFactory: (_, _) => new ValueContainer<T>(value));
111111

112+
/// <summary>
113+
/// Adds the specified key and value to the store if the key does not already exist. If the key already exists,
114+
/// retrieves the existing value associated with the key.
115+
/// </summary>
116+
/// <typeparam name="T">
117+
/// The type of the value associated with the specified key. Must be a non-nullable type.
118+
/// </typeparam>
119+
/// <param name="key">
120+
/// The key to be checked or added to the store. Must not be null.
121+
/// </param>
122+
/// <param name="valueFactory">
123+
/// A function that produces the value to add if the key does not already exist. Must not be null.
124+
/// </param>
125+
/// <returns>
126+
/// The value associated with the specified key. This will either be the existing value if the key was already in
127+
/// the store, or the newly added value obtained from the value factory if the key was not present.
128+
/// </returns>
129+
public T GetOrAdd<T>(TKey key, Func<T> valueFactory) where T : notnull => _storage.GetOrAdd(key, _ => new ValueContainer<T>(valueFactory())).GetAsValue<T>();
130+
112131
/// <summary>
113132
/// Attempts to retrieve a value of the specified type associated with the given key.
114133
/// </summary>

src/CodeOfChaos.Types/CodeOfChaos.Types.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- Main package name -->
1010
<PackageId>CodeOfChaos.Types</PackageId>
11-
<Version>0.9.0</Version>
11+
<Version>0.10.0</Version>
1212
<Authors>Anna Sas</Authors>
1313
<Description>A library of common types and classes within the CodeOfChaos project</Description>
1414
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-types</PackageProjectUrl>

tests/Tests.CodeOfChaos.Types.DataSeeder/Tests.CodeOfChaos.Types.DataSeeder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1515
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
1616
<PackageReference Include="Moq" Version="4.20.72"/>
17-
<PackageReference Include="TUnit" Version="0.6.154" />
17+
<PackageReference Include="TUnit" Version="0.6.156" />
1818
<PackageReference Include="Bogus" Version="35.6.1"/>
1919
</ItemGroup>
2020

tests/Tests.CodeOfChaos.Types.TypedValueStore/Tests.CodeOfChaos.Types.TypedValueStore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<ItemGroup>
1515
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1616
<PackageReference Include="Moq" Version="4.20.72"/>
17-
<PackageReference Include="TUnit" Version="0.6.154" />
17+
<PackageReference Include="TUnit" Version="0.6.156" />
1818
<PackageReference Include="Bogus" Version="35.6.1"/>
1919
</ItemGroup>
2020

tests/Tests.CodeOfChaos.Types.TypedValueStore/TypedValueStoreTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,45 @@ await Assert.That(store)
379379
Console.WriteLine($"Time to retrieve {numberOfItems} items: {retrievalTime} ms");
380380
}
381381

382+
[Test]
383+
public async Task GetOrAdd_ShouldReturnExistingValue() {
384+
// Arrange
385+
const string key = "testKey";
386+
const string existingValue = "existingValue";
387+
const string newValue = "newValue";
388+
var store = new TypedValueStore();
389+
store.TryAdd(key, existingValue); // Assuming Add is used to prefill values
390+
391+
// Act
392+
// Try to get the value using GetOrAdd, if the key exists it should return the existing value
393+
var result = store.GetOrAdd(key, () => newValue);
394+
395+
// Assert
396+
await Assert.That(result).IsEqualTo(existingValue).Because("Expected the existing value to be returned.");
397+
}
398+
399+
[Test]
400+
public async Task GetOrAdd_ShouldReturnNewValue()
401+
{
402+
// Arrange: Create an instance of TypedValueStore.
403+
var store = new TypedValueStore();
404+
405+
// Define a key that does not initially exist in the store.
406+
const string key = "newKey";
407+
const string expectedValue = "newValue";
408+
409+
// Act: Call GetOrAdd with the new key and a factory returning the desired value.
410+
string actualValue = store.GetOrAdd(key, () => expectedValue);
411+
412+
// Assert: Verify that the returned value is the newly added value.
413+
await Assert.That(actualValue).IsEqualTo(expectedValue).Because("Expected the newly added value to be returned.");
414+
415+
// Assert: Verify that the value is now actually stored and can be fetched by the key.
416+
bool isKeyPresent = store.TryGetValue(key, out string? retrievedValue);
417+
await Assert.That(isKeyPresent).IsTrue().Because("The key should be present after GetOrAdd.");
418+
await Assert.That(retrievedValue).IsNotNullOrEmpty().And.IsEqualTo(expectedValue).Because("The stored value should match the value returned by GetOrAdd.");
419+
}
420+
382421
#region TryAdd_ShouldAddNewItem
383422
private static async Task TryAdd_ShouldAddNewItem<T>(string key, T value) where T : notnull {
384423
// Arrange

tests/Tests.CodeOfChaos.Types/Tests.CodeOfChaos.Types.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ItemGroup>
1414
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1515
<PackageReference Include="Moq" Version="4.20.72"/>
16-
<PackageReference Include="TUnit" Version="0.6.154" />
16+
<PackageReference Include="TUnit" Version="0.6.156" />
1717
<PackageReference Include="Bogus" Version="35.6.1"/>
1818
</ItemGroup>
1919

0 commit comments

Comments
 (0)