Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2013, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.util;
     33 
     34 import com.google.common.collect.Lists;
     35 import org.junit.Assert;
     36 import org.junit.Test;
     37 
     38 import java.util.List;
     39 
     40 public class StringWrapperTest {
     41     @Test
     42     public void testWrapStringByWords() {
     43         validateResult2(new String[]{"abc", "abcdef", "abcdef"},
     44                 "abc\nabcdefabcdef", 6);
     45 
     46         validateResult2(new String[]{"abc", "abcdef", " ", "abcdef"},
     47                 "abc\nabcdef abcdef", 6);
     48 
     49         validateResult2(new String[]{"abc", "abcde ", "fabcde", "f"},
     50                 "abc\nabcde fabcdef", 6);
     51 
     52         validateResult2(new String[]{"abc def ghi ", "kjl mon pqr ", "stu vwx yz"},
     53                 "abc def ghi kjl mon pqr stu vwx yz", 14);
     54 
     55         validateResult2(new String[]{"abcdefg", "hikjlmo", "npqrstu", "vwxyz"},
     56                 "abcdefghikjlmonpqrstuvwxyz", 7);
     57 
     58         validateResult2(new String[]{"abc", "defhig"},
     59                 "abc\ndefhig", 20);
     60     }
     61 
     62     @Test
     63     public void testWrapString() {
     64         validateResult(
     65                 new String[]{"abc", "abcdef", "abcdef"},
     66                 StringWrapper.wrapString("abc\nabcdefabcdef", 6, null));
     67 
     68         validateResult(
     69                 new String[]{"abc"},
     70                 StringWrapper.wrapString("abc", 6, new String[3]));
     71 
     72         validateResult(
     73                 new String[]{"abc"},
     74                 StringWrapper.wrapString("abc", 6, new String[0]));
     75 
     76         validateResult(
     77                 new String[]{"abc"},
     78                 StringWrapper.wrapString("abc", 6, new String[1]));
     79 
     80         validateResult(
     81                 new String[]{""},
     82                 StringWrapper.wrapString("", 6, new String[3]));
     83 
     84         validateResult(
     85                 new String[]{"abcdef"},
     86                 StringWrapper.wrapString("abcdef", 6, new String[3]));
     87 
     88         validateResult(
     89                 new String[]{"abcdef", "abcdef"},
     90                 StringWrapper.wrapString("abcdef\nabcdef", 6, new String[3]));
     91 
     92         validateResult(
     93                 new String[]{"abc", "", "def"},
     94                 StringWrapper.wrapString("abc\n\ndef", 6, new String[3]));
     95 
     96         validateResult(
     97                 new String[]{"", "abcdef"},
     98                 StringWrapper.wrapString("\nabcdef", 6, new String[3]));
     99 
    100         validateResult(
    101                 new String[]{"", "", "abcdef"},
    102                 StringWrapper.wrapString("\n\nabcdef", 6, new String[3]));
    103 
    104         validateResult(
    105                 new String[]{"", "", "abcdef"},
    106                 StringWrapper.wrapString("\n\nabcdef", 6, new String[4]));
    107 
    108         validateResult(
    109                 new String[]{"", "", "abcdef", ""},
    110                 StringWrapper.wrapString("\n\nabcdef\n\n", 6, new String[4]));
    111 
    112         validateResult(
    113                 new String[]{"", "", "abcdef", "a", ""},
    114                 StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[4]));
    115 
    116         validateResult(
    117                 new String[]{"", "", "abcdef", "a", ""},
    118                 StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[0]));
    119 
    120         validateResult(
    121                 new String[]{"", "", "abcdef", "a", ""},
    122                 StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[5]));
    123 
    124         validateResult(
    125                 new String[]{"", "", "a", "b", "c", "d", "e", "f", "a", ""},
    126                 StringWrapper.wrapString("\n\nabcdefa\n\n", 1, new String[5]));
    127     }
    128 
    129     public static void validateResult(String[] expected, String[] actual) {
    130         Assert.assertTrue(actual.length >= expected.length);
    131 
    132         int i;
    133         for (i=0; i<actual.length; i++) {
    134             if (actual[i] == null) {
    135                 Assert.assertTrue(i == expected.length);
    136                 return;
    137             }
    138             Assert.assertTrue(i < expected.length);
    139             Assert.assertEquals(expected[i], actual[i]);
    140         }
    141     }
    142 
    143     public static void validateResult2(String[] expected, String textToWrap, int maxWidth) {
    144         List<String> result = Lists.newArrayList(StringWrapper.wrapStringOnBreaks(textToWrap, maxWidth));
    145 
    146         Assert.assertEquals(expected.length, result.size());
    147         int i;
    148         for (i=0; i<result.size(); i++) {
    149             Assert.assertTrue(i < expected.length);
    150             Assert.assertEquals(expected[i], result.get(i));
    151         }
    152     }
    153 }
    154