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