如何对Ook语言集成示例进行单元测试?
我正在使用 MEF 编写自定义语言着色器作为 Visual Studio 扩展。我的大部分代码来自此处提供的 Ook 语言集成示例。我的着色器已经准备就绪,但现在我需要为我的实现提供适当的单元测试套件。
考虑到这些东西的 MEF 性质,我想知道哪种方法适合单元测试。我应该直接从单元测试中引用标记器并测试 GetTags 方法吗?我应该将 MEF 纳入我的单元测试中吗?有没有关于测试基于 MEF 的着色器和自定义 Intellisense 实现的示例?
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 directly 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我想我有办法做到这一点。不知道这是否是理想的方法,但可以让我测试我的核心逻辑。功劳归于 HQL Language Service for Visual Studio 背后的人,他们向我展示了如何为 ITextSnapshots 创建存根和致诺亚·理查兹,他的拼写检查器扩展有一组单元测试,为我的测试设置场景。
下面是我的 TestFixture 的代码:
在 TestQuery 方法中,我首先调用 SnapshotUtil 上的 CreateSnapshot。这是一个有用的类,在 HQL 语言服务测试项目中找到,它将根据某些输入文本创建 ITextSnapshot 对象,它模拟 VS 将为我的扩展提供的内容。然后我使用 MyTokenTagProvider 为我创建一个标记器。在这里,我使用这个名为 Moq 的强大东西来创建 ITextBuffer 对象的 Mock,它是CreateTagger 的预期输入参数。
现在我们有了一个标记器,剩下的就是用它来检查它是否正在完成工作。 GetTags 是我想在这里测试的核心方法,因为它将接受我的输入查询并输出它能够识别的一系列标签。为了获取实际的单词,我们检查每个标签的跨度并使用它从原始查询中提取单词。 NUnit 的 CollectionAssert 方法将确保输出的单词列表与预期列表匹配。
希望这对创建和测试 VS 编辑器扩展的其他人有用。
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:
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.