Alex is a Software Engineer working on Android development tools, especially Android Studio, at Google. His interests include Java, API design, OOP, IDEs and testing. Alex spends some of his spare time working on Open Source, blogging, writing technical articles, and speaking at international conferences. The opinions expressed here represent his own and not those of his employer. Alex is a DZone MVB and is not an employee of DZone and has posted 49 posts at DZone. You can read more from them at their website. View Full User Profile

Testing Formatting in Eclipse Editors: Real Unit Tests

03.12.2012
| 5330 views |
  • submit to reddit

In my previous post I described how I tested formatting in an Eclipse editor. The decision to write a functional test made sense back then because I didn’t know how formatting internally works but I knew what to expect from formatting, from the user’s perspective. In addition, I was itchy to try this cool tool, SWTBot :)

Luckily, Moritz Eysholdt, a Xtext committer, was kind enough to leave a comment in my previous post explaining how he unit-tests formatting in a Xtext-based editor (take a look at his great presentation: “Test-Driven Development of Xtext DSLs“.)

Moritz’s approach to unit testing is similar to mine in the sense that he separates test data from test code. The difference is that in his approach, test data is not in the test class itself, but in separate files. I personally prefer to have everything, test code and test data, in one place. Having test data as comments is, so far, my favorite way to separate it from test code.

Thanks to Moritz now I know that I can use INodeModelFormatter to unit-test formatting. My new formatting test looks like this:

public class ProtobufFormatter_Test {
  @Rule public CommentReaderRule commentReader = overrideRuntimeModuleWith(unitTestModule());
 
  @Inject private INodeModelFormatter formatter;
 
  // import 'dummy.proto';import 'google/protobuf/descriptor.proto';
 
  // import 'dummy.proto';
  // import 'google/protobuf/descriptor.proto';
  @Test public void should_format_normal_import() {
    ICompositeNode rootNode = commentReader.rootNode();
    IFormattedRegion region = formatter.format(rootNode, 0, rootNode.getText().length());
    String formatted = region.getFormattedText();
    assertThat(formatted, equalTo(commentReader.expectedText()));
  }
}

The most interesting part in the code above is line 2. The JUnit rule CommentReaderRule is the one doing the heavy lifting:

  1. Reads the comments in each test method
  2. Parses the first comment (the text before formatting)
  3. Makes the second comment (the formatted text) available via the method expectedText

Another interesting aspect of this testing approach is that each test method has exactly the same code, all the context is in the comments. After removing some duplication, my test class looks like this:

 // package com.google.proto.test;import 'google/protobuf/descriptor.proto';
 
  // package com.google.proto.test;
  //
  // import 'google/protobuf/descriptor.proto';
  @Test public void should_format_package() {
    assertThatFormattingWorksCorrectly();
  }
 
  // import 'dummy.proto';import 'google/protobuf/descriptor.proto';
 
  // import 'dummy.proto';
  // import 'google/protobuf/descriptor.proto';
  @Test public void should_format_normal_import() {
    assertThatFormattingWorksCorrectly();
  }
 
  private void assertThatFormattingWorksCorrectly() {
    ICompositeNode rootNode = commentReader.rootNode();
    IFormattedRegion region = formatter.format(rootNode, 0, rootNode.getText().length());
    String formatted = region.getFormattedText();
    assertThat(formatted, equalTo(commentReader.expectedText()));
  }

 

Now my tests run super fast (no UI and no OSGi) and are reliable. Sweet!

Note: This new approach works with Xtext-based editors only. The previous one did not rely on Xtext internals and could be used to test formatting on any Eclipse editor.

Feedback is always welcome :)

 

Published at DZone with permission of Alex Ruiz, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)