Чистый код. Создание, анализ и рефакторинг — страница 52 из 94

    String failure= new ComparisonCompactor(0, null, "a").compact(null);

    assertEquals("expected: but was:", failure);

  }


  public void testComparisonErrorWithExpectedNullContext() {

    String failure= new ComparisonCompactor(2, null, "a").compact(null);

    assertEquals("expected: but was:", failure);

  }


  public void testBug609972() {

    String failure= new ComparisonCompactor(10, "S&P500", "0").compact(null);

    assertEquals("expected:<[S&P50]0> but was:<[]0>", failure);

  }

}

Я провел для ComparisonCompactor анализ покрытия кода на основе этих тестов. В ходе тестирования обеспечивалось 100%-ное покрытие: была выполнена каждая строка кода, каждая команда if и цикл for. Я удостоверился в том, что код работает правильно, а также преисполнился уважения к мастерству его авторов.

Код ComparisonCompactor приведен в листинге 15.2. Не жалейте времени и как следует разберитесь в нем. Вероятно, вы согласитесь с тем, что код достаточно выразителен, обладает логичным разбиением и простой структурой. А когда вы закончите, мы вместе начнем придираться к мелочам.


Листинг 15.2. ComparisonCompactor.java (исходный код)

package junit.framework;


public class ComparisonCompactor {


  private static final String ELLIPSIS = "...";

  private static final String DELTA_END = "]";

  private static final String DELTA_START = "[";


  private int fContextLength;

  private String fExpected;

  private String fActual;

  private int fPrefix;

  private int fSuffix;

  public ComparisonCompactor(int contextLength,

                             String expected,

                               String actual) {

    fContextLength = contextLength;

    fExpected = expected;

    fActual = actual;

  }


  public String compact(String message) {

    if (fExpected == null || fActual == null || areStringsEqual())

      return Assert.format(message, fExpected, fActual);

    findCommonPrefix();

    findCommonSuffix();

    String expected = compactString(fExpected);

    String actual = compactString(fActual);

    return Assert.format(message, expected, actual);

  }


  private String compactString(String source) {

    String result = DELTA_START +

                      source.substring(fPrefix, source.length() -

                        fSuffix + 1) + DELTA_END;

    if (fPrefix > 0)

      result = computeCommonPrefix() + result;

    if (fSuffix > 0)

      result = result + computeCommonSuffix();

    return result;

  }


  private void findCommonPrefix() {

    fPrefix = 0;

    int end = Math.min(fExpected.length(), fActual.length());

    for (; fPrefix < end; fPrefix++) {

      if (fExpected.charAt(fPrefix) != fActual.charAt(fPrefix))

        break;

    }

  }


  private void findCommonSuffix() {

    int expectedSuffix = fExpected.length() - 1;

    int actualSuffix = fActual.length() - 1;

    for (;

   actualSuffix >= fPrefix && expectedSuffix >= fPrefix;

          actualSuffix--, expectedSuffix--) {

      if (fExpected.charAt(expectedSuffix) != fActual.charAt(actualSuffix))

        break;

    }

    fSuffix = fExpected.length() - expectedSuffix;

  }

  private String computeCommonPrefix() {


Листинг 15.2 (продолжение)

    return (fPrefix > fContextLength ? ELLIPSIS : "") +

             fExpected.substring(Math.max(0, fPrefix - fContextLength),

                                    fPrefix);

  }


  private String computeCommonSuffix() {

    int end = Math.min(fExpected.length() - fSuffix + 1 + fContextLength,

                         fExpected.length());

    return fExpected.substring(fExpected.length() - fSuffix + 1, end) +

           (fExpected.length() - fSuffix + 1 < fExpected.length() -

            fContextLength ? ELLIPSIS : "");

  }


  private boolean areStringsEqual() {

    return fExpected.equals(fActual);

  }

}

Вероятно, вы найдете в этом модуле некоторые недочеты. В нем встречаются длинные выражения, какие-то малопонятные +1 и т.д. Но в целом модуль весьма хорош. В конце концов, он мог бы выглядеть и так, как показано в листинге 15.3.


Листинг 15.3. ComparisonCompator.java (переработанная версия)

package junit.framework;


public class ComparisonCompactor {

  private int ctxt;

  private String s1;

  private String s2;

  private int pfx;

  private int sfx;


  public ComparisonCompactor(int ctxt, String s1, String s2) {

    this.ctxt = ctxt;

    this.s1 = s1;

    this.s2 = s2;

  }


  public String compact(String msg) {

    if (s1 == null || s2 == null || s1.equals(s2))

      return Assert.format(msg, s1, s2);


    pfx = 0;

    for (; pfx < Math.min(s1.length(), s2.length()); pfx++) {

      if (s1.charAt(pfx) != s2.charAt(pfx))

        break;

    }

    int sfx1 = s1.length() - 1;

    int sfx2 = s2.length() - 1;

    for (; sfx2 >= pfx && sfx1 >= pfx; sfx2--, sfx1--) {

      if (s1.charAt(sfx1) != s2.charAt(sfx2))

        break;

    }

    sfx = s1.length() - sfx1;

    String cmp1 = compactString(s1);

    String cmp2 = compactString(s2);

    return Assert.format(msg, cmp1, cmp2);

  }


  private String compactString(String s) {

    String result =

      "[" + s.substring(pfx, s.length() - sfx + 1) + "]";

    if (pfx > 0)

      result = (pfx > ctxt ? "..." : "") +

        s1.substring(Math.max(0, pfx - ctxt), pfx) + result;

    if (sfx > 0) {

      int end = Math.min(s1.length() - sfx + 1 + ctxt, s1.length());

      result = result + (s1.substring(s1.length() - sfx + 1, end) +