开发者

How to unit test the Ook Language Integration example?

I am writing a custom language colorizer as a Visual Studio extension using MEF. Most of my code comes from the Ook Language Integration sample that is available here. My colorizer is pretty much ready, but now I need to provide an appropriate unit test suite for my implementation.

Given the MEF nature of this stuff, I wonder which is the appropriate approach to unit testing. Should I just direct开发者_StackOverflow中文版ly reference my Tagger from my unit test and test the GetTags method? Should I involve MEF in my unit tests? Are there any examples out there about testing MEF based colorizers and custom Intellisense implementations?


OK, so I think I got an approach to do this. Don't know if it is the ideal approach, but allows me to test my core logic. The credits go to the guys behind the HQL Language Service for Visual Studio who showed me how to create stubs for ITextSnapshots and to Noah Richards whose Spell Checker extension has as set of unit tests that set the scenario for my ones.

Here the code for my TestFixture:

[TestFixture]
public class TokenTaggerTests
{
  [Test]
  public void CanGetTagsForSimpleSelect()
  {
    TestQuery("SELECT * FROM Books", new List<string>() { "SELECT", "*", "FROM", "Books"});      
  }

  private void TestQuery(string query, List<string> expected)
  {
    ITextSnapshot snapshot = SnapshotUtil.CreateSnapshot(query);
    NormalizedSnapshotSpanCollection spans = new NormalizedSnapshotSpanCollection(snapshot.CreateSpanFromLineNumber(0));

    MyTokenTagProvider provider = new MyTokenTagProvider();
    ITagger<ITag> tagger = provider.CreateTagger<ITag>(new Mock<ITextBuffer>().Object);

    List<string> words = tagger.GetTags(spans).Select(s => query.Substring(s.Span.Start, s.Span.Length)).ToList();

    string errorMessage = string.Format("Got list: [{0}].  Expected: [{1}]", string.Join(", ", words), string.Join(", ", expected));

    CollectionAssert.AreEqual(expected, words, errorMessage);
  }
}

In the TestQuery method I start by calling CreateSnapshot on SnapshotUtil. That is a useful class, found in the HQL Language Service test project that would create ITextSnapshot objects based on some input text, which simulates what VS would provide to my extension. Then I go and use MyTokenTagProvider to create a tagger for me. Here I use this powerful thing called Moq to create a Mock of an ITextBuffer object, which is the expected input parameter for CreateTagger.

Now that we have a tagger, the rest is just playing around with it to check if it is doing the job. GetTags is the core method I want to test here, as it is the one that will take my input query and output the series of tags it was able to identify. To get the actual words, we examine each tag's span and use it to extract the word from the original query. The CollectionAssert method of NUnit will make sure the output list of words matches the expected list.

Hopefully this is useful for anybody else out there creating and testing VS editor extensions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜