Package ar.com.dgarcia.java_verbs.api

Examples of ar.com.dgarcia.java_verbs.api.Verb


        return defineVerb(functionName, verbDefinition);
    }

    @Override
    public Verb named(String functionName) {
        Verb verb = verbs.get(functionName);
        if(verb == null){
            throw new VerbException("The requested verb["+functionName+"] does not exists");
        }
        return verb;
    }
View Full Code Here


     * @param functionName The name for the verb
     * @param verbDefinition The new definition
     * @return The existing or created verb
     */
    private Verb defineVerb(String functionName, Lambda verbDefinition) {
        Verb existingVerb = verbs.get(functionName);
        if(existingVerb != null){
            existingVerb.redefine(verbDefinition);
            return existingVerb;
        }
        VerbImpl createdVerb = VerbImpl.create(functionName, verbDefinition);
        verbs.put(functionName, createdVerb);
        return createdVerb;
View Full Code Here

    @Override
    public void define() {

        describe("a piece of behavior", ()->{

            Verb verb = Verbs.in("tests").asFunction("name", () -> "John Smith");

            it("is a function defined in a namespace", () -> {
                String returnedValue = verb.apply();

                assertThat(returnedValue).isEqualTo("John Smith");
            });

            it("that can be used to implement a method", () -> {
                TestObject testObject = new TestObject();

                assertThat(testObject.name()).isEqualTo("John Smith");
            });

        });

        describe("functions can be shared", () -> {

            Verbs.in("tests").asFunction("play", (first, second) -> first + " is playing with " + second);

            it("between types with a common ancestor", () -> {
                Human brother = Human.create("Hansel");
                Human sister = Human.create("Gretel");

                assertThat(brother.playWith(sister)).isEqualTo("Hansel is playing with Gretel");
                assertThat(sister.playWith(brother)).isEqualTo("Gretel is playing with Hansel");
            });

            it("between unrelated types", () -> {
                Cat garfield = Cat.create("Garfield");
                Dog odie = Dog.create("Odie");

                assertThat(garfield.playWith(odie)).isEqualTo("Garfield is playing with Odie");
                assertThat(odie.playWith(garfield)).isEqualTo("Odie is playing with Garfield");
            });

        });

        describe("when sharing among unrelated types", () -> {

            Verbs.in("tests").asFunction("drive", (person, car) -> person + " is driving " + car);

            it("method names can differ to improve expressiveness", () -> {
                Human schummy = Human.create("Schumacher");
                Car fantasticCar = Car.create("KITT");

                assertThat(schummy.drive(fantasticCar)).isEqualTo("Schumacher is driving KITT");
                assertThat(fantasticCar.drivenBy(schummy)).isEqualTo("Schumacher is driving KITT");
            });

        });

        describe("a transfer that involves 3 parties: sender, receiver and the transferred object", () -> {

            Sender planetEarth = Sender.create("Earth");
            Receiver moon = Receiver.create("Moon");
            Spaceship apollo11 = Spaceship.create("Apollo11");

            Verbs.in("tests").asFunction("transfer", (object, origin, destination) -> object + " is transferred from " + origin + " to " + destination);

            it("can be expressed with the sender as the subject", () -> {

                assertThat(planetEarth.sendTo(moon, apollo11)).isEqualTo("Apollo11 is transferred from Earth to Moon");
            });

            it("with the receiver as subject", () -> {

                assertThat(moon.receiveFrom(planetEarth, apollo11)).isEqualTo("Apollo11 is transferred from Earth to Moon");
            });

            it("or the object as the subject", () -> {

                assertThat(apollo11.travelBetween(planetEarth, moon)).isEqualTo("Apollo11 is transferred from Earth to Moon");
            });
        });

        describe("behavior", ()->{

            Verb verb = Verbs.in("tests").asFunction("salute", () -> "Hello");


            it("can be re-defined in runtime", ()->{
                assertThat(verb.<String>apply()).isEqualTo("Hello");

                Verbs.in("tests").asFunction("salute", () -> "Bye");

                assertThat(verb.<String>apply()).isEqualTo("Bye");
            });

        });

    }
View Full Code Here

        context().operation(()-> mock(TestOperation.class));

        describe("a verb with no return value", () -> {

            it("and no argument", () -> {
                Verb verb = Verbs.in("test").asAction("test", () -> context().operation().operationWithNoReturn());

                verb.apply();

                verify(context().operation()).operationWithNoReturn();
            });

            it("1 argument", () -> {
                Verb verb = Verbs.in("test").asAction("test", (first) -> context().operation().operationWithNoReturn(first));

                verb.apply("1");

                verify(context().operation()).operationWithNoReturn("1");
            });

            it("2 arguments", () -> {
                Verb verb = Verbs.in("test").asAction("test", (first, second) -> context().operation().operationWithNoReturn(first, second));

                verb.apply("1", "2");

                verify(context().operation()).operationWithNoReturn("1", "2");
            });

            it("3 arguments", () -> {
                Verb verb = Verbs.in("test").asAction("test", (first, second, third) -> context().operation().operationWithNoReturn(first, second, third));

                verb.apply("1", "2", "3");

                verify(context().operation()).operationWithNoReturn("1", "2", "3");
            });

            it("4 arguments", () -> {
                Verb verb = Verbs.in("test").asAction("test", (first, second, third, fourth, extra) -> context().operation().operationWithNoReturn(first, second, third, fourth));

                verb.apply("1", "2", "3", "4");

                verify(context().operation()).operationWithNoReturn("1", "2", "3", "4");
            });

            it("5 or more arguments", () -> {
                Verb verb = Verbs.in("test").asAction("test", (first, second, third, fourth, extra) -> context().operation().operationWithNoReturn(first, second, third, fourth, extra[0]));

                verb.apply("1", "2", "3", "4", "5");

                verify(context().operation()).operationWithNoReturn("1", "2", "3", "4", "5");
            });

        });

        describe("a verb with return value", ()->{

            it("and no argument", ()->{
                Verb verb = Verbs.in("test").asFunction("test", () -> context().operation().operationWithReturn());

                verb.apply();

                verify(context().operation()).operationWithReturn();
            });

            it("1 argument", ()->{
                Verb verb = Verbs.in("test").asFunction("test", (first) -> context().operation().operationWithReturn(first));

                verb.apply("1");

                verify(context().operation()).operationWithReturn("1");
            });

            it("2 arguments", ()->{
                Verb verb = Verbs.in("test").asFunction("test", (first, second) -> context().operation().operationWithReturn(first, second));

                verb.apply("1", "2");

                verify(context().operation()).operationWithReturn("1","2");
            });

            it("3 arguments", ()->{
                Verb verb = Verbs.in("test").asFunction("test", (first, second, third) -> context().operation().operationWithReturn(first, second, third));

                verb.apply("1", "2", "3");

                verify(context().operation()).operationWithReturn("1","2", "3");
            });

            it("4 arguments", ()->{
                Verb verb = Verbs.in("test").asFunction("test", (first, second, third, fourth, extra) -> context().operation().operationWithReturn(first, second, third, fourth));

                verb.apply("1", "2", "3", "4");

                verify(context().operation()).operationWithReturn("1", "2", "3", "4");
            });

            it("5 or more arguments", ()->{
                Verb verb = Verbs.in("test").asFunction("test", (first, second, third, fourth, extra) -> context().operation().operationWithReturn(first, second, third, fourth, extra[0]));

                verb.apply("1", "2", "3", "4", "5");

                verify(context().operation()).operationWithReturn("1", "2", "3", "4", "5");
            });
        });
View Full Code Here

TOP

Related Classes of ar.com.dgarcia.java_verbs.api.Verb

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.