Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Unit test generation should consider the receiver type #2282

Closed
Guitarbum722 opened this issue Jan 28, 2019 · 11 comments
Closed

Unit test generation should consider the receiver type #2282

Guitarbum722 opened this issue Jan 28, 2019 · 11 comments

Comments

@Guitarbum722
Copy link

Guitarbum722 commented Jan 28, 2019

Description:
The Go Plugin doesn't seem to consider the receiver type for a method. So, if there are >= 1 method names in the same file, then clicking "generate unit tests for function" creates test functions for all of them.

This references an issue with gotests. However, that same gotests issue explains that the receiver type is in fact supported.

I tried this by running the following (as an example) in the command line using gotests directly.
$ gotests -w -only ^PersonValidate$ .
This works as expected, and there is only a test function generated for the Person type.

Version of VS Code, the Go plugin and Go:
VSCode: 1.30.2 61122f88f0bf01e2ac16bdb9e1bc4571755f5bd8 x64
Go Plugin: 0.8.0
Go: 1.11.2 darwin/amd64

OS/Platform you are working on
MacOS / AMD64

Reproducible steps (1... 2... 3...) that cause the issue

  1. right click a method name
  2. select "Generate Unit Tests for Function
  3. all methods that share the same name but are on different types have a test function generated

What you expected to see, versus what you actually saw

I would expect that the Go extension uses gotests with the -only flag and prepends the type name with the regexp. Therefore, one single test function is generated for the respective method that was selected.

Instead, any methods that have the same name in the file have a test function generated.

Great extension though! 👍 Hopefully this is enough info.

@ramya-rao-a
Copy link
Contributor

Thanks for reporting @Guitarbum722

So looks like all we need to do is identify the receiver type, convert it to title case, prepend it to the function name and then pass the resulting string to gotests

PRs are welcome to fix this
Code Pointers:

@jamesgeorge007
Copy link
Contributor

@ramya-rao-a I'm new here. I would like to give it a try 👏
Are the changes to be made within getFunctions() method where the current function is being figured out before generateTests() is being invoked.
Can you provide a better picture regarding what is to be done 👍

@Guitarbum722
Copy link
Author

I tried a couple of other scenarios that should probably be taken into consideration for this.

As mentioned, the receiver type can be prepended to the function's name in the regexp passed to gotests. However, it would be converted with strings.Title().
So if the receiver type is camel-cased with more than just the first char as upper (eg. type FooBar struct...) then gotests would change it to Foobar, and the desired type name prepended in the argument to gotests would not match.

So, I would think the Go Extension would need to do the same thing to normalize the regexp passed to gotests.

@ramya-rao-a
Copy link
Contributor

@jamesgeorge007 Yes, the changes should be done inside getFunctions before calling generateTests . I would suggest to add breakpoints and understand the current flow for Go: Generate Unit Tests for Function command.

Thanks for the note @Guitarbum722

@open-source-explorer
Copy link

@ramya-rao-a I'm a newbie here. I didn't get it really what you meant by the receiver here.
Everything is to be done with funcName variable ?

@open-source-explorer
Copy link

open-source-explorer commented Feb 7, 2019

Is this the code-part to be considered within the getFunctions method:-
https://github.com/Microsoft/vscode-go/blob/9631a26af65876b9fa96da8340fa869bccbcf84a/src/goGenerateTests.ts#L105

@ramya-rao-a
Copy link
Contributor

@open-source-explorer That would give just the function name, where as what we need is the receiver type as well. Since getFunctions should return range information as well. Using that you can get the declaration line for the function, from which you should be able to get the receiver information.

Regarding receivers, https://tour.golang.org/methods/3 is a simple introduction

@open-source-explorer
Copy link

@ramya-rao-a I'm not sure on how to retrieve the receiver type in this context 🤔
As per what I see currentFunction is an object with couple of properties, name being one.
What is to be done inorder to fetch the receiver information?

@ramya-rao-a
Copy link
Contributor

One of the properties would be location. You can pass this to the getText() function of the document to get the text of the entire test function, and then parse the receiver info out from that text

@ramya-rao-a
Copy link
Contributor

This issue has been resolved in the latest beta version of this extension.

@ramya-rao-a
Copy link
Contributor

This issue has been resolved in the latest version (0.13.0) of this extension

Thanks for the PR @marcel-basel and Thanks for reporting @Guitarbum722!

Happy Coding!

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants