Skip to content

Commit

Permalink
feat: if a variable is a Func<T> then it's invoked when accessed. (#259)
Browse files Browse the repository at this point in the history
* feat: if a variable is a Func<T> then it's invoked when accessed.
  • Loading branch information
Seddryck authored Jan 13, 2024
1 parent 51aa837 commit a12482f
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,151 +3,12 @@
using NUnit.Framework.Constraints;
using System.Data;

namespace Expressif.Testing;
namespace Expressif.Testing.Values;

public class ContextTest
public class ContextCurrentObjectTest
{
[SetUp]
public void Setup()
{ }

[Test]
[TestCase("foo")]
[TestCase("@foo")]
public void VariableAdd_OneVariable_CanBeRetrieved(string name)
{
var context = new Context();
Assert.That(context.Variables, Has.Count.EqualTo(0));
context.Variables.Add<string>(name, "123");
Assert.That(context.Variables, Has.Count.EqualTo(1));
Assert.Multiple(() =>
{
Assert.That(context.Variables.Contains(name), Is.True);
Assert.That(context.Variables.Contains(name.Replace("@", "")), Is.True);
});
}

[Test]
[TestCase("foo", "foo")]
[TestCase("@foo", "@foo")]
[TestCase("@foo", "foo")]
[TestCase("foo", "@foo")]
public void VariableAdd_TwiceTheSameVariable_ThrowException(string name1, string name2)
{
var context = new Context();
context.Variables.Add<string>(name1, "123");
Assert.That(() => context.Variables.Add<string>(name2, "456"), Throws.TypeOf<VariableAlreadyExistingException>());
}

[Test]
[TestCase("foo", "foo")]
[TestCase("@foo", "@foo")]
[TestCase("@foo", "foo")]
[TestCase("foo", "@foo")]
public void VariableSet_TwiceTheSameVariable_FinalValue(string name1, string name2)
{
var context = new Context();
Assert.That(context.Variables, Has.Count.EqualTo(0));
context.Variables.Set(name1, "123");
Assert.That(context.Variables, Has.Count.EqualTo(1));
context.Variables.Set(name2, "456");
Assert.That(context.Variables, Has.Count.EqualTo(1));
Assert.That(context.Variables[name1], Is.EqualTo("456"));
}

[Test]
[TestCase("foo", "foo")]
[TestCase("@foo", "@foo")]
[TestCase("@foo", "foo")]
[TestCase("foo", "@foo")]
public void VariableRemove_TwiceTheSameVariable_NoVariableRemaining(string name1, string name2)
{
var context = new Context();
Assert.That(context.Variables, Has.Count.EqualTo(0));
context.Variables.Set(name1, "123");
Assert.That(context.Variables, Has.Count.EqualTo(1));
context.Variables.Remove(name2);
Assert.That(context.Variables, Has.Count.EqualTo(0));
}

[Test]
[TestCase("foo", true)]
[TestCase("@foo", true)]
[TestCase("@bar", false)]
[TestCase("bar", false)]
public void VariableContains_FooExisting_CorrectResult(string name, bool expected)
{
var context = new Context(new() { { "foo", "123" } });
Assert.That(context.Variables.Contains(name), Is.EqualTo(expected));
}

[Test]
[TestCase("foo", true)]
[TestCase("@foo", true)]
[TestCase("@bar", false)]
[TestCase("bar", false)]
public void VariableTryGetValue_FooExisting_CorrectResult(string name, bool expected)
{
var context = new Context(new() { { "foo", "123" } });
Assert.Multiple(() =>
{
Assert.That(context.Variables.TryGetValue(name, out var result), Is.EqualTo(expected));
if (expected)
Assert.That(result, Is.EqualTo("123"));
else
Assert.That(result, Is.Null);
});
}

[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()
public void Name_DictionaryWithExistingKey_KeyReturned()
{
var context = new Context();
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 }, { "bar", 456 } });
Expand All @@ -164,7 +25,7 @@ public void CurrentObjectName_DictionaryWithExistingKey_KeyReturned()
}

[Test]
public void CurrentObjectName_ObjectWithExistingProperty_ValueReturned()
public void Name_ObjectWithExistingProperty_ValueReturned()
{
var context = new Context();
context.CurrentObject.Set(new { foo = 123, bar = 456 });
Expand All @@ -181,7 +42,7 @@ public void CurrentObjectName_ObjectWithExistingProperty_ValueReturned()
}

[Test]
public void CurrentObjectName_DataRowWithExistingColumn_ValueReturned()
public void Name_DataRowWithExistingColumn_ValueReturned()
{
var dt = new DataTable();
dt.Columns.Add("foo", typeof(int));
Expand All @@ -204,7 +65,7 @@ public void CurrentObjectName_DataRowWithExistingColumn_ValueReturned()
}

[Test]
public void CurrentObjectName_DictionaryWithUnavailableKey_ThrowsException()
public void Name_DictionaryWithUnavailableKey_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 } });
Expand All @@ -221,7 +82,7 @@ public void CurrentObjectName_DictionaryWithUnavailableKey_ThrowsException()
}

[Test]
public void CurrentObjectName_AnonymousObjectWithUnavailableProperty_ThrowsException()
public void Name_AnonymousObjectWithUnavailableProperty_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new { foo = 123 });
Expand All @@ -234,13 +95,13 @@ public void CurrentObjectName_AnonymousObjectWithUnavailableProperty_ThrowsExcep
{
Assert.That(() => context.CurrentObject["foo"], Throws.Nothing);
Assert.That(() => context.CurrentObject["bar"], Throws.TypeOf<ArgumentException>()
.With.Message.EqualTo("Cannot find a property named 'bar' in the object of type '<>f__AnonymousType1`1'."));
.With.Message.StartsWith("Cannot find a property named 'bar' in the object of type '<>f__AnonymousType"));
});
}

private record ObjectTest(string Name) { }
[Test]
public void CurrentObjectName_ObjectWithUnavailableProperty_ThrowsException()
public void Name_ObjectWithUnavailableProperty_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new ObjectTest("foo"));
Expand All @@ -253,7 +114,7 @@ public void CurrentObjectName_ObjectWithUnavailableProperty_ThrowsException()
}

[Test]
public void CurrentObjectName_ObjectNull_ThrowsException()
public void Name_ObjectNull_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(null);
Expand All @@ -269,7 +130,7 @@ public void CurrentObjectName_ObjectNull_ThrowsException()
}

[Test]
public void CurrentObjectName_DataRowWithUnavailableColumn_ThrowsException()
public void Name_DataRowWithUnavailableColumn_ThrowsException()
{
var dt = new DataTable();
dt.Columns.Add("foo", typeof(int));
Expand All @@ -291,7 +152,7 @@ public void CurrentObjectName_DataRowWithUnavailableColumn_ThrowsException()
}

[Test]
public void CurrentObjectName_List_ThrowsException()
public void Name_List_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new List<int> { 123 });
Expand All @@ -305,7 +166,7 @@ public void CurrentObjectName_List_ThrowsException()
[Test]
[TestCase("foo", true)]
[TestCase("bar", false)]
public void CurrentObjectNameTryGetValue_NameExisting_CorrectResult(string name, bool expected)
public void NameTryGetValue_NameExisting_CorrectResult(string name, bool expected)
{
var context = new Context();
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 } });
Expand All @@ -320,7 +181,7 @@ public void CurrentObjectNameTryGetValue_NameExisting_CorrectResult(string name,
}

[Test]
public void CurrentObjectIndex_ListWithExistingIndex_ValueReturned()
public void Index_ListWithExistingIndex_ValueReturned()
{
var context = new Context();
context.CurrentObject.Set(new List<int>() { 123, 456 });
Expand All @@ -337,7 +198,7 @@ public void CurrentObjectIndex_ListWithExistingIndex_ValueReturned()
}

[Test]
public void CurrentObjectIndex_DataRowWithExistingColumn_ValueReturned()
public void Index_DataRowWithExistingColumn_ValueReturned()
{
var dt = new DataTable();
dt.Columns.Add("foo", typeof(int));
Expand All @@ -360,7 +221,7 @@ public void CurrentObjectIndex_DataRowWithExistingColumn_ValueReturned()
}

[Test]
public void CurrentObjectIndex_ListWithUnavailableIndex_ThrowsException()
public void Index_ListWithUnavailableIndex_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new List<int>() { 123 });
Expand All @@ -377,7 +238,7 @@ public void CurrentObjectIndex_ListWithUnavailableIndex_ThrowsException()
}

[Test]
public void CurrentObjectIndex_DataRowWithUnavailableColumn_ThrowsException()
public void Index_DataRowWithUnavailableColumn_ThrowsException()
{
var dt = new DataTable();
dt.Columns.Add("foo", typeof(int));
Expand All @@ -399,7 +260,7 @@ public void CurrentObjectIndex_DataRowWithUnavailableColumn_ThrowsException()
}

[Test]
public void CurrentObjectIndex_Object_ThrowsException()
public void Index_Object_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new { foo = 123 });
Expand All @@ -411,7 +272,7 @@ public void CurrentObjectIndex_Object_ThrowsException()
}

[Test]
public void CurrentObjectIndex_Dictionary_ThrowsException()
public void Index_Dictionary_ThrowsException()
{
var context = new Context();
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 }, { "bar", 456 } });
Expand All @@ -425,7 +286,7 @@ public void CurrentObjectIndex_Dictionary_ThrowsException()
[Test]
[TestCase(0, true)]
[TestCase(100, false)]
public void CurrentObjectIndexTryGetValue_IndexExisting_CorrectResult(int index, bool expected)
public void IndexTryGetValue_IndexExisting_CorrectResult(int index, bool expected)
{
var context = new Context();
context.CurrentObject.Set(new List<int>() { 123, 456 });
Expand All @@ -440,7 +301,7 @@ public void CurrentObjectIndexTryGetValue_IndexExisting_CorrectResult(int index,
}

[Test]
public void CurrentObjectValue_Any_CorrectResult()
public void Value_Any_CorrectResult()
{
var context = new Context();
Assert.That(context.CurrentObject.Value, Is.Null);
Expand All @@ -463,7 +324,7 @@ private class DataRowWrapper(DataRow row) : ILiteDataRow
}

[Test]
public void CurrentObjectIndex_IReadOnlyDataRowWithUnavailableColumn_ThrowsException()
public void Index_IReadOnlyDataRowWithUnavailableColumn_ThrowsException()
{
var dt = new DataTable();
dt.Columns.Add("foo", typeof(int));
Expand All @@ -487,7 +348,7 @@ public void CurrentObjectIndex_IReadOnlyDataRowWithUnavailableColumn_ThrowsExcep
}

[Test]
public void CurrentObjectName_IReadOnlyDataRowWithExistingColumn_ValueReturned()
public void Name_IReadOnlyDataRowWithExistingColumn_ValueReturned()
{
var dt = new DataTable();
dt.Columns.Add("foo", typeof(int));
Expand Down
Loading

0 comments on commit a12482f

Please sign in to comment.