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

Custom pretty printing in utests #778

Closed
wants to merge 10 commits into from

Conversation

br4sco
Copy link
Contributor

@br4sco br4sco commented Aug 16, 2023

This PR adds the option to supply a function that specifies the output string for the left-hand and right-hand sides of failing utests. The semantics and syntax are best described with an example. The following code snippet:

...
mexpr
let eq : Int -> String -> Bool = lam l. lam r. eqi 0 (string2int r) in
let toString : Int -> String -> String = lam l. lam r.
  join ["left hand: ", int2string l, ", right hand: \"", r, "\""]
in
utest 0 with "1" using eq in               -- Failing
utest 0 with "1" using eq else toString in -- Failing
utest 0 with "0" using eq else toString in -- Passing
()

will on execution output:

 ** Unit test FAILED: <fileinfo> **
    LHS: 0
    RHS: ['1']
    Using: eq

 ** Unit test FAILED: <fileinfo> **
left hand: 0, right hand: "1"
    Using: eq
.ERROR! 1 successful tests and 2 failed tests.

The else function has the signature a -> b -> String and is only applied to the left-hand side and the right-hand side if a test fails (the standard formatting is omitted in this case). Currently, you always have to specify the custom equality function if you want to also specify the else function due to how the bootparser is implemented. We can relax this later on if we want to.
This PR addresses #677.

You can find the above example in test/examples/utest/utest-with-onfail.mc.

@johnwikman
Copy link
Contributor

Looks good! But I would also like to have the option to get LHS: ... RHS: ... formatting for free, i.e. that I just provide string conversion on either side:

let toString : Int -> String -> (String, String) =
    lam l. lam r. (int2string l, concat "strint: " r)
in
utest 0 with "1" using eq onfail toString in
-- This would output
-- ** Unit test FAILED: <fileinfo> **
--    LHS: 0
--    RHS: strint: 1
--    Using: eq

-- or even more convenient
utest 0 with "1" using eq onfail (int2string, concat "strint: ") in

@br4sco
Copy link
Contributor Author

br4sco commented Aug 17, 2023

I propose that we implement that functionality as a library function defaultOnfail : all a. all b. (a -> String) -> (b -> String) -> a -> b -> String rather than adding more keywords and variations on the utest term. Or do you want to replace the current signature so that "RHS:" and "LHS:" are always printed?

@johnwikman
Copy link
Contributor

Yes that sounds like a good solution. Can probably be part of a separate PR then

@br4sco
Copy link
Contributor Author

br4sco commented Aug 18, 2023

Changed onfail to else.

@br4sco br4sco mentioned this pull request Aug 24, 2023
@br4sco br4sco closed this Aug 24, 2023
@br4sco br4sco deleted the utest-failing-fun branch November 12, 2023 09:11
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.

3 participants