Skip to content
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

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
15 changes: 13 additions & 2 deletions src/main/java/org/fest/assertions/api/StringAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author Alex Ruiz
* @author Joel Costigliola
* @author Mikhail Mazursky
* @author Nicolas François
* @author Nicolas Francois
*/
public class StringAssert extends AbstractAssert<StringAssert, String> implements EnumerableAssert<StringAssert, String> {

Expand Down Expand Up @@ -91,6 +91,17 @@ public StringAssert isEqualToIgnoringCase(String expected) {
}

/**
* Verifies that the actual {@code String} contains only once the pattern.
* @param pattern the given {@code String} to be contained in the actual {@code String}.
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code String} does not contain the given one.
*/
public StringAssert containsOnlyOnce(String pattern) {
strings.assertContainsOnlyOnce(info, actual, pattern);
return this;
}

/**
* Verifies that the actual {@code String} contains the given sequence.
* @param sequence the sequence to search for.
* @return {@code this} assertion object.
Expand Down Expand Up @@ -207,7 +218,7 @@ public StringAssert doesNotMatch(Pattern pattern) {
strings.assertDoesNotMatch(info, actual, pattern);
return this;
}

/** {@inheritDoc} */
public StringAssert usingElementComparator(Comparator<? super String> customComparator) {
// TODO maybe use Comparator<? super Character>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.fest.assertions.error;
Copy link
Contributor

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


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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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" :
"expecting:\n<%s>\n to appear only once in:\n<%s>\n but it appeared %s times"

}

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same idea : "expecting:\n<%s>\n to appear only once in:\n<%s>\n but it did not appear"

}
}
25 changes: 25 additions & 0 deletions src/main/java/org/fest/assertions/internal/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename pattern to sequence (pattern makes me think we are using regexp pattern matching)

if (actual == null || pattern == null) throw failures.failure(info, shouldContainsOnlyOnce(actual, pattern, 0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be consistent with the existing code, you should use :

  • checkSequenceIsNotNull(sequence); to check the given string
  • assertNotNull(info, actual); to check actual

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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}

/**
Expand Down
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add the following tests to be complete :

  • should_pass_if_actual_is_contains_given_string_only_once
  • should_fail_if_actual_is_null
  • should_throw_error_if_string_to_find_is_null


@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));
}
}