• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.base.ReflectionFreeAssertThrows.assertThrows;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import junit.framework.TestCase;
24 
25 /**
26  * Unit test for {@link Ascii}.
27  *
28  * @author Craig Berry
29  */
30 @GwtCompatible
31 public class AsciiTest extends TestCase {
32 
33   /**
34    * The Unicode points {@code 00c1} and {@code 00e1} are the upper- and lowercase forms of
35    * A-with-acute-accent, {@code Á} and {@code á}.
36    */
37   private static final String IGNORED = "`10-=~!@#$%^&*()_+[]\\{}|;':\",./<>?'\u00c1\u00e1\n";
38 
39   private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
40   private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
41 
testToLowerCase()42   public void testToLowerCase() {
43     assertEquals(LOWER, Ascii.toLowerCase(UPPER));
44     assertSame(LOWER, Ascii.toLowerCase(LOWER));
45     assertEquals(IGNORED, Ascii.toLowerCase(IGNORED));
46     assertEquals("foobar", Ascii.toLowerCase("fOobaR"));
47   }
48 
testToUpperCase()49   public void testToUpperCase() {
50     assertEquals(UPPER, Ascii.toUpperCase(LOWER));
51     assertSame(UPPER, Ascii.toUpperCase(UPPER));
52     assertEquals(IGNORED, Ascii.toUpperCase(IGNORED));
53     assertEquals("FOOBAR", Ascii.toUpperCase("FoOBAr"));
54   }
55 
testCharsIgnored()56   public void testCharsIgnored() {
57     for (char c : IGNORED.toCharArray()) {
58       String str = String.valueOf(c);
59       assertEquals(str, c, Ascii.toLowerCase(c));
60       assertEquals(str, c, Ascii.toUpperCase(c));
61       assertFalse(str, Ascii.isLowerCase(c));
62       assertFalse(str, Ascii.isUpperCase(c));
63     }
64   }
65 
testCharsLower()66   public void testCharsLower() {
67     for (char c : LOWER.toCharArray()) {
68       String str = String.valueOf(c);
69       assertTrue(str, c == Ascii.toLowerCase(c));
70       assertFalse(str, c == Ascii.toUpperCase(c));
71       assertTrue(str, Ascii.isLowerCase(c));
72       assertFalse(str, Ascii.isUpperCase(c));
73     }
74   }
75 
testCharsUpper()76   public void testCharsUpper() {
77     for (char c : UPPER.toCharArray()) {
78       String str = String.valueOf(c);
79       assertFalse(str, c == Ascii.toLowerCase(c));
80       assertTrue(str, c == Ascii.toUpperCase(c));
81       assertFalse(str, Ascii.isLowerCase(c));
82       assertTrue(str, Ascii.isUpperCase(c));
83     }
84   }
85 
testTruncate()86   public void testTruncate() {
87     assertEquals("foobar", Ascii.truncate("foobar", 10, "..."));
88     assertEquals("fo...", Ascii.truncate("foobar", 5, "..."));
89     assertEquals("foobar", Ascii.truncate("foobar", 6, "..."));
90     assertEquals("...", Ascii.truncate("foobar", 3, "..."));
91     assertEquals("foobar", Ascii.truncate("foobar", 10, "…"));
92     assertEquals("foo…", Ascii.truncate("foobar", 4, "…"));
93     assertEquals("fo--", Ascii.truncate("foobar", 4, "--"));
94     assertEquals("foobar", Ascii.truncate("foobar", 6, "…"));
95     assertEquals("foob…", Ascii.truncate("foobar", 5, "…"));
96     assertEquals("foo", Ascii.truncate("foobar", 3, ""));
97     assertEquals("", Ascii.truncate("", 5, ""));
98     assertEquals("", Ascii.truncate("", 5, "..."));
99     assertEquals("", Ascii.truncate("", 0, ""));
100   }
101 
testTruncateIllegalArguments()102   public void testTruncateIllegalArguments() {
103     assertThrows(IllegalArgumentException.class, () -> Ascii.truncate("foobar", 2, "..."));
104 
105     assertThrows(IllegalArgumentException.class, () -> Ascii.truncate("foobar", 8, "1234567890"));
106 
107     assertThrows(IllegalArgumentException.class, () -> Ascii.truncate("foobar", -1, "..."));
108 
109     assertThrows(IllegalArgumentException.class, () -> Ascii.truncate("foobar", -1, ""));
110   }
111 
testEqualsIgnoreCase()112   public void testEqualsIgnoreCase() {
113     assertTrue(Ascii.equalsIgnoreCase("", ""));
114     assertFalse(Ascii.equalsIgnoreCase("", "x"));
115     assertFalse(Ascii.equalsIgnoreCase("x", ""));
116     assertTrue(Ascii.equalsIgnoreCase(LOWER, UPPER));
117     assertTrue(Ascii.equalsIgnoreCase(UPPER, LOWER));
118     // Create new strings here to avoid early-out logic.
119     assertTrue(Ascii.equalsIgnoreCase(new String(IGNORED), new String(IGNORED)));
120     // Compare to: "\u00c1".equalsIgnoreCase("\u00e1") == true
121     assertFalse(Ascii.equalsIgnoreCase("\u00c1", "\u00e1"));
122     // Test chars just outside the alphabetic range ('A'-1 vs 'a'-1, 'Z'+1 vs 'z'+1)
123     assertFalse(Ascii.equalsIgnoreCase("@", "`"));
124     assertFalse(Ascii.equalsIgnoreCase("[", "{"));
125   }
126 
127   @GwtIncompatible // String.toUpperCase() has browser semantics
testEqualsIgnoreCaseUnicodeEquivalence()128   public void testEqualsIgnoreCaseUnicodeEquivalence() {
129     // Note that it's possible in future that the JDK's idea to toUpperCase() or equalsIgnoreCase()
130     // may change and break assumptions in this test [*]. This is not a bug in the implementation of
131     // Ascii.equalsIgnoreCase(), but it is a signal that its documentation may need updating as
132     // regards edge cases.
133 
134     // The Unicode point {@code 00df} is the lowercase form of sharp-S (ß), whose uppercase is "SS".
135     assertEquals("PASSWORD", "pa\u00dfword".toUpperCase()); // [*]
136     assertFalse("pa\u00dfword".equalsIgnoreCase("PASSWORD")); // [*]
137     assertFalse(Ascii.equalsIgnoreCase("pa\u00dfword", "PASSWORD"));
138   }
139 }
140