• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junitparams.naming;
2 
3 import junitparams.JUnitParamsRunner;
4 import junitparams.Parameters;
5 import junitparams.internal.TestMethod;
6 import org.junit.Ignore;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.junit.runners.model.FrameworkMethod;
10 import org.junit.runners.model.TestClass;
11 
12 import java.lang.reflect.Method;
13 
14 import static org.junit.Assert.assertEquals;
15 
16 @RunWith(JUnitParamsRunner.class)
17 public class MacroSubstitutionNamingStrategyTest {
18 
parametersForTestNaming()19     public Object parametersForTestNaming() {
20         return new Object[]{new Object[]{"withoutTestCaseAnnotation", "[0] value (withoutTestCaseAnnotation)"},
21                             new Object[]{"withAnnotationWithoutTemplate", "[0] value (withAnnotationWithoutTemplate)"},
22                             new Object[]{"withEmptyTemplate", "[0] value (withEmptyTemplate)"},
23                             new Object[]{"whenTemplateResultedToEmptyName", "[0] value (whenTemplateResultedToEmptyName)"},
24                             new Object[]{"withoutMacro", "plain name"}, new Object[]{"withIndexMacro", "0"},
25                             new Object[]{"withParamsMacro", "value"},
26                             new Object[]{"withMethodNameMacro", "withMethodNameMacro"},
27                             new Object[]{"withCombinationOfMacros", "0: withCombinationOfMacros(value)"},
28                             new Object[]{"withMacroNameWrittenInDifferentCase", "value value value"},
29                             new Object[]{"withMethodParameterIndexInsideArgumentsArray", "value"},
30                             new Object[]{"withMethodParameterIndexOutsideArgumentsArray", "Here is 100 argument:"},
31                             new Object[]{"whenGivenMacroDoesntExist", "{not_existing_macro}"}};
32     }
33 
34     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
35     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
36     @Ignore
37     @Test
38     @Parameters
testNaming(String methodName, String expectedTestCaseName)39     public void testNaming(String methodName, String expectedTestCaseName) throws NoSuchMethodException {
40         TestCaseNamingStrategy strategy = createNamingStrategyForMethod(methodName, String.class);
41 
42         String name = strategy.getTestCaseName(0, new Object[]{"value"});
43 
44         assertEquals(expectedTestCaseName, name);
45     }
46 
withoutTestCaseAnnotation(String param1)47     public void withoutTestCaseAnnotation(String param1) { }
48 
49     @TestCaseName("plain name")
withoutMacro(String param1)50     public void withoutMacro(String param1) { }
51 
52     @TestCaseName("{index}")
withIndexMacro(String param1)53     public void withIndexMacro(String param1) { }
54 
55     @TestCaseName("{params}")
withParamsMacro(String param1)56     public void withParamsMacro(String param1) { }
57 
58     @TestCaseName("{method}")
withMethodNameMacro(String param1)59     public void withMethodNameMacro(String param1) { }
60 
61     @TestCaseName
withAnnotationWithoutTemplate(String param1)62     public void withAnnotationWithoutTemplate(String param1) { }
63 
64     @TestCaseName("")
withEmptyTemplate(String param1)65     public void withEmptyTemplate(String param1) { }
66 
67     @TestCaseName("{index}: {method}({params})")
withCombinationOfMacros(String param1)68     public void withCombinationOfMacros(String param1) { }
69 
70     @TestCaseName("{params} {PARAMS} {PaRams}")
withMacroNameWrittenInDifferentCase(String param1)71     public void withMacroNameWrittenInDifferentCase(String param1) { }
72 
73     @TestCaseName("{0}")
withMethodParameterIndexInsideArgumentsArray(String param1)74     public void withMethodParameterIndexInsideArgumentsArray(String param1) { }
75 
76     @TestCaseName("Here is 100 argument:{100}")
withMethodParameterIndexOutsideArgumentsArray(String param1)77     public void withMethodParameterIndexOutsideArgumentsArray(String param1) { }
78 
79     @TestCaseName("{100}")
whenTemplateResultedToEmptyName(String param1)80     public void whenTemplateResultedToEmptyName(String param1) { }
81 
82     @TestCaseName("{not_existing_macro}")
whenGivenMacroDoesntExist(String param1)83     public void whenGivenMacroDoesntExist(String param1) { }
84 
createNamingStrategyForMethod(String name, Class... parameterTypes)85     private TestCaseNamingStrategy createNamingStrategyForMethod(String name, Class... parameterTypes) throws NoSuchMethodException {
86         TestMethod method = getCurrentClassMethod(name, parameterTypes);
87 
88         return new MacroSubstitutionNamingStrategy(method);
89     }
90 
getCurrentClassMethod(String name, Class... parameterTypes)91     private TestMethod getCurrentClassMethod(String name, Class... parameterTypes) throws NoSuchMethodException {
92         final Method method = MacroSubstitutionNamingStrategyTest.class.getMethod(name, parameterTypes);
93         return new TestMethod(new FrameworkMethod(method),
94                 new TestClass(this.getClass()));
95     }
96 }
97