-
Notifications
You must be signed in to change notification settings - Fork 171
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
Added Capitalized #1465
Added Capitalized #1465
Changes from 1 commit
e0d374b
d9a55ab
06a1c65
114a646
03c34da
33c1c2c
09b9113
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2020 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.cactoos.text; | ||
|
||
import org.cactoos.Text; | ||
|
||
/** | ||
* Text in capitalized case, changed the first character to title case, no other | ||
* characters are changed. | ||
* | ||
* @since 0.46 | ||
*/ | ||
public class Capitalized extends TextEnvelope { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn The class should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
*/ | ||
public Capitalized(final String text) { | ||
this(new TextOf(text)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
*/ | ||
public Capitalized(final Text text) { | ||
super( | ||
new TextOf( | ||
() -> { | ||
String capitalized = text.asString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn Let's remove mutable variable from here. I guess There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done but I am not fully convinced to using Ternary. It seems be more complicated than previous version. |
||
if (!capitalized.isEmpty()) { | ||
final Text first = new Sub(capitalized, 0, 1); | ||
final Text upper = new Upper(first); | ||
if (!upper.equals(first)) { | ||
capitalized = new StringBuilder().append(upper).append(new Sub(text, 1)) | ||
.toString(); | ||
} | ||
} | ||
return capitalized; | ||
})); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2020 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.cactoos.text; | ||
|
||
import org.junit.Test; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn Please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope I did it in proper way, I need to update path to @test annotation to allow Eclipse to handle it. |
||
import org.llorllale.cactoos.matchers.Assertion; | ||
import org.llorllale.cactoos.matchers.TextIs; | ||
|
||
/** | ||
* Test case for {@link Capitalized}. | ||
* @since 0.46 | ||
*/ | ||
public class CapitalizedTest { | ||
/** | ||
* Text starting with upper case character. | ||
*/ | ||
private static final String UPPER_CASE = "Abc"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn I believe this constant is not necessary. If it causes Qulice complain, it would be better to suppress that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
@Test | ||
public void capitalizeEmptyText() { | ||
new Assertion<>( | ||
"Can't capitalize an empty text", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn I suggest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to use "Can't" because as I see it it used commonly in other tests in similar context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn please let's use must as it is the default we use in cactoos (there are still examples where can't is used but this should be changed bit by bit) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
new Capitalized(new TextOf("")), | ||
new TextIs("") | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void capitalizeSingleLowerCaseText() { | ||
new Assertion<>( | ||
"Can't capitalize single lower case text", | ||
new Capitalized(new TextOf("f")), | ||
new TextIs("F") | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void capitalizeSingleUpperCaseText() { | ||
new Assertion<>( | ||
"Can't capitalize single upper case text", | ||
new Capitalized(new TextOf("F")), | ||
new TextIs("F") | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void capitalizeTextStartingWithUpperCaseCharacter() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kokodyn There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
new Assertion<>( | ||
"Can't capitalize text starting with upper case character", | ||
new Capitalized(new TextOf(CapitalizedTest.UPPER_CASE)), | ||
new TextIs(CapitalizedTest.UPPER_CASE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void capitalizeTextStartingWithLowerCaseCharacter() { | ||
new Assertion<>( | ||
"Can't capitalize text starting with lower case character", | ||
new Capitalized(new TextOf("xyz")), | ||
new TextIs("Xyz") | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void capitalizeString() { | ||
new Assertion<>( | ||
"Can't capitalize string", | ||
new Capitalized("foo"), | ||
new TextIs("Foo") | ||
).affirm(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kokodyn May be we should name it
Title
instead?Should it make all non-first characters lower-cased?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will create ticket for "Capitalized" issue and we can discuss further,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created issue for this:
#1466