Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing Formatting in Eclipse Editors: Real Unit Tests

DZone's Guide to

Testing Formatting in Eclipse Editors: Real Unit Tests

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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 :)

 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Alex Ruiz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}