Skip to content

Commit

Permalink
test all types of plugin validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Apr 24, 2024
1 parent c15b6a7 commit 872769b
Showing 1 changed file with 73 additions and 43 deletions.
116 changes: 73 additions & 43 deletions enigma/src/test/java/org/quiltmc/enigma/PluginValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,88 @@ public void testIdValidation() {
Enigma.builder().setPlugins(List.of(new IdTestPlugin())).build();
}

@Test
public void testDuplicateServices() {
Enigma.builder().setPlugins(List.of(new DuplicateIdTestPlugin())).build();
}

@Test
public void testDuplicateFileTypes() {
Enigma.builder().setPlugins(List.of(new DuplicateFileTypeTestPlugin())).build();
}

private static void registerService(EnigmaPluginContext ctx, String id) {
registerService(ctx, id, id);
}

private static void registerService(EnigmaPluginContext ctx, String id, String fileType) {
ctx.registerService(ReadWriteService.TYPE, ctx1 -> new ReadWriteService() {
@Override
public void write(EntryTree<EntryMapping> mappings, MappingDelta<EntryMapping> delta, Path path, ProgressListener progress, MappingSaveParameters saveParameters) {
}

@Override
public EntryTree<EntryMapping> read(Path path, ProgressListener progress) {
return null;
}

@Override
public FileType getFileType() {
return new FileType.File(fileType);
}

@Override
public boolean supportsReading() {
return false;
}

@Override
public boolean supportsWriting() {
return false;
}

@Override
public String getId() {
return id;
}
});
}

private static class DuplicateFileTypeTestPlugin implements EnigmaPlugin {
@Override
public void init(EnigmaPluginContext ctx) {
registerService(ctx, "test:grind", "gaming");
Assertions.assertThrows(IllegalStateException.class, () -> registerService(ctx, "test:slay", "gaming"));
}
}

private static class DuplicateIdTestPlugin implements EnigmaPlugin {
@Override
public void init(EnigmaPluginContext ctx) {
registerService(ctx, "grind:ground");
Assertions.assertThrows(IllegalStateException.class, () -> registerService(ctx, "grind:ground"));
}
}

private static class IdTestPlugin implements EnigmaPlugin {
@Override
public void init(EnigmaPluginContext ctx) {
// empty
Assertions.assertThrows(IllegalArgumentException.class, () -> this.registerService(ctx, ""));
Assertions.assertThrows(IllegalArgumentException.class, () -> registerService(ctx, ""));
// no namespace
Assertions.assertThrows(IllegalArgumentException.class, () -> this.registerService(ctx, "grind"));
Assertions.assertThrows(IllegalArgumentException.class, () -> registerService(ctx, "grind"));
// slashes in wrong place
Assertions.assertThrows(IllegalArgumentException.class, () -> this.registerService(ctx, "grind/grind:ground"));
Assertions.assertThrows(IllegalArgumentException.class, () -> registerService(ctx, "grind/grind:ground"));
// uppercase chars
Assertions.assertThrows(IllegalArgumentException.class, () -> this.registerService(ctx, "grind:Ground"));
Assertions.assertThrows(IllegalArgumentException.class, () -> registerService(ctx, "grind:Ground"));
// invalid chars
Assertions.assertThrows(IllegalArgumentException.class, () -> this.registerService(ctx, "grind:ground!"));
Assertions.assertThrows(IllegalArgumentException.class, () -> registerService(ctx, "grind:ground!"));
// valid
this.registerService(ctx, "grind:ground");
this.registerService(ctx, "grind:ground_");
this.registerService(ctx, "grind:ground_grind/g_rind2");
this.registerService(ctx, "grind:ground/grind");
this.registerService(ctx, "grind:ground/grind/grind");
}

private void registerService(EnigmaPluginContext ctx, String id) {
ctx.registerService(ReadWriteService.TYPE, ctx1 -> new ReadWriteService() {
@Override
public void write(EntryTree<EntryMapping> mappings, MappingDelta<EntryMapping> delta, Path path, ProgressListener progress, MappingSaveParameters saveParameters) {
}

@Override
public EntryTree<EntryMapping> read(Path path, ProgressListener progress) {
return null;
}

@Override
public FileType getFileType() {
return new FileType.File(id);
}

@Override
public boolean supportsReading() {
return false;
}

@Override
public boolean supportsWriting() {
return false;
}

@Override
public String getId() {
return id;
}
});
registerService(ctx, "grind:ground");
registerService(ctx, "grind:ground_");
registerService(ctx, "grind:ground_grind/g_rind2");
registerService(ctx, "grind:ground/grind");
registerService(ctx, "grind:ground/grind/grind");
}
}
}

0 comments on commit 872769b

Please sign in to comment.