### Question:
Printer { public void print(String format, Object... args) { convertArgs(args); formatter.format(format, args); throwExceptionIfNecessary(); } Printer(Context ctx, Formatter formatter); void iprint(String format, Object... args); void print(String format, Object... args); void indent(); void unindent();  }### Answer:
@Test void print_Class() { addProcessor( new TestProcessor() { @Override protected void run() { Formatter formatter = new Formatter(); Printer printer = new Printer(ctx, formatter); printer.print("%1$s", String.class); assertEquals("java.lang.String", formatter.toString()); } }); }
@Test void print_TypeMirror() { addProcessor( new TestProcessor() { @Override protected void run() { TypeElement typeElement = ctx.getMoreElements().getTypeElement(String.class); TypeMirror typeMirror = typeElement.asType(); Formatter formatter = new Formatter(); Printer printer = new Printer(ctx, formatter); printer.print("%1$s", typeMirror); assertEquals("java.lang.String", formatter.toString()); } }); }
@Test void print_TypeElement() { addProcessor( new TestProcessor() { @Override protected void run() { TypeElement typeElement = ctx.getMoreElements().getTypeElement(String.class); Formatter formatter = new Formatter(); Printer printer = new Printer(ctx, formatter); printer.print("%1$s", typeElement); assertEquals("java.lang.String", formatter.toString()); } }); }
@Test void print_Element() { Class<?> testClass = getClass(); addProcessor( new TestProcessor() { @Override protected void run() { TypeElement typeElement = ctx.getMoreElements().getTypeElement(testClass); VariableElement field = ElementFilter.fieldsIn(typeElement.getEnclosedElements()).stream() .filter(f -> f.getSimpleName().contentEquals("field")) .findFirst() .orElseThrow(AssertionError::new); Formatter formatter = new Formatter(); Printer printer = new Printer(ctx, formatter); printer.print("%1$s", field); assertEquals("field", formatter.toString()); } }); }
@Test void print_List() { addProcessor( new TestProcessor() { @Override protected void run() { TypeElement typeElement = ctx.getMoreElements().getTypeElement(String.class); TypeMirror typeMirror = typeElement.asType(); List<Object> list = Arrays.asList( String.class, typeElement, typeMirror, Collections.singletonList(Integer.class)); Formatter formatter = new Formatter(); Printer printer = new Printer(ctx, formatter); printer.print("%1$s", list); assertEquals( "java.lang.String, java.lang.String, java.lang.String, java.lang.Integer", formatter.toString()); } }); }
@Test void print_Code() { addProcessor( new TestProcessor() { @Override protected void run() { Code code = new Code(p -> p.print("%1$s", String.class)); Formatter formatter = new Formatter(); Printer printer = new Printer(ctx, formatter); printer.print("<%1$s>", code); assertEquals("<java.lang.String>", formatter.toString()); } }); }