Remote Ping-Pong Challenge: Your turn, who's next? (Java) 32

Posted by Brett Schuchert Fri, 19 Jun 2009 06:11:00 GMT

So I want to try a form of TDD using Ping-Pong. Problem is, you are not where I can pair with you directly. So I figured we’d try a slower-form. I’ll post the beginning of a problem. First person to post the next response “wins” – that’s where the next person should pick up from. We’ll continue until the problem is “finished.”

Interested? First the ground rules, then the problem and the starting code. This is Java. However, if someone responds in another language, that’s cool. I might try and follow up, but you’re welcome to do so yourself. I can imagine having multiple threads going on at the same time. (If you’d like your own language thread, ask and I’ll post another blog entry for that particular language.)

Ground Rules

  • Follow the three rules of TDD + Refactoring.
  • Fix the one failing test.
  • Add one to three more tests before posting.
  • Make sure you leave one and only one failing test for the next person to fix. (Meaning you can add two passing tests but leave a third test failing, or you can just add one failing test).
  • You can/should refactor the test code and the production code
  • If you feel a test is wrong, you may change it, but be prepared to justify it.
  • I’ll serve as the customer, so I get to define what is “correct” – however, I can be argued with and I can lose arguments (see some of my other blog postings for evidence).
  • Feel free to respond with comments/suggestions rather than additional tests.
  • Feel free to apply refactorings like extract method, rename, split loop, ... Keep the code clean!

The Problem

Translate infix notation to postfix notation. Consider reviewing The Shunting Yard Algorithm. But that’s a guideline.

The Start

Here is the test and production code. Note, when you respond, surround your code with one of the following pairs of HTML tags:
  • <typo:code>, and </typo:code>
  • <pre>, and </pre>

InfixToPostfixConverterTest

package com.om.example;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class InfixToPostfixConverterTest {
   private final String infix;
   private final String expectedPostfix;

   @Parameters
   public static Collection<String[]> data() {
      ArrayList<String[]> values = new ArrayList<String[]>();

      addTestCase(values, null, "");
      addTestCase(values, "", "");
      addTestCase(values, "45", "45");
      addTestCase(values, "+", "+");
      addTestCase(values, "3 + 8", "3 8 +");

      return values;
   }

   private static void addTestCase(ArrayList<String[]> values, String infix,
         String expectedPostfix) {
      values.add(new String[] { infix, expectedPostfix });
   }

   public InfixToPostfixConverterTest(String infix, String expectedPostfix) {
      this.infix = infix;
      this.expectedPostfix = expectedPostfix;
   }

   @Test
   public void checkTranslation() {
      InfixToPostfixConverter converter = new InfixToPostfixConverter();
      String result = converter.translate(infix);

      assertEquals(expectedPostfix, result);
   }
}

InfixToPostfixConverter

package com.om.example;

public class InfixToPostfixConverter {
   public String translate(String string) {
      if(null == string)
         return "";

      return string;
   }
}