Skip to content
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

Make valid language tag regex case sensitive #1117

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// See https://wiki.openstreetmap.org/wiki/Multilingual_names
public static final Predicate<String> VALID_NAME_TAGS =
Pattern
.compile("^name:[a-z]{2,3}(-[a-z]{4})?([-_](x-)?[a-z]{2,})?(-([a-z]{2}|\\d{3}))?$", Pattern.CASE_INSENSITIVE)
.compile("^name:[a-z]{2,3}(-[A-Z][a-z]{3})?([-_](x-)?[a-z]{2,})?(-([A-Z]{2}|\\d{3}))?$")

Check warning on line 13 in planetiler-core/src/main/java/com/onthegomap/planetiler/util/LanguageUtils.java

View workflow job for this annotation

GitHub Actions / Analyze with Sonar

MAJOR CODE_SMELL

Simplify this regular expression to reduce its complexity from 24 to the 20 allowed. rule: java:S5843 (https://sonarcloud.io/organizations/onthegomap/rules?open=java%3AS5843&rule_key=java%3AS5843) issue url: https://sonarcloud.io/project/issues?pullRequest=1117&open=AZNoAJIoCsc-P9l_q2uk&id=onthegomap_planetiler
.asMatchPredicate();
// See https://github.com/onthegomap/planetiler/issues/86
// Match strings that only contain latin characters.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.onthegomap.planetiler.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

class LanguageUtilsTest {

Expand All @@ -26,28 +29,41 @@ void testRemoveNonLatin(String in, String out) {
}

@ParameterizedTest
@CsvSource(value = {
"name:es, true",
"name:en-US, true",
"name:fr-x-gallo, true",
"name:ko-Latn, true",
"name:be-tarask, true",
"name:ja_rm, true",
"name:ja_kana, true",
"name:vls, true",
"name:zh-hant-CN, true",
"name:zh_pinyin, true",
"name:zh_zhuyin, true",
"name:zh-Latn-tongyong, true",
"name:zh-Latn-pinyin, true",
"name:zh-Latn-wadegiles, true",
"name:yue-Latn-jyutping, true",
"nombre, false",
"name:, false",
"name:xxxxx, false",
}, nullValues = "null")
void testIsValidOsmNameTag(String in, boolean out) {
assertEquals(out, LanguageUtils.isValidOsmNameTag(in));
@ValueSource(strings = {
"name:es",
"name:en-US",
"name:en-001",
"name:fr-x-gallo",
"name:ko-Latn",
"name:be-tarask",
"name:ja_rm",
"name:ja_kana",
"name:vls",
"name:zh-hant-CN",
"name:zh_pinyin",
"name:zh_zhuyin",
"name:zh-Latn-tongyong",
"name:zh-Latn-pinyin",
"name:zh-Latn-wadegiles",
"name:yue-Latn-jyutping",
"name:tec",
"name:be-tarask",
"name:nan-Latn-pehoeji",
"name:zh-Latn-pinyin",
})
void testIsValidOsmNameTag(String in) {
assertTrue(LanguageUtils.isValidOsmNameTag(in));
}

@ParameterizedTest
@ValueSource(strings = {
"nombre",
"name:",
"name:xxxxx",
"name:TEC",
})
void testIsNotValidOsmNameTag(String in) {
assertFalse(LanguageUtils.isValidOsmNameTag(in));
}

}
Loading