CaseFormatTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 * Copyright (C) 2006 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
17package com.google.common.base;
18
19import static com.google.common.base.CaseFormat.LOWER_CAMEL;
20import static com.google.common.base.CaseFormat.LOWER_HYPHEN;
21import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE;
22import static com.google.common.base.CaseFormat.UPPER_CAMEL;
23import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
24
25import com.google.common.annotations.GwtCompatible;
26
27import junit.framework.TestCase;
28
29/**
30 * Unit test for {@link CaseFormat}.
31 *
32 * @author Mike Bostock
33 */
34@GwtCompatible
35public class CaseFormatTest extends TestCase {
36
37  public void testIdentity() {
38    for (CaseFormat from : CaseFormat.values()) {
39      assertSame(from + " to " + from, "foo", from.to(from, "foo"));
40      for (CaseFormat to : CaseFormat.values()) {
41        assertEquals(from + " to " + to, "", from.to(to, ""));
42        assertEquals(from + " to " + to, " ", from.to(to, " "));
43      }
44    }
45  }
46
47  public void testNullPointer() {
48    try {
49      LOWER_CAMEL.to(null, "");
50      fail();
51    } catch (NullPointerException expected) {}
52    try {
53      LOWER_CAMEL.to(LOWER_HYPHEN, null);
54      fail();
55    } catch (NullPointerException expected) {}
56  }
57
58  public void testLowerHyphenToLowerHyphen() {
59    assertEquals("foo", LOWER_HYPHEN.to(LOWER_HYPHEN, "foo"));
60    assertEquals("foo-bar", LOWER_HYPHEN.to(LOWER_HYPHEN, "foo-bar"));
61  }
62
63  public void testLowerHyphenToLowerUnderscore() {
64    assertEquals("foo", LOWER_HYPHEN.to(LOWER_UNDERSCORE, "foo"));
65    assertEquals("foo_bar", LOWER_HYPHEN.to(LOWER_UNDERSCORE, "foo-bar"));
66  }
67
68  public void testLowerHyphenToLowerCamel() {
69    assertEquals("foo", LOWER_HYPHEN.to(LOWER_CAMEL, "foo"));
70    assertEquals("fooBar", LOWER_HYPHEN.to(LOWER_CAMEL, "foo-bar"));
71  }
72
73  public void testLowerHyphenToUpperCamel() {
74    assertEquals("Foo", LOWER_HYPHEN.to(UPPER_CAMEL, "foo"));
75    assertEquals("FooBar", LOWER_HYPHEN.to(UPPER_CAMEL, "foo-bar"));
76  }
77
78  public void testLowerHyphenToUpperUnderscore() {
79    assertEquals("FOO", LOWER_HYPHEN.to(UPPER_UNDERSCORE, "foo"));
80    assertEquals("FOO_BAR", LOWER_HYPHEN.to(UPPER_UNDERSCORE, "foo-bar"));
81  }
82
83  public void testLowerUnderscoreToLowerHyphen() {
84    assertEquals("foo", LOWER_UNDERSCORE.to(LOWER_HYPHEN, "foo"));
85    assertEquals("foo-bar", LOWER_UNDERSCORE.to(LOWER_HYPHEN, "foo_bar"));
86  }
87
88  public void testLowerUnderscoreToLowerUnderscore() {
89    assertEquals("foo", LOWER_UNDERSCORE.to(LOWER_UNDERSCORE, "foo"));
90    assertEquals("foo_bar", LOWER_UNDERSCORE.to(LOWER_UNDERSCORE, "foo_bar"));
91  }
92
93  public void testLowerUnderscoreToLowerCamel() {
94    assertEquals("foo", LOWER_UNDERSCORE.to(LOWER_CAMEL, "foo"));
95    assertEquals("fooBar", LOWER_UNDERSCORE.to(LOWER_CAMEL, "foo_bar"));
96  }
97
98  public void testLowerUnderscoreToUpperCamel() {
99    assertEquals("Foo", LOWER_UNDERSCORE.to(UPPER_CAMEL, "foo"));
100    assertEquals("FooBar", LOWER_UNDERSCORE.to(UPPER_CAMEL, "foo_bar"));
101  }
102
103  public void testLowerUnderscoreToUpperUnderscore() {
104    assertEquals("FOO", LOWER_UNDERSCORE.to(UPPER_UNDERSCORE, "foo"));
105    assertEquals("FOO_BAR", LOWER_UNDERSCORE.to(UPPER_UNDERSCORE, "foo_bar"));
106  }
107
108  public void testLowerCamelToLowerHyphen() {
109    assertEquals("foo", LOWER_CAMEL.to(LOWER_HYPHEN, "foo"));
110    assertEquals("foo-bar", LOWER_CAMEL.to(LOWER_HYPHEN, "fooBar"));
111    assertEquals("h-t-t-p", LOWER_CAMEL.to(LOWER_HYPHEN, "HTTP"));
112  }
113
114  public void testLowerCamelToLowerUnderscore() {
115    assertEquals("foo", LOWER_CAMEL.to(LOWER_UNDERSCORE, "foo"));
116    assertEquals("foo_bar", LOWER_CAMEL.to(LOWER_UNDERSCORE, "fooBar"));
117    assertEquals("h_t_t_p", LOWER_CAMEL.to(LOWER_UNDERSCORE, "hTTP"));
118  }
119
120  public void testLowerCamelToLowerCamel() {
121    assertEquals("foo", LOWER_CAMEL.to(LOWER_CAMEL, "foo"));
122    assertEquals("fooBar", LOWER_CAMEL.to(LOWER_CAMEL, "fooBar"));
123  }
124
125  public void testLowerCamelToUpperCamel() {
126    assertEquals("Foo", LOWER_CAMEL.to(UPPER_CAMEL, "foo"));
127    assertEquals("FooBar", LOWER_CAMEL.to(UPPER_CAMEL, "fooBar"));
128    assertEquals("HTTP", LOWER_CAMEL.to(UPPER_CAMEL, "hTTP"));
129  }
130
131  public void testLowerCamelToUpperUnderscore() {
132    assertEquals("FOO", LOWER_CAMEL.to(UPPER_UNDERSCORE, "foo"));
133    assertEquals("FOO_BAR", LOWER_CAMEL.to(UPPER_UNDERSCORE, "fooBar"));
134  }
135
136  public void testUpperCamelToLowerHyphen() {
137    assertEquals("foo", UPPER_CAMEL.to(LOWER_HYPHEN, "Foo"));
138    assertEquals("foo-bar", UPPER_CAMEL.to(LOWER_HYPHEN, "FooBar"));
139  }
140
141  public void testUpperCamelToLowerUnderscore() {
142    assertEquals("foo", UPPER_CAMEL.to(LOWER_UNDERSCORE, "Foo"));
143    assertEquals("foo_bar", UPPER_CAMEL.to(LOWER_UNDERSCORE, "FooBar"));
144  }
145
146  public void testUpperCamelToLowerCamel() {
147    assertEquals("foo", UPPER_CAMEL.to(LOWER_CAMEL, "Foo"));
148    assertEquals("fooBar", UPPER_CAMEL.to(LOWER_CAMEL, "FooBar"));
149    assertEquals("hTTP", UPPER_CAMEL.to(LOWER_CAMEL, "HTTP"));
150  }
151
152  public void testUpperCamelToUpperCamel() {
153    assertEquals("Foo", UPPER_CAMEL.to(UPPER_CAMEL, "Foo"));
154    assertEquals("FooBar", UPPER_CAMEL.to(UPPER_CAMEL, "FooBar"));
155  }
156
157  public void testUpperCamelToUpperUnderscore() {
158    assertEquals("FOO", UPPER_CAMEL.to(UPPER_UNDERSCORE, "Foo"));
159    assertEquals("FOO_BAR", UPPER_CAMEL.to(UPPER_UNDERSCORE, "FooBar"));
160    assertEquals("H_T_T_P", UPPER_CAMEL.to(UPPER_UNDERSCORE, "HTTP"));
161    assertEquals("H__T__T__P", UPPER_CAMEL.to(UPPER_UNDERSCORE, "H_T_T_P"));
162  }
163
164  public void testUpperUnderscoreToLowerHyphen() {
165    assertEquals("foo", UPPER_UNDERSCORE.to(LOWER_HYPHEN, "FOO"));
166    assertEquals("foo-bar", UPPER_UNDERSCORE.to(LOWER_HYPHEN, "FOO_BAR"));
167  }
168
169  public void testUpperUnderscoreToLowerUnderscore() {
170    assertEquals("foo", UPPER_UNDERSCORE.to(LOWER_UNDERSCORE, "FOO"));
171    assertEquals("foo_bar", UPPER_UNDERSCORE.to(LOWER_UNDERSCORE, "FOO_BAR"));
172  }
173
174  public void testUpperUnderscoreToLowerCamel() {
175    assertEquals("foo", UPPER_UNDERSCORE.to(LOWER_CAMEL, "FOO"));
176    assertEquals("fooBar", UPPER_UNDERSCORE.to(LOWER_CAMEL, "FOO_BAR"));
177  }
178
179  public void testUpperUnderscoreToUpperCamel() {
180    assertEquals("Foo", UPPER_UNDERSCORE.to(UPPER_CAMEL, "FOO"));
181    assertEquals("FooBar", UPPER_UNDERSCORE.to(UPPER_CAMEL, "FOO_BAR"));
182    assertEquals("HTTP", UPPER_UNDERSCORE.to(UPPER_CAMEL, "H_T_T_P"));
183  }
184
185  public void testUpperUnderscoreToUpperUnderscore() {
186    assertEquals("FOO", UPPER_UNDERSCORE.to(UPPER_UNDERSCORE, "FOO"));
187    assertEquals("FOO_BAR", UPPER_UNDERSCORE.to(UPPER_UNDERSCORE, "FOO_BAR"));
188  }
189}
190