public static Builder builder(Location location) { return new AutoValue_ProtoFileElement.Builder() .location(location) .imports(ImmutableList.<String>of()) .publicImports(ImmutableList.<String>of()) .types(ImmutableList.<TypeElement>of()) .services(ImmutableList.<ServiceElement>of()) .extendDeclarations(ImmutableList.<ExtendElement>of()) .options(ImmutableList.<OptionElement>of()); }
@Test public void addMultiplePublicDependencies() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("example.other", "example.another")) .types(ImmutableList.of(element)) .build(); assertThat(file.publicImports()).hasSize(2); }
@Test public void addMultipleDependencies() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.other", "example.another")) .types(ImmutableList.of(element)) .build(); assertThat(file.imports()).hasSize(2); }
@Test public void stringWithSingleQuotes() { String proto = "" + "message Foo {\n" + " optional string name = 1 [default = 'single\"quotes'];\n" + "}"; FieldElement field = FieldElement.builder(location.at(2, 3)) .label(OPTIONAL) .type("string") .name("name") .tag(1) .defaultValue("single\"quotes") .build(); TypeElement messageElement = MessageElement.builder(location.at(1, 1)) .name("Foo") .fields(ImmutableList.of(field)) .build(); ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.of(messageElement)) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void packageDeclaration() throws Exception { String proto = "" + "package google.protobuf;\n" + "option java_package = \"com.google.protobuf\";\n" + "\n" + "// The protocol compiler can output a FileDescriptorSet containing the .proto\n" + "// files it parses.\n" + "message FileDescriptorSet {\n" + "}\n"; ProtoFileElement expected = ProtoFileElement.builder(location) .packageName("google.protobuf") .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(6, 1)) .name("FileDescriptorSet") .documentation("The protocol compiler can output a FileDescriptorSet containing " + "the .proto\nfiles it parses.") .build())) .options(ImmutableList.of( OptionElement.create("java_package", Kind.STRING, "com.google.protobuf"))) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void proto3MessageFieldsPermitRepeated() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Message {\n" + " repeated string a = 1;\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location.at(3, 3)) .label(REPEATED) .type("string") .name("a") .tag(1) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void simpleWithExtendsToSchema() { ProtoFileElement file = ProtoFileElement.builder(location) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(5, 1)).name("Extend").build())) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location) .name("Message") .build())) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n" + "\n" + "extend Extend {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void syntaxMayFollowCommentsAndEmptyLines() throws Exception { String proto = "" + "/* comment 1 */\n" + "// comment 2\n" + "\n" + "syntax = \"proto3\";\n" + "message Foo {}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(5, 1)) .name("Foo") .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void simpleWithBothImportsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.thing")) .publicImports(ImmutableList.of("example.other")) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "import \"example.thing\";\n" + "import public \"example.other\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void noWhitespace() { String proto = "message C {optional A.B ab = 1;}"; ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(1, 1)) .name("C").fields(ImmutableList.of(FieldElement.builder(location.at(1, 12)) .label(OPTIONAL) .type("A.B") .name("ab") .tag(1) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); } }
@Test public void reserved() { String proto = "" + "message Foo {\n" + " reserved 10, 12 to 14, 'foo';" + "}"; TypeElement message = MessageElement.builder(location.at(1, 1)) .name("Foo") .reserveds(ImmutableList.of(ReservedElement.create(location.at(2, 3), "", ImmutableList.<Object>of(10, Range.closed(12, 14), "foo")))) .build(); ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.of(message)) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void simpleWithOptionsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); OptionElement option = OptionElement.create("kit", Kind.STRING, "kat"); ProtoFileElement file = ProtoFileElement.builder(location) .options(ImmutableList.of(option)) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "option kit = \"kat\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void simpleWithServicesToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ServiceElement service = ServiceElement.builder(location).name("Service").build(); ProtoFileElement file = ProtoFileElement.builder(location) .types(ImmutableList.of(element)) .services(ImmutableList.of(service)) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n" + "\n" + "service Service {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void syntaxToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .syntax(PROTO_2) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "syntax = \"proto2\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); } }
@Test public void simpleWithPublicImportsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("example.other")) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "import public \"example.other\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void simpleWithImportsToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.other")) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "import \"example.other\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void syntaxSpecified() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Foo {}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Foo") .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
ProtoFileElement toElement() { return ProtoFileElement.builder(location) .imports(imports) .publicImports(publicImports) .packageName(packageName) .types(Type.toElements(types)) .services(Service.toElements(services)) .extendDeclarations(Extend.toElements(extendList)) .options(options.toElements()) .syntax(syntax) .build(); }
@Test public void addMultipleOptions() { TypeElement element = MessageElement.builder(location).name("Message").build(); OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); ProtoFileElement file = ProtoFileElement.builder(location) .options(ImmutableList.of(kitKat, fooBar)) .types(ImmutableList.of(element)) .build(); assertThat(file.options()).hasSize(2); }
@Test public void simpleToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }