Writing unit tests for pygame

in #pygame6 years ago (edited)

Over the last few weeks I have been getting an urge to get more involved with open source projects that aren't my own. So after finding this post on Reddit, went over to pygame's repository straight away and looked for an issue I could start working on. Since a lot of the code is in C I decided it would be best to start off writing unit tests in Python and get familiar with the project first, so that's what I did!

Ellipses

As stated in issue #233 pygame had (it has now been fixed by someone else) a problem where ellipses with an odd width and height were being drawn 1 pixel smaller than they should. As you can see in the image below, an ellipse with an even height and width is drawn correctly




A 4x4 ellipse on a 4x4 surface

However, when drawing an ellipse with and odd height or odd width is not drawn correctly.




A 5x5 ellipse on a 5x5 surface

Solution

The unit test I created for this was one that creates a surface of a certain width and height, then creates an ellipse with the same width and height on the surface. Since the surface and the ellipse are the same size, you would expect all four sides of the ellipse to be touching the edge of the surface, right? So it then creates four lists containing the values of the top, left, right and bottom borders respectively. Finally it checks if each of these lists contains the colour of the ellipse, and if they don't then the test fails of course.

Issue(s):
https://github.com/pygame/pygame/issues/233

Pull request(s):
https://github.com/pygame/pygame/pull/501

More ellipses and lines

After discussing the unit test I had written on the pygame Discord with someone (the guy who would eventually fix the problem) I came to the conclusion that I should write a unit test that is more comprehensive.

Ellipse

I updated the draw.ellipse unit test so that it now also includes a test that checks if at least two sides of an ellipse, that has a width and height that is 1 pixel smaller than the surface, is touching the surface's border.

It also cleans up the code slightly, including a better way of retrieving the border's values and adds reusability in the form of additional functions.

(Aa)line(s)

While updating the aforementioned unit test I was also looking at issue #395, which described that lines drawn with the pygame.draw.aalines() function seem to be washed out of the blue color.




A washed out blue aaline

The way I tackled this unit test was by creating all possible surfaces (ARGB, RGBA) with and without SRC_ALPHA, with different sizes and depths, then drawing aaline(s) on it of a certain colour, then checking if the colour was drawn correctly. To make sure this wasn't just a problem for aaline(s), I also added tests for the draw.line functions as well.

So in summary - for draw.aaline and draw.line it now draws a line of differing colours on all types of surfaces and checks if the colour stays the same at position (0, 0).

The same is done for draw.aalines and draw.lines, except these tests draw a line around the border of the given surface then check if all pixels are of the given colour.

Gaps?!




Aaline(s) that contain gaps

While doing some research about this I was running my code and noticed that some of the aaline(s) either weren't being drawn at all or contained gaps, so I decided to also create an issue for this (#512) and write unit tests for it as well. So this means there is also a test that checks if the line contains any gaps (or is not drawn at all when close to a surface's border). Once again the same is done for draw.aalines and draw.lines.

Issue(s):
https://github.com/pygame/pygame/issues/233
https://github.com/pygame/pygame/issues/395
https://github.com/pygame/pygame/issues/512

Pull request(s):
https://github.com/pygame/pygame/pull/510

My experience

All in all it was very fun discussing potential solutions on pygame's Discord with other contributors. I must say that I also learnt a lot doing this, and also having my code reviewed by others has helped me improve, so I would definitely recommend that you start contributing to other people's (open source) projects as well!

Repository

https://github.com/pygame/pygame/

GitHub Account

https://github.com/amosbastian

Sort:  

Congrats - having a couple of commits on well-known projects like Pygame always feels good. :)

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @emrebeyler!

So far this week you've reviewed 3 contributions. Keep up the good work!

Pygame is an awesome library. It's super useful as a teaching tool as well.

I hadn't heard of if before I found the Reddit post, but it definitely looks really cool. Just have to wrap my head around some of the C code (I really neglected learning it properly at university) and then I'll have an attempt at actually trying to fix some of the issues as well as writing unit tests!

Yea, I have never played around with the C code, I have only used the library when using python.

Grats! Awesome post :)

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 15 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 5 SBD worth and should receive 95 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,
trufflepig
TrufflePig

Hi @amosbastian!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @amosbastian!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62937.86
ETH 3092.40
USDT 1.00
SBD 3.87