Skip to content

Commit

Permalink
Merge pull request #69 from xodial/xodial/entity-table-set
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa authored Feb 16, 2025
2 parents 1e5f0c7 + bd9634e commit 8849473
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Chickensoft.Collections.Tests/src/EntityTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void Initializes() {
}

[Fact]
public void StoresValues() {
public void SetStoresValuesAndOverwritesExistingValues() {
var table = new EntityTable();

table.Set("a", "one");
Expand All @@ -20,13 +20,25 @@ public void StoresValues() {
table.Get<string>("a").ShouldBe("one");
table.Get<object>("b").ShouldNotBeNull();

table.Set("a", "two");
table.Get<string>("a").ShouldBe("two");

table.Remove("a");
table.Remove(null);

table.Get<string>("a").ShouldBeNull();
table.Get<object>("b").ShouldNotBeNull();
}

[Fact]
public void TryAddOnlyStoresValuesForNewKeys() {
var table = new EntityTable();

table.TryAdd("a", "one").ShouldBeTrue();
table.TryAdd("a", "two").ShouldBeFalse();
table.Get<string>("a").ShouldBe("one");
}

[Fact]
public void Clears() {
var table = new EntityTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ public class EntityTable<TId> where TId : notnull {
/// </summary>
/// <param name="id">Entity id.</param>
/// <param name="entity">Entity object.</param>
public void Set(TId id, object entity) => _entities.TryAdd(id, entity);
public void Set(TId id, object entity) => _entities[id] = entity;

/// <summary>
/// Attempts to add an entity to the table returning true if successful.
/// </summary>
/// <param name="id">Entity id.</param>
/// <param name="entity">Entity object.</param>
/// <returns>`true` if the entity was added, `false` otherwise.</returns>
public bool TryAdd(TId id, object entity) => _entities.TryAdd(id, entity);

/// <summary>
/// Remove an entity from the table.
Expand Down

0 comments on commit 8849473

Please sign in to comment.