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

Proposal: Make Plugins Testable #280

Open
yonilerner opened this issue Jan 21, 2024 · 3 comments
Open

Proposal: Make Plugins Testable #280

yonilerner opened this issue Jan 21, 2024 · 3 comments
Milestone

Comments

@yonilerner
Copy link

yonilerner commented Jan 21, 2024

Hey friends, I have a sorta insane idea I want to run by you. Ive been working on my plugin with CounterStrikeSharp and its great, but I do most of my development on macos, so Ive been organizing my code so that much of it can run outside of the game server environment so i can test it on my mac.

My goal was to make my plugin testable without having to run a server. Both because of my personal environment limitations, but also because I think having plugins be testable in general will make them more reliable and improve the quality of plugins.

So I started playing with the idea of having everything that interacts with a server be wrapped in interfaces that would allow my plugin code to be 95% runnable outside the server, with a default implementation of each interface of course requiring the server. And then separately you could have test implementations to run your logic in a testable environment without the server present.

This PR has a proof of concept of the interfaces and a concrete implementation, but no test implementation yet yonilerner/cs2-retakes-allocator#73. Because I cant force CSS classes to implement my interfaces, I have to rewrap everything. Basically having every class (or at least the big ones like CCSPlayerController to start) implement an interface (eg. ICCSPlayerController) that defines all the methods and fields but would then allow tests to mock any of them (saves all the wrapping hassle).

Here are some key lines of code from my example PR that demonstrates the idea:

This is of course not the only way to make plugins testable, just the one I started playing around with. Before I went further on this work, I wanted to pose to the community: Is there general interest in making plugins (and even CounterStrikeSharp itself) more testable? Is this something that people would find value including in CounterStrikeSharp by default, so that any plugin - and CounterStrikeSharp internals - can be easily tested without having to jump through the hoops I did?

Let me know what you think. Thanks!

@B3none
Copy link
Collaborator

B3none commented Jan 22, 2024

An interface for the CCSPlayerController would help me write unit tests for the Menus as I'm struggling to mock that class without it. 👍

@roflmuffin
Copy link
Owner

I have a draft PR #331 which auto-generates interfaces for schema classes; though how we can update the core to use these interfaces without breaking backwards compat is up in the air at this point.

@yonilerner
Copy link
Author

Oh this is incredible, Im gonna poke around next time I have a chance. Thank you!

@roflmuffin roflmuffin added this to the Future milestone Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants