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

Inconsistent nature of messageFromMsgAndArgs #1679

Open
Antonboom opened this issue Nov 13, 2024 · 5 comments
Open

Inconsistent nature of messageFromMsgAndArgs #1679

Antonboom opened this issue Nov 13, 2024 · 5 comments

Comments

@Antonboom
Copy link

Antonboom commented Nov 13, 2024

Description

Hello, team!

I was surprised, but testify allows to set non-string in msgAndArgs... if this is the single argument:

if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}

So I can do

assert.Equal(a, b, new(time.Time))

But cannot

assert.Equal(a, b, new(time.Time), 1, 2, 3) // Panics.

Proposed solution

Remove support of non-string single arg (as rare and strange? case) and allows only msg string + args ...any.

Use case

f-assertions future (#1089 (comment)).

@Antonboom
Copy link
Author

Antonboom commented Nov 13, 2024

Some real examples of using non-string arg: Antonboom/testifylint#169 (comment)

And this explores the another solution – to implement args ...any printing like

package fmt
func Print(a ...any) (n int, err error)
func Printf(format string, a ...any) (n int, err error)

Thus, support both options (formatted print and not formatted print of N args). But I am not happy with it :(

@brackendawson
Copy link
Collaborator

My word! I didn't know that was supported. I'd prefer to entirely remove the msgAndArgs argument from the "non-f" assertions. But that and removing this is a breaking change, no?

@Antonboom
Copy link
Author

Antonboom commented Nov 16, 2024

But that and removing this is a breaking change, no?

Technically no (because no signature changes), but logically yes (panicking of existing code) 🤔
(I am about stop supporting the single not-string arg, not about removing. Removing, of course, is breaking change).

But I need to know "party vector", if 💯 you are moving to "entirely remove the msgAndArgs argument from the "non-f" assertions", then I can enable warning on this single-not-string arg case in testifylint.

Currently I propose use f-assertions only if format string is used, but this special case is ignored, that leads to checker inconsistency 😞

@brackendawson
Copy link
Collaborator

I would go with always direct people to the f functions. The variadic just shouldn't be used, even for this sprint like behavior.

@Antonboom
Copy link
Author

Got it. Thanks 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants