Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only initialize once #15

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading