Skip to content

Commit

Permalink
#2780 introduced method for splitting fqn to class and subname
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Sep 1, 2024
1 parent 04e5f91 commit 4e50fb6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 Alexandr Evstigneev
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
Expand Down Expand Up @@ -236,6 +237,24 @@ public static String getCanonicalNamespaceName(@NotNull String name) {
return packageName == null ? Collections.emptyList() : StringUtil.split(getCanonicalNamespaceName(packageName), NAMESPACE_SEPARATOR);
}

@Contract("null -> null")
public static @Nullable @NlsSafe Pair<@Nullable String, @Nullable String> splitNames(@Nullable @NlsSafe String fqn) {
if (fqn == null || fqn.isEmpty()) {
return null;
}
if (fqn.endsWith(NAMESPACE_SEPARATOR)) {
return Pair.create(getCanonicalName(fqn), null);
}
var sepIndex = fqn.lastIndexOf(NAMESPACE_SEPARATOR);
if (sepIndex < 0) {
return Pair.create(null, fqn);
}
if (sepIndex == 0) {
return Pair.create(MAIN_NAMESPACE_NAME, fqn.substring(NAMESPACE_SEPARATOR.length()));
}
return Pair.create(fqn.substring(0, sepIndex), fqn.substring(sepIndex + NAMESPACE_SEPARATOR.length()));
}

/**
* Searching of namespace element is in. If no explicit namespaces defined, main is returned
*
Expand Down
70 changes: 70 additions & 0 deletions plugin/src/test/java/unit/perl/PerlPackageUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package unit.perl;

import base.PerlLightTestCase;
import com.intellij.testFramework.Parameterized;
import com.perl5.lang.perl.util.PerlPackageUtil;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.Collections;

@SuppressWarnings("Junit4RunWithInspection")
@RunWith(Parameterized.class)
public class PerlPackageUtilTest extends PerlLightTestCase {
private final @Nullable String myFqn;
private final @Nullable String myPackageName;
private final @Nullable String mySubName;

public PerlPackageUtilTest(@Nullable String fqn, @Nullable String packageName, @Nullable String subName) {
myFqn = fqn;
myPackageName = packageName;
mySubName = subName;
}

@Test
public void test() {
var result = PerlPackageUtil.splitNames(myFqn);
if (myFqn == null || myFqn.isEmpty()) {
assertNull(result);
}
else {
assertNotNull(result);
assertEquals(myPackageName, result.getFirst());
assertEquals(mySubName, result.getSecond());
}
}

@org.junit.runners.Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> fakeData() {
return Collections.emptyList();
}

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> realData(Class<?> clazz) {
return Arrays.asList(new Object[][]{
{null, null, null},
{"", null, null},
{"::subname", "main", "subname"},
{"Foo::", "Foo", null},
{"Foo::subname", "Foo", "subname"},
});
}
}

0 comments on commit 4e50fb6

Please sign in to comment.