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

#99 feat: Include argument 'comment' in assertions #211

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

PetterHopp
Copy link
Contributor

I suggest to include the argument 'comment' in assertion functions.
This suggestion appends the comment after the standard message and may be considered as a solution of
issue #99. The original wish was to include the comment before the standard message.
The changes are partly tested, but I have not been able to perform a full test of all changes (all assertion functions will be changed).
Be aware that I suggest to put the argument 'comment' before the argument 'add'. the reason is that I think this is a logical order. However, this my introduce breaking changes if users have been giving the arguments to assertion functions without using the name of the argument, i.e. not written "add = collection" but only "collection".

Included help text for argument 'comment' in assertion functions.
I suggest to include the argument 'comment' in assertion-functions. 
The suggestion appends the comment after the standard message and may be considered as a solution of 
issue mllg#99. The original wish was to place the comment before the standard message.
I have put the argument 'comment' before the argument 'add'. This may be a breaking change if users of the assertion functions has listed the arguments without the argument name, i.e has not used "... , add = collection", but just written (... , collection).
Inclusion of the argument 'comment' in assert to make it in agrement with including the argument 'comment' in other assertion functions. In the case of 'combine = "or"', the comment is placed on a new line. In the case of 'combine = "add"', the comment is appended each assertion that fails and is on the same line. If there are more than one assertion that fails, this is not optimal.
@PetterHopp
Copy link
Contributor Author

I had a look at the failed test for Windows. The test failed due to too many arguments for a function which I believe is OK as I have added an argument.

@mllg
Copy link
Owner

mllg commented Sep 24, 2021

Thanks for the PR. I'll first try to release a new version of checkmate in 2-3 weeks, then review and merge this one.

@PetterHopp
Copy link
Contributor Author

PetterHopp commented Sep 24, 2021 via email

@novica
Copy link

novica commented Jan 9, 2024

Hi. Is this PR going to be merged?

@renozao
Copy link

renozao commented Jun 11, 2024

@PetterHopp I would also love to see this capability too!
For checks that happens in functions called by the end-user, I think it is must to provide a more specific and informative error message that relates to the nature/purpose of the arguments and function.

For a while in my work, I define dynamic wrappers (assert_*_msg) around all existing checkmate functions to get this capability and it proved to provide great value, both for the developer and the end-user.

@mllg would you consider merging the PR?

Some comments/suggestions:

  • This is a capability that is provided by argument msg in assertthat::assert_that() or info in the expect_* functions. One might consider using one of these 2 established names instead of comment.
  • One could have another argument append = TRUE|FALSE, which would control whether the custom message is appended to the standard message or should replace it. This enables building cleaner messages in cases where the standard error message gets very busy listing lots of values.

Thanks! 🙏

@PetterHopp
Copy link
Contributor Author

@renozao Thank you for supporting the possibility of adding a comment in assertion functions.
I agree that comment may not be the best name for the argument. Of the suggestions msg and info, I would prefer msg. info is used expect-functions as in package testthat, but the argument is soft-deprecated in testthat and one is advised to not use it in new code.
The argument append = TRUE|FALSE is an excellent idea that I fully support.

@tdeenes
Copy link
Contributor

tdeenes commented Aug 7, 2024

@PetterHopp @mllg I would rather use fmt as the name of the argument, and instead of introducing an other argument append, I suggest that fmt shall be the same as the fmt argument of base::sprintf(). Then, makeAssertion would look like this:

makeAssertion <-  function (x, res, var.name, collection, fmt = NULL) {
    if (!isTRUE(res)) {
        assertString(var.name, .var.name = ".var.name")
        assertString(fmt, .var.name = "fmt", null.ok = TRUE)
        if (is.null(collection)) {
            if (is.null(fmt)) {
              fmt <- "Assertion on '%s' failed: %s." 
            }
            mstop(fmt, var.name, res, call. = sys.call(-2L))
        }
        assertClass(collection, "AssertCollection", .var.name = "add")
        if (is.null(fmt)) {
          fmt <- "Variable '%s': %s."
        }
        collection$push(sprintf(fmt, var.name, res))
    }
    return(invisible(x))
}

One could then customize how the error message is structured, e.g.:

object_to_test <- "a"
custom_fmt <- "Argument '%1$s' is incorrect and must be fixed. Details: %2$s"
assert_int(object_to_test, fmt = custom_fmt)

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.

5 participants