flutter-intellij icon indicating copy to clipboard operation
flutter-intellij copied to clipboard

Feature request : Shortcut to create tests

Open jaumard opened this issue 6 years ago • 14 comments

Would be nice to have a shortcut to create test when we have one of those popup: capture d ecran 2018-12-27 a 13 12 18 capture d ecran 2018-12-27 a 13 11 57

A bonus would be that if the class is a widget it create a template for widget test, if not unit test.

That a functionality we have already on Android: capture d ecran 2018-12-27 a 13 13 53

It's very useful because it create a template test file directly in same place/package as the class. For our case if we have a class in lib/catalog/bloc/mybloc.dart it will create a test file in test/catalog/bloc/mybloc_test.dart

jaumard avatar Dec 27 '18 12:12 jaumard

Hey @pq I'm trying to familiarize myself with the project by implementing this feature, can you tell me where I can find the code for this actions please ?

capture d ecran 2019-01-05 a 12 08 43

because I don't find how I can add my action here :/ thanks !

jaumard avatar Jan 05 '19 11:01 jaumard

From what I've understand it's an IntentAction that I need, but I need to know if I'm in the a context of a class or not :/ finding the code that do those actions will help me a lot to do that

jaumard avatar Jan 05 '19 13:01 jaumard

@jaumard: awesome that you're looking at this!

As a general rule if you're trying to figure out how something works in IDEA, it's best to grab the source and search around. (Their docs aren't great.) These instructions for setting up the Dart Plugin for development may be handy:

https://github.com/JetBrains/intellij-plugins/blob/master/Dart/README.md

I'll take a look myself and see if I find anything promising and report back...

pq avatar Jan 06 '19 15:01 pq

OK. After some poking around I have a few ideas.

Once you have the source, I'd start by looking for contributors to the testCreator extension point. Look at this for example:

<testCreator language="JAVA" implementationClass="com.intellij.testIntegration.JavaTestCreator"/>

I'm guessing you could model a FlutterTestCreator after this?

I need to know if I'm in the a context of a class or not :/ finding the code that do those actions will help me a lot to do that

Once you've got a test creator stubbed out, I'd look in the Flutter IntelliJ sources for places that reference PsiElement. (io.flutter.FlutterUtils#isFlutteryFile may come in handy too.)

Have fun!

pq avatar Jan 06 '19 15:01 pq

@pq thanks ! Yeah basically that's what I did, I retrieved Dart plugin and this plugin and was trying to dig into those ^^ but even like that I find it really difficult to do things lol (for example I still don't know how convert to statefulWidget work as I didn't find the code for that...) it take my one day just to be able to have Create Test when I do alt+enter on the class name lool Now I'll check the JavaTestCreator and try to make a FlutterTestCreator ^^ At least work in progress :)

jaumard avatar Jan 07 '19 08:01 jaumard

Great progress!

One reason you may not have found anything close to what you want to do is that everything with deep Dart language model knowledge (e.g., code generation, fixes and refactoring) is implemented in the (Dart) analysis server. The more I think about this task, the more I think that the test code generation part should happen there. Could you give some details about the logic you want to implement?

FYI @scheglov @bwilkerson

pq avatar Jan 07 '19 14:01 pq

Ho that might explain it ! :) but even the text of the action I wasn't able to find it lool because from there I would have been able to follow the white rabbit in the correct class to see its all in analysis server.

What I want to implement in short term is :

  • to create an empty test (or widget test) file under the same package as the source (but in test directory).
  • if test already exist, "create test" become "go to test" and it open the test file on click

In long term I want :

  • a popup to choose which method I want to tests and generate a scaffold of those method tests.
  • checkbox to generate or not setup teardown methods

jaumard avatar Jan 07 '19 14:01 jaumard

OK. On second thought this might be more easily done on the client-side.

In the Dart plugin sources, com.jetbrains.lang.dart.ide.generation.BaseDartGenerateHandler may be a good place to start looking. Also, maybe com.jetbrains.lang.dart.ide.actions.CreateDartFileAction.

@alexander-doroshko might have ideas too (but may not chime in for a while since he's on vacation)

pq avatar Jan 07 '19 18:01 pq

I would strongly urge us to support this via server so that we don't have to do the same work in multiple clients. I believe that this would align nicely with the LSP Code Lens Request, and we could add something similar to the server's protocol.

bwilkerson avatar Jan 07 '19 18:01 bwilkerson

So bottom line :) better if I wait a bit right ? ^^

jaumard avatar Jan 08 '19 12:01 jaumard

If it's something you'd really like to see happen, I'd suggest opening a bug in the SDK issue tracker (https://github.com/dart-lang/sdk/issues/new) and reference back to this issue so we can track it.

Thanks!

pq avatar Jan 08 '19 14:01 pq

done @pq :) feel free to add more information if you can, as I don't know that much the analysis server and the way it works https://github.com/dart-lang/sdk/issues/35595

jaumard avatar Jan 08 '19 14:01 jaumard

A feature like this is now a part of the Flutter VSCode Addon.

Is it also planned for IntelliJ?

DFelten avatar Aug 03 '20 05:08 DFelten

It's been 3 years. Please, add this feature already

Nikitae57 avatar Jan 31 '22 06:01 Nikitae57

please-pretty

hardikm9850 avatar Apr 30 '23 11:04 hardikm9850

/fyi @bwilkerson

pq avatar May 01 '23 17:05 pq

This would be so great, please consider adding it

bkraszewski avatar Oct 25 '23 07:10 bkraszewski