Skip to content

Commit

Permalink
Merge pull request #15 from chickensoft-games/fix/initialize-once
Browse files Browse the repository at this point in the history
fix: only initialize once
  • Loading branch information
jolexxa authored Jul 24, 2024
2 parents c29c892 + 4d5283b commit 6ce2426
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 6ce2426

Please sign in to comment.