public static void howToMakeObjectsWithPropertiesThatAreCollections() { AppleCart cart = make(a(AppleCart, with(apples, listOf( an(Apple, with(ripeness, 0.5)), an(Apple, with(ripeness, 0.35)) )))); FruitBowl bowl = make(a(FruitBowl, with(contents, listOf( an(Apple, with(2, leaves)), an(Apple, with(3, leaves)), a(Banana, with(ripeness, 0.25)), a(Banana, with(ripeness, 0.99)) )))); }
@SafeVarargs public static <T> Donor<T> from(T ... values) { return from(asList(values)); }
@SafeVarargs public static <T> Donor<T> fromRepeating(T ... values) { return fromRepeating(asList(values)); } }
@Test public void canExplicitlyDeclareThatDifferentInstancesHaveTheSamePropertyValueInstance() { Maker<Identified> anIdentified = an(Identified, with(identity, theSame(Identity, with(name, "original-name")))); Identified x = make(anIdentified); Identified y = make(anIdentified); assertThat(x.identity, sameInstance(y.identity)); }
@Test public void allocatingNamesFromACollection() { SortedSet<String> names = new TreeSet<>(); names.add("Bob"); names.add("Alice"); names.add("Carol"); names.add("Dave"); Maker<NamedThing> aNamedThing = a(NamedThing, with(name, from(names))); NamedThing thing0 = make(aNamedThing); NamedThing thing1 = make(aNamedThing); NamedThing thing2 = make(aNamedThing); NamedThing thing3 = make(aNamedThing); assertThat(thing0.name, equalTo("Alice")); assertThat(thing1.name, equalTo("Bob")); assertThat(thing2.name, equalTo("Carol")); assertThat(thing3.name, equalTo("Dave")); } }
@Test public void sharingDefinitionsAvoidsAliasingErrors() { Maker<ThingToMake> x99Maker = a(ThingToMake, with(name, "x"), with(age, 99)); Maker<ThingToMake> x77Maker = x99Maker.but(with(age, 77)); ThingToMake x99 = x99Maker.make(); ThingToMake x77 = x77Maker.make(); assertThat("x99.name", x99.name, equalTo("x")); assertThat("x77.name", x77.name, equalTo("x")); assertThat("x99.age", x99.age, equalTo(99)); assertThat("x77.age", x77.age, equalTo(77)); } }
@Test public void overridesDefaultValuesWithExplicitProperties() { ThingToMake madeThing = make(a(ThingToMake, with(name, "Bob"), with(age, 10))); assertThat(madeThing.name, equalTo("Bob")); assertThat(madeThing.age, equalTo(10)); ThingToMake differentName = make(a(ThingToMake, with(name, "Bill"))); assertThat(differentName.name, equalTo("Bill")); }
public static void howToMakeSimpleObjects() { Maker<Apple> appleWith2Leaves = an(Apple, with(2, leaves)); Maker<Apple> ripeApple = appleWith2Leaves.but(with(ripeness, 0.9)); Maker<Apple> unripeApple = appleWith2Leaves.but(with(ripeness, 0.125)); Apple apple1 = make(ripeApple); Apple apple2 = make(unripeApple); Banana defaultBanana = make(a(Banana)); Banana straightBanana = make(a(Banana, with(curve, 0.0))); Banana squishyBanana = make(a(Banana, with(ripeness, 1.0))); }
@Test public void allocatingUniqueNames() { class UUIDValue implements Donor<String> { @Override public String value() { return UUID.randomUUID().toString(); } } Maker<NamedThing> aNamedThing = a(NamedThing, with(name, new UUIDValue())); NamedThing thing0 = make(aNamedThing); NamedThing thing1 = make(aNamedThing); assertThat(thing0.name, not(equalTo(thing1.name))); }
@SafeVarargs public static <T> Donor<T> from(T ... values) { return from(asList(values)); }
@SafeVarargs public static <T> Donor<T> fromRepeating(T ... values) { return fromRepeating(asList(values)); } }