Quick-start guide

From cdd
Jump to: navigation, search

When finished with this example you should end up with the following file structure:

C:/Projects/MyProject/myproject.laja
C:/Projects/MyProject/src/main/java/com/myproject/Main.java
C:/Projects/MyProject/src/main/java/com/myproject/Person.java
C:/Projects/MyProject/src/main/java/com/myproject/PersonState.java
C:/Projects/MyProject/src/main/java/com/myproject/PersonCreator.java


Now let's get started with CDD:

  • Make sure Laja is installed.
  • Create a project directory e.g. C:/Projects/MyProject.
  • Create the source directory and package structure in the project root, e.g. C:/Projects/MyProject/src/main/java/com/myproject.


Create the Laja template file myproject.laja and put it in the project root directory:

#generateCdd({
  srcDir: "{.}/src"
})


Create the classes PersonState.java:

package com.myproject;
 
public class PersonState {
    int age;
    String givenName;
    String surname;
}


Create the class PersonCreator:

package com.myproject;
 
public class PersonCreator {
    PersonState state;
}


Open a Unix shell or DOS prompt, go to the project root and enter laja myproject:

C:\Projects\MyProject>laja myproject

Which gives the output:

Reading file "myproject.laja"
  Writing to file "C:/MyProject/src/main/java/com/myproject/PersonState.java" - OK
  Writing to file "C:/MyProject/src/main/java/com/myproject/PersonCreator.java" - OK
  Processed 2 classes: 2 changed.


Create the class Person.java:

package com.myproject;
 
import static com.myproject.PersonCreator.PersonBehaviour;
 
public class Person extends PersonBehaviour {
 
    public Person(PersonState state) {
        super(state);
    }
 
    public boolean isTeenager() {
        return state.age >= 13 && state.age <= 19;
    }
}


Edit PersonCreator and add the method asPerson:

public class PersonCreator implements PersonCreatorMaker {
...
    private final PersonMutableState state;
 
    public Person asPerson() {
        return new Person(state.asImmutable());
    }
 
    // ===== Generated code =====
...


Regenerate:

C:\MyProject>laja myproject


Create the class Main.java:

package com.myproject;
 
import static com.myproject.PersonCreator.createPerson;
 
public class Main {
 
    public static void main(String[] args) {
        Person person = createPerson().age(18).givenName("Ingmar").surname("Bergman").asPerson();
 
        System.out.println("Is teenager: " + person.isTeenager());
        System.out.println(person);
    }
}


If executed you will get the output:

Is teenager: true
Person{age=18, givenName="Ingmar", surname="Bergman"}


Next >