Skip to content

Commit

Permalink
Polish and directory test
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 26, 2023
1 parent 2da006d commit 2c11b28
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "Record the presence of source files that occur more than once in an LST.";
return "Record the presence of LSTs with duplicate paths, indicating that the same file was parsed more than once.";
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/LanguageComposition.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String getDescription() {
}

@Data
static class Accumulator {
public static class Accumulator {
Map<String, Map<String, Counts>> folderToLanguageToCounts = new HashMap<>();
}

Expand All @@ -73,7 +73,7 @@ private static String containingFolderPath(SourceFile s) {
if(lastSlash == -1) {
return "";
}
return sourcePath.substring(0, lastSlash);
return s.getSourcePath().toString().substring(0, lastSlash);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static class Row {
description = "The language of the source file.")
String language;

@Column(displayName = "Class",
description = "The OpenRewrite class that was parsed for this source file.")
String parserClass;
@Column(displayName = "LST type",
description = "The Lossless Semantic Tree type of this source file.")
String sourceFileType;

@Column(displayName = "Lines of text",
description = "The number of lines of text in the source file. " +
Expand Down
46 changes: 42 additions & 4 deletions src/test/java/org/openrewrite/LanguageCompositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import org.openrewrite.table.LanguageCompositionPerRepository;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpecs;
import org.openrewrite.text.PlainText;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.PathUtils.separatorsToSystem;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.SourceSpecs.dir;
import static org.openrewrite.test.SourceSpecs.text;
Expand All @@ -31,9 +34,7 @@ public class LanguageCompositionTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new LanguageComposition())
.expectedCyclesThatMakeChanges(0)
.cycles(1);
spec.recipe(new LanguageComposition());
}

@Test
Expand Down Expand Up @@ -73,7 +74,7 @@ void javaAndPlainText() {
spec.dataTable(LanguageCompositionPerFolder.Row.class, table -> {
assertThat(table).hasSize(2);
assertThat(table).contains(
new LanguageCompositionPerFolder.Row("src/java/main/com/whatever", "Java", 1, 3),
new LanguageCompositionPerFolder.Row(separatorsToSystem("src/java/main/com/whatever"), "Java", 1, 3),
new LanguageCompositionPerFolder.Row("", "Plain text", 2, 4)
);
});
Expand Down Expand Up @@ -131,4 +132,41 @@ void foo() {
)
);
}

@Test
void folderCounts() {
rewriteRun(
spec -> {
spec.dataTable(LanguageCompositionPerFile.Row.class, table -> {
assertThat(table).hasSize(3);
assertThat(table).containsOnlyOnce(
new LanguageCompositionPerFile.Row(separatorsToSystem("src/main/file.txt"), "Plain text", PlainText.class.getName(), 2, false),
new LanguageCompositionPerFile.Row(separatorsToSystem("src/resources/file.txt"), "Plain text", PlainText.class.getName(), 3, false),
new LanguageCompositionPerFile.Row(separatorsToSystem("file.txt"), "Plain text", PlainText.class.getName(), 4, false)
);
});
spec.dataTable(LanguageCompositionPerFolder.Row.class, table -> {
assertThat(table).hasSize(3);
assertThat(table).containsOnlyOnce(
new LanguageCompositionPerFolder.Row(separatorsToSystem("src/main"), "Plain text", 1, 2),
new LanguageCompositionPerFolder.Row(separatorsToSystem("src/resources"), "Plain text", 1, 3),
new LanguageCompositionPerFolder.Row(separatorsToSystem(""), "Plain text", 1, 4)
);
});
},
dir("src",
dir("main", textFileWithLineCount(2)),
dir("resources", textFileWithLineCount(3))
),
textFileWithLineCount(4)
);
}

SourceSpecs textFileWithLineCount(int lineCount) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < lineCount; i++) {
sb.append("line ").append(i).append("\n");
}
return text(sb.toString());
}
}

0 comments on commit 2c11b28

Please sign in to comment.