• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Guava Authors
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 
17 package com.google.common.base;
18 
19 import static com.google.common.collect.ImmutableList.toImmutableList;
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.annotations.GwtIncompatible;
24 import com.google.common.base.Splitter.MapSplitter;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.testing.NullPointerTester;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.regex.Pattern;
31 import junit.framework.TestCase;
32 
33 /** @author Julien Silland */
34 @GwtCompatible(emulated = true)
35 public class SplitterTest extends TestCase {
36 
37   private static final Splitter COMMA_SPLITTER = Splitter.on(',');
38 
testSplitNullString()39   public void testSplitNullString() {
40     try {
41       COMMA_SPLITTER.split(null);
42       fail();
43     } catch (NullPointerException expected) {
44     }
45   }
46 
testCharacterSimpleSplit()47   public void testCharacterSimpleSplit() {
48     String simple = "a,b,c";
49     Iterable<String> letters = COMMA_SPLITTER.split(simple);
50     assertThat(letters).containsExactly("a", "b", "c").inOrder();
51   }
52 
53   /**
54    * All of the infrastructure of split and splitToString is identical, so we do one test of
55    * splitToString. All other cases should be covered by testing of split.
56    *
57    * <p>TODO(user): It would be good to make all the relevant tests run on both split and
58    * splitToString automatically.
59    */
testCharacterSimpleSplitToList()60   public void testCharacterSimpleSplitToList() {
61     String simple = "a,b,c";
62     List<String> letters = COMMA_SPLITTER.splitToList(simple);
63     assertThat(letters).containsExactly("a", "b", "c").inOrder();
64   }
65 
testCharacterSimpleSplitToStream()66   public void testCharacterSimpleSplitToStream() {
67     String simple = "a,b,c";
68     List<String> letters = COMMA_SPLITTER.splitToStream(simple).collect(toImmutableList());
69     assertThat(letters).containsExactly("a", "b", "c").inOrder();
70   }
71 
testToString()72   public void testToString() {
73     assertEquals("[]", COMMA_SPLITTER.split("").toString());
74     assertEquals("[a, b, c]", COMMA_SPLITTER.split("a,b,c").toString());
75     assertEquals("[yam, bam, jam, ham]", Splitter.on(", ").split("yam, bam, jam, ham").toString());
76   }
77 
testCharacterSimpleSplitWithNoDelimiter()78   public void testCharacterSimpleSplitWithNoDelimiter() {
79     String simple = "a,b,c";
80     Iterable<String> letters = Splitter.on('.').split(simple);
81     assertThat(letters).containsExactly("a,b,c").inOrder();
82   }
83 
testCharacterSplitWithDoubleDelimiter()84   public void testCharacterSplitWithDoubleDelimiter() {
85     String doubled = "a,,b,c";
86     Iterable<String> letters = COMMA_SPLITTER.split(doubled);
87     assertThat(letters).containsExactly("a", "", "b", "c").inOrder();
88   }
89 
testCharacterSplitWithDoubleDelimiterAndSpace()90   public void testCharacterSplitWithDoubleDelimiterAndSpace() {
91     String doubled = "a,, b,c";
92     Iterable<String> letters = COMMA_SPLITTER.split(doubled);
93     assertThat(letters).containsExactly("a", "", " b", "c").inOrder();
94   }
95 
testCharacterSplitWithTrailingDelimiter()96   public void testCharacterSplitWithTrailingDelimiter() {
97     String trailing = "a,b,c,";
98     Iterable<String> letters = COMMA_SPLITTER.split(trailing);
99     assertThat(letters).containsExactly("a", "b", "c", "").inOrder();
100   }
101 
testCharacterSplitWithLeadingDelimiter()102   public void testCharacterSplitWithLeadingDelimiter() {
103     String leading = ",a,b,c";
104     Iterable<String> letters = COMMA_SPLITTER.split(leading);
105     assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
106   }
107 
testCharacterSplitWithMultipleLetters()108   public void testCharacterSplitWithMultipleLetters() {
109     Iterable<String> testCharacteringMotto =
110         Splitter.on('-').split("Testing-rocks-Debugging-sucks");
111     assertThat(testCharacteringMotto)
112         .containsExactly("Testing", "rocks", "Debugging", "sucks")
113         .inOrder();
114   }
115 
testCharacterSplitWithMatcherDelimiter()116   public void testCharacterSplitWithMatcherDelimiter() {
117     Iterable<String> testCharacteringMotto =
118         Splitter.on(CharMatcher.whitespace()).split("Testing\nrocks\tDebugging sucks");
119     assertThat(testCharacteringMotto)
120         .containsExactly("Testing", "rocks", "Debugging", "sucks")
121         .inOrder();
122   }
123 
testCharacterSplitWithDoubleDelimiterOmitEmptyStrings()124   public void testCharacterSplitWithDoubleDelimiterOmitEmptyStrings() {
125     String doubled = "a..b.c";
126     Iterable<String> letters = Splitter.on('.').omitEmptyStrings().split(doubled);
127     assertThat(letters).containsExactly("a", "b", "c").inOrder();
128   }
129 
testCharacterSplitEmptyToken()130   public void testCharacterSplitEmptyToken() {
131     String emptyToken = "a. .c";
132     Iterable<String> letters = Splitter.on('.').trimResults().split(emptyToken);
133     assertThat(letters).containsExactly("a", "", "c").inOrder();
134   }
135 
testCharacterSplitEmptyTokenOmitEmptyStrings()136   public void testCharacterSplitEmptyTokenOmitEmptyStrings() {
137     String emptyToken = "a. .c";
138     Iterable<String> letters = Splitter.on('.').omitEmptyStrings().trimResults().split(emptyToken);
139     assertThat(letters).containsExactly("a", "c").inOrder();
140   }
141 
testCharacterSplitOnEmptyString()142   public void testCharacterSplitOnEmptyString() {
143     Iterable<String> nothing = Splitter.on('.').split("");
144     assertThat(nothing).containsExactly("").inOrder();
145   }
146 
testCharacterSplitOnEmptyStringOmitEmptyStrings()147   public void testCharacterSplitOnEmptyStringOmitEmptyStrings() {
148     assertThat(Splitter.on('.').omitEmptyStrings().split("")).isEmpty();
149   }
150 
testCharacterSplitOnOnlyDelimiter()151   public void testCharacterSplitOnOnlyDelimiter() {
152     Iterable<String> blankblank = Splitter.on('.').split(".");
153     assertThat(blankblank).containsExactly("", "").inOrder();
154   }
155 
testCharacterSplitOnOnlyDelimitersOmitEmptyStrings()156   public void testCharacterSplitOnOnlyDelimitersOmitEmptyStrings() {
157     Iterable<String> empty = Splitter.on('.').omitEmptyStrings().split("...");
158     assertThat(empty).isEmpty();
159   }
160 
testCharacterSplitWithTrim()161   public void testCharacterSplitWithTrim() {
162     String jacksons =
163         "arfo(Marlon)aorf, (Michael)orfa, afro(Jackie)orfa, " + "ofar(Jemaine), aff(Tito)";
164     Iterable<String> family =
165         COMMA_SPLITTER
166             .trimResults(CharMatcher.anyOf("afro").or(CharMatcher.whitespace()))
167             .split(jacksons);
168     assertThat(family)
169         .containsExactly("(Marlon)", "(Michael)", "(Jackie)", "(Jemaine)", "(Tito)")
170         .inOrder();
171   }
172 
testStringSimpleSplit()173   public void testStringSimpleSplit() {
174     String simple = "a,b,c";
175     Iterable<String> letters = Splitter.on(",").split(simple);
176     assertThat(letters).containsExactly("a", "b", "c").inOrder();
177   }
178 
testStringSimpleSplitWithNoDelimiter()179   public void testStringSimpleSplitWithNoDelimiter() {
180     String simple = "a,b,c";
181     Iterable<String> letters = Splitter.on(".").split(simple);
182     assertThat(letters).containsExactly("a,b,c").inOrder();
183   }
184 
testStringSplitWithDoubleDelimiter()185   public void testStringSplitWithDoubleDelimiter() {
186     String doubled = "a,,b,c";
187     Iterable<String> letters = Splitter.on(",").split(doubled);
188     assertThat(letters).containsExactly("a", "", "b", "c").inOrder();
189   }
190 
testStringSplitWithDoubleDelimiterAndSpace()191   public void testStringSplitWithDoubleDelimiterAndSpace() {
192     String doubled = "a,, b,c";
193     Iterable<String> letters = Splitter.on(",").split(doubled);
194     assertThat(letters).containsExactly("a", "", " b", "c").inOrder();
195   }
196 
testStringSplitWithTrailingDelimiter()197   public void testStringSplitWithTrailingDelimiter() {
198     String trailing = "a,b,c,";
199     Iterable<String> letters = Splitter.on(",").split(trailing);
200     assertThat(letters).containsExactly("a", "b", "c", "").inOrder();
201   }
202 
testStringSplitWithLeadingDelimiter()203   public void testStringSplitWithLeadingDelimiter() {
204     String leading = ",a,b,c";
205     Iterable<String> letters = Splitter.on(",").split(leading);
206     assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
207   }
208 
testStringSplitWithMultipleLetters()209   public void testStringSplitWithMultipleLetters() {
210     Iterable<String> testStringingMotto = Splitter.on("-").split("Testing-rocks-Debugging-sucks");
211     assertThat(testStringingMotto)
212         .containsExactly("Testing", "rocks", "Debugging", "sucks")
213         .inOrder();
214   }
215 
testStringSplitWithDoubleDelimiterOmitEmptyStrings()216   public void testStringSplitWithDoubleDelimiterOmitEmptyStrings() {
217     String doubled = "a..b.c";
218     Iterable<String> letters = Splitter.on(".").omitEmptyStrings().split(doubled);
219     assertThat(letters).containsExactly("a", "b", "c").inOrder();
220   }
221 
testStringSplitEmptyToken()222   public void testStringSplitEmptyToken() {
223     String emptyToken = "a. .c";
224     Iterable<String> letters = Splitter.on(".").trimResults().split(emptyToken);
225     assertThat(letters).containsExactly("a", "", "c").inOrder();
226   }
227 
testStringSplitEmptyTokenOmitEmptyStrings()228   public void testStringSplitEmptyTokenOmitEmptyStrings() {
229     String emptyToken = "a. .c";
230     Iterable<String> letters = Splitter.on(".").omitEmptyStrings().trimResults().split(emptyToken);
231     assertThat(letters).containsExactly("a", "c").inOrder();
232   }
233 
testStringSplitWithLongDelimiter()234   public void testStringSplitWithLongDelimiter() {
235     String longDelimiter = "a, b, c";
236     Iterable<String> letters = Splitter.on(", ").split(longDelimiter);
237     assertThat(letters).containsExactly("a", "b", "c").inOrder();
238   }
239 
testStringSplitWithLongLeadingDelimiter()240   public void testStringSplitWithLongLeadingDelimiter() {
241     String longDelimiter = ", a, b, c";
242     Iterable<String> letters = Splitter.on(", ").split(longDelimiter);
243     assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
244   }
245 
testStringSplitWithLongTrailingDelimiter()246   public void testStringSplitWithLongTrailingDelimiter() {
247     String longDelimiter = "a, b, c, ";
248     Iterable<String> letters = Splitter.on(", ").split(longDelimiter);
249     assertThat(letters).containsExactly("a", "b", "c", "").inOrder();
250   }
251 
testStringSplitWithDelimiterSubstringInValue()252   public void testStringSplitWithDelimiterSubstringInValue() {
253     String fourCommasAndFourSpaces = ",,,,    ";
254     Iterable<String> threeCommasThenThreeSpaces = Splitter.on(", ").split(fourCommasAndFourSpaces);
255     assertThat(threeCommasThenThreeSpaces).containsExactly(",,,", "   ").inOrder();
256   }
257 
testStringSplitWithEmptyString()258   public void testStringSplitWithEmptyString() {
259     try {
260       Splitter.on("");
261       fail();
262     } catch (IllegalArgumentException expected) {
263     }
264   }
265 
testStringSplitOnEmptyString()266   public void testStringSplitOnEmptyString() {
267     Iterable<String> notMuch = Splitter.on(".").split("");
268     assertThat(notMuch).containsExactly("").inOrder();
269   }
270 
testStringSplitOnEmptyStringOmitEmptyString()271   public void testStringSplitOnEmptyStringOmitEmptyString() {
272     assertThat(Splitter.on(".").omitEmptyStrings().split("")).isEmpty();
273   }
274 
testStringSplitOnOnlyDelimiter()275   public void testStringSplitOnOnlyDelimiter() {
276     Iterable<String> blankblank = Splitter.on(".").split(".");
277     assertThat(blankblank).containsExactly("", "").inOrder();
278   }
279 
testStringSplitOnOnlyDelimitersOmitEmptyStrings()280   public void testStringSplitOnOnlyDelimitersOmitEmptyStrings() {
281     Iterable<String> empty = Splitter.on(".").omitEmptyStrings().split("...");
282     assertThat(empty).isEmpty();
283   }
284 
testStringSplitWithTrim()285   public void testStringSplitWithTrim() {
286     String jacksons =
287         "arfo(Marlon)aorf, (Michael)orfa, afro(Jackie)orfa, " + "ofar(Jemaine), aff(Tito)";
288     Iterable<String> family =
289         Splitter.on(",")
290             .trimResults(CharMatcher.anyOf("afro").or(CharMatcher.whitespace()))
291             .split(jacksons);
292     assertThat(family)
293         .containsExactly("(Marlon)", "(Michael)", "(Jackie)", "(Jemaine)", "(Tito)")
294         .inOrder();
295   }
296 
297   @GwtIncompatible // Splitter.onPattern
testPatternSimpleSplit()298   public void testPatternSimpleSplit() {
299     String simple = "a,b,c";
300     Iterable<String> letters = Splitter.onPattern(",").split(simple);
301     assertThat(letters).containsExactly("a", "b", "c").inOrder();
302   }
303 
304   @GwtIncompatible // Splitter.onPattern
testPatternSimpleSplitWithNoDelimiter()305   public void testPatternSimpleSplitWithNoDelimiter() {
306     String simple = "a,b,c";
307     Iterable<String> letters = Splitter.onPattern("foo").split(simple);
308     assertThat(letters).containsExactly("a,b,c").inOrder();
309   }
310 
311   @GwtIncompatible // Splitter.onPattern
testPatternSplitWithDoubleDelimiter()312   public void testPatternSplitWithDoubleDelimiter() {
313     String doubled = "a,,b,c";
314     Iterable<String> letters = Splitter.onPattern(",").split(doubled);
315     assertThat(letters).containsExactly("a", "", "b", "c").inOrder();
316   }
317 
318   @GwtIncompatible // Splitter.onPattern
testPatternSplitWithDoubleDelimiterAndSpace()319   public void testPatternSplitWithDoubleDelimiterAndSpace() {
320     String doubled = "a,, b,c";
321     Iterable<String> letters = Splitter.onPattern(",").split(doubled);
322     assertThat(letters).containsExactly("a", "", " b", "c").inOrder();
323   }
324 
325   @GwtIncompatible // Splitter.onPattern
testPatternSplitWithTrailingDelimiter()326   public void testPatternSplitWithTrailingDelimiter() {
327     String trailing = "a,b,c,";
328     Iterable<String> letters = Splitter.onPattern(",").split(trailing);
329     assertThat(letters).containsExactly("a", "b", "c", "").inOrder();
330   }
331 
332   @GwtIncompatible // Splitter.onPattern
testPatternSplitWithLeadingDelimiter()333   public void testPatternSplitWithLeadingDelimiter() {
334     String leading = ",a,b,c";
335     Iterable<String> letters = Splitter.onPattern(",").split(leading);
336     assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
337   }
338 
339   // TODO(kevinb): the name of this method suggests it might not actually be testing what it
340   // intends to be testing?
341   @GwtIncompatible // Splitter.onPattern
testPatternSplitWithMultipleLetters()342   public void testPatternSplitWithMultipleLetters() {
343     Iterable<String> testPatterningMotto =
344         Splitter.onPattern("-").split("Testing-rocks-Debugging-sucks");
345     assertThat(testPatterningMotto)
346         .containsExactly("Testing", "rocks", "Debugging", "sucks")
347         .inOrder();
348   }
349 
350   @GwtIncompatible // java.util.regex.Pattern
literalDotPattern()351   private static Pattern literalDotPattern() {
352     return Pattern.compile("\\.");
353   }
354 
355   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitWithDoubleDelimiterOmitEmptyStrings()356   public void testPatternSplitWithDoubleDelimiterOmitEmptyStrings() {
357     String doubled = "a..b.c";
358     Iterable<String> letters = Splitter.on(literalDotPattern()).omitEmptyStrings().split(doubled);
359     assertThat(letters).containsExactly("a", "b", "c").inOrder();
360   }
361 
362   @GwtIncompatible // java.util.regex.Pattern
363   @AndroidIncompatible // Bug in older versions of Android we test against, since fixed.
testPatternSplitLookBehind()364   public void testPatternSplitLookBehind() {
365     if (!CommonPattern.isPcreLike()) {
366       return;
367     }
368     String toSplit = ":foo::barbaz:";
369     String regexPattern = "(?<=:)";
370     Iterable<String> split = Splitter.onPattern(regexPattern).split(toSplit);
371     assertThat(split).containsExactly(":", "foo:", ":", "barbaz:").inOrder();
372     // splits into chunks ending in :
373   }
374 
375   @GwtIncompatible // java.util.regex.Pattern
376   @AndroidIncompatible // Bug in older versions of Android we test against, since fixed.
testPatternSplitWordBoundary()377   public void testPatternSplitWordBoundary() {
378     String string = "foo<bar>bletch";
379     Iterable<String> words = Splitter.on(Pattern.compile("\\b")).split(string);
380     assertThat(words).containsExactly("foo", "<", "bar", ">", "bletch").inOrder();
381   }
382 
383   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitWordBoundary_singleCharInput()384   public void testPatternSplitWordBoundary_singleCharInput() {
385     String string = "f";
386     Iterable<String> words = Splitter.on(Pattern.compile("\\b")).split(string);
387     assertThat(words).containsExactly("f").inOrder();
388   }
389 
390   @AndroidIncompatible // Apparently Gingerbread's regex API is buggy.
391   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitWordBoundary_singleWordInput()392   public void testPatternSplitWordBoundary_singleWordInput() {
393     String string = "foo";
394     Iterable<String> words = Splitter.on(Pattern.compile("\\b")).split(string);
395     assertThat(words).containsExactly("foo").inOrder();
396   }
397 
398   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitEmptyToken()399   public void testPatternSplitEmptyToken() {
400     String emptyToken = "a. .c";
401     Iterable<String> letters = Splitter.on(literalDotPattern()).trimResults().split(emptyToken);
402     assertThat(letters).containsExactly("a", "", "c").inOrder();
403   }
404 
405   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitEmptyTokenOmitEmptyStrings()406   public void testPatternSplitEmptyTokenOmitEmptyStrings() {
407     String emptyToken = "a. .c";
408     Iterable<String> letters =
409         Splitter.on(literalDotPattern()).omitEmptyStrings().trimResults().split(emptyToken);
410     assertThat(letters).containsExactly("a", "c").inOrder();
411   }
412 
413   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitOnOnlyDelimiter()414   public void testPatternSplitOnOnlyDelimiter() {
415     Iterable<String> blankblank = Splitter.on(literalDotPattern()).split(".");
416 
417     assertThat(blankblank).containsExactly("", "").inOrder();
418   }
419 
420   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitOnOnlyDelimitersOmitEmptyStrings()421   public void testPatternSplitOnOnlyDelimitersOmitEmptyStrings() {
422     Iterable<String> empty = Splitter.on(literalDotPattern()).omitEmptyStrings().split("...");
423     assertThat(empty).isEmpty();
424   }
425 
426   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitMatchingIsGreedy()427   public void testPatternSplitMatchingIsGreedy() {
428     String longDelimiter = "a, b,   c";
429     Iterable<String> letters = Splitter.on(Pattern.compile(",\\s*")).split(longDelimiter);
430     assertThat(letters).containsExactly("a", "b", "c").inOrder();
431   }
432 
433   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitWithLongLeadingDelimiter()434   public void testPatternSplitWithLongLeadingDelimiter() {
435     String longDelimiter = ", a, b, c";
436     Iterable<String> letters = Splitter.on(Pattern.compile(", ")).split(longDelimiter);
437     assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
438   }
439 
440   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitWithLongTrailingDelimiter()441   public void testPatternSplitWithLongTrailingDelimiter() {
442     String longDelimiter = "a, b, c/ ";
443     Iterable<String> letters = Splitter.on(Pattern.compile("[,/]\\s")).split(longDelimiter);
444     assertThat(letters).containsExactly("a", "b", "c", "").inOrder();
445   }
446 
447   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitInvalidPattern()448   public void testPatternSplitInvalidPattern() {
449     try {
450       Splitter.on(Pattern.compile("a*"));
451       fail();
452     } catch (IllegalArgumentException expected) {
453     }
454   }
455 
456   @GwtIncompatible // java.util.regex.Pattern
testPatternSplitWithTrim()457   public void testPatternSplitWithTrim() {
458     String jacksons =
459         "arfo(Marlon)aorf, (Michael)orfa, afro(Jackie)orfa, " + "ofar(Jemaine), aff(Tito)";
460     Iterable<String> family =
461         Splitter.on(Pattern.compile(","))
462             .trimResults(CharMatcher.anyOf("afro").or(CharMatcher.whitespace()))
463             .split(jacksons);
464     assertThat(family)
465         .containsExactly("(Marlon)", "(Michael)", "(Jackie)", "(Jemaine)", "(Tito)")
466         .inOrder();
467   }
468 
testSplitterIterableIsUnmodifiable_char()469   public void testSplitterIterableIsUnmodifiable_char() {
470     assertIteratorIsUnmodifiable(COMMA_SPLITTER.split("a,b").iterator());
471   }
472 
testSplitterIterableIsUnmodifiable_string()473   public void testSplitterIterableIsUnmodifiable_string() {
474     assertIteratorIsUnmodifiable(Splitter.on(",").split("a,b").iterator());
475   }
476 
477   @GwtIncompatible // java.util.regex.Pattern
testSplitterIterableIsUnmodifiable_pattern()478   public void testSplitterIterableIsUnmodifiable_pattern() {
479     assertIteratorIsUnmodifiable(Splitter.on(Pattern.compile(",")).split("a,b").iterator());
480   }
481 
assertIteratorIsUnmodifiable(Iterator<?> iterator)482   private void assertIteratorIsUnmodifiable(Iterator<?> iterator) {
483     iterator.next();
484     try {
485       iterator.remove();
486       fail();
487     } catch (UnsupportedOperationException expected) {
488     }
489   }
490 
testSplitterIterableIsLazy_char()491   public void testSplitterIterableIsLazy_char() {
492     assertSplitterIterableIsLazy(COMMA_SPLITTER);
493   }
494 
testSplitterIterableIsLazy_string()495   public void testSplitterIterableIsLazy_string() {
496     assertSplitterIterableIsLazy(Splitter.on(","));
497   }
498 
499   @GwtIncompatible // java.util.regex.Pattern
500   @AndroidIncompatible // not clear that j.u.r.Matcher promises to handle mutations during use
testSplitterIterableIsLazy_pattern()501   public void testSplitterIterableIsLazy_pattern() {
502     if (!CommonPattern.isPcreLike()) {
503       return;
504     }
505     assertSplitterIterableIsLazy(Splitter.onPattern(","));
506   }
507 
508   /**
509    * This test really pushes the boundaries of what we support. In general the splitter's behaviour
510    * is not well defined if the char sequence it's splitting is mutated during iteration.
511    */
assertSplitterIterableIsLazy(Splitter splitter)512   private void assertSplitterIterableIsLazy(Splitter splitter) {
513     StringBuilder builder = new StringBuilder();
514     Iterator<String> iterator = splitter.split(builder).iterator();
515 
516     builder.append("A,");
517     assertEquals("A", iterator.next());
518     builder.append("B,");
519     assertEquals("B", iterator.next());
520     builder.append("C");
521     assertEquals("C", iterator.next());
522     assertFalse(iterator.hasNext());
523   }
524 
testFixedLengthSimpleSplit()525   public void testFixedLengthSimpleSplit() {
526     String simple = "abcde";
527     Iterable<String> letters = Splitter.fixedLength(2).split(simple);
528     assertThat(letters).containsExactly("ab", "cd", "e").inOrder();
529   }
530 
testFixedLengthSplitEqualChunkLength()531   public void testFixedLengthSplitEqualChunkLength() {
532     String simple = "abcdef";
533     Iterable<String> letters = Splitter.fixedLength(2).split(simple);
534     assertThat(letters).containsExactly("ab", "cd", "ef").inOrder();
535   }
536 
testFixedLengthSplitOnlyOneChunk()537   public void testFixedLengthSplitOnlyOneChunk() {
538     String simple = "abc";
539     Iterable<String> letters = Splitter.fixedLength(3).split(simple);
540     assertThat(letters).containsExactly("abc").inOrder();
541   }
542 
testFixedLengthSplitSmallerString()543   public void testFixedLengthSplitSmallerString() {
544     String simple = "ab";
545     Iterable<String> letters = Splitter.fixedLength(3).split(simple);
546     assertThat(letters).containsExactly("ab").inOrder();
547   }
548 
testFixedLengthSplitEmptyString()549   public void testFixedLengthSplitEmptyString() {
550     String simple = "";
551     Iterable<String> letters = Splitter.fixedLength(3).split(simple);
552     assertThat(letters).containsExactly("").inOrder();
553   }
554 
testFixedLengthSplitEmptyStringWithOmitEmptyStrings()555   public void testFixedLengthSplitEmptyStringWithOmitEmptyStrings() {
556     assertThat(Splitter.fixedLength(3).omitEmptyStrings().split("")).isEmpty();
557   }
558 
testFixedLengthSplitIntoChars()559   public void testFixedLengthSplitIntoChars() {
560     String simple = "abcd";
561     Iterable<String> letters = Splitter.fixedLength(1).split(simple);
562     assertThat(letters).containsExactly("a", "b", "c", "d").inOrder();
563   }
564 
testFixedLengthSplitZeroChunkLen()565   public void testFixedLengthSplitZeroChunkLen() {
566     try {
567       Splitter.fixedLength(0);
568       fail();
569     } catch (IllegalArgumentException expected) {
570     }
571   }
572 
testFixedLengthSplitNegativeChunkLen()573   public void testFixedLengthSplitNegativeChunkLen() {
574     try {
575       Splitter.fixedLength(-1);
576       fail();
577     } catch (IllegalArgumentException expected) {
578     }
579   }
580 
testLimitLarge()581   public void testLimitLarge() {
582     String simple = "abcd";
583     Iterable<String> letters = Splitter.fixedLength(1).limit(100).split(simple);
584     assertThat(letters).containsExactly("a", "b", "c", "d").inOrder();
585   }
586 
testLimitOne()587   public void testLimitOne() {
588     String simple = "abcd";
589     Iterable<String> letters = Splitter.fixedLength(1).limit(1).split(simple);
590     assertThat(letters).containsExactly("abcd").inOrder();
591   }
592 
testLimitFixedLength()593   public void testLimitFixedLength() {
594     String simple = "abcd";
595     Iterable<String> letters = Splitter.fixedLength(1).limit(2).split(simple);
596     assertThat(letters).containsExactly("a", "bcd").inOrder();
597   }
598 
testLimit1Separator()599   public void testLimit1Separator() {
600     String simple = "a,b,c,d";
601     Iterable<String> items = COMMA_SPLITTER.limit(1).split(simple);
602     assertThat(items).containsExactly("a,b,c,d").inOrder();
603   }
604 
testLimitSeparator()605   public void testLimitSeparator() {
606     String simple = "a,b,c,d";
607     Iterable<String> items = COMMA_SPLITTER.limit(2).split(simple);
608     assertThat(items).containsExactly("a", "b,c,d").inOrder();
609   }
610 
testLimitExtraSeparators()611   public void testLimitExtraSeparators() {
612     String text = "a,,,b,,c,d";
613     Iterable<String> items = COMMA_SPLITTER.limit(2).split(text);
614     assertThat(items).containsExactly("a", ",,b,,c,d").inOrder();
615   }
616 
testLimitExtraSeparatorsOmitEmpty()617   public void testLimitExtraSeparatorsOmitEmpty() {
618     String text = "a,,,b,,c,d";
619     Iterable<String> items = COMMA_SPLITTER.limit(2).omitEmptyStrings().split(text);
620     assertThat(items).containsExactly("a", "b,,c,d").inOrder();
621   }
622 
testLimitExtraSeparatorsOmitEmpty3()623   public void testLimitExtraSeparatorsOmitEmpty3() {
624     String text = "a,,,b,,c,d";
625     Iterable<String> items = COMMA_SPLITTER.limit(3).omitEmptyStrings().split(text);
626     assertThat(items).containsExactly("a", "b", "c,d").inOrder();
627   }
628 
testLimitExtraSeparatorsTrim()629   public void testLimitExtraSeparatorsTrim() {
630     String text = ",,a,,  , b ,, c,d ";
631     Iterable<String> items = COMMA_SPLITTER.limit(2).omitEmptyStrings().trimResults().split(text);
632     assertThat(items).containsExactly("a", "b ,, c,d").inOrder();
633   }
634 
testLimitExtraSeparatorsTrim3()635   public void testLimitExtraSeparatorsTrim3() {
636     String text = ",,a,,  , b ,, c,d ";
637     Iterable<String> items = COMMA_SPLITTER.limit(3).omitEmptyStrings().trimResults().split(text);
638     assertThat(items).containsExactly("a", "b", "c,d").inOrder();
639   }
640 
testLimitExtraSeparatorsTrim1()641   public void testLimitExtraSeparatorsTrim1() {
642     String text = ",,a,,  , b ,, c,d ";
643     Iterable<String> items = COMMA_SPLITTER.limit(1).omitEmptyStrings().trimResults().split(text);
644     assertThat(items).containsExactly("a,,  , b ,, c,d").inOrder();
645   }
646 
testLimitExtraSeparatorsTrim1NoOmit()647   public void testLimitExtraSeparatorsTrim1NoOmit() {
648     String text = ",,a,,  , b ,, c,d ";
649     Iterable<String> items = COMMA_SPLITTER.limit(1).trimResults().split(text);
650     assertThat(items).containsExactly(",,a,,  , b ,, c,d").inOrder();
651   }
652 
testLimitExtraSeparatorsTrim1Empty()653   public void testLimitExtraSeparatorsTrim1Empty() {
654     String text = "";
655     Iterable<String> items = COMMA_SPLITTER.limit(1).split(text);
656     assertThat(items).containsExactly("").inOrder();
657   }
658 
testLimitExtraSeparatorsTrim1EmptyOmit()659   public void testLimitExtraSeparatorsTrim1EmptyOmit() {
660     String text = "";
661     Iterable<String> items = COMMA_SPLITTER.omitEmptyStrings().limit(1).split(text);
662     assertThat(items).isEmpty();
663   }
664 
testInvalidZeroLimit()665   public void testInvalidZeroLimit() {
666     try {
667       COMMA_SPLITTER.limit(0);
668       fail();
669     } catch (IllegalArgumentException expected) {
670     }
671   }
672 
673   @GwtIncompatible // NullPointerTester
testNullPointers()674   public void testNullPointers() {
675     NullPointerTester tester = new NullPointerTester();
676     tester.testAllPublicStaticMethods(Splitter.class);
677     tester.testAllPublicInstanceMethods(COMMA_SPLITTER);
678     tester.testAllPublicInstanceMethods(COMMA_SPLITTER.trimResults());
679   }
680 
testMapSplitter_trimmedBoth()681   public void testMapSplitter_trimmedBoth() {
682     Map<String, String> m =
683         COMMA_SPLITTER
684             .trimResults()
685             .withKeyValueSeparator(Splitter.on(':').trimResults())
686             .split("boy  : tom , girl: tina , cat  : kitty , dog: tommy ");
687     ImmutableMap<String, String> expected =
688         ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy");
689     assertThat(m).isEqualTo(expected);
690     assertThat(m.entrySet()).containsExactlyElementsIn(expected.entrySet()).inOrder();
691   }
692 
testMapSplitter_trimmedEntries()693   public void testMapSplitter_trimmedEntries() {
694     Map<String, String> m =
695         COMMA_SPLITTER
696             .trimResults()
697             .withKeyValueSeparator(":")
698             .split("boy  : tom , girl: tina , cat  : kitty , dog: tommy ");
699     ImmutableMap<String, String> expected =
700         ImmutableMap.of("boy  ", " tom", "girl", " tina", "cat  ", " kitty", "dog", " tommy");
701 
702     assertThat(m).isEqualTo(expected);
703     assertThat(m.entrySet()).containsExactlyElementsIn(expected.entrySet()).inOrder();
704   }
705 
testMapSplitter_trimmedKeyValue()706   public void testMapSplitter_trimmedKeyValue() {
707     Map<String, String> m =
708         COMMA_SPLITTER
709             .withKeyValueSeparator(Splitter.on(':').trimResults())
710             .split("boy  : tom , girl: tina , cat  : kitty , dog: tommy ");
711     ImmutableMap<String, String> expected =
712         ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy");
713     assertThat(m).isEqualTo(expected);
714     assertThat(m.entrySet()).containsExactlyElementsIn(expected.entrySet()).inOrder();
715   }
716 
testMapSplitter_notTrimmed()717   public void testMapSplitter_notTrimmed() {
718     Map<String, String> m =
719         COMMA_SPLITTER
720             .withKeyValueSeparator(":")
721             .split(" boy:tom , girl: tina , cat :kitty , dog:  tommy ");
722     ImmutableMap<String, String> expected =
723         ImmutableMap.of(" boy", "tom ", " girl", " tina ", " cat ", "kitty ", " dog", "  tommy ");
724     assertThat(m).isEqualTo(expected);
725     assertThat(m.entrySet()).containsExactlyElementsIn(expected.entrySet()).inOrder();
726   }
727 
testMapSplitter_CharacterSeparator()728   public void testMapSplitter_CharacterSeparator() {
729     // try different delimiters.
730     Map<String, String> m =
731         Splitter.on(",").withKeyValueSeparator(':').split("boy:tom,girl:tina,cat:kitty,dog:tommy");
732     ImmutableMap<String, String> expected =
733         ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy");
734 
735     assertThat(m).isEqualTo(expected);
736     assertThat(m.entrySet()).containsExactlyElementsIn(expected.entrySet()).inOrder();
737   }
738 
testMapSplitter_multiCharacterSeparator()739   public void testMapSplitter_multiCharacterSeparator() {
740     // try different delimiters.
741     Map<String, String> m =
742         Splitter.on(",")
743             .withKeyValueSeparator(":^&")
744             .split("boy:^&tom,girl:^&tina,cat:^&kitty,dog:^&tommy");
745     ImmutableMap<String, String> expected =
746         ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy");
747 
748     assertThat(m).isEqualTo(expected);
749     assertThat(m.entrySet()).containsExactlyElementsIn(expected.entrySet()).inOrder();
750   }
751 
testMapSplitter_emptySeparator()752   public void testMapSplitter_emptySeparator() {
753     try {
754       COMMA_SPLITTER.withKeyValueSeparator("");
755       fail();
756     } catch (IllegalArgumentException expected) {
757     }
758   }
759 
testMapSplitter_malformedEntry()760   public void testMapSplitter_malformedEntry() {
761     try {
762       COMMA_SPLITTER.withKeyValueSeparator("=").split("a=1,b,c=2");
763       fail();
764     } catch (IllegalArgumentException expected) {
765     }
766   }
767 
768   /**
769    * Testing the behavior in https://github.com/google/guava/issues/1900 - this behavior may want to
770    * be changed?
771    */
testMapSplitter_extraValueDelimiter()772   public void testMapSplitter_extraValueDelimiter() {
773     try {
774       COMMA_SPLITTER.withKeyValueSeparator("=").split("a=1,c=2=");
775       fail();
776     } catch (IllegalArgumentException expected) {
777     }
778   }
779 
testMapSplitter_orderedResults()780   public void testMapSplitter_orderedResults() {
781     Map<String, String> m =
782         COMMA_SPLITTER.withKeyValueSeparator(":").split("boy:tom,girl:tina,cat:kitty,dog:tommy");
783 
784     assertThat(m.keySet()).containsExactly("boy", "girl", "cat", "dog").inOrder();
785     assertThat(m)
786         .isEqualTo(ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy"));
787 
788     // try in a different order
789     m = COMMA_SPLITTER.withKeyValueSeparator(":").split("girl:tina,boy:tom,dog:tommy,cat:kitty");
790 
791     assertThat(m.keySet()).containsExactly("girl", "boy", "dog", "cat").inOrder();
792     assertThat(m)
793         .isEqualTo(ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy"));
794   }
795 
testMapSplitter_duplicateKeys()796   public void testMapSplitter_duplicateKeys() {
797     try {
798       COMMA_SPLITTER.withKeyValueSeparator(":").split("a:1,b:2,a:3");
799       fail();
800     } catch (IllegalArgumentException expected) {
801     }
802   }
803 
testMapSplitter_varyingTrimLevels()804   public void testMapSplitter_varyingTrimLevels() {
805     MapSplitter splitter = COMMA_SPLITTER.trimResults().withKeyValueSeparator(Splitter.on("->"));
806     Map<String, String> split = splitter.split(" x -> y, z-> a ");
807     assertThat(split).containsEntry("x ", " y");
808     assertThat(split).containsEntry("z", " a");
809   }
810 }
811