Skip to content

Issue 1673 adding upper hyphen type case format #5304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions guava-tests/test/com/google/common/base/CaseFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

package com.google.common.base;

import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.LOWER_HYPHEN;
import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import junit.framework.TestCase;

import static com.google.common.base.CaseFormat.*;

/**
* Unit test for {@link CaseFormat}.
*
Expand Down Expand Up @@ -60,6 +56,59 @@ public void testLowerHyphenToLowerHyphen() {
assertEquals("foo-bar", LOWER_HYPHEN.to(LOWER_HYPHEN, "foo-bar"));
}

public void testUpperHyphenToUpperHyphen() {
assertEquals("FOO", UPPER_HYPHEN.to(UPPER_HYPHEN, "FOO"));
assertEquals("FOO-BAR", UPPER_HYPHEN.to(UPPER_HYPHEN, "FOO-BAR"));

}

public void testUpperHyphenToUpperUnderscore() {
assertEquals("FOO", UPPER_HYPHEN.to(UPPER_UNDERSCORE, "FOO"));
assertEquals("FOO_BAR", UPPER_HYPHEN.to(UPPER_UNDERSCORE, "FOO-BAR"));

}

public void testLowerUnderscoreToUpperHyphen() {
assertEquals("FOO", LOWER_UNDERSCORE.to(UPPER_HYPHEN, "foo"));
assertEquals("FOO-BAR", LOWER_UNDERSCORE.to(UPPER_HYPHEN, "foo_bar"));

}

public void testUpperUnderscoreToUpperHyphen() {
assertEquals("FOO", UPPER_UNDERSCORE.to(UPPER_HYPHEN, "FOO"));
assertEquals("FOO-BAR", UPPER_UNDERSCORE.to(UPPER_HYPHEN, "FOO_BAR"));
}

public void testLowerHyphenToUpperHyphen() {
assertEquals("FOO", LOWER_HYPHEN.to(UPPER_HYPHEN, "foo"));
assertEquals("FOO-BAR", LOWER_HYPHEN.to(UPPER_HYPHEN, "foo-bar"));
}

public void testUpperHyphenToLowerHyphen() {
assertEquals("foo", UPPER_HYPHEN.to(LOWER_HYPHEN, "FOO"));
assertEquals("foo-bar", UPPER_HYPHEN.to(LOWER_HYPHEN, "FOO-BAR"));
}

public void testUpperHyphenToLowerCamel() {
assertEquals("foo", UPPER_HYPHEN.to(LOWER_CAMEL, "FOO"));
assertEquals("fooBar", UPPER_HYPHEN.to(LOWER_CAMEL, "FOO-BAR"));
}

public void testUpperHyphenToUpperCamel() {
assertEquals("Foo", UPPER_HYPHEN.to(UPPER_CAMEL, "FOO"));
assertEquals("FooBar", UPPER_HYPHEN.to(UPPER_CAMEL, "FOO-BAR"));
}

public void testUpperCamelToUpperHyphen() {
assertEquals("FOO", UPPER_CAMEL.to(UPPER_HYPHEN, "Foo"));
assertEquals("FOO-BAR", UPPER_CAMEL.to(UPPER_HYPHEN, "FooBar"));
}

public void testLowerCamelToUpperHyphen() {
assertEquals("FOO", LOWER_CAMEL.to(UPPER_HYPHEN, "foo"));
assertEquals("FOO-BAR", LOWER_CAMEL.to(UPPER_HYPHEN, "fooBar"));
}

public void testLowerHyphenToLowerUnderscore() {
assertEquals("foo", LOWER_HYPHEN.to(LOWER_UNDERSCORE, "foo"));
assertEquals("foo_bar", LOWER_HYPHEN.to(LOWER_UNDERSCORE, "foo-bar"));
Expand Down
31 changes: 31 additions & 0 deletions guava/src/com/google/common/base/CaseFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ String convert(CaseFormat format, String s) {
if (format == UPPER_UNDERSCORE) {
return Ascii.toUpperCase(s.replace('-', '_'));
}
if (format == UPPER_HYPHEN) {
return Ascii.toUpperCase(s);
}
return super.convert(format, s);
}
},
/** Hyphenated variable naming convention, e.g., "UPPER_HYPHEN". */
UPPER_HYPHEN(CharMatcher.is('-'), "-") {
@Override
String normalizeWord(String word) {
return Ascii.toUpperCase(word);
}

@Override
String convert(CaseFormat format, String s) {
if (format == UPPER_UNDERSCORE) {
return s.replace('-', '_');
}
if (format == LOWER_UNDERSCORE) {
return Ascii.toUpperCase(s.replace('_', '-'));
}
if (format == LOWER_HYPHEN) {
return Ascii.toLowerCase(s);
}
return super.convert(format, s);
}
},
Expand All @@ -63,7 +87,11 @@ String convert(CaseFormat format, String s) {
if (format == UPPER_UNDERSCORE) {
return Ascii.toUpperCase(s);
}
if (format == UPPER_HYPHEN) {
return Ascii.toUpperCase(s.replace("_", "-"));
}
return super.convert(format, s);

}
},

Expand Down Expand Up @@ -103,6 +131,9 @@ String convert(CaseFormat format, String s) {
if (format == LOWER_UNDERSCORE) {
return Ascii.toLowerCase(s);
}
if (format == UPPER_HYPHEN) {
return s.replace("_", "-");
}
return super.convert(format, s);
}
};
Expand Down