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

Add support for unit tests, and a few unit tests #10636

Merged
merged 4 commits into from Apr 16, 2023

Conversation

rubidium42
Copy link
Contributor

Motivation / Problem

Tests for new and existing code will make development & refactoring easier.
And will make it easier to detect regressions and unintended changes.
See #9038 and #7796. Triggered by #10570 which adds some tests as constexpr code.

Description

Split building of OpenTTD into a "library" and the executable, by extracting the main() functions into separate files. And then building the library without the main() function and the openttd executable is the main() function with the library linked. The library is not an actual library, but some construct in CMake that prevents building the same objects twice, since the library is going to be linked with the unit tests and then run.

Add catch2 as third-parties library. It does not exist as library in ubuntu 20.04, so otherwise it would require updating those. Furthermore there were issues in getting it to work with Windows and Mac OS X. By third-partying it, it will just be available and do its thing.

Add unit test from #9038/#7796 and convert constexpr tests of #10570 to a proper unit test, and integrate catch2 into the build. Add an extra executable (openttd_test) that links the new "library" and the unit tests. With the integration it automatically gets picked up with ctest, and be run in parallel with the regression tests.

Limitations

I'm not sure whether OS/2 still builds.
I'm not sure whether the release workflows still function.
It has been a long time whack-a-moling to get this functioning, so I'm not sure whether I did the right things.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

TEST_CASE("GreatestCommonDivisorTest - Negative")
{
CHECK(4 == GreatestCommonDivisor(4, -52));
// CHECK(3 == GreatestCommonDivisor(-27, 6)); // error - returns -3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented code, and an error elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon an error elsewhere. It's a bit via-via-via (#9038 -> #7796 -> #7254), but there was some discussion about it before without real remedy https://github.com/OpenTTD/OpenTTD/pull/7254/files#r259590007
In any case, I copied this test straight from #9038, which copied it from #7796.

@PeterN
Copy link
Member

PeterN commented Apr 13, 2023

Had a look to see if hierarchical tests could be done, it looks like it's supported by catch2 by way of SECTIONs, but ctest itself doesn't do anything with those and only shows the top-level TEST_CASE.

Copy link
Member

@PeterN PeterN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me.

@rubidium42 rubidium42 merged commit 43a7e54 into OpenTTD:master Apr 16, 2023
18 checks passed
@rubidium42 rubidium42 deleted the unit-test branch April 16, 2023 16:58
@WimLeflere
Copy link
Contributor

Nice work!

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

Successfully merging this pull request may close these issues.

None yet

4 participants