|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package org.rascalmpl.vscode.lsp.rascal.model; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.net.URISyntaxException; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.mockito.Mock; |
| 37 | +import org.rascalmpl.uri.URIResolverRegistry; |
| 38 | +import org.rascalmpl.values.IRascalValueFactory; |
| 39 | + |
| 40 | +import io.usethesource.vallang.ISourceLocation; |
| 41 | + |
| 42 | +public class PathConfigsTest { |
| 43 | + private static final IRascalValueFactory VF = IRascalValueFactory.getInstance(); |
| 44 | + private static final URIResolverRegistry reg = URIResolverRegistry.getInstance(); |
| 45 | + |
| 46 | + |
| 47 | + private static void checkRoot(ISourceLocation project, String modulePath) throws URISyntaxException { |
| 48 | + var m = VF.sourceLocation(project.getScheme(), project.getAuthority(), project.getPath() + "/" + modulePath); |
| 49 | + var root = PathConfigs.inferProjectRoot(m); |
| 50 | + assertEquals(project, root); |
| 51 | + } |
| 52 | + |
| 53 | + @Mock PathConfigDiagnostics diagnostics; |
| 54 | + private PathConfigs configs; |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() { |
| 58 | + configs = new PathConfigs(Executors.newCachedThreadPool(), diagnostics); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void standardRoot() throws URISyntaxException { |
| 63 | + checkRoot(VF.sourceLocation("std", "", ""), "IO.rsc"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void nestedStandardRoot() throws URISyntaxException { |
| 68 | + checkRoot(VF.sourceLocation("std", "", ""), "util/Maybe.rsc"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void lspRoot() throws URISyntaxException { |
| 73 | + checkRoot(VF.sourceLocation("cwd", "", ""), "src/main/rascal/library/util/LanguageServer.src"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void lspTargetRoot() throws URISyntaxException { |
| 78 | + checkRoot(VF.sourceLocation("cwd", "", ""), "target/classes/library/util/LanguageServer.rsc"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void nestedProjectRoot() throws URISyntaxException { |
| 83 | + checkRoot(VF.sourceLocation("cwd", "", "src/test/resources/project-a"), "src/Module.rsc"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void pathConfigForStandardModule() throws URISyntaxException, IOException { |
| 88 | + var pcfg = configs.lookupConfig(VF.sourceLocation("std", "", "IO.rsc")); |
| 89 | + assertEquals(reg.logicalToPhysical(VF.sourceLocation("std", "", "")), pcfg.getProjectRoot()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void pathConfigForLsp() throws URISyntaxException, IOException { |
| 94 | + var pcfg = configs.lookupConfig(VF.sourceLocation("project", "rascal-lsp", "")); |
| 95 | + assertEquals(reg.logicalToPhysical(VF.sourceLocation("project", "rascal-lsp", "")), pcfg.getProjectRoot()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void pathConfigForLspModule() throws URISyntaxException, IOException { |
| 100 | + var pcfg = configs.lookupConfig(VF.sourceLocation("project", "rascal-lsp", "src/main/rascal/library/util/LanguageServer.src")); |
| 101 | + assertEquals(reg.logicalToPhysical(VF.sourceLocation("project", "rascal-lsp", "")), pcfg.getProjectRoot()); |
| 102 | + } |
| 103 | +} |
0 commit comments