#50 "contains only once" #94

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

P4uline commented Aug 7, 2012

I added "contains only once" functionality.

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

...org/fest/assertions/error/ShouldContainsOnlyOnce.java
@@ -0,0 +1,26 @@
+package org.fest.assertions.error;
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

the code is not formatted according to fest formatter

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

...org/fest/assertions/error/ShouldContainsOnlyOnce.java
+ 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);
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

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"

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

...org/fest/assertions/error/ShouldContainsOnlyOnce.java
+ 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);
+ }
+
+ 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);
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

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

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

src/main/java/org/fest/assertions/internal/Strings.java
+ /**
+ * 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) {
+ if (actual == null || pattern == null) throw failures.failure(info, shouldContainsOnlyOnce(actual, pattern, 0));
+ 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;
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

format code

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

...ons/internal/Strings_assertContainsOnlyOnce_Test.java
@@ -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 {
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

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

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

src/main/java/org/fest/assertions/internal/Strings.java
@@ -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) {
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

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

@joel-costigliola joel-costigliola commented on the diff Aug 9, 2012

src/main/java/org/fest/assertions/internal/Strings.java
@@ -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) {
+ if (actual == null || pattern == null) throw failures.failure(info, shouldContainsOnlyOnce(actual, pattern, 0));
@joel-costigliola

joel-costigliola Aug 9, 2012

Contributor

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

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

alexruiz commented Oct 5, 2012

I'm sorry, but I'm reverting this change. I don't think this is general enough to be part of FEST. We hope to make FEST easy to extend so users can add their own assertions painlessly.

alexruiz reopened this Oct 5, 2012

Contributor

joel-costigliola commented Oct 11, 2012

I think this assertion is valuable because I don't see any other obvious/direct way to do it and because it comes from a Fest user that needed it for its project and contributed it (as part of a hackergarten btw).

My 2cts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment