public String generate(Object mock, Method method, Object[] arguments) { final List<Matcher> matcherList = ArgumentsProcessor.argumentsToMatchers(arguments); final PrintSettings printSettings = new PrintSettings(); MatchersPrinter matchersPrinter = new MatchersPrinter(); String methodName = Whitebox.getType(mock).getName() + "." + method.getName(); String invocation = methodName + matchersPrinter.getArgumentsLine(matcherList, printSettings); if (printSettings.isMultiline() || (!matcherList.isEmpty() && invocation.length() > Whitebox.<Integer> getInternalState( PrintSettings.class, "MAX_LINE_LENGTH"))) { return methodName + matchersPrinter.getArgumentsBlock(matcherList, printSettings); } else { return invocation; } }
private List<SelfDescribing> applyPrintSettings(List<Matcher> matchers, PrintSettings printSettings) { List<SelfDescribing> withPrintSettings = new LinkedList<SelfDescribing>(); int i = 0; for (final Matcher matcher : matchers) { if (matcher instanceof ContainsExtraTypeInformation && printSettings.extraTypeInfoFor(i)) { withPrintSettings.add(((ContainsExtraTypeInformation) matcher).withExtraTypeInfo()); } else { withPrintSettings.add(matcher); } i++; } return withPrintSettings; }
protected String toString(List<Matcher> matchers, PrintSettings printSettings) { MatchersPrinter matchersPrinter = new MatchersPrinter(); String method = qualifiedMethodName(); String invocation = method + matchersPrinter.getArgumentsLine(matchers, printSettings); if (printSettings.isMultiline() || (!matchers.isEmpty() && invocation.length() > MAX_LINE_LENGTH)) { return method + matchersPrinter.getArgumentsBlock(matchers, printSettings); } else { return invocation; } }
@Test public void shouldDescribeTypeInfoOnlyMarkedMatchers() { //when String line = printer.getArgumentsLine((List) Arrays.asList(new Equals(1L), new Equals(2)), PrintSettings.verboseMatchers(1)); //then assertEquals("(1, (Integer) 2);", line); }
@Test public void shouldGetVerboseArgumentsInBlock() { //when String line = printer.getArgumentsBlock((List) Arrays.asList(new Equals(1L), new Equals(2)), PrintSettings.verboseMatchers(0, 1)); //then assertEquals("(\n (Long) 1,\n (Integer) 2\n);", line); }
@Test public void shouldGetVerboseArgumentsEvenIfSomeMatchersAreNotVerbose() { //when String line = printer.getArgumentsLine((List) Arrays.asList(new Equals(1L), NotNull.NOT_NULL), PrintSettings.verboseMatchers(0)); //then assertEquals("((Long) 1, notNull());", line); }
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) { Description result = new StringDescription(); result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings)); return result.toString(); }
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) { Description result = new StringDescription(); result.appendList("(\n ", ",\n ", "\n);", applyPrintSettings(matchers, printSettings)); return result.toString(); }
public String toString() { return invocation.toString(matchers, new PrintSettings()); }
public String toString(PrintSettings printSettings) { return invocation.toString(matchers, printSettings); }
public String toString() { return toString(argumentsToMatchers(), new PrintSettings()); }
public String toString(PrintSettings printSettings) { return toString(argumentsToMatchers(), printSettings); }
@Test public void shouldGetArgumentsLine() { String line = printer.getArgumentsLine((List) Arrays.asList(new Equals(1), new Equals(2)), new PrintSettings()); assertEquals("(1, 2);", line); }
@Test public void shouldGetArgumentsBlock() { String line = printer.getArgumentsBlock((List) Arrays.asList(new Equals(1), new Equals(2)), new PrintSettings()); assertEquals("(\n 1,\n 2\n);", line); }
public String toString() { return new PrintSettings().print(matchers, invocation); }
public String toString() { return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this); }
@Override public String toString() { return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this); }