Skip to content

Commit

Permalink
fix: restore some missing properties for context (#246)
Browse files Browse the repository at this point in the history
- Keys for ContextVariables returning all keys
- Value for ContextObject
  • Loading branch information
Seddryck committed Jan 6, 2024
1 parent beb20f1 commit 27dd2da
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
57 changes: 57 additions & 0 deletions Expressif.Testing/ContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,53 @@ public void VariableTryGetValue_FooExisting_CorrectResult(string name, bool expe
});
}

[Test]
public void VariableCount_AddRemove_CorrectResult()
{
var context = new Context(new() { { "foo", "123" } });
Assert.That(context.Variables.Count, Is.EqualTo(1));

context.Variables.Add<string>("bar", "456");
Assert.That(context.Variables.Count, Is.EqualTo(2));

context.Variables.Remove("foo");
Assert.That(context.Variables.Count, Is.EqualTo(1));
context.Variables.Remove("bar");
Assert.That(context.Variables.Count, Is.EqualTo(0));
}

[Test]
public void VariableKeys_AddRemove_CorrectResult()
{
var context = new Context(new() { { "foo", "123" } });
Assert.Multiple(() =>
{
Assert.That(context.Variables.Keys, Does.Contain("foo"));
Assert.That(context.Variables.Keys, Does.Not.Contain("bar"));
});

context.Variables.Add<string>("bar", "456");
Assert.Multiple(() =>
{
Assert.That(context.Variables.Keys, Does.Contain("foo"));
Assert.That(context.Variables.Keys, Does.Contain("bar"));
});

context.Variables.Remove("foo");
Assert.Multiple(() =>
{
Assert.That(context.Variables.Keys, Does.Not.Contain("foo"));
Assert.That(context.Variables.Keys, Does.Contain("bar"));
});

context.Variables.Remove("bar");
Assert.Multiple(() =>
{
Assert.That(context.Variables.Keys, Does.Not.Contain("foo"));
Assert.That(context.Variables.Keys, Does.Not.Contain("bar"));
});
}

[Test]
public void CurrentObjectName_DictionaryWithExistingKey_KeyReturned()
{
Expand Down Expand Up @@ -359,4 +406,14 @@ public void CurrentObjectIndexTryGetValue_IndexExisting_CorrectResult(int index,
Assert.That(result, Is.Null);
});
}

[Test]
public void CurrentObjectValue_Any_CorrectResult()
{
var context = new Context();
Assert.That(context.CurrentObject.Value, Is.Null);

context.CurrentObject.Set(new List<int>() { 123, 456 });
Assert.That(context.CurrentObject.Value, Is.AssignableTo<IList<int>>());
}
}
2 changes: 1 addition & 1 deletion Expressif/Values/ContextObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Expressif.Values;

public class ContextObject
{
protected object? Value { get; private set; }
public object? Value { get; private set; }
private PropertyInfo[]? Cache { get; set; }

public void Set(object value)
Expand Down
4 changes: 3 additions & 1 deletion Expressif/Values/ContextVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void Remove(string name)
Variables.Remove(name);
}

public int Count { get => Variables.Count; }
public int Count => Variables.Count;

public ICollection<string> Keys => Variables.Keys;

public object? this[string name]
=> Variables.TryGetValue(name.StartsWith('@') ? name[1..] : name, out var value)
Expand Down

0 comments on commit 27dd2da

Please sign in to comment.