summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-25 10:25:24 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-25 10:25:24 +0100
commit2a37bf78f778c2955a80f0bbf0ac7d4c14f5c55f (patch)
tree815b68061e1516c78f096e846bf1636ff3ceb6a3
parent7e0999200b57bfd01b04a8dd96cd8eed997ad2e2 (diff)
Add test for source file namespace
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java23
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/HobgoblinCompilerTests.java31
2 files changed, 51 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java
index 7945944..6daa31b 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java
@@ -10,18 +10,20 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.ArrayList;
public class HobgoblinCompiler {
+ public static final String FILE_NAME_EXTENSION = ".hob";
+
private HobgoblinCompiler() {
}
public static void compile(Path projectRoot) throws IOException {
var sourceDirectory = projectRoot.resolve("src");
var sourceFiles = sourceDirectory.toFile()
- .listFiles(file -> file.toPath().toString().endsWith(".hob"));
+ .listFiles(file -> file.toPath().toString().endsWith(FILE_NAME_EXTENSION));
for (var sourceFile : sourceFiles) {
- // TODO: derive namespace name from source path
- var namespaceName = NamespaceName.of();
+ var namespaceName = sourceFileNamespaceName(sourceDirectory, sourceFile.toPath());
var sourceContents = Files.readString(sourceFile.toPath(), StandardCharsets.UTF_8);
var sourceText = SourceText.fromString(sourceFile.toPath().toString(), sourceContents);
var untypedNamespaceNode = Parser.parseNamespace(sourceText, namespaceName);
@@ -30,4 +32,19 @@ public class HobgoblinCompiler {
var typedNamespaceNode = TypeChecker.typeCheckNamespace(untypedNamespaceNode, typeCheckerContext);
}
}
+
+ static NamespaceName sourceFileNamespaceName(Path sourceDirectory, Path sourceFilePath) {
+ var relativePath = sourceDirectory.relativize(sourceFilePath);
+
+ var parts = new ArrayList<String>();
+ for (var i = 0; i < relativePath.getNameCount() - 1; i++) {
+ parts.add(relativePath.getName(i).toString());
+ }
+
+ var fileName = relativePath.getName(relativePath.getNameCount() - 1).toString();
+ var lastPart = fileName.substring(0, fileName.length() - FILE_NAME_EXTENSION.length());
+ parts.add(lastPart);
+
+ return new NamespaceName(parts);
+ }
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/HobgoblinCompilerTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/HobgoblinCompilerTests.java
new file mode 100644
index 0000000..87f4e36
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/HobgoblinCompilerTests.java
@@ -0,0 +1,31 @@
+package org.zwobble.hobgoblin.compiler;
+
+import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.types.NamespaceName;
+
+import java.nio.file.Path;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class HobgoblinCompilerTests {
+ @Test
+ public void sourceFileInSourceDirectoryUsesFilenameAsNamespaceName() {
+ var sourceDirectory = Path.of("a/b/src");
+ var sourceFilePath = Path.of("a/b/src/blah.hob");
+
+ var namespaceName = HobgoblinCompiler.sourceFileNamespaceName(sourceDirectory, sourceFilePath);
+
+ assertThat(namespaceName, equalTo(NamespaceName.of("blah")));
+ }
+
+ @Test
+ public void sourceFileInSourceSubdirectoriesUsesSubdirectoriesAndFilenameAsNamespaceName() {
+ var sourceDirectory = Path.of("a/b/src");
+ var sourceFilePath = Path.of("a/b/src/one/two/blah.hob");
+
+ var namespaceName = HobgoblinCompiler.sourceFileNamespaceName(sourceDirectory, sourceFilePath);
+
+ assertThat(namespaceName, equalTo(NamespaceName.of("one", "two", "blah")));
+ }
+}