Skip to content

Commit

Permalink
Add hasLength(int), isEmpty(), isNotEmpty() to array subjects.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=77092298
  • Loading branch information
kluever authored and cgruber committed Oct 13, 2014
1 parent 8b0f3b9 commit bcee2b5
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.common.truth;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.List;

/**
Expand All @@ -29,6 +31,25 @@ public abstract class AbstractArraySubject<S extends AbstractArraySubject<S, T>,
super(failureStrategy, subject);
}

public void isEmpty() {
if (!listRepresentation().isEmpty()) {
fail("is empty");
}
}

public void isNotEmpty() {
if (listRepresentation().isEmpty()) {
fail("is not empty");
}
}

public void hasLength(int length) {
checkArgument(length >= 0, "length (%s) must be >= 0");
if (listRepresentation().size() != length) {
fail("has length", length);
}
}

@Override public S named(String name) {
return (S) super.named(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
@RunWith(JUnit4.class)
public class ObjectArraySubjectTest {

private static final Object[] EMPTY = new Object[0];

@Test public void isEqualTo() {
assertThat(objectArray("A", 5L)).isEqualTo(objectArray("A", 5L));
}
Expand All @@ -47,6 +49,57 @@ public class ObjectArraySubjectTest {
assertThat(objectArray("A", 5L)).asList().has().anyOf("A");
}

@Test public void hasLength() {
assertThat(EMPTY).hasLength(0);
assertThat(objectArray("A", 5L)).hasLength(2);
}

@Test public void hasLengthFail() {
try {
assertThat(objectArray("A", 5L)).hasLength(1);
throw new Error("Expected to throw.");
} catch (AssertionError e) {
assertThat(e.getMessage())
.is("Not true that <(Object[]) [A, 5]> has length <1>");
}
}

@Test public void hasLengthNegative() {
try {
assertThat(objectArray(2, 5)).hasLength(-1);
throw new Error("Expected to throw.");
} catch (IllegalArgumentException expected) {
}
}

@Test public void isEmpty() {
assertThat(EMPTY).isEmpty();
}

@Test public void isEmptyFail() {
try {
assertThat(objectArray("A", 5L)).isEmpty();
throw new Error("Expected to throw.");
} catch (AssertionError e) {
assertThat(e.getMessage())
.is("Not true that <(Object[]) [A, 5]> is empty");
}
}

@Test public void isNotEmpty() {
assertThat(objectArray("A", 5L)).isNotEmpty();
}

@Test public void isNotEmptyFail() {
try {
assertThat(EMPTY).isNotEmpty();
throw new Error("Expected to throw.");
} catch (AssertionError e) {
assertThat(e.getMessage())
.is("Not true that <(Object[]) []> is not empty");
}
}

@Test public void isEqualTo_Fail_UnequalOrdering() {
try {
assertThat(objectArray("A", 5L)).isEqualTo(objectArray(5L, "A"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@RunWith(JUnit4.class)
public class PrimitiveIntArraySubjectTest {

private static final int[] EMPTY = new int[0];

@Test public void isEqualTo() {
assertThat(array(2, 5)).isEqualTo(array(2, 5));
}
Expand All @@ -44,6 +46,57 @@ public class PrimitiveIntArraySubjectTest {
assertThat(array(5, 2, 9)).asList().containsAllOf(2, 9);
}

@Test public void hasLength() {
assertThat(EMPTY).hasLength(0);
assertThat(array(2, 5)).hasLength(2);
}

@Test public void hasLengthFail() {
try {
assertThat(array(2, 5)).hasLength(1);
throw new Error("Expected to throw.");
} catch (AssertionError e) {
assertThat(e.getMessage())
.is("Not true that <(int[]) [2, 5]> has length <1>");
}
}

@Test public void hasLengthNegative() {
try {
assertThat(array(2, 5)).hasLength(-1);
throw new Error("Expected to throw.");
} catch (IllegalArgumentException expected) {
}
}

@Test public void isEmpty() {
assertThat(EMPTY).isEmpty();
}

@Test public void isEmptyFail() {
try {
assertThat(array(2, 5)).isEmpty();
throw new Error("Expected to throw.");
} catch (AssertionError e) {
assertThat(e.getMessage())
.is("Not true that <(int[]) [2, 5]> is empty");
}
}

@Test public void isNotEmpty() {
assertThat(array(2, 5)).isNotEmpty();
}

@Test public void isNotEmptyFail() {
try {
assertThat(EMPTY).isNotEmpty();
throw new Error("Expected to throw.");
} catch (AssertionError e) {
assertThat(e.getMessage())
.is("Not true that <(int[]) []> is not empty");
}
}

@Test public void isEqualTo_Fail_UnequalOrdering() {
try {
assertThat(array(2, 3)).isEqualTo(array(3, 2));
Expand Down

0 comments on commit bcee2b5

Please sign in to comment.