Skip to content

Commit

Permalink
fix: only initialize once
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Jul 24, 2024
1 parent c29c892 commit 4d5283b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Chickensoft.AutoInject.Tests/src/auto_node/AutoNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void IMixin<IAutoNode>.Handler() { }
new void Handler() {
// IAutoOn isn't called since its handler does nothing.
(this as IAutoConnect).Handler();
(this as IAutoInit).Handler();
// IDependent invokes IAutoInit, so we don't invoke it directly.
(this as IProvider).Handler();
(this as IDependent).Handler();
}
Expand Down
13 changes: 11 additions & 2 deletions Chickensoft.AutoInject.Tests/test/fixtures/AutoSetupTestNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ namespace Chickensoft.AutoInject.Tests.Fixtures;
public partial class AutoInitTestNode : Node2D {
public override void _Notification(int what) => this.Notify(what);

public bool SetupCalled { get; set; }
public int Called { get; set; }

public void Initialize() => SetupCalled = true;
public void Initialize() => Called++;
}

[Meta(typeof(IAutoNode))]
public partial class AutoInitTestAutoNode : Node2D {
public override void _Notification(int what) => this.Notify(what);

public int Called { get; set; }

public void Initialize() => Called++;
}

[Meta(typeof(IAutoInit))]
Expand Down
13 changes: 12 additions & 1 deletion Chickensoft.AutoInject.Tests/test/src/AutoInitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void SetsUpNode() {

node._Notification((int)Node.NotificationReady);

node.SetupCalled.ShouldBeTrue();
node.Called.ShouldBe(1);
}

[Test]
Expand All @@ -29,11 +29,22 @@ public void DefaultImplementationDoesNothing() {
public void IsTestingCreatesStateIfSetFirst() {
var node = new AutoInitTestNode();
(node as IAutoInit).IsTesting = true;
// Should do nothing on a non-ready notification
node._Notification((int)Node.NotificationEnterTree);
}

[Test]
public void HandlerDoesNotWorkIfNotGodotNode() => Should.NotThrow(() => {
var node = new NotAGodotNode();
(node as IAutoInit).Handler();
});

[Test]
public void AutoNodeMixinOnlyCallsInitializeOnce() {
var node = new AutoInitTestAutoNode();

node._Notification((int)Node.NotificationReady);

node.Called.ShouldBe(1);
}
}

0 comments on commit 4d5283b

Please sign in to comment.