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

Replace instance trait inheritance with imports in tests #3304

Merged
merged 3 commits into from
Mar 5, 2020

Conversation

travisbrown
Copy link
Contributor

@travisbrown travisbrown commented Feb 19, 2020

This PR does several things to clean up the Cats tests (the first addresses #3262):

  1. Uses imports instead of inheritance to bring instances and syntax into scope.
  2. Uses imports instead of nested packaging to bring definitions in cats / alleycats into scope.
  3. Sorts imports consistently.
  4. Fixes a few minor packaging inconsistencies (like some of the cats-free tests being in cats.tests and others in cats.free).

The issue is that the tests are currently set up in a way that saves a few imports, but at the expense of not exercising normal library usage (see #3262 for an example of this causing problems).

After this change the tests do a better job of reflecting what users actually do (e.g. they don't nest stuff inside package cats and they don't extend a giant stack of 14 AllInstancesBinCompatX traits).

Compile times

Speeding up test compilation was a non-goal for this change, but I was curious and it seems a little faster (consistently 76s vs. 82s for testsJVM/test:compile after testsJVM/test:clean on 2.12 on my desktop).

The WIP part

Update: the catsSyntaxEq "override" did work just fine, even after switching to imports, but I went ahead and changed to more granular imports for cats.syntax anyway, since trying it out turned up two existing bugs (fixed separately in #3305 and #3306). I don't see any point in switching to granular imports for cats.instances since these will probably be gone entirely very soon.

I'm opening this so I don't lose track of it, but I still want to make sure the change doesn't cause problems with respect to the === situation (previously we were overriding catsSyntaxEq to make it non-explicit, and now might need to change all of the cats.syntax.all imports to exclude it instead).

There's also one test class (AlgebraInvariantSuite) where some function instances needed in the tests weren't resolving correctly when I switched to imports, so I'm still using the instance traits there. I'll try to take a closer look at it, but I think it's something specific to cats.laws.discipline.eq and I'm not too worried about it.

@codecov-io
Copy link

codecov-io commented Feb 19, 2020

Codecov Report

Merging #3304 into master will increase coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3304      +/-   ##
==========================================
+ Coverage   93.31%   93.32%   +0.01%     
==========================================
  Files         378      378              
  Lines        7689     7689              
  Branches      206      206              
==========================================
+ Hits         7175     7176       +1     
+ Misses        514      513       -1
Flag Coverage Δ
#scala_version_212 93.38% <ø> (ø) ⬆️
#scala_version_213 93.11% <ø> (+0.01%) ⬆️
Impacted Files Coverage Δ
testkit/src/main/scala/cats/tests/Helpers.scala 100% <ø> (ø) ⬆️
.../main/scala-2.13+/cats/data/NonEmptyLazyList.scala 79.09% <0%> (+0.9%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8e2c21...ed84680. Read the comment docs.

@travisbrown travisbrown requested review from kailuowang and LukaJCB and removed request for kailuowang February 20, 2020 12:26
@travisbrown travisbrown changed the title Replace instance trait inheritance with imports in tests [WIP] Replace instance trait inheritance with imports in tests Feb 20, 2020
@travisbrown travisbrown added this to the 2.2.0-M1 milestone Feb 20, 2020
LukaJCB
LukaJCB previously approved these changes Feb 21, 2020
LukaJCB
LukaJCB previously approved these changes Mar 2, 2020
rossabaker
rossabaker previously approved these changes Mar 3, 2020
Copy link
Member

@rossabaker rossabaker left a comment

Choose a reason for hiding this comment

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

I think more normal library usage is to just import cats.implicits._ rather than à la carte syntax, so that goes a bit against the "test it how people use it" principle. Then again, you found bugs. I wonder whether we'd find more with the omnibus import, but I can't think of a convenient way to test both.

@travisbrown
Copy link
Contributor Author

@rossabaker I agree that there are trade-offs here. In my view moving away from inheritance for these instances is a major improvement in this respect (tests reflecting usage), and à la carte vs. kitchen sink is a smaller matter. I'd also prefer to use kitchen sink here, if we had decent tests for the à la carte style, but unfortunately we don't.

There's also the question about what the recommended style should be after #3043. At least initially in 2.2, cats.implicits._ won't change at all, because if we made the standard library instances non-implicit, that would change prioritization in some cases, and I think we want to stay 100% source-compatible during a transition period. But for the vast majority of use cases cats.implicits._ will be replaceable with cats.syntax.all._.

@travisbrown
Copy link
Contributor Author

@rossabaker Switching to kitchen sink wouldn't be all that hard, though. We'd have to reinstate catsSyntaxEq, delete all the cats.syntax.xyz._ imports, and add cats.implicits._ everywhere—maybe 15 minutes to write a little script and make sure things work.

@travisbrown
Copy link
Contributor Author

@rossabaker Oh, I almost already did that—I've just rebased to make it a little easier to isolate the kitchen-sink-to-à-la-carte change. If you check out 8dd80e5 ("Replace instance trait inheritance with imports in tests") you'll get more or less the kitchen sink version (although not cats.implicits._ but cats.instances.all._ plus cats.syntax.all._).

When it's green I'll merge the three rebased commits without squashing in case we want to revisit this in the future (the changes are identical to what was reviewed, just reordered slightly).

@travisbrown
Copy link
Contributor Author

Ugh, sorry, will need one last 👍 after the rebase. @LukaJCB, mind giving it one more? 😄

@travisbrown travisbrown added this to the 2.2.0-M1 milestone Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants