View Javadoc

1   /*
2    * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.util.lang;
17  
18  import static org.hamcrest.CoreMatchers.equalTo;
19  import static org.hamcrest.CoreMatchers.is;
20  import static org.hamcrest.CoreMatchers.nullValue;
21  import static org.hamcrest.MatcherAssert.assertThat;
22  
23  import org.junit.Test;
24  import org.junit.experimental.categories.Category;
25  
26  import de.smartics.testdoc.annotations.TestDocHint;
27  import de.smartics.testdoc.annotations.Uut;
28  import de.smartics.testdoc.categories.Technical;
29  import de.smartics.testdoc.categories.type.Coverage;
30  
31  /**
32   * Tests {@link StringFunction#chop(StringBuilder)}.
33   */
34  @Uut(type = StringFunction.class, method = "chop(StringBuilder)")
35  public class StringFunctionChopTest
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    // ****************************** Inner Classes *****************************
44  
45    // ********************************* Methods ********************************
46  
47    // --- prepare --------------------------------------------------------------
48  
49    // --- helper ---------------------------------------------------------------
50  
51    @Test
52    @TestDocHint(sentence = "chop: null -> null", indexSortKey = "001")
53    public void returnsNullIfNullIsPassedAsArgument()
54    {
55      final String result = StringFunction.chop(null);
56      assertThat(result, is(nullValue()));
57    }
58  
59    @Test
60    @TestDocHint(sentence = "chop: \"\" -> \"\"", indexSortKey = "002")
61    public void returnsTheEmptyStringIfTheEmptyStringIsPassedAsArgument()
62    {
63      final String result = StringFunction.chop(new StringBuilder(""));
64      assertThat(result, is(equalTo("")));
65    }
66  
67    @Test
68    @TestDocHint(sentence = "chop: \"x\" -> \"\"", indexSortKey = "003")
69    @Category(Technical.class)
70    public void chopsTheLastCharacterIfThereIsOne()
71    {
72      final String result = StringFunction.chop(new StringBuilder("x"));
73      assertThat(result, is(equalTo("")));
74    }
75  
76    @Test
77    @TestDocHint(sentence = "chop: \"abc\" -> \"ab\"", indexSortKey = "004")
78    public void chopsTheLastCharacterIfNotEmpty()
79    {
80      final String result = StringFunction.chop(new StringBuilder("abc"));
81      assertThat(result, is(equalTo("ab")));
82    }
83  
84    @Test
85    @TestDocHint(sentence = "chop: \"abc\\r\\n\" -> \"abc\"",
86        indexSortKey = "005")
87    public void chopsCarrigeReturnNewLineAsOneCharacter()
88    {
89      final String result = StringFunction.chop(new StringBuilder("abc\r\n"));
90      assertThat(result, is(equalTo("abc")));
91    }
92  
93    @Test
94    @TestDocHint(sentence = "chop: \"\\r\" -> \"\"", indexSortKey = "006")
95    @Category(Technical.class)
96    public void chopsCarrigeReturnWithoutNewLineAsOneChar()
97    {
98      final String result = StringFunction.chop(new StringBuilder("\r"));
99      assertThat(result, is(equalTo("")));
100   }
101 
102   @Test
103   @TestDocHint(sentence = "chop: \"\\r\\n\" -> \"\"", indexSortKey = "007")
104   @Category(Technical.class)
105   public void chopsCarrigeReturnNewLineAsOneCharacterIfTheyAreTheOnlyChars()
106   {
107     final String result = StringFunction.chop(new StringBuilder("\r\n"));
108     assertThat(result, is(equalTo("")));
109   }
110 
111   @Test
112   @TestDocHint(sentence = "chop: \"\\n\" -> \"\"", indexSortKey = "008")
113   @Category(Technical.class)
114   public void handlesASingleNewlineCorrectly()
115   {
116     final String result = StringFunction.chop(new StringBuilder("\n"));
117     assertThat(result, is(equalTo("")));
118   }
119 
120   @Test
121   @TestDocHint(sentence = "chop: \"ab\\n\" -> \"ab\"", indexSortKey = "009")
122   @Category(Coverage.class)
123   public void handlesLengthGreaterOneAndNewlineCorrectly()
124   {
125     final String result = StringFunction.chop(new StringBuilder("ab\n"));
126     assertThat(result, is(equalTo("ab")));
127   }
128 
129   @Test
130   @TestDocHint(sentence = "chop: \"\\r\" -> \"\"", indexSortKey = "010")
131   @Category(Technical.class)
132   public void handlesASingleCarrigeReturnCorrectly()
133   {
134     final String result = StringFunction.chop(new StringBuilder("\r"));
135     assertThat(result, is(equalTo("")));
136   }
137 
138   // --- tests ----------------------------------------------------------------
139 
140 }