-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
483 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
Chickensoft.GoDotCollections.Tests/test/src/AutoPropTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
namespace Chickensoft.GoDotCollections.Tests; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Godot; | ||
using GoDotCollections; | ||
using GoDotTest; | ||
using Shouldly; | ||
|
||
public class NotifierTest : TestClass { | ||
public NotifierTest(Node testScene) : base(testScene) { } | ||
|
||
public static class Utils { | ||
public static void ClearWeakReference(WeakReference weakReference) { | ||
weakReference.Target = null; | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
} | ||
|
||
public static WeakReference CreateWeakReference() => new( | ||
new AutoProp<int>(1) | ||
); | ||
|
||
[Test] | ||
public void Initializes() { | ||
var subject = new AutoProp<int>(1); | ||
subject.Value.ShouldBe(1); | ||
} | ||
|
||
[Test] | ||
public void InitializesWithComparer() { | ||
var subject = new AutoProp<int>(1, EqualityComparer<int>.Default); | ||
subject.Value.ShouldBe(1); | ||
subject.Comparer.ShouldBe(EqualityComparer<int>.Default); | ||
} | ||
|
||
[Test] | ||
public void SyncCallsHandlerImmediatelyAndAllowsUnsubscribe() { | ||
using var subject = new AutoProp<int>(1); | ||
|
||
var changedCalled = 0; | ||
var syncCalled = 0; | ||
|
||
void onSync(int value) { | ||
value.ShouldBe(1); | ||
syncCalled++; | ||
} | ||
|
||
subject.Changed += (value) => changedCalled++; | ||
subject.Sync += onSync; | ||
|
||
changedCalled.ShouldBe(0); | ||
syncCalled.ShouldBe(1); | ||
|
||
subject.Sync -= onSync; | ||
subject.OnNext(2); | ||
|
||
changedCalled.ShouldBe(1); | ||
} | ||
|
||
[Test] | ||
public void ClearsEventHandlers() { | ||
using var subject = new AutoProp<int>(1); | ||
|
||
var changedCalled = 0; | ||
var syncCalled = 0; | ||
var completedCalled = 0; | ||
var errorCalled = 0; | ||
|
||
subject.Changed += (value) => changedCalled++; | ||
subject.Sync += (value) => syncCalled++; | ||
subject.Completed += () => completedCalled++; | ||
subject.Error += (exception) => errorCalled++; | ||
|
||
subject.Clear(); | ||
|
||
subject.OnNext(2); | ||
subject.OnCompleted(); | ||
subject.OnError(new InvalidOperationException()); | ||
|
||
changedCalled.ShouldBe(0); | ||
syncCalled.ShouldBe(1); | ||
completedCalled.ShouldBe(0); | ||
errorCalled.ShouldBe(0); | ||
} | ||
|
||
[Test] | ||
public void CompletesAndBlocksOtherActions() { | ||
using var subject = new AutoProp<int>(1); | ||
|
||
var changedCalled = 0; | ||
var syncCalled = 0; | ||
var completedCalled = 0; | ||
var errorCalled = 0; | ||
|
||
subject.Changed += (value) => changedCalled++; | ||
subject.Sync += (value) => syncCalled++; | ||
subject.Completed += () => completedCalled++; | ||
subject.Error += (exception) => errorCalled++; | ||
|
||
subject.OnCompleted(); | ||
subject.OnCompleted(); | ||
subject.OnNext(2); | ||
subject.Value.ShouldBe(1); | ||
subject.OnError(new InvalidOperationException()); | ||
|
||
changedCalled.ShouldBe(0); | ||
syncCalled.ShouldBe(1); | ||
completedCalled.ShouldBe(1); | ||
errorCalled.ShouldBe(0); | ||
} | ||
|
||
[Test] | ||
public void CallsErrorHandler() { | ||
using var subject = new AutoProp<int>(1); | ||
|
||
var errorCalled = 0; | ||
|
||
void onError(Exception exception) { | ||
exception.ShouldBeOfType<InvalidOperationException>(); | ||
errorCalled++; | ||
} | ||
|
||
subject.Error += onError; | ||
|
||
subject.OnError(new InvalidOperationException()); | ||
errorCalled.ShouldBe(1); | ||
|
||
subject.Error -= onError; | ||
|
||
subject.OnError(new InvalidOperationException()); | ||
errorCalled.ShouldBe(1); | ||
} | ||
|
||
[Test] | ||
public void DisposesCorrectly() { | ||
var subject = new AutoProp<int>(1); | ||
|
||
var changedCalled = 0; | ||
var syncCalled = 0; | ||
var completedCalled = 0; | ||
var errorCalled = 0; | ||
|
||
subject.Changed += (value) => changedCalled++; | ||
subject.Sync += (value) => syncCalled++; | ||
subject.Completed += () => completedCalled++; | ||
subject.Error += (exception) => errorCalled++; | ||
|
||
subject.Dispose(); | ||
|
||
subject.OnNext(2); | ||
subject.OnCompleted(); | ||
subject.OnError(new InvalidOperationException()); | ||
subject.Dispose(); | ||
|
||
changedCalled.ShouldBe(0); | ||
syncCalled.ShouldBe(1); | ||
completedCalled.ShouldBe(0); | ||
errorCalled.ShouldBe(0); | ||
} | ||
|
||
[Test] | ||
public void Finalizes() { | ||
// Weak reference has to be created and cleared from a static function | ||
// or else the GC won't ever collect it :P | ||
var subject = CreateWeakReference(); | ||
Utils.ClearWeakReference(subject); | ||
} | ||
|
||
[Test] | ||
public void DoesNotCallHandlersIfValueHasNotChanged() { | ||
var subject = new AutoProp<int>(1); | ||
|
||
var changedCalled = 0; | ||
var syncCalled = 0; | ||
|
||
subject.Changed += (value) => changedCalled++; | ||
subject.Sync += (value) => syncCalled++; | ||
|
||
subject.OnNext(2); | ||
subject.OnNext(2); | ||
|
||
changedCalled.ShouldBe(1); | ||
syncCalled.ShouldBe(2); | ||
} | ||
|
||
[Test] | ||
public void DoesNotCallHandlerWhileInsideHandler() { | ||
var subject = new AutoProp<int>(1); | ||
|
||
var changes = new List<int>(); | ||
var syncs = new List<int>(); | ||
|
||
subject.Changed += (value) => { | ||
changes.Add(value); | ||
subject.OnNext(3); | ||
}; | ||
subject.Sync += (value) => syncs.Add(value); | ||
|
||
subject.OnNext(2); | ||
|
||
changes.ShouldBe(new[] { 2, 3 }); | ||
syncs.ShouldBe(new[] { 1, 2, 3 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Chickensoft.GoDotCollections.Tests")] |
Oops, something went wrong.