19
19
import org .junit .jupiter .api .Test ;
20
20
import org .openrewrite .*;
21
21
import org .openrewrite .marker .SearchResult ;
22
- import org .openrewrite .test .RecipeSpec ;
23
22
import org .openrewrite .test .RewriteTest ;
24
23
import org .openrewrite .text .PlainText ;
25
24
import org .openrewrite .text .PlainTextVisitor ;
26
25
27
26
import static org .assertj .core .api .Assertions .assertThat ;
28
27
import static org .openrewrite .test .SourceSpecs .text ;
29
28
30
- class RecipeRunStatsTest implements RewriteTest {
29
+ @ AllArgsConstructor
30
+ class RecipeWithApplicabilityTest extends Recipe {
31
+ @ Override
32
+ public String getDisplayName () {
33
+ return "Recipe with an applicability test" ;
34
+ }
31
35
32
- @ AllArgsConstructor
33
- static class RecipeWithApplicabilityTest extends Recipe {
34
- @ Override
35
- public String getDisplayName () {
36
- return "Recipe with an applicability test" ;
37
- }
36
+ @ Override
37
+ public String getDescription () {
38
+ return "This recipe is a test utility which exists to exercise RecipeRunStats." ;
39
+ }
38
40
39
- @ Override
40
- public String getDescription () {
41
- return "This recipe is a test utility which exists to exercise RecipeRunStats." ;
42
- }
41
+ @ Option (displayName = "New text" , example = "txt" )
42
+ String newText ;
43
43
44
- @ Override
45
- public TreeVisitor <?, ExecutionContext > getVisitor () {
46
- return Preconditions .check (
47
- new PlainTextVisitor <>() {
48
- @ Override
49
- public PlainText visitText (PlainText text , ExecutionContext ctx ) {
50
- if (!"sam" .equals (text .getText ())) {
51
- return SearchResult .found (text );
52
- }
53
- return text ;
54
- }
55
- },
56
- new PlainTextVisitor <>() {
57
- @ Override
58
- public PlainText visitText (PlainText tree , ExecutionContext ctx ) {
59
- return tree .withText ("sam" );
44
+ @ Override
45
+ public TreeVisitor <?, ExecutionContext > getVisitor () {
46
+ return Preconditions .check (
47
+ new PlainTextVisitor <>() {
48
+ @ Override
49
+ public PlainText visitText (PlainText text , ExecutionContext ctx ) {
50
+ if (!"sam" .equals (text .getText ())) {
51
+ return SearchResult .found (text );
60
52
}
61
- });
62
- }
53
+ return text ;
54
+ }
55
+ },
56
+ new PlainTextVisitor <>() {
57
+ @ Override
58
+ public PlainText visitText (PlainText tree , ExecutionContext ctx ) {
59
+ return tree .withText (newText );
60
+ }
61
+ }
62
+ );
63
63
}
64
+ }
64
65
65
- @ Override
66
- public void defaults (RecipeSpec spec ) {
67
- spec .recipe (new RecipeWithApplicabilityTest ());
68
- }
66
+ class RecipeRunStatsTest implements RewriteTest {
69
67
70
68
@ DocumentExample
71
69
@ Test
72
70
void singleRow () {
73
71
rewriteRun (
74
- spec -> spec .dataTable (RecipeRunStats .Row .class , rows -> {
72
+ spec -> spec
73
+ .recipe (new RecipeWithApplicabilityTest ("sam" ))
74
+ .dataTable (RecipeRunStats .Row .class , rows -> {
75
75
assertThat (rows )
76
76
.as ("Running a single recipe on a single source should produce a single row in the RecipeRunStats table" )
77
77
.hasSize (1 );
@@ -89,4 +89,34 @@ void singleRow() {
89
89
text ("samuel" , "sam" )
90
90
);
91
91
}
92
- }
92
+
93
+ @ Test
94
+ void sourceFilesCountStatsForSameRecipe () {
95
+ rewriteRun (
96
+ spec -> spec
97
+ .recipeFromYaml ("""
98
+ ---
99
+ type: specs.openrewrite.org/v1beta/recipe
100
+ name: org.openrewrite.SeveralMethodNameChangeRecipes
101
+ description: Test.
102
+ recipeList:
103
+ - org.openrewrite.table.RecipeWithApplicabilityTest:
104
+ newText: sam1
105
+ - org.openrewrite.table.RecipeWithApplicabilityTest:
106
+ newText: sam2
107
+ """ ,
108
+ "org.openrewrite.SeveralMethodNameChangeRecipes" )
109
+ .dataTable (RecipeRunStats .Row .class , rows -> {
110
+ assertThat (rows )
111
+ .as ("Running declarative recipe with parametrized recipe a single source should produce a two rows in the RecipeRunStats table" )
112
+ .hasSize (2 );
113
+ for (RecipeRunStats .Row row : rows ) {
114
+ assertThat (row .getSourceFiles ())
115
+ .as ("If the same recipe runs with different parameters it shouldn't increment source files count" )
116
+ .isEqualTo (2 );
117
+ }
118
+ }).expectedCyclesThatMakeChanges (2 ),
119
+ text ("sem" , "sam2" )
120
+ );
121
+ }
122
+ }
0 commit comments