Skip to content
Open
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
14 changes: 14 additions & 0 deletions LangSpec2/src/org/lara/langspec2/validation/SpecValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static List<String> collectErrors(WeaverModel model) {
checkNoSelfTypeInTypeDefs(model, errors);
checkReservedKeywords(model, errors);
checkDefaultAttributes(model, errors);
checkEnumDisplays(model, errors);

return errors;
}
Expand Down Expand Up @@ -209,5 +210,18 @@ private static void checkDefaultAttributes(WeaverModel model, List<String> error
}
}

private static void checkEnumDisplays(WeaverModel model, List<String> errors) {
for (var ed : model.getEnumDefs().values()) {
var displays = new HashSet<String>();
for (var value : ed.values()) {
var display = value.display() != null ? value.display() : value.value();
if (!displays.add(display)) {
errors.add("Duplicate display '" + display + "' in enum '" + ed.name()
+ "': fromDisplay() resolves to the first constant, making the other one unreachable");
}
}
}
}

private record MemberSignature(String name, List<JpDataType> paramTypes) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.lara.langspec2.validation;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.lara.langspec2.model.EnumDef;
import org.lara.langspec2.model.EnumValue;
import org.lara.langspec2.model.JpClass;
import org.lara.langspec2.model.WeaverModel;

class SpecValidatorTest {

private static WeaverModel newModel() {
return new WeaverModel("Prefix", "example.pkg", new JpClass("global"));
}

@Test
void duplicateDisplaysInEnumAreReported() {
var model = newModel();
model.addEnumDef(new EnumDef("Kind", List.of(
new EnumValue("A"),
new EnumValue("B"),
new EnumValue("C", "A"))));

var errors = SpecValidator.collectErrors(model);

assertThat(errors).anyMatch(error -> error.contains("Duplicate display 'A' in enum 'Kind'"));
}

@Test
void distinctDisplaysInEnumAreAccepted() {
var model = newModel();
model.addEnumDef(new EnumDef("Kind", List.of(
new EnumValue("A"),
new EnumValue("B"),
new EnumValue("C", "c"))));

var errors = SpecValidator.collectErrors(model);

assertThat(errors).isEmpty();
}
}
49 changes: 49 additions & 0 deletions Lara-JS/code/generate-ts-joinpoints.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fs from "fs";
import os from "os";
import path from "path";
import { generateEnums } from "../scripts/generate-ts-joinpoints.ts";

describe("generateEnums", () => {
it("preserves the enum values from the language specification", () => {
const outputDirectory = fs.mkdtempSync(
path.join(os.tmpdir(), "lara-build-interfaces-"),
);
const outputPath = path.join(outputDirectory, "Joinpoints.ts");

try {
const outputFile = fs.openSync(outputPath, "w");
try {
generateEnums(
[
{
name: "StorageClass",
entries: [
{ name: "NONE", value: "NONE" },
{ name: "PRIVATE_EXTERN", value: "PRIVATE_EXTERN" },
{ name: "STATIC", value: "STATIC" },
],
},
{
name: "AccessSpecifier",
entries: [
{ name: "DEFAULT", value: "DEFAULT" },
{ name: "STATIC", value: "static" },
],
},
],
outputFile,
);
} finally {
fs.closeSync(outputFile);
}

const output = fs.readFileSync(outputPath, "utf8");
expect(output).toContain(' STATIC: "STATIC",');
expect(output).toContain(' PRIVATE_EXTERN: "PRIVATE_EXTERN",');
expect(output).toContain('export const AccessSpecifier = {');
expect(output).toContain(' STATIC: "static",');
} finally {
fs.rmSync(outputDirectory, { recursive: true, force: true });
}
});
});
6 changes: 3 additions & 3 deletions Lara-JS/scripts/convert-joinpoint-specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type JSON_EnumSpecification = {
type: "enum";
name: string;
extends?: string;
children: { value: string }[];
children: { value: string; display?: string }[];
};

export type ConvertedSpecification = {
Expand Down Expand Up @@ -74,7 +74,7 @@ export type ConvertedParameter = {
export type ConvertedEnum = {
name: string;
extends?: string;
entries: string[];
entries: { name: string; value: string }[];
};

export function convertSpecification(input: JSON_LanguageSpecification, baseJoinPointSpec?: ConvertedSpecification | undefined): ConvertedSpecification {
Expand Down Expand Up @@ -367,7 +367,7 @@ function convertEnum(e: JSON_EnumSpecification): ConvertedEnum {
name: e.name,
extends: e.extends,
entries: e.children.map((child) => {
return child.value;
return { name: child.value, value: child.display ?? child.value };
}),
};
}
Expand Down
2 changes: 1 addition & 1 deletion Lara-JS/scripts/generate-ts-joinpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function generateEnum(e: ConvertedEnum, outputFile: number) {
*/\n`);
fs.writeSync(outputFile, `export const ${e.name} = {\n`);
e.entries.forEach((entry) => {
fs.writeSync(outputFile, ` ${entry}: "${entry.toLowerCase()}",\n`);
fs.writeSync(outputFile, ` ${entry.name}: "${entry.value}",\n`);
});
fs.writeSync(outputFile, `} as const;\n`);
fs.writeSync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void testRunCommand_withNullTimeout_handlesGracefully() {
// Given
String command = "echo hello";
boolean printToConsole = false;
long timeoutNanos = 2000000000; // 2 seconds
Long timeoutNanos = null;

// When
ProcessOutputAsString result = LaraSystemTools.runCommand(command, workingDirectory, printToConsole, timeoutNanos);
Expand Down
Loading