3

When I write a test method, I type "testm", hit tab and magically see:

[TestMethod]
public void MethodName()
{

}

When I type the methodName it is highlighted (can't show that here) as a "field" that I'm filling in. I'm sure you're all familiar with this behavior.

Personally, I like names for my test methods like Can_My_Method_Do_That_Thing instead of CanMyMethodDoThatThing. I find them much easier to read, and most times they're really a sentence anyway.

For reasons I'd rather not get into, I have a difficult time typing all those _ characters and I'd like to be able to use the space bar, and have the spaces in the name automatically replaced when I hit "Enter".

I hear that Visual Studio is extendable and customizable and so on. Is it extensible enough to do this?

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Scott Baker
  • 10,013
  • 17
  • 56
  • 102

4 Answers4

7

You can implement and use your own code snippets and Microsoft provides a very nice guide on how this could be done: Walkthrough: Implementing Code Snippets

To have a quick look at how the "testm" Expansion (that's the Snippet Type) is "partially" implemented you may go to c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Test\ directory and edit the testmethod.snippet file.

I have never implemented this kind of "Expansion" myself, but Microsoft's Extending the Editor website is a really good source of info of how this can be achieved. This is where you should start lookin'.

nowaq
  • 2,748
  • 19
  • 25
  • 1
    I think the entire point was not the snippets, but the underscores. – user541686 Nov 29 '11 at 04:52
  • @Mehrdad I think that the point was to achieve reasonable support for _ based names in the Visual Studio Editor. To have it in place you would most probably work with Code Snippets, [Smart Tags](http://msdn.microsoft.com/en-us/library/ee334190.aspx), Highlights, etc extensions. All of that is well described on msdn pages. – nowaq Nov 29 '11 at 08:54
2

If you install CodeRush Xpress then not only do you get some great free productivity and refactoring tools but there is also an open source community of plugins for CodeRush Xpress.

I found this plugin that does (almost) exactly what you want.

The caveat is that it is developed for NUnit and not MSTest so you will get a [Test] attribute instead of a [TestMethod] attribute. Since this is open source, it should be fairly easy to modify the code to your requirements.

Paul Haley
  • 405
  • 3
  • 12
1

The best method i would suggest is look into the Editor extensibility and work it out. Following steps is what you might need to do.

  1. Map a key to your "underscorize" action.
  2. Using the editor extensibility points, you can get access to the Selected text, process it.
  3. And finally replace it.

One suggestion. Though the learning curve might be a bit high with having to go through MEF and stuff like that. But its worth it.

kernelman
  • 41
  • 2
  • 15
0

Another approach worth considering would be to use an external tool to remap the keyboard. For example, it should be straightforward to get AutoHotKey to react to the testm[Tab] sequence of keystrokes and switch into a mode where it maps spaces to underscores (or deletes each space as soon as it's typed and adds an underscore). Similarly, the Enter key could switch it out of that mode again.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93