All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pivotallabs.greatexpectations.ExpectGenerator Maven / Gradle / Ivy

The newest version!
package com.pivotallabs.greatexpectations;

import com.pivotallabs.greatexpectations.matchers.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;

public class ExpectGenerator {
  private Set importedPackages = new HashSet();
  private String packageName;
  private PrintStream out;

  public ExpectGenerator(String packageName) {
    importedPackages.add("java.lang");
    importedPackages.add(packageName);
    importedPackages.add(BaseMatcher.class.getPackage().getName());
    importedPackages.add(ObjectMatcher.class.getPackage().getName());
    importedPackages.add(packageName);

    this.packageName = packageName;
    out = System.out;
  }

  public String generateFor(Class matcherClass) {
    String name = className(matcherClass);
    MatcherOf annotation = matcherClass.getAnnotation(MatcherOf.class);

    StringBuilder buf = new StringBuilder();
    for (Class targetClass : annotation.value()) {
      if (targetClass.equals(Boolean.class)) {
        buf.append("    public static BooleanMatcher expect(boolean actual) {\n" +
                   "        return wrapped(BooleanMatcher.class, actual);\n" +
                   "    }\n");
      }

      String type = className(targetClass);
      String genericSig = annotation.directObject() ? "" : "";
      String returnType = annotation.directObject() ? ", X" : "")
          .append(", M extends ")
          .append(name)
          .append(genericSig)
          .append("> ")
          .append(name)
          .append(returnType)
          .append("?> expect(T actual) {\n" + "        return wrapped(")
          .append(name)
          .append(".class, actual);\n" + "    }");
    }
    return buf.toString();
  }

  public void generate() {
    generate(matcherClasses());
  }

  public void generate(List> classes) {
    out.println("package " + packageName + ";");
    out.println();
    out.println("import " + ObjectMatcher.class.getPackage().getName() + ".*;");
    out.println("import static " + GreatExpectations.class.getName() + ".wrapped;");
    out.println();

    out.println("public class Expect {");
    for (Class aClass : classes) {
      try {
        out.println(generateFor(aClass));
      } catch (Exception e) {
        throw new RuntimeException("error generating expect() for " + aClass.getName(), e);
      }
    }
    out.println("}");
  }

  public void setOut(PrintStream out) {
    this.out = out;
  }

  public static void main(String[] args) throws IOException {
    String packageName = args.length == 0 ? ExpectGenerator.class.getPackage().getName() : args[0];
    ExpectGenerator expectGenerator = new ExpectGenerator(packageName);
    if (args.length == 3 && args[1].equals("--outFile")) {
      expectGenerator.setOut(new PrintStream(new File(args[2])));
    }
    expectGenerator.generate();
  }

  public List> matcherClasses() {
    return new ArrayList>(
        Arrays.>asList(
            ObjectMatcher.class,
            BooleanMatcher.class,
            ComparableMatcher.class,
            DateMatcher.class,
            IterableMatcher.class,
            SetMatcher.class,
            LongMatcher.class,
            StringMatcher.class
        )
    );
  }

  private String className(Class matcherClass) {
    String name;
    String packageName = matcherClass.getPackage().getName();
    if (importedPackages.contains(packageName)) {
      name = matcherClass.getSimpleName();
    } else {
      name = matcherClass.getName();
    }
    return name;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy