octo pres

This commit is contained in:
Joel Costigliola 2014-02-18 23:42:31 +01:00
parent 79c76af5a3
commit e0000c7fec
6 changed files with 60 additions and 52 deletions

Binary file not shown.

Binary file not shown.

View File

@ -83,7 +83,7 @@ public abstract class AbstractAssertionsExamples {
protected final TolkienCharacter galadriel = new TolkienCharacter("Galadriel", 3000, ELF);
protected final TolkienCharacter elrond = new TolkienCharacter("Elrond", 3000, ELF);
protected final TolkienCharacter guruk = new TolkienCharacter("Guruk", 20, ORC);
protected final List<TolkienCharacter> fellowshipOfTheRing = new ArrayList<TolkienCharacter>();
protected final List<TolkienCharacter> fellowshipOfTheRing = new ArrayList<>();
protected final List<TolkienCharacter> orcsWithHobbitPrisoners = new ArrayList<TolkienCharacter>();
// Rings and their bearer

View File

@ -72,32 +72,32 @@ public class UsingConditionExamples extends AbstractAssertionsExamples {
assertThat(newLinkedHashSet("Luke", "Yoda")).have(jediPower);
assertThat(newLinkedHashSet("Leia", "Solo")).doNotHave(jediPower);
// areAtLeast & areNotAtLeast
// areAtLeast
assertThat(newLinkedHashSet("Luke", "Yoda", "Leia")).areAtLeast(2, jedi);
assertThat(newLinkedHashSet("Luke", "Yoda", "Obiwan")).areAtLeast(2, jedi);
// haveAtLeast & doNotHaveAtLeast
// haveAtLeast
assertThat(newLinkedHashSet("Luke", "Yoda", "Leia")).haveAtLeast(2, jediPower);
assertThat(newLinkedHashSet("Luke", "Yoda", "Obiwan")).haveAtLeast(2, jediPower);
// areAtMost & areNotAtMost
// areAtMost
assertThat(newLinkedHashSet("Luke", "Yoda", "Leia")).areAtMost(2, jedi);
assertThat(newLinkedHashSet("Luke", "Solo", "Leia")).areAtMost(2, jedi);
// haveAtMost & doNotHaveAtMost
// haveAtMost
assertThat(newLinkedHashSet("Luke", "Yoda", "Leia")).haveAtMost(2, jediPower);
assertThat(newLinkedHashSet("Luke", "Solo", "Leia")).haveAtMost(2, jediPower);
// areExactly & areNotExactly
// areExactly
assertThat(newLinkedHashSet("Luke", "Yoda", "Leia")).areExactly(2, jedi);
// haveExactly & haveNotExactly
// haveExactly
assertThat(newLinkedHashSet("Luke", "Yoda", "Leia")).haveExactly(2, jediPower);
}
@SuppressWarnings("unchecked")
@Test
public void has_not_condition_example() {
public void combined_condition_example() {
assertThat("Yoda").has(jediPower);
assertThat("Yoda").has(allOf(jediPower, not(sithPower)));
assertThat("Solo").has(not(jediPower));
@ -116,14 +116,7 @@ public class UsingConditionExamples extends AbstractAssertionsExamples {
};
};
private final Condition<String> jediPower = new Condition<String>("jedi power") {
private final Set<String> jedis = newLinkedHashSet("Luke", "Yoda", "Obiwan");
@Override
public boolean matches(String actual) {
return jedis.contains(actual);
};
};
private final Condition<String> jediPower = jedi;
private final Condition<String> sith = new Condition<String>("sith") {
private final Set<String> siths = newLinkedHashSet("Sidious", "Vader", "Plagueis");

View File

@ -34,14 +34,14 @@ public class MultimapAssertionsExamples extends AbstractAssertionsExamples {
@Test
public void multimap_assertions_examples() {
Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
Multimap<String, String> nbaTeams = ArrayListMultimap.create();
nbaTeams.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
nbaTeams.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
assertThat(actual).hasSize(6);
assertThat(actual).containsKeys("Lakers", "Spurs");
assertThat(actual).contains(entry("Lakers", "Kobe Bryant"), entry("Spurs", "Tim Duncan"));
assertThat(actual).containsValues("Kareem Abdul Jabbar", "Tony Parker");
assertThat(nbaTeams).hasSize(6);
assertThat(nbaTeams).containsKeys("Lakers", "Spurs");
assertThat(nbaTeams).contains(entry("Lakers", "Kobe Bryant"), entry("Spurs", "Tim Duncan"));
assertThat(nbaTeams).containsValues("Kareem Abdul Jabbar", "Tony Parker");
Multimap<String, String> emptyMultimap = ArrayListMultimap.create();
assertThat(emptyMultimap).isEmpty();

View File

@ -68,61 +68,75 @@ public class OctoDemo extends AbstractAssertionsExamples {
public void string_assertions_examples() {
assertThat("Frodo").startsWith("Fro").endsWith("do").hasSize(5);
assertThat("Frodo").containsIgnoringCase("froo");
assertThat("Frodo").contains("rod")
.doesNotContain("fro")
.containsOnlyOnce("do"); // see javadoc
.containsOnlyOnce("do"); // see javadoc
// you can ignore case for equals check
assertThat("Frodo").isEqualToIgnoringCase("FROdO");
// using regex
assertThat("Frodo").matches("..o.o").doesNotMatch(".*d");
}
@Test
public void iterable_assertions_examples() {
@Test
public void iterable_assertions_examples() {
assertThat(elvesRings).isNotEmpty()
.hasSize(3)
.contains(nenya)
.doesNotContain(oneRing);
assertThat(elvesRings).isNotEmpty()
.hasSize(3)
.contains(oneRing, vilya)
.doesNotContain(oneRing);
// with containsOnly, all the elements must be present (but the order is not important)
assertThat(elvesRings).containsOnly(nenya, vilya, narya)
.doesNotContainNull()
.doesNotHaveDuplicates();
// with containsOnly, all the elements must be present (but the order is not important)
assertThat(elvesRings).containsOnly(nenya, vilya, narya)
.doesNotContainNull()
.doesNotHaveDuplicates();
// you can also check the start or end of your collection/iterable
Iterable<Ring> allRings = newArrayList(oneRing, vilya, nenya, narya, dwarfRing, manRing);
assertThat(allRings).startsWith(oneRing, vilya).endsWith(dwarfRing, manRing);
assertThat(allRings).containsAll(elvesRings);
// you can also check the start or end of your collection/iterable
Iterable<Ring> allRings = newArrayList(oneRing, vilya, nenya, narya, dwarfRing, manRing);
assertThat(allRings).startsWith(oneRing, vilya).endsWith(dwarfRing, manRing);
assertThat(allRings).containsAll(elvesRings);
// to show an error message, let's replace narya by the one ring
elvesRings.remove(narya);
elvesRings.add(oneRing);
try {
assertThat(elvesRings).containsOnly(nenya, vilya, narya);
} catch (AssertionError e) {
logAssertionErrorMessage("containsOnly", e);
}
}
// to show an error message, let's replace narya by the one ring
elvesRings.remove(narya);
elvesRings.add(oneRing);
try {
assertThat(elvesRings).containsOnly(nenya, vilya, narya);
} catch (AssertionError e) {
logAssertionErrorMessage("containsOnly", e);
}
}
@Test
public void iterable_assertions_on_extracted_property_values_example() {
List<String> names = newArrayList();
for (TolkienCharacter tolkienCharacter : fellowshipOfTheRing) {
names.add(tolkienCharacter.getName());
}
// extract simple property values having a java standard type
assertThat(fellowshipOfTheRing).extracting("name").contains("Frodo", "Gandalf", "Legolas")
.doesNotContain("Sauron", "Elrond");
// extracting a public field
assertThat(fellowshipOfTheRing).extracting("age").contains(33, 38, 36);
// extract nested property on Race
assertThat(fellowshipOfTheRing).extracting("race.name").contains("Hobbit", "Elf")
.doesNotContain("Orc");
// extract 'name', 'age' and Race name values.
assertThat(fellowshipOfTheRing).extracting("name", "race.name").contains(tuple("Boromir", "Man"),
tuple("Sam", "Hobbit"),
tuple("Legolas", "Elf"));
assertThat(fellowshipOfTheRing).extracting("name", "race.name")
.contains(tuple("Boromir", "Man"),
tuple("Sam", "Hobbit"),
tuple("Frodo", "Dwarf"),
tuple("Legolas", "Elf"));
}
@Test
@ -148,7 +162,8 @@ public class OctoDemo extends AbstractAssertionsExamples {
.hasSize(4)
.contains(entry(oneRing, frodo), entry(nenya, galadriel))
.doesNotContain(entry(oneRing, aragorn))
.containsKey(Ring.nenya)
.containsKey(nenya)
.containsKeys(narya, vilya)
.containsValue(frodo)
.doesNotContainValue(sam);
}
@ -193,7 +208,7 @@ public class OctoDemo extends AbstractAssertionsExamples {
@Test
public void bdd_assertions_examples() {
//given
List<BasketBallPlayer> bulls = new ArrayList<BasketBallPlayer>();
List<BasketBallPlayer> bulls = new ArrayList<>();
//when
bulls.add(rose);