Skip to content

Commit

Permalink
add tests (closes #34)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadako committed Dec 17, 2024
1 parent 943c35a commit 0865a05
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/TinkState/Tests/TestDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,36 @@ string s(IReadOnlyDictionary<int, string> d)
Assert.That(auto.Value, Is.EqualTo("0-!,1-a,3-c"));
}

[Test]
public void TestObserveAndDictionaryUsedInAuto()
{
var dict = Observable.Dictionary<int, string>();
dict[1] = "a";
dict[2] = "b";
var dictAsObservable = dict.Observe();

List<string> Sorted(IEnumerable<string> values)
{
var list = new List<string>(values);
list.Sort();
return list;
}

var auto = Observable.Auto(() =>
{
var a = string.Join(',', Sorted(dict.Values.ToArray()));
var b = string.Join('.', Sorted(dictAsObservable.Value.Values));
return $"{a}-{b}";
});

Assert.That(auto.Value, Is.EqualTo("a,b-a.b"));

dict[1] = "A";
dict[2] = "B";
dict[3] = "C";
Assert.That(auto.Value, Is.EqualTo("A,B,C-A.B.C"));
}

void Helper<R>(Func<R> compute, R initialExpectedValue, Action<Action<R, Action>> tests)
{
var auto = Observable.Auto(compute);
Expand Down
24 changes: 24 additions & 0 deletions src/TinkState/Tests/TestList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ public void TestObserve()
Assert.That(auto.Value, Is.EqualTo("0,1,2,3"));
}


[Test]
public void TestObserveAndListUsedInAuto()
{
var list = Observable.List<string>();
list.Add("a");
list.Add("b");
var listAsObservable = list.Observe();

var auto = Observable.Auto(() =>
{
var a = string.Join(',', list);
var b = string.Join('.', listAsObservable.Value);
return $"{a}-{b}";
});

Assert.That(auto.Value, Is.EqualTo("a,b-a.b"));

list[0] = "A";
list[1] = "B";
list.Add("C");
Assert.That(auto.Value, Is.EqualTo("A,B,C-A.B.C"));
}

void Helper<R>(Func<R> compute, R initialExpectedValue, Action<Action<R, Action>> tests)
{
var auto = Observable.Auto(compute);
Expand Down

0 comments on commit 0865a05

Please sign in to comment.