1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.reporting;
6 
7 import org.mockito.ArgumentMatcher;
8 import org.mockito.internal.invocation.ArgumentsProcessor;
9 import org.mockito.internal.matchers.text.MatchersPrinter;
10 import org.mockito.internal.util.MockUtil;
11 import org.mockito.invocation.Invocation;
12 import org.mockito.invocation.MatchableInvocation;
13 
14 import java.util.Arrays;
15 import java.util.LinkedList;
16 import java.util.List;
17 
18 public class PrintSettings {
19 
20     public static final int MAX_LINE_LENGTH = 45;
21     private boolean multiline;
22     private List<Integer> withTypeInfo = new LinkedList<Integer>();
23 
setMultiline(boolean multiline)24     public void setMultiline(boolean multiline) {
25         this.multiline = multiline;
26     }
27 
isMultiline()28     public boolean isMultiline() {
29         return multiline;
30     }
31 
verboseMatchers(Integer .... indexesOfMatchers)32     public static PrintSettings verboseMatchers(Integer ... indexesOfMatchers) {
33         PrintSettings settings = new PrintSettings();
34         settings.setMatchersToBeDescribedWithExtraTypeInfo(indexesOfMatchers);
35         return settings;
36     }
37 
extraTypeInfoFor(int argumentIndex)38     public boolean extraTypeInfoFor(int argumentIndex) {
39         return withTypeInfo.contains(argumentIndex);
40     }
41 
setMatchersToBeDescribedWithExtraTypeInfo(Integer[] indexesOfMatchers)42     public void setMatchersToBeDescribedWithExtraTypeInfo(Integer[] indexesOfMatchers) {
43         this.withTypeInfo = Arrays.asList(indexesOfMatchers);
44     }
45 
print(List<ArgumentMatcher> matchers, Invocation invocation)46     public String print(List<ArgumentMatcher> matchers, Invocation invocation) {
47         MatchersPrinter matchersPrinter = new MatchersPrinter();
48         String qualifiedName = MockUtil.getMockName(invocation.getMock()) + "." + invocation.getMethod().getName();
49         String invocationString = qualifiedName + matchersPrinter.getArgumentsLine(matchers, this);
50         if (isMultiline() || (!matchers.isEmpty() && invocationString.length() > MAX_LINE_LENGTH)) {
51             return qualifiedName + matchersPrinter.getArgumentsBlock(matchers, this);
52         } else {
53             return invocationString;
54         }
55     }
56 
print(Invocation invocation)57     public String print(Invocation invocation) {
58         return print(ArgumentsProcessor.argumentsToMatchers(invocation.getArguments()), invocation);
59     }
60 
print(MatchableInvocation invocation)61     public String print(MatchableInvocation invocation) {
62         return print(invocation.getMatchers(), invocation.getInvocation());
63     }
64 }
65