|
| 1 | +package io.github.protogenerator.plugin; |
| 2 | + |
| 3 | +import com.google.protobuf.DescriptorProtos; |
| 4 | +import com.google.protobuf.compiler.PluginProtos; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 10 | + |
| 11 | +public class StrictProtoGeneratorTest { |
| 12 | + @Test |
| 13 | + @DisplayName("returns empty response when proto file has no messages") |
| 14 | + void sut_returns_empty_response_when_no_messages_exist() { |
| 15 | + // Arrange |
| 16 | + DescriptorProtos.FileDescriptorProto file = DescriptorProtos.FileDescriptorProto.newBuilder() |
| 17 | + .setName("empty.proto") |
| 18 | + .setPackage("io.github.protogenerator.example") |
| 19 | + .build(); |
| 20 | + |
| 21 | + PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.newBuilder() |
| 22 | + .addProtoFile(file) |
| 23 | + .build(); |
| 24 | + |
| 25 | + // Act |
| 26 | + PluginProtos.CodeGeneratorResponse actual = StrictProtoGenerator.process(request); |
| 27 | + |
| 28 | + // Assert |
| 29 | + assertEquals(0, actual.getFileCount()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @DisplayName("returns constructor helpers for multiple proto files") |
| 34 | + void sut_returns_constructor_helpers_for_multiple_proto_files() { |
| 35 | + // Arrange |
| 36 | + DescriptorProtos.DescriptorProto message1 = DescriptorProtos.DescriptorProto.newBuilder() |
| 37 | + .setName("Person") |
| 38 | + .addField(DescriptorProtos.FieldDescriptorProto.newBuilder() |
| 39 | + .setName("name") |
| 40 | + .setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING) |
| 41 | + .build()) |
| 42 | + .build(); |
| 43 | + |
| 44 | + DescriptorProtos.DescriptorProto message2 = DescriptorProtos.DescriptorProto.newBuilder() |
| 45 | + .setName("Company") |
| 46 | + .addField(DescriptorProtos.FieldDescriptorProto.newBuilder() |
| 47 | + .setName("companyName") |
| 48 | + .setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING) |
| 49 | + .build()) |
| 50 | + .build(); |
| 51 | + |
| 52 | + DescriptorProtos.FileDescriptorProto file1 = DescriptorProtos.FileDescriptorProto.newBuilder() |
| 53 | + .setName("person.proto") |
| 54 | + .setPackage("io.github.protogenerator.people") |
| 55 | + .addMessageType(message1) |
| 56 | + .build(); |
| 57 | + |
| 58 | + DescriptorProtos.FileDescriptorProto file2 = DescriptorProtos.FileDescriptorProto.newBuilder() |
| 59 | + .setName("company.proto") |
| 60 | + .setPackage("io.github.protogenerator.business") |
| 61 | + .addMessageType(message2) |
| 62 | + .build(); |
| 63 | + |
| 64 | + PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.newBuilder() |
| 65 | + .addProtoFile(file1) |
| 66 | + .addProtoFile(file2) |
| 67 | + .build(); |
| 68 | + |
| 69 | + // Act |
| 70 | + PluginProtos.CodeGeneratorResponse actual = StrictProtoGenerator.process(request); |
| 71 | + |
| 72 | + // Assert |
| 73 | + assertEquals(2, actual.getFileCount()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @DisplayName("returns helper with default package when proto package is missing") |
| 78 | + void sut_returns_helper_with_default_package_when_proto_package_missing() { |
| 79 | + // Arrange |
| 80 | + DescriptorProtos.DescriptorProto message = DescriptorProtos.DescriptorProto.newBuilder() |
| 81 | + .setName("OrphanMessage") |
| 82 | + .build(); |
| 83 | + |
| 84 | + DescriptorProtos.FileDescriptorProto file = DescriptorProtos.FileDescriptorProto.newBuilder() |
| 85 | + .setName("orphan.proto") |
| 86 | + .addMessageType(message) |
| 87 | + .build(); |
| 88 | + |
| 89 | + PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.newBuilder() |
| 90 | + .addProtoFile(file) |
| 91 | + .build(); |
| 92 | + |
| 93 | + // Act |
| 94 | + PluginProtos.CodeGeneratorResponse actual = StrictProtoGenerator.process(request); |
| 95 | + |
| 96 | + // Assert |
| 97 | + assertEquals(1, actual.getFileCount()); |
| 98 | + PluginProtos.CodeGeneratorResponse.File generatedFile = actual.getFile(0); |
| 99 | + assertTrue(generatedFile.getName().endsWith("OrphanMessageConstructor.java")); |
| 100 | + } |
| 101 | +} |
0 commit comments