-
Notifications
You must be signed in to change notification settings - Fork 69
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
#50 "contains only once" #94
base: master
Are you sure you want to change the base?
Changes from all commits
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,26 @@ | ||
package org.fest.assertions.error; | ||
|
||
public class ShouldContainsOnlyOnce extends BasicErrorMessageFactory { | ||
|
||
public static ErrorMessageFactory shouldContainsOnlyOnce(String actual, String expected, int occurences) { | ||
if (actual == null || expected == null) { | ||
return new ShouldContainsOnlyOnce(); | ||
} | ||
if (occurences == 0) { | ||
return new ShouldContainsOnlyOnce(actual, expected); | ||
} | ||
return new ShouldContainsOnlyOnce(actual, expected, occurences); | ||
} | ||
|
||
private ShouldContainsOnlyOnce() { | ||
super("expecting:<%s> or <%s> not to be null", "", ""); | ||
} | ||
|
||
private ShouldContainsOnlyOnce(String actual, String expected, int occurences) { | ||
super("expecting:\n<%s>\n to appear only once in:\n<%s>\n but appears %s times", expected, actual, occurences); | ||
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 think we can improve a little bit the error message with "it appeared" : |
||
} | ||
|
||
private ShouldContainsOnlyOnce(String actual, String expected) { | ||
super("expecting:\n<%s>\n to appear only once in:\n<%s>\n but it doesn't appear", expected, actual); | ||
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. same idea : |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import static org.fest.assertions.error.ShouldBeNullOrEmpty.shouldBeNullOrEmpty; | ||
import static org.fest.assertions.error.ShouldContainString.shouldContain; | ||
import static org.fest.assertions.error.ShouldContainString.shouldContainIgnoringCase; | ||
import static org.fest.assertions.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce; | ||
import static org.fest.assertions.error.ShouldEndWith.shouldEndWith; | ||
import static org.fest.assertions.error.ShouldHaveSameSizeAs.shouldHaveSameSizeAs; | ||
import static org.fest.assertions.error.ShouldHaveSize.shouldHaveSize; | ||
|
@@ -247,6 +248,30 @@ public void assertEqualsIgnoringCase(AssertionInfo info, String actual, String e | |
private boolean areEqualIgnoringCase(String actual, String expected) { | ||
if (actual == null) return expected == null; | ||
return actual.equalsIgnoreCase(expected); | ||
} | ||
|
||
/** | ||
* Verifies that actual {@code String}s contains only once the pattern {@code String}. | ||
* @param info contains information about the assertion. | ||
* @param actual the actual {@code String}. | ||
* @param pattern the given {@code String}. | ||
* @throws AssertionError if the actual {@code String}s does not contains only once the given {@code String}. | ||
*/ | ||
public void assertContainsOnlyOnce(AssertionInfo info, String actual, String pattern) { | ||
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. rename |
||
if (actual == null || pattern == null) throw failures.failure(info, shouldContainsOnlyOnce(actual, pattern, 0)); | ||
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. to be consistent with the existing code, you should use :
|
||
int occurences = containsOnlyOnce(actual, pattern); | ||
if (occurences == 1) return; | ||
throw failures.failure(info, shouldContainsOnlyOnce(actual, pattern, occurences)); | ||
} | ||
|
||
private int containsOnlyOnce(String actual, String pattern) { | ||
int occurences = 0; | ||
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. format code |
||
for (int i = 0; i <= (actual.length() - pattern.length()); i ++) { | ||
if (actual.substring(i, i + pattern.length()).equals(pattern)) { | ||
occurences ++; | ||
} | ||
} | ||
return occurences; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.fest.assertions.api; | ||
|
||
import static junit.framework.Assert.assertSame; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.fest.assertions.internal.Strings; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
/** | ||
* Tests for <code>{@link StringAssert#containsOnlyOnce(String)}</code>. | ||
* | ||
*/ | ||
public class StringAssert_containsOnlyOnce_Test { | ||
|
||
private Strings strings; | ||
private StringAssert assertions; | ||
|
||
@Before | ||
public void setUp() { | ||
strings = mock(Strings.class); | ||
assertions = new StringAssert("Yodayoda"); | ||
assertions.strings = strings; | ||
} | ||
|
||
@Test | ||
public void should_verify_that_actual_contains_only_once_given_String() { | ||
assertions.containsOnlyOnce("dayo"); | ||
verify(strings).assertContainsOnlyOnce(assertions.info, assertions.actual, "dayo"); | ||
} | ||
|
||
@Test | ||
public void should_return_this() { | ||
StringAssert returned = assertions.containsOnlyOnce("dayo"); | ||
assertSame(assertions, returned); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.fest.assertions.error; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static org.fest.assertions.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce; | ||
|
||
import org.fest.assertions.internal.TestDescription; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ShouldContainsOnlyOnce_Test { | ||
|
||
private ErrorMessageFactory factoryWithSeveralOccurences; | ||
|
||
private ErrorMessageFactory factoryWithNoOccurence; | ||
|
||
private ErrorMessageFactory factoryWithBothParameterNull; | ||
|
||
private ErrorMessageFactory factoryWithActualParameterNull; | ||
|
||
private ErrorMessageFactory factoryWithExpectedParameterNull; | ||
|
||
@Before | ||
public void setUp() { | ||
factoryWithSeveralOccurences = shouldContainsOnlyOnce("aaamotifmotifaabbbmotifaaa", "motif", 3); | ||
factoryWithNoOccurence = shouldContainsOnlyOnce("aaamodifmoifaabbbmotfaaa", "motif", 0); | ||
factoryWithBothParameterNull = shouldContainsOnlyOnce(null, null, 0); | ||
factoryWithActualParameterNull = shouldContainsOnlyOnce(null, "motif", 0); | ||
factoryWithExpectedParameterNull = shouldContainsOnlyOnce("actual", null, 0); | ||
} | ||
|
||
@Test | ||
public void should_create_error_message_when_expected_string_appears_several_times() { | ||
String message = factoryWithSeveralOccurences.create(new TestDescription("Test")); | ||
assertEquals("[Test] expecting:\n<'motif'>\n to appear only once in:\n<'aaamotifmotifaabbbmotifaaa'>\n but appears 3 times", | ||
message); | ||
} | ||
|
||
@Test | ||
public void should_create_error_message_when_expected_string_does_not_appear() { | ||
String message = factoryWithNoOccurence.create(new TestDescription("Test")); | ||
assertEquals("[Test] expecting:\n<'motif'>\n to appear only once in:\n<'aaamodifmoifaabbbmotfaaa'>\n but it doesn't appear", | ||
message); | ||
} | ||
|
||
@Test | ||
public void should_create_error_message_when_expected_string_is_null() { | ||
String message = factoryWithExpectedParameterNull.create(new TestDescription("Test")); | ||
assertEquals("[Test] expecting:<actual> or <pattern> not to be null", | ||
message); | ||
} | ||
|
||
@Test | ||
public void should_create_error_message_when_actual_string_is_null() { | ||
String message = factoryWithActualParameterNull.create(new TestDescription("Test")); | ||
assertEquals("[Test] expecting:<actual> or <pattern> not to be null", | ||
message); | ||
} | ||
|
||
@Test | ||
public void should_create_error_message_when_both_string_are_null() { | ||
String message = factoryWithBothParameterNull.create(new TestDescription("Test")); | ||
assertEquals("[Test] expecting:<actual> or <pattern> not to be null", | ||
message); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.fest.assertions.internal; | ||
|
||
import static org.fest.assertions.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce; | ||
import static org.fest.assertions.test.TestData.someInfo; | ||
import static org.fest.assertions.test.TestFailures.failBecauseExpectedAssertionErrorWasNotThrown; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.fest.assertions.core.AssertionInfo; | ||
import org.junit.Test; | ||
|
||
public class Strings_assertContainsOnlyOnce_Test extends AbstractTest_for_Strings { | ||
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. You should add the following tests to be complete :
|
||
|
||
@Test | ||
public void should_fail_if_actual_contains_more_than_once_given_string() { | ||
AssertionInfo info = someInfo(); | ||
try { | ||
strings.assertContainsOnlyOnce(info, "Yodayoda", "oda"); | ||
} catch (AssertionError e) { | ||
verifyFailureThrownWhenDoesNotContainsOnlyOnce(info, "Yodayoda", "oda", 2); | ||
return; | ||
} | ||
failBecauseExpectedAssertionErrorWasNotThrown(); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_actual_does_not_contains_given_string() { | ||
AssertionInfo info = someInfo(); | ||
try { | ||
strings.assertContainsOnlyOnce(info, "Yoda", "Luke"); | ||
} catch (AssertionError e) { | ||
verifyFailureThrownWhenDoesNotContainsOnlyOnce(info, "Yoda", "Luke", 0); | ||
return; | ||
} | ||
failBecauseExpectedAssertionErrorWasNotThrown(); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_both_Strings_are_null() { | ||
AssertionInfo info = someInfo(); | ||
try { | ||
strings.assertContainsOnlyOnce(someInfo(), null, null); | ||
} catch (AssertionError e) { | ||
verifyFailureThrownWhenDoesNotContainsOnlyOnce(info, null, null, 0); | ||
return; | ||
} | ||
failBecauseExpectedAssertionErrorWasNotThrown(); | ||
} | ||
|
||
private void verifyFailureThrownWhenDoesNotContainsOnlyOnce(AssertionInfo info, String actual, String expected, int occurences) { | ||
verify(failures).failure(info, shouldContainsOnlyOnce(actual, expected, occurences)); | ||
} | ||
} |
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.
the code is not formatted according to fest formatter