0

I am experimenting with a .NET testing framework along the lines of rspec and would like to implement a custom editor in Visual Studio for this purpose.

For the uninitiated, rspec is a testing framework for ruby and tests (or specs) look like this:

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    bowling.hit(0)
    bowling.score.should eq(0)
  end
end

In ruby, it is possible to write rspec tests as a block nested anonymous functions without any enclosing method or class - this is dealt with by the framework behind the scenes at runtime.

I would like to do something along these lines in Visual Studio 2012 and/or 2013. A complete C# test file might look something like this:

using System;
using BowlingApp;
using Specs;

@spec DefaultSpec 

Describe(typeof(Bowling), "#score", () => {
    It("returns 0 for all gutter game", () => {
        var bowling = new Bowling();
        bowling.Hit(0);
        Expect(bowling.Score).ToEqual(0);
    });
});

... where the @spec statement specifies the base class for the "enclosing" type that wil be generated behind the scenes (at design time, in this case). The base class would also supply the Describe, It and Expect methods used in the example above.

In order to make this testing framework practical to use, I would need a custom VS editor for the format above with syntax highlighting and intellisense.

Is it possible to "repurpose" the existing C# editor in VS to do this or would I have to do everything from scratch? If so, are there any open source tools or frameworks that might help me create such an editor?

Rasmus B
  • 67
  • 12
  • Visual Studio's unit testing is quite extensible, and you will already find several custom test frameworks out there. I think your goal here would be far easier if you define a base class for your tests (thus providing `Describe`, `It`, etc) and use the usual syntax to let your test classes extend your base class. Then you can focus on the framework and test runners, and not the custom syntax. The result, I think, would be more palatable to people accustomed to C#, and not *hugely* different from what you have here. – Chris Nielsen Oct 21 '13 at 19:26
  • @chris: You're missing the point. I do test-driven development in C# for a living and I know what the options are. I'm not interested in existing testing frameworks - I want to create a new one along the lines of rspec. – Rasmus B Oct 21 '13 at 19:30
  • I understand that you are creating a new testing framework. I am simply suggesting that you think about creating it without also creating a new syntax to go with it. Perhaps you could clarify why your new framework would benefit from a new syntax, in a way that could not be expressed using the existing syntax? – Chris Nielsen Oct 21 '13 at 19:40
  • See also http://stackoverflow.com/questions/15839507/does-c-sharp-have-a-rspec-like-testing-framework-that-makes-grouping-like-tests – Matthew Strawbridge Oct 21 '13 at 19:43
  • @chris: If you want to understand *why* I'm interested in creating such a framework I suggest you familiarize yourself with rspec (or Jasmine, which is very similar). However, my question is *not* about testing frameworks, it is about how to create a custom editor in Visual Studio. If you have something to contribute on this topic it would be much appreciated. – Rasmus B Oct 21 '13 at 19:48
  • @matthew: Again, my question is about how to create a Visual Studio editor. – Rasmus B Oct 21 '13 at 19:50
  • @RasmusB I understand your question: that's why I posted the link as a comment and not as an answer. Even if you don't find it useful, other people coming across this question in future might. If your question is really "How do I create a Visual Studio extension" then it will likely get closed for being too broad. – Matthew Strawbridge Oct 21 '13 at 20:02
  • I'm not asking how to create a Visual Studio extension in general. I am asking specifically about creating an editor for the code above and whether there might be any way to accomplish this with the existing VS C# editor or open source frameworks/tools. – Rasmus B Oct 21 '13 at 20:09

1 Answers1

0

Is there a simple way to do it? No. You can do tricks like this with projection buffers, but it's really, really hard to get right.

Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • Very interesting. I had a quick peek at http://msdn.microsoft.com/en-us/library/dd885240(v=vs.100).aspx#projection and will look into this further. – Rasmus B Oct 22 '13 at 01:30
  • The problem though is getting the language service to deal with the projection buffer in the first place. Also, this still presumes you have some way to reparse the code directly into a real C# file, which might be tricky depending upon how fancy of a syntax you want. – Jason Malinowski Oct 22 '13 at 04:53
  • It would not have to be that fancy, really. The file could just consist of a single class with a single method that contains all the anonymous functions. If I could basically just hide the class and method declaration (while still maintaining the using statements), that would go a long way. There are more things I would like to do but this would be a great starting point. – Rasmus B Oct 22 '13 at 10:29
  • The @spec declaration could be implemented at a later point. As long as it is possible to mix in extension methods with using statements all spec classes could inherit from a standard base class. – Rasmus B Oct 22 '13 at 10:35
  • See also http://stackoverflow.com/questions/5849205/how-to-write-a-visual-studio-extension-for-a-template-or-markup-language-that-su – Rasmus B Oct 22 '13 at 11:07
  • A bit of googling has confirmed that projection buffers are probably the best way to accomplish what I want. Unfortunately, it also seems that this area is pretty much undocumented and that no tutorials or examples whatsoever are available... which means that it may not be a feasible solution after all. Are there any alternatives? – Rasmus B Oct 22 '13 at 11:22
  • Not really, no. I really should have answered your question with "no, there is no easy way, go try something else." Extensibility for adding custom syntax is not something we currently support, hence the difficulty and lack of samples. – Jason Malinowski Oct 22 '13 at 14:34
  • I guess that answers my question, at least for now - thanks for the input. – Rasmus B Oct 22 '13 at 16:10