-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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.Scalar; | ||
import org.cactoos.Text; | ||
|
||
/** | ||
* Tests if this Text contains other Text. | ||
* | ||
* @since 1.0 | ||
*/ | ||
public final class Contains implements Scalar<Boolean> { | ||
|
||
/** | ||
* The origin. | ||
*/ | ||
private final Text origin; | ||
|
||
/** | ||
* The other. | ||
*/ | ||
private final Text other; | ||
|
||
/** | ||
* Ctor. | ||
* @param origin The origin | ||
* @param other The other | ||
*/ | ||
public Contains(final String origin, final String other) { | ||
this(new TextOf(origin), new TextOf(other)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param origin The origin | ||
* @param other The other | ||
*/ | ||
public Contains(final String origin, final Text other) { | ||
this(new TextOf(origin), other); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param origin The origin | ||
* @param other The other | ||
*/ | ||
public Contains(final Text origin, final String other) { | ||
this(origin, new TextOf(other)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param origin The origin | ||
* @param other The other | ||
*/ | ||
public Contains(final Text origin, final Text other) { | ||
this.origin = origin; | ||
this.other = other; | ||
} | ||
|
||
@Override | ||
public Boolean value() throws Exception { | ||
return this.origin.asString().contains(this.other.asString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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; | ||
import org.llorllale.cactoos.matchers.Assertion; | ||
import org.llorllale.cactoos.matchers.ScalarHasValue; | ||
|
||
/** | ||
* Test case for {@link Contains}. | ||
* @since 1.0 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
*/ | ||
public final class ContainsTest { | ||
|
||
@Test | ||
public void textContainsText() { | ||
new Assertion<>( | ||
"Text contains other Text", | ||
new Contains( | ||
new TextOf("Elegant Object"), | ||
new TextOf("Elegant") | ||
), | ||
new ScalarHasValue<>(Boolean.TRUE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void textDoesNotContainText() { | ||
new Assertion<>( | ||
"Text does not contain other Text", | ||
new Contains( | ||
new TextOf("Java is awesome"), | ||
new TextOf("good") | ||
), | ||
new ScalarHasValue<>(Boolean.FALSE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void textContainsString() { | ||
new Assertion<>( | ||
"Text contains other String", | ||
new Contains( | ||
new TextOf("The quick brown fox"), | ||
"fox" | ||
), | ||
new ScalarHasValue<>(Boolean.TRUE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void textDoesNotContainString() { | ||
new Assertion<>( | ||
"Text does not contain other String", | ||
new Contains( | ||
new TextOf("Stack Overflow"), | ||
"nope" | ||
), | ||
new ScalarHasValue<>(Boolean.FALSE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void stringContainsText() { | ||
new Assertion<>( | ||
"String contains other Text", | ||
new Contains( | ||
"Terra incognita", | ||
new TextOf("cognita") | ||
), | ||
new ScalarHasValue<>(Boolean.TRUE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void stringDoesNotContainText() { | ||
new Assertion<>( | ||
"String does not contain other Text", | ||
new Contains( | ||
"ratio", | ||
new TextOf("Cogito egro sum") | ||
), | ||
new ScalarHasValue<>(Boolean.FALSE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void stringContainsString() { | ||
new Assertion<>( | ||
"String contains other String", | ||
new Contains( | ||
"Lazy dog", | ||
"dog" | ||
), | ||
new ScalarHasValue<>(Boolean.TRUE) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void stringDoesNotContainString() { | ||
new Assertion<>( | ||
"String does not contain other String", | ||
new Contains( | ||
"Lorem ipsum", | ||
"test" | ||
), | ||
new ScalarHasValue<>(Boolean.FALSE) | ||
).affirm(); | ||
} | ||
} |