feat(pbs): add explicit guest lifecycle hooks
This commit is contained in:
+90
@@ -0,0 +1,90 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.PBSSemanticTokenProvider;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.ModuleId;
|
||||
import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsGuestLifecycleSemanticsTest {
|
||||
@Test void permitsGlobalsClosuresAndDocOnBothHooks() {
|
||||
var source = """
|
||||
declare global state: int = 0;
|
||||
[Doc(markdown = \"\"\"Pause preparation.\"\"\")]
|
||||
[BeforePause] fn arbitraryName() -> void {
|
||||
let delta = 2;
|
||||
let effect = fn [delta]() -> int { return delta; };
|
||||
state += effect();
|
||||
}
|
||||
[BeforeResume] fn anotherName() -> void { state += 3; }
|
||||
""";
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
assertFalse(diagnostics.hasErrors(), () -> diagnostics.stream().map(d -> d.getMessage()).toList().toString());
|
||||
var tokens = new PBSSemanticTokenProvider().tokenize(source);
|
||||
for (var name : List.of("BeforePause", "BeforeResume")) {
|
||||
assertTrue(tokens.stream().anyMatch(t -> t.startOffset() == source.indexOf(name) && t.semanticKey().equals("pbs-lifecycle")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test void rejectsInvalidSourceSurfacesWithSourceLocations() {
|
||||
for (var marker : List.of("BeforePause", "BeforeResume")) {
|
||||
for (var source : List.of(
|
||||
"["+marker+"] fn bad(x: int) -> void {}",
|
||||
"["+marker+"] fn bad() -> int { return 1; }",
|
||||
"["+marker+"] fn bad<T>() -> void {}",
|
||||
"["+marker+"(value = 1)] fn bad() -> void {}",
|
||||
"["+marker+"][Init] fn bad() -> void {}",
|
||||
"["+marker+"][Frame] fn bad() -> void {}",
|
||||
"[BeforePause][BeforeResume] fn bad() -> void {}",
|
||||
"declare struct Box() { ["+marker+"] fn bad() -> void {} }",
|
||||
"declare service Box { ["+marker+"] fn bad() -> void {} }",
|
||||
"declare contract Box { ["+marker+"] fn bad() -> void; }",
|
||||
"["+marker+"] declare global bad: int = 0;")) {
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(7), source, diagnostics);
|
||||
assertTrue(diagnostics.hasErrors(), source);
|
||||
assertTrue(diagnostics.stream().anyMatch(d -> d.getSpan().getFileId().getId() == 7
|
||||
&& d.getSpan().getEnd() > d.getSpan().getStart()), source);
|
||||
}
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var id = new FileId(8);
|
||||
PbsParser.parse(PbsLexer.lex("["+marker+"] fn bad() -> void {}", id, diagnostics), id, diagnostics,
|
||||
PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
assertTrue(diagnostics.hasErrors());
|
||||
}
|
||||
}
|
||||
|
||||
private PbsParsedSourceFile file(int id, String source) {
|
||||
var file = new FileId(id);
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var ast = PbsParser.parse(PbsLexer.lex(source, file, diagnostics), file, diagnostics);
|
||||
assertFalse(diagnostics.hasErrors());
|
||||
return new PbsParsedSourceFile(file, ast, new ModuleId(id), SourceKind.PROJECT);
|
||||
}
|
||||
|
||||
@Test void uniquenessIsPerImageAndRepeatedDeclarationsAreNotCountedTwice() {
|
||||
var frame = file(0, "[Frame] fn frame() -> void {}");
|
||||
for (var marker : List.of("BeforePause", "BeforeResume")) {
|
||||
var first = file(1, "["+marker+"] fn first() -> void {}");
|
||||
var second = file(2, "["+marker+"] fn second() -> void {}");
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
new PbsProjectLifecycleSemanticsValidator().validate(List.of(frame, first, first), diagnostics);
|
||||
assertFalse(diagnostics.hasErrors());
|
||||
new PbsProjectLifecycleSemanticsValidator().validate(List.of(frame, first, second), diagnostics);
|
||||
var duplicates = diagnostics.stream().filter(d -> d.getCode().equals("E_SEM_DUPLICATE_LIFECYCLE_ROLE")).toList();
|
||||
assertEquals(1, duplicates.size());
|
||||
assertEquals(2, duplicates.getFirst().getSpan().getFileId().getId());
|
||||
assertTrue(duplicates.getFirst().getMessage().contains("first"));
|
||||
assertTrue(duplicates.getFirst().getMessage().contains("second"));
|
||||
assertTrue(duplicates.getFirst().getMessage().contains(marker));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user