franela / goblin

Minimal and Beautiful Go testing framework
MIT License
887 stars 79 forks source link

Accept various types of messages in assertions #78

Closed scfarley closed 4 years ago

scfarley commented 4 years ago

Expand all assertions to accept various types of messages to emit as output when the expression evaluates to false. This changes permits unit tests to use any assertion call for returning a message instead of just IsTrue() and IsFalse().

The change from accepting only strings to any value allows for simplification of unit tests that just print the error string.

This allows replacing:

if err != nil {
        g.Fail(err.Error())
}

With:

g.Assert(err == nil).IsTrue(err)

The logic to format the messages into output relies upon fmt.Sprintf(). It simply concatenates the messages together with one exception: messages that are only whitespace. In this case, it wraps the whitespace with square brackets to make it more obvious that a message was provided albeit unseen.

Bump CI version of Go to 1.12 which is the oldest version still supported by the Go developers. It is also includes strings.Builder which is needed by the new assertion message code.

Correct spelling and remove trailing spaces.

scfarley commented 4 years ago

Ouch. It appears strings.Builder is from Go 1.10, so this code will not work with Go 1.6.3 used in the CI build. Versions before Go 1.12 are no longer supported by the Go team: https://golang.org/doc/devel/release.html#policy What is the policy for goblin?

marcosnils commented 4 years ago

Sounds good to bump the tests against > 1.12 versions. I like this PR as well. Do you want to bump circle versions in this PR as well?

scfarley commented 4 years ago

Thank you. I really like Goblin and tend to push it to the limit in my tests. Oh, the horrors I do to it! :grin:

I bumped the CI version to 1.12 (oldest supported at this time). I presume "circle versions" meant CI version.

marcosnils commented 4 years ago

Thank you for contributing. I'll be merging this since test pass and it doesn't break any backwards compatibility API.

Eventually I guess we'll have to add go modules support so we do proper versioning.