Skip to content

Commit a12482f

Browse files
authored
feat: if a variable is a Func<T> then it's invoked when accessed. (#259)
* feat: if a variable is a Func<T> then it's invoked when accessed.
1 parent 51aa837 commit a12482f

File tree

3 files changed

+218
-165
lines changed

3 files changed

+218
-165
lines changed

Expressif.Testing/ContextTest.cs renamed to Expressif.Testing/Values/ContextCurrentObjectTest.cs

Lines changed: 23 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -3,151 +3,12 @@
33
using NUnit.Framework.Constraints;
44
using System.Data;
55

6-
namespace Expressif.Testing;
6+
namespace Expressif.Testing.Values;
77

8-
public class ContextTest
8+
public class ContextCurrentObjectTest
99
{
10-
[SetUp]
11-
public void Setup()
12-
{ }
13-
14-
[Test]
15-
[TestCase("foo")]
16-
[TestCase("@foo")]
17-
public void VariableAdd_OneVariable_CanBeRetrieved(string name)
18-
{
19-
var context = new Context();
20-
Assert.That(context.Variables, Has.Count.EqualTo(0));
21-
context.Variables.Add<string>(name, "123");
22-
Assert.That(context.Variables, Has.Count.EqualTo(1));
23-
Assert.Multiple(() =>
24-
{
25-
Assert.That(context.Variables.Contains(name), Is.True);
26-
Assert.That(context.Variables.Contains(name.Replace("@", "")), Is.True);
27-
});
28-
}
29-
30-
[Test]
31-
[TestCase("foo", "foo")]
32-
[TestCase("@foo", "@foo")]
33-
[TestCase("@foo", "foo")]
34-
[TestCase("foo", "@foo")]
35-
public void VariableAdd_TwiceTheSameVariable_ThrowException(string name1, string name2)
36-
{
37-
var context = new Context();
38-
context.Variables.Add<string>(name1, "123");
39-
Assert.That(() => context.Variables.Add<string>(name2, "456"), Throws.TypeOf<VariableAlreadyExistingException>());
40-
}
41-
42-
[Test]
43-
[TestCase("foo", "foo")]
44-
[TestCase("@foo", "@foo")]
45-
[TestCase("@foo", "foo")]
46-
[TestCase("foo", "@foo")]
47-
public void VariableSet_TwiceTheSameVariable_FinalValue(string name1, string name2)
48-
{
49-
var context = new Context();
50-
Assert.That(context.Variables, Has.Count.EqualTo(0));
51-
context.Variables.Set(name1, "123");
52-
Assert.That(context.Variables, Has.Count.EqualTo(1));
53-
context.Variables.Set(name2, "456");
54-
Assert.That(context.Variables, Has.Count.EqualTo(1));
55-
Assert.That(context.Variables[name1], Is.EqualTo("456"));
56-
}
57-
58-
[Test]
59-
[TestCase("foo", "foo")]
60-
[TestCase("@foo", "@foo")]
61-
[TestCase("@foo", "foo")]
62-
[TestCase("foo", "@foo")]
63-
public void VariableRemove_TwiceTheSameVariable_NoVariableRemaining(string name1, string name2)
64-
{
65-
var context = new Context();
66-
Assert.That(context.Variables, Has.Count.EqualTo(0));
67-
context.Variables.Set(name1, "123");
68-
Assert.That(context.Variables, Has.Count.EqualTo(1));
69-
context.Variables.Remove(name2);
70-
Assert.That(context.Variables, Has.Count.EqualTo(0));
71-
}
72-
73-
[Test]
74-
[TestCase("foo", true)]
75-
[TestCase("@foo", true)]
76-
[TestCase("@bar", false)]
77-
[TestCase("bar", false)]
78-
public void VariableContains_FooExisting_CorrectResult(string name, bool expected)
79-
{
80-
var context = new Context(new() { { "foo", "123" } });
81-
Assert.That(context.Variables.Contains(name), Is.EqualTo(expected));
82-
}
83-
84-
[Test]
85-
[TestCase("foo", true)]
86-
[TestCase("@foo", true)]
87-
[TestCase("@bar", false)]
88-
[TestCase("bar", false)]
89-
public void VariableTryGetValue_FooExisting_CorrectResult(string name, bool expected)
90-
{
91-
var context = new Context(new() { { "foo", "123" } });
92-
Assert.Multiple(() =>
93-
{
94-
Assert.That(context.Variables.TryGetValue(name, out var result), Is.EqualTo(expected));
95-
if (expected)
96-
Assert.That(result, Is.EqualTo("123"));
97-
else
98-
Assert.That(result, Is.Null);
99-
});
100-
}
101-
102-
[Test]
103-
public void VariableCount_AddRemove_CorrectResult()
104-
{
105-
var context = new Context(new() { { "foo", "123" } });
106-
Assert.That(context.Variables.Count, Is.EqualTo(1));
107-
108-
context.Variables.Add<string>("bar", "456");
109-
Assert.That(context.Variables.Count, Is.EqualTo(2));
110-
111-
context.Variables.Remove("foo");
112-
Assert.That(context.Variables.Count, Is.EqualTo(1));
113-
context.Variables.Remove("bar");
114-
Assert.That(context.Variables.Count, Is.EqualTo(0));
115-
}
116-
117-
[Test]
118-
public void VariableKeys_AddRemove_CorrectResult()
119-
{
120-
var context = new Context(new() { { "foo", "123" } });
121-
Assert.Multiple(() =>
122-
{
123-
Assert.That(context.Variables.Keys, Does.Contain("foo"));
124-
Assert.That(context.Variables.Keys, Does.Not.Contain("bar"));
125-
});
126-
127-
context.Variables.Add<string>("bar", "456");
128-
Assert.Multiple(() =>
129-
{
130-
Assert.That(context.Variables.Keys, Does.Contain("foo"));
131-
Assert.That(context.Variables.Keys, Does.Contain("bar"));
132-
});
133-
134-
context.Variables.Remove("foo");
135-
Assert.Multiple(() =>
136-
{
137-
Assert.That(context.Variables.Keys, Does.Not.Contain("foo"));
138-
Assert.That(context.Variables.Keys, Does.Contain("bar"));
139-
});
140-
141-
context.Variables.Remove("bar");
142-
Assert.Multiple(() =>
143-
{
144-
Assert.That(context.Variables.Keys, Does.Not.Contain("foo"));
145-
Assert.That(context.Variables.Keys, Does.Not.Contain("bar"));
146-
});
147-
}
148-
14910
[Test]
150-
public void CurrentObjectName_DictionaryWithExistingKey_KeyReturned()
11+
public void Name_DictionaryWithExistingKey_KeyReturned()
15112
{
15213
var context = new Context();
15314
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 }, { "bar", 456 } });
@@ -164,7 +25,7 @@ public void CurrentObjectName_DictionaryWithExistingKey_KeyReturned()
16425
}
16526

16627
[Test]
167-
public void CurrentObjectName_ObjectWithExistingProperty_ValueReturned()
28+
public void Name_ObjectWithExistingProperty_ValueReturned()
16829
{
16930
var context = new Context();
17031
context.CurrentObject.Set(new { foo = 123, bar = 456 });
@@ -181,7 +42,7 @@ public void CurrentObjectName_ObjectWithExistingProperty_ValueReturned()
18142
}
18243

18344
[Test]
184-
public void CurrentObjectName_DataRowWithExistingColumn_ValueReturned()
45+
public void Name_DataRowWithExistingColumn_ValueReturned()
18546
{
18647
var dt = new DataTable();
18748
dt.Columns.Add("foo", typeof(int));
@@ -204,7 +65,7 @@ public void CurrentObjectName_DataRowWithExistingColumn_ValueReturned()
20465
}
20566

20667
[Test]
207-
public void CurrentObjectName_DictionaryWithUnavailableKey_ThrowsException()
68+
public void Name_DictionaryWithUnavailableKey_ThrowsException()
20869
{
20970
var context = new Context();
21071
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 } });
@@ -221,7 +82,7 @@ public void CurrentObjectName_DictionaryWithUnavailableKey_ThrowsException()
22182
}
22283

22384
[Test]
224-
public void CurrentObjectName_AnonymousObjectWithUnavailableProperty_ThrowsException()
85+
public void Name_AnonymousObjectWithUnavailableProperty_ThrowsException()
22586
{
22687
var context = new Context();
22788
context.CurrentObject.Set(new { foo = 123 });
@@ -234,13 +95,13 @@ public void CurrentObjectName_AnonymousObjectWithUnavailableProperty_ThrowsExcep
23495
{
23596
Assert.That(() => context.CurrentObject["foo"], Throws.Nothing);
23697
Assert.That(() => context.CurrentObject["bar"], Throws.TypeOf<ArgumentException>()
237-
.With.Message.EqualTo("Cannot find a property named 'bar' in the object of type '<>f__AnonymousType1`1'."));
98+
.With.Message.StartsWith("Cannot find a property named 'bar' in the object of type '<>f__AnonymousType"));
23899
});
239100
}
240101

241102
private record ObjectTest(string Name) { }
242103
[Test]
243-
public void CurrentObjectName_ObjectWithUnavailableProperty_ThrowsException()
104+
public void Name_ObjectWithUnavailableProperty_ThrowsException()
244105
{
245106
var context = new Context();
246107
context.CurrentObject.Set(new ObjectTest("foo"));
@@ -253,7 +114,7 @@ public void CurrentObjectName_ObjectWithUnavailableProperty_ThrowsException()
253114
}
254115

255116
[Test]
256-
public void CurrentObjectName_ObjectNull_ThrowsException()
117+
public void Name_ObjectNull_ThrowsException()
257118
{
258119
var context = new Context();
259120
context.CurrentObject.Set(null);
@@ -269,7 +130,7 @@ public void CurrentObjectName_ObjectNull_ThrowsException()
269130
}
270131

271132
[Test]
272-
public void CurrentObjectName_DataRowWithUnavailableColumn_ThrowsException()
133+
public void Name_DataRowWithUnavailableColumn_ThrowsException()
273134
{
274135
var dt = new DataTable();
275136
dt.Columns.Add("foo", typeof(int));
@@ -291,7 +152,7 @@ public void CurrentObjectName_DataRowWithUnavailableColumn_ThrowsException()
291152
}
292153

293154
[Test]
294-
public void CurrentObjectName_List_ThrowsException()
155+
public void Name_List_ThrowsException()
295156
{
296157
var context = new Context();
297158
context.CurrentObject.Set(new List<int> { 123 });
@@ -305,7 +166,7 @@ public void CurrentObjectName_List_ThrowsException()
305166
[Test]
306167
[TestCase("foo", true)]
307168
[TestCase("bar", false)]
308-
public void CurrentObjectNameTryGetValue_NameExisting_CorrectResult(string name, bool expected)
169+
public void NameTryGetValue_NameExisting_CorrectResult(string name, bool expected)
309170
{
310171
var context = new Context();
311172
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 } });
@@ -320,7 +181,7 @@ public void CurrentObjectNameTryGetValue_NameExisting_CorrectResult(string name,
320181
}
321182

322183
[Test]
323-
public void CurrentObjectIndex_ListWithExistingIndex_ValueReturned()
184+
public void Index_ListWithExistingIndex_ValueReturned()
324185
{
325186
var context = new Context();
326187
context.CurrentObject.Set(new List<int>() { 123, 456 });
@@ -337,7 +198,7 @@ public void CurrentObjectIndex_ListWithExistingIndex_ValueReturned()
337198
}
338199

339200
[Test]
340-
public void CurrentObjectIndex_DataRowWithExistingColumn_ValueReturned()
201+
public void Index_DataRowWithExistingColumn_ValueReturned()
341202
{
342203
var dt = new DataTable();
343204
dt.Columns.Add("foo", typeof(int));
@@ -360,7 +221,7 @@ public void CurrentObjectIndex_DataRowWithExistingColumn_ValueReturned()
360221
}
361222

362223
[Test]
363-
public void CurrentObjectIndex_ListWithUnavailableIndex_ThrowsException()
224+
public void Index_ListWithUnavailableIndex_ThrowsException()
364225
{
365226
var context = new Context();
366227
context.CurrentObject.Set(new List<int>() { 123 });
@@ -377,7 +238,7 @@ public void CurrentObjectIndex_ListWithUnavailableIndex_ThrowsException()
377238
}
378239

379240
[Test]
380-
public void CurrentObjectIndex_DataRowWithUnavailableColumn_ThrowsException()
241+
public void Index_DataRowWithUnavailableColumn_ThrowsException()
381242
{
382243
var dt = new DataTable();
383244
dt.Columns.Add("foo", typeof(int));
@@ -399,7 +260,7 @@ public void CurrentObjectIndex_DataRowWithUnavailableColumn_ThrowsException()
399260
}
400261

401262
[Test]
402-
public void CurrentObjectIndex_Object_ThrowsException()
263+
public void Index_Object_ThrowsException()
403264
{
404265
var context = new Context();
405266
context.CurrentObject.Set(new { foo = 123 });
@@ -411,7 +272,7 @@ public void CurrentObjectIndex_Object_ThrowsException()
411272
}
412273

413274
[Test]
414-
public void CurrentObjectIndex_Dictionary_ThrowsException()
275+
public void Index_Dictionary_ThrowsException()
415276
{
416277
var context = new Context();
417278
context.CurrentObject.Set(new Dictionary<string, object> { { "foo", 123 }, { "bar", 456 } });
@@ -425,7 +286,7 @@ public void CurrentObjectIndex_Dictionary_ThrowsException()
425286
[Test]
426287
[TestCase(0, true)]
427288
[TestCase(100, false)]
428-
public void CurrentObjectIndexTryGetValue_IndexExisting_CorrectResult(int index, bool expected)
289+
public void IndexTryGetValue_IndexExisting_CorrectResult(int index, bool expected)
429290
{
430291
var context = new Context();
431292
context.CurrentObject.Set(new List<int>() { 123, 456 });
@@ -440,7 +301,7 @@ public void CurrentObjectIndexTryGetValue_IndexExisting_CorrectResult(int index,
440301
}
441302

442303
[Test]
443-
public void CurrentObjectValue_Any_CorrectResult()
304+
public void Value_Any_CorrectResult()
444305
{
445306
var context = new Context();
446307
Assert.That(context.CurrentObject.Value, Is.Null);
@@ -463,7 +324,7 @@ private class DataRowWrapper(DataRow row) : ILiteDataRow
463324
}
464325

465326
[Test]
466-
public void CurrentObjectIndex_IReadOnlyDataRowWithUnavailableColumn_ThrowsException()
327+
public void Index_IReadOnlyDataRowWithUnavailableColumn_ThrowsException()
467328
{
468329
var dt = new DataTable();
469330
dt.Columns.Add("foo", typeof(int));
@@ -487,7 +348,7 @@ public void CurrentObjectIndex_IReadOnlyDataRowWithUnavailableColumn_ThrowsExcep
487348
}
488349

489350
[Test]
490-
public void CurrentObjectName_IReadOnlyDataRowWithExistingColumn_ValueReturned()
351+
public void Name_IReadOnlyDataRowWithExistingColumn_ValueReturned()
491352
{
492353
var dt = new DataTable();
493354
dt.Columns.Add("foo", typeof(int));

0 commit comments

Comments
 (0)