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

Port node package tests to quicktest #289

Merged
merged 1 commit into from
Nov 10, 2021
Merged

Port node package tests to quicktest #289

merged 1 commit into from
Nov 10, 2021

Conversation

masih
Copy link
Member

@masih masih commented Nov 9, 2021

Port the tests in node package to quicktest; use:

  • qt.Assert for wish.Require
  • qt.Check for wish.Wish
  • qt.IsTrue for ShouldEqualovertrue`
  • qt.IsFalse for ShouldEqual over false
  • qt.IsNil for ShouldEqual over nil

Port closeEnough Wish checker to its equivalent in quicktest.

Relates to:

@masih masih marked this pull request as ready for review November 9, 2021 13:28
@masih masih requested review from warpfork and mvdan November 9, 2021 13:28
Copy link
Contributor

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work!

By the way, you might want to encode those "shorter qt expression" nits as sed lines, so you can automatically apply those cleanups throughout your PRs.

// If a datamodel.Node is the expected value, a full deep qt.Equals is used as normal.
var closeEnough = &closeEnoughChecker{}

var _ qt.Checker = (*closeEnoughChecker)(nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clever :) I like how this turned out, even if implementing a custom checker with quicktest is slightly more verbose.

you could almost use qt.CmpEquals with https://pkg.go.dev/github.com/google/go-cmp/cmp#Comparer here, except that Comparer expects func(T, T) bool, so it only kicks in when the two types being compared are exactly equal. The logic here is different, as it is more flexible.

node/tests/testcase.go Outdated Show resolved Hide resolved
node/tests/testcase.go Outdated Show resolved Hide resolved
node/tests/testcase.go Outdated Show resolved Hide resolved
Wish(t, err, ShouldEqual, nil)
Wish(t, datamodel.DeepEqual(n, n3), ShouldEqual, true)
qt.Check(t, err, qt.Equals, nil)
qt.Check(t, datamodel.DeepEqual(n, n3), qt.Equals, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mental TODO: in the future, we might be able to expose datamodel.DeepEqual as a go-cmp Comparer. Definitely out of scope for this refactor, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could also use the printer package and then do string diff on the results.

That would simultaneously be very complete, and produce very readable debugable output, and also happily ignore the actual node implementation strategy (which is usually what we want).

Copy link
Member Author

@masih masih Nov 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quicktest comes with CmpEquals which looks pretty flexible; I wonder if we could use that here?

I'll add a TODO in code for this so that we don't forget :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what I was alluding to. But you need a custom comparer, because by default, CmpEquals will just loop infinitely. This is because bindnode ipld.Node implementations contain schema types, which have cyclic pointers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally arrived there via the flat datamodel.DeepEqual, which works, but also doesn't give good errors. I think either a custom go-cmp comparer, or comparing encoded versions like "printer" or "dagjson" like Eric mentions, would work better.

Copy link
Member Author

@masih masih Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted to write a checker that works with printer here but looks like there are TODOs in the printer that tests hit. Going to use datamodel.DeepEqual going forward.

Used dagjson to compare equality; checker implemented here

@mvdan
Copy link
Contributor

mvdan commented Nov 9, 2021

BTW, not sure if you're aware, but qt.Check does a t.Error, whereas qt.Assert does a t.Fatal. I think in general we want the latter. For example:

foo, err := bar()
qt.Check(t, err, qt.IsNil) // assume this errors because err != nil

qt.Check(t, foo.Baz, qt.Equals, "xyz") // panic! foo is nil

If you use qt.Assert on both lines, then we stop when err != nil, and we don't panic.

qt.Check is for the few times where we do want to continue running a test even when it errors once. That's commonly used e.g. when you check multiple properties in a row, so you want to report all the errors, and not just the first one.

@masih
Copy link
Member Author

masih commented Nov 9, 2021

Thank you for pointing that out @mvdan; I interpreted tests as wish.Require should do t.fatal and wish.Wish should do t.Error then used the equivalent qt function to keep the behaviour consistent with what's there just now.

Do you think the behaviour of tests should change?

@mvdan
Copy link
Contributor

mvdan commented Nov 9, 2021

If you did that intentionally, then SGTM. Keeping existing behavior is better for the sake of a smooth refactor. I was bringing it up in case it flew under your radar.

@masih
Copy link
Member Author

masih commented Nov 9, 2021

Thank you; I intentionally wanted to keep changes as net-zero as possible

Copy link
Collaborator

@warpfork warpfork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Wow, we have a lot of tests!)

Nice. It'll be great to have one fewer testing system in the dependency tree by the end of this (and the similar PRs).

Port the tests in `node` package to quicktest; use:
 - `qt.Assert` for `wish.Require`
 - `qt.Check` for `wish.Wish`
 - `qt.IsTrue` for ShouldEqual` over `true`
 - `qt.IsFalse` for ShouldEqual` over `false`
 - `qt.IsNil` for ShouldEqual` over `nil`

Port `closeEnough` Wish checker to its equivalent in quicktest.

Relates to:
 - #219
@masih masih merged commit f3e128b into master Nov 10, 2021
@masih masih deleted the masih/qt-port-node branch November 10, 2021 09:37
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