untoldwind / KontrolSystem2

Autopilot scripting system for KSP2
Other
54 stars 15 forks source link

Question about unity test #120

Closed lefouvert closed 3 months ago

lefouvert commented 8 months ago

version 0.5.2.5 (Ckan)

I saw than core::testing give use access to what seems to be tools for unity test. Since it's a better practice to test our code, I want to give it a try.

So there is this function : console::unit.to2

use { truncate } from core::math

const Sexagesimal: float = 60.0

pub fn hms(timespan: float) -> (hour: int, minute: int, second: float) = {
    // floor((t%B^n)÷B^(n-1)) where B is base and n is base shift
    return (
        hour: truncate(timespan / Sexagesimal**2).to_int,
        minute: truncate((timespan % Sexagesimal**2) / Sexagesimal).to_int,
        second: timespan % Sexagesimal
    )
}

As I want to test it, I made this very basic function (inspired from std::lambert_test) : test::console::unit.to2

use * from core::testing
use * from console::unit

test fn hms_test() -> Unit = {
    const testcases: (expectedHour: int, expectedMinute: int, expectedSecond: float, givenTimespan: float)[] =[
        (expectedHour: 0, expectedMinute: 0, expectedSecond: 0.0, givenTimespan: 0.0),
        (expectedHour: 0, expectedMinute: 0, expectedSecond: 7.8493, givenTimespan: 7.8493),
        (expectedHour: 0, expectedMinute: 1, expectedSecond: 0.0, givenTimespan: 60.0),
        (expectedHour: 1, expectedMinute: 0, expectedSecond: 0.0, givenTimespan: 3600.0),
        (expectedHour: 0, expectedMinute: 2, expectedSecond: 0.0, givenTimespan: 120.0),
        (expectedHour: 2, expectedMinute: 0, expectedSecond: 0.0, givenTimespan: 7200.0),
        (expectedHour: 0, expectedMinute: 2, expectedSecond: 7.8493, givenTimespan: 127.8493),
        (expectedHour: 2, expectedMinute: 2, expectedSecond: 3.849032, givenTimespan: 7323.849032),
        (expectedHour: 3, expectedMinute: 5, expectedSecond: 47.56382901, givenTimespan: 11147.56382901),
        (expectedHour: 0, expectedMinute: 0, expectedSecond: 47.56382901, givenTimespan: 11147.56382901)
    ]

    for (testcase in testcases) {
        let (hour, minute, second) = hms(testcase.givenTimespan)
        assert_int(testcase.expectedHour, hour)
        assert_int(testcase.expectedMinute, minute)
        assert_float(testcase.expectedSecond, second, 1e-5)

        fail_test("hms() didn't meet requirement")
    }
}
}

the last test case (expectedHour: 0, expectedMinute: 0, expectedSecond: 47.56382901, givenTimespan: 11147.56382901) is obviously wrong and should fail the test.

How could I run this test since it's not when code is compiled ? When and where does fail_test prompt his message ?

untoldwind commented 8 months ago

Yes, the core::testing module and test functions are supposed to be used in unit tests. At the moment these are only used as part of the build processes. I.e. you will find a lot of to2 tests in these folders:

The corresponding test-runners are theses (which are themselves regular xunit tests):

At the moment this is the only way to run to2-testsuits. I am not sure if it makes sense to add an in-game UI for this. ... maybe as an extension for the vscode plugin

lefouvert commented 8 months ago

Ok, I get it. That's why I didn't found how to launch them, I was missing the C# part ^^. As I have succeed to build the project on VSC (at the cost of 6 hours of tries and researches, because of NuGgets didn't update by themselves, and the MSbuild builder is severely outdated when it really need to be build from «dotnet»), I should be able to run my unity tests. I'm not sure an ingame interface is needed to run the unity test, I think your are right, a VSC plugin should be enought. (The best of the best would be to be able to compile TO2 code directly from VSC, if it's not already the case. Sometimes, I launch the game only for this). But as already said, I come from very old fashioned practice (I still work on a terminal !), where unity test are run handly (with debugger capture to show data) and reported on Word. An awfull way imho. So it's expected I struggle a little bit to fluently found thoses processes and Xunit is not an know way to work for me. And my vision could be outdated.

untoldwind commented 7 months ago

I think there is a problem with the current version of msbuild. I am still using msbuild 15.0 which seems to work fine for linux and windows.

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 60 days with no activity.

lefouvert commented 5 months ago

How I'm supposed to implement new features to my code since you added custom unit tests D':`, Now I have to test all my code !

(Joking, it's for the best)

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] commented 3 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.