-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Localization tests #2582
Localization tests #2582
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,14 @@ private static Set<LocalizationEntry> findLocalizationEntriesInFiles(Localizatio | |
} | ||
} | ||
|
||
public static Set<LocalizationEntry> findLocalizationParametersStringsInJavaFiles(LocalizationBundleForTest type) | ||
throws IOException { | ||
return Files.walk(Paths.get("src/main")) | ||
.filter(LocalizationParser::isJavaFile) | ||
.flatMap(path -> getLocalizationParametersInJavaFile(path, type).stream()) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private static Set<LocalizationEntry> findLocalizationEntriesInJavaFiles(LocalizationBundleForTest type) | ||
throws IOException { | ||
return Files.walk(Paths.get("src/main")) | ||
|
@@ -141,6 +149,26 @@ private static List<LocalizationEntry> getLanguageKeysInJavaFile(Path path, Loca | |
return result; | ||
} | ||
|
||
private static List<LocalizationEntry> getLocalizationParametersInJavaFile(Path path, LocalizationBundleForTest type) { | ||
List<LocalizationEntry> result = new LinkedList<>(); | ||
|
||
try { | ||
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); | ||
String content = String.join("\n", lines); | ||
|
||
List<String> keys = JavaLocalizationEntryParser.getLocalizationParameter(content, type); | ||
|
||
for (String key : keys) { | ||
result.add(new LocalizationEntry(path, key, type)); | ||
} | ||
|
||
} catch (IOException ignore) { | ||
ignore.printStackTrace(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Loads the fxml file and returns all used language resources. | ||
*/ | ||
|
@@ -192,31 +220,13 @@ static class JavaLocalizationEntryParser { | |
private static final Pattern QUOTATION_SYMBOL = Pattern.compile("QUOTATIONPLACEHOLDER"); | ||
|
||
public static List<String> getLanguageKeysInString(String content, LocalizationBundleForTest type) { | ||
List<String> parameters = getLocalizationParameter(content, type); | ||
|
||
List<String> result = new LinkedList<>(); | ||
|
||
Matcher matcher; | ||
if (type == LocalizationBundleForTest.LANG) { | ||
matcher = LOCALIZATION_START_PATTERN.matcher(content); | ||
} else { | ||
matcher = LOCALIZATION_MENU_START_PATTERN.matcher(content); | ||
} | ||
while (matcher.find()) { | ||
// find contents between the brackets, covering multi-line strings as well | ||
int index = matcher.end(); | ||
int brackets = 1; | ||
StringBuilder buffer = new StringBuilder(); | ||
while (brackets != 0) { | ||
char c = content.charAt(index); | ||
if (c == '(') { | ||
brackets++; | ||
} else if (c == ')') { | ||
brackets--; | ||
} | ||
buffer.append(c); | ||
index++; | ||
} | ||
for (String param : parameters) { | ||
|
||
String parsedContentsOfLangMethod = ESCAPED_QUOTATION_SYMBOL.matcher(buffer.toString()).replaceAll("QUOTATIONPLACEHOLDER"); | ||
String parsedContentsOfLangMethod = ESCAPED_QUOTATION_SYMBOL.matcher(param).replaceAll("QUOTATIONPLACEHOLDER"); | ||
|
||
// only retain what is within quotation | ||
StringBuilder b = new StringBuilder(); | ||
|
@@ -259,6 +269,39 @@ public static List<String> getLanguageKeysInString(String content, LocalizationB | |
return result; | ||
} | ||
|
||
public static List<String> getLocalizationParameter(String content, LocalizationBundleForTest type) { | ||
List<String> result = new LinkedList<>(); | ||
|
||
Matcher matcher; | ||
if (type == LocalizationBundleForTest.LANG) { | ||
matcher = LOCALIZATION_START_PATTERN.matcher(content); | ||
} else { | ||
matcher = LOCALIZATION_MENU_START_PATTERN.matcher(content); | ||
} | ||
while (matcher.find()) { | ||
// find contents between the brackets, covering multi-line strings as well | ||
int index = matcher.end(); | ||
int brackets = 1; | ||
StringBuilder buffer = new StringBuilder(); | ||
while (brackets != 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look in our StringUtil class, there is already a method for finding braces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the hints, but this is old code which I didnt write and don't wanna change if not really necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you created a test for it right? So you could check it.... |
||
char c = content.charAt(index); | ||
if (c == '(') { | ||
brackets++; | ||
} else if (c == ')') { | ||
brackets--; | ||
} | ||
// skip closing brackets | ||
if (brackets != 0) { | ||
buffer.append(c); | ||
} | ||
index++; | ||
} | ||
// trim newlines and whitespace | ||
result.add(buffer.toString().trim()); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could directly use the Files.find method and pass the filter predicate as argument. maxDepth would then be Integer.MaxValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just looked at it. Doesn't seem like a real change to me except that its a parameter then?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit different. Instead of collecting all files and then filtering on the list(the stream), the Files.find directly invokes the predicate on each file and the file is only included in the stream if it matches:
https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#find-java.nio.file.Path-int-java.util.function.BiPredicate-java.nio.file.FileVisitOption...-