flame icon indicating copy to clipboard operation
flame copied to clipboard

Testing if `Forge2DGame` has a given callback.

Open alestiago opened this issue 3 years ago • 5 comments

What could be improved

Include "something " (see Possible Solutions) that allows testing if a given Forge2DGame has registered a given ContactCallback.

Why should this be improved

To allow writing tests that ensure a given ContactCallback has been registered.

Any risks?

No

More information

Possible Solutions

  1. Make _contactCallbacks in Forge2DGame and _callbacks in ContactCallbacks public with @visibeForTesting
  2. Define a method in Forge2DGame that allows checking if a callback is registered (hasCallback). Defining the same method for ContactCallbacks.
  3. Exposing _callbacks from ContactCallbacks and contactCallbacks from Forge2DGame entirely and deprecating register, deregister and clear since it can be done with add, remove and `clear.
  4. Exposing a getter with an UnmodifiableListView (read-only list) in both ContactCallbacks and Forge2DGame.

Please if you have other possible solutions consider joining the issue. Or even if you have a preference for one of the above also join this issue. Any contribution is helpful!


I am highly interested in working on a PR for this.

alestiago avatar Mar 02 '22 19:03 alestiago

We should go over #1322 and see if that is feasible first, so that the tests aren't in vane. Are you interested in taking over #1322? What is left is basically convert the examples to the new method, update the docs and write some tests for it.

spydon avatar Mar 02 '22 22:03 spydon

@alestiago just double checking, are you interested in taking #1322? It's of course totally fine if you are not. :slightly_smiling_face:

spydon avatar Mar 14 '22 16:03 spydon

I might be interested @spydon, do you know why #1322 is still open? Is this only due to required doc changes, tests and feasibility?

alestiago avatar Mar 24 '22 22:03 alestiago

I might be interested @spydon, do you know why #1322 is still open? Is this only due to required doc changes, tests and feasibility?

Yes, but that "only" might be pretty big. 😄 We should deprecate the other way too.

spydon avatar Mar 24 '22 23:03 spydon

One can test a Forge2DGame has a given callback with something like:

void beginContact(Forge2DGame game, BodyComponent bodyA, BodyComponent bodyB) {
  assert(
    bodyA.body.fixtures.isNotEmpty && bodyB.body.fixtures.isNotEmpty,
    'Bodies require fixtures to contact each other.',
  );

  final fixtureA = bodyA.body.fixtures.first;
  final fixtureB = bodyB.body.fixtures.first;
  final contact = Contact.init(fixtureA, 0, fixtureB, 0);
  game.world.contactManager.contactListener?.beginContact(contact);
}

And then testing that the side effects are matched.

alestiago avatar Apr 14 '22 02:04 alestiago